#96922FAL file DuplicationBehavior::REPLACE is using public file path
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.
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
Related issues 1
Files 1
History 67
-
Sascha Egerer changed Related to #98005 <span data-processor="relativetime" data-value="2022-08-05T19:13:36+00:00" > 08/05/22, 19:13 </span>
-
Simon Schaufelberger commented <span data-processor="relativetime" data-value="2022-08-05T19:18:38+00:00" > 08/05/22, 19:18 </span>
-
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>
-
Sascha Egerer changed
Bildschirmfoto 2022-08-05 um 21.21.49.png<span data-processor="relativetime" data-value="2022-08-05T19:23:33+00:00" > 08/05/22, 19:23 </span> -
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 -
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 extractedResourceStorage::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
viahasFileInFolder()/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 throwsInvalidArgumentException1325842622.getPublicUrl()is not a path. It is resolved through the
@GeneratePublicUrlForResourceEvent@, then possibly through anOnlineMediaHelper, 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
baseUriis 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()returnsnull— the expression then collapses to
Environment::getPublicPath() . '/', i.e. a directory.file_exists()returns
truefor 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
withoutbaseUri, 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): stringreturns the real
path forLocalDriverand a temporary copy for every other driver. Since the method is
on the interface, theinstanceof AbstractFileguard 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. - a
-
Gerrit Code Review pushed patch set 1 for main to change 97471 status
NewUnder Review <span data-processor="relativetime" data-value="2026-11-09T20:41:49+00:00" > 11/09/26, 20:41 </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?