Issue Bug Under Review

#96922FAL file DuplicationBehavior::REPLACE is using public file path

Sascha Egerer 02/16/22, 10:28 updated 11/09/26, 20:41

Description

When a file is uploaded with an existing name there are different behaviors to choose from.
One is the DuplicationBehavior::REPLACE which should replace the existing file.

Currently the “public path” of the file is used to get the file on the storage which does not make sense and fails, if the public path is returning something else then it is on the storage.

See https://github.com/TYPO3/typo3/blob/bc15ad4b24a3c76087a2aa1172d44cb84fe0569c/typo3/sysext/core/Classes/Resource/ResourceStorage.php#L2068

The public path must not be used to do file operations on the storage but only to provide a path for the public (web) usage.

Reviews 1

Files 1

History 67

  1. Simon Schaufelberger commented <span data-processor="relativetime" data-value="2022-08-05T19:18:38+00:00" > 08/05/22, 19:18 </span>

    > Currently the “public path” of the file is used to get the file on the storage which does not make sense and fails, if the public path is returning something else then it is on the storage.

    How can this be reproduced?

  2. Sascha Egerer changed attached Bildschirmfoto 2022-08-05 um 21.21.49.png <span data-processor="relativetime" data-value="2022-08-05T19:22:02+00:00" > 08/05/22, 19:22 </span>

  3. Sascha Egerer commented and changed attached Bildschirmfoto 2022-08-05 um 21.23.50.png <span data-processor="relativetime" data-value="2022-08-05T19:23:56+00:00" > 08/05/22, 19:23 </span>

    Add a `baseUri` like `my-custom-uri-path/` in the File Storage record.
    Bildschirmfoto%202022-08-05%20um%2021.23.50.png

  4. Georg Ringer commented <span data-processor="relativetime" data-value="2026-08-13T18:20:02+00:00" > 08/13/26, 18:20 </span>

    Still reproducible on current main. Two notes to make the report easier to pick up.

    Where the code lives now

    The line referenced in the description has moved. It is no longer in renameFile()
    but in the extracted ResourceStorage::handleRenameFile():

    } elseif ($conflictMode === DuplicationBehavior::REPLACE) {
        if ($file instanceof AbstractFile) {
            $sourceFileIdentifier = substr($file->getCombinedIdentifier(), 0, (int)strrpos($file->getCombinedIdentifier(), '/') + 1) . $targetFileName;
            $sourceFile = $this->getResourceFactoryInstance()->getFileObjectFromCombinedIdentifier($sourceFileIdentifier);
            $file = $this->replaceFile($sourceFile, Environment::getPublicPath() . '/' . $file->getPublicUrl());
        }
    }

    Note that addUploadedFile() — the other place that handles
    @DuplicationBehavior::REPLACE@ — is already correct: it resolves the existing file
    via hasFileInFolder() / getFileInFolder() and passes the local upload path to
    @replaceFile()@. Only the rename path is affected.

    Why the public URL is wrong here

    replaceFile() takes a local file path; handleReplaceFile() validates it with
    @file_exists()@ and otherwise throws InvalidArgumentException 1325842622.

    getPublicUrl() is not a path. It is resolved through the
    @GeneratePublicUrlForResourceEvent@, then possibly through an OnlineMediaHelper, and
    only as a last step through $this->driver->getPublicUrl() including the storage’s
    @baseUri@. Its return type is ?string. So the current expression breaks whenever:

    • a baseUri is configured on the storage (the reproduction in #note-7)
    • a remote/CDN driver returns an absolute URL, giving
      getPublicPath() . '/https://…'
    • the storage is not public and the URL is an eID dump URL rather than a path
    • getPublicUrl() returns null — the expression then collapses to
      Environment::getPublicPath() . '/', i.e. a directory. file_exists() returns
      true for it, so the bad value is not caught and reaches
      $this->driver->replaceFile().

    That it works at all is an artifact of the default setup: a local fileadmin/ storage
    without baseUri, where the public URL and the relative path happen to be identical.

    Suggested fix

    Ask the driver for a local copy instead of constructing a path from the public URL:

    $file = $this->replaceFile($sourceFile, $file->getForLocalProcessing(false));

    FileInterface::getForLocalProcessing(bool $writable = true): string returns the real
    path for LocalDriver and a temporary copy for every other driver. Since the method is
    on the interface, the instanceof AbstractFile guard could be dropped as well — but
    that is unrelated cleanup and should not be part of the bugfix.

    One thing to verify before patching

    Independent of the path problem, the semantics of this branch look questionable: $file
    (the file being renamed) is never removed, because $this->driver->renameFile() threw
    before doing anything. So after “rename a.txt to b.txt, overwrite”, the result should be
    b.txt holding a.txt’s content and a.txt still sitting next to it. I have only read
    this from the code, not executed it — but if it holds, the one-line fix above is not
    sufficient and a functional test should cover the leftover source file as well.