Review main Merged

89740[BUGFIX] Allow replacing files with different mime-type and extension

Oliver Hader CR +2 V +2 Normal +115 −26 updated 07/08/25, 08:39

Commit message

Replacing an existing file.pdf (having mime-type `application/pdf`)
with another file.png (having mime-type `image/png`) is denied due
to the mismatching mime-type to extension consistency check.

The file abstraction layer performed two distinct operations
(first replace file contents, then rename file name) which caused
this problem.

The new `ResourceStorage::replaceAndRenameUploadedFile()` method is
dedicated for that purpose, enabling the resource storage to
know about the overall intention and allowing to perform a proper
consistency check.

In case the flag "keep the current filename" is enabled in the user
interface, the filename part is kept, however the fileextension is
adjusted if necessary.

As a drive-by change, the resource integrity error messages are
now properly revelead in errors stemming from the "Rename" action.
This is implemented without the need for a XLF label change just by
string concatenation.

Review

  • Code review CR +2 Done
    • Oliver Hader +2
    • Georg Ringer +1
    • Garvin Hicking +1
    • Andreas Kienast +1
  • Verified V +2 Done
    • Oliver Hader +2
    • Georg Ringer +1
    • Garvin Hicking +1
    • Andreas Kienast +1
    • core-ci +1 CI

Issues 1

Files 6

Review log 2631

Show 56 earlier messages
  1. Oliver Hader uploaded patch set 1 06/17/25, 10:13

  2. core-ci voted on patch set 1 V +1 06/17/25, 10:20 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/80419

  3. Garvin Hicking commented on patch set 1 06/18/25, 06:27

    This change is ready for review.
    Looks very promising, is this ready for review or are you still iterating on it?

    ResourceStorage.php:1929

    I wonder, should we still fire events just in case extensions want to also handle a case where a renaming was triggered? So that maybe new file contents can be evaluatex even if the filename did not change but contents did? I am not sure though if the ReplaceEvent is called in that case and my point is moot, haven't fully reviewed this yet.
  4. Garvin Hicking marked it work in progress 06/18/25, 06:28

  5. Oliver Hader commented on patch set 1 07/03/25, 07:46

    This change is ready for review.
  6. Oliver Hader commented on patch set 1 07/03/25, 07:47

    Reply

    I think it's fine for being reviewed
  1. Oliver Hader commented on patch set 1 07/03/25, 07:50

    ResourceStorage.php:1929 Reply

    I think it should be behave like `renameFile()` and not dispatch events, when the file name has not changed, and actually no renaming would be performed.
  2. Garvin Hicking commented on patch set 1 07/03/25, 08:28

    ResourceStorage.php:1929 Reply

    Yes, I see this now. I misread the code that it would now no longer trigger the event. However, also before the change the event is not triggered if the file is the same. So all good, sorry for the noise.
  3. Simon Schaufelberger updated the commit message in patch set 2 V +1 07/03/25, 09:15

  4. Simon Schaufelberger commented on patch set 2 07/03/25, 09:18

    could this be covered with a test?
  5. core-ci voted on patch set 2 V +1 07/03/25, 09:23 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/81276

  6. Oliver Hader commented on patch set 2 07/03/25, 12:21

    Reply

    Unfortunately not, since `ExtendedFileUtility` still relies on `$_FILES` and should be migrated to `UploadedFile` first...
  7. Oliver Hader commented on patch set 2 07/03/25, 13:56

    Reply

    I started replacing `$_FILES` in a separate patch... https://review.typo3.org/c/Packages/TYPO3.CMS/+/89983
  8. Garvin Hicking uploaded patch set 3 V +1 07/03/25, 19:13

  9. Garvin Hicking commented on patch set 3 CR +1 V +1 07/03/25, 19:15

    The "Replace" part works well for me, but I believe we should also cover the "Rename" scenario (especially when re-uploading a file with the same name, which can trigger some other variant of replacement). If you believe this should be a separate or follow-up patch, that would be fine by me. This patch here already improves the most important functionality. That's why I'm upvoting it despite the following issues: I have tested this by creating a folder with a few files (jpg, txt, vcf, zip, doc, yaml). I then tried to just rename (distinct action, not "Replace"!) a file (without uploading a new one) from "config.yaml" to "config.txt". Both filetypes are allowed, so I would expect this to work. However, this fails with a resource integrity error. I altered the patch slightly to be able to read the actual error message why the resource integrity fails, because this is currently not revealed. Please check if that is acceptable. Now for the next test, I created PNG file "image.jpg" so that I could replace the existing "image.jpg" with the new (PNG!) contents - there's a distinct popup that handles this. This also fails, because the MIME type of the file is recognized as image/png to mismatch image/jpg (even though both types would be generally allowed). I would expect this to work and change the file extension to "png" later on (as described in the forge issue), like the "replace" logic allows you to do. After that I tested the actual "Replace" functionality for the "image.jpg" (jpg) file with my "image.jpg" (png) one. This failed with the error message `Mime-type "image/png" not allowed for file extension "jpg" (expected: image/jpeg).`, independent of the "Keep current filename" checkbox state (tested both). I do think this is a bit edge-casey. But if you use the Thumbnail view instead list view, "Rename" is all you get there. Which might lead people into thinking this is all the get to replace files. After that I actually used "image.png" as a replacement file, and this works. Also tested it with jpg to zip. And it properly disallows jpg to a format like exe.
  10. Garvin Hicking commented on patch set 3 07/03/25, 19:16

    Reply

    (

    Reply

    (unresolving, don't know why gerrit always stores it wrong)
  11. core-ci voted on patch set 3 V -1 07/03/25, 19:20 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/81289

  12. Garvin Hicking uploaded patch set 4 CR +1 V +1 07/03/25, 20:34

  13. core-ci voted on patch set 4 V +1 07/03/25, 20:42 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/81291

  14. Garvin Hicking voted on patch set 4 CR +1 V +1 07/03/25, 20:46

  15. Oliver Hader commented on patch set 4 07/05/25, 08:06

    ResourceController.php:74

  16. Oliver Hader commented on patch set 4 07/05/25, 08:22

    Reply

    What would be the use-cases for renaming a file extensions with an obvious incorrect mime-type - e.g. renaming `image.png` (image/png) to `image.jpg` (still using image/png)? Scenarios like renaming `file.txt` (text/plain) to `file.css` (text/css) might be feasible by utilizing the mime-type compatibility map `...['text/plain]['css'] = 'text/css'`. However renaming it back from `file.css` (text/css) to `file.txt` (text/plain) is a different story. The issue with only having "rename" in the thumbnail view is a UX bug from my point of view and should be addressed in a separate issue.
  17. Garvin Hicking commented on patch set 4 CR 0 V 0 07/05/25, 09:28

    Reply

    Ack. Can look into this in a follow-up for the context menu missing. That would then make the jpg<>png rename moot. Remaining the txt<>yaml, rss<>opml<>xml, conf<>txt, bak<>everything? parts remain to fixup mistakenly uploaded files in text format, where mime guessing might not match the intent. on the other hand, we could also give a tip like "use replace instead of rename if mime types mismatch" as a flashmessage?

    ResourceController.php:74 Reply

    Ah. How do you feel adding a method like "getMergedMessages($glue=' - ')" to ResultException which would be callable in these two places so we don't need to do similar array_map stuff in all places? (I can inmplement this if you agree)
  18. Oliver Hader uploaded patch set 5 V +1 07/07/25, 16:16

  19. Oliver Hader commented on patch set 4 07/07/25, 16:21

    Reply

    Let's work on the renaming aspect in a different issue. The replacement aspect looks straight-forward to me. The renaming aspect still is a bit fuzzy - and I acknowledge that using `file.css.bak` might be a valid use-case - but still I don't know yet, how to put that into deterministic logic.

    ResourceController.php:74 Reply

    Instead of adding the rendering logic to the exception, I've moved that to a new trait. I hope that's fine for you. At least it avoids having to repeat the `array_map` logic multiple times.
  20. core-ci voted on patch set 5 V +1 07/07/25, 16:24 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/81436

  21. Garvin Hicking commented on patch set 5 CR +1 V +1 07/07/25, 18:46

    Reply

    Now I remembered why this felt familiar: https://forge.typo3.org/issues/104330 I also remember I failed to get this simple context menu item implemented. Guess one of use needs gently nudge Andi. ;o) I've created a follow-up for the "rename text files" issue: https://forge.typo3.org/issues/107053

    ResourceController.php:74 Reply

    I'm not a trait-fan myself, but this is fine of course - still a win in my book to consolidate this :)
  22. Andreas Kienast voted on patch set 5 CR +1 V +1 07/08/25, 06:33

  23. Georg Ringer voted on patch set 5 CR +1 V +1 07/08/25, 06:49

  24. Oliver Hader voted on patch set 5 CR +2 V +2 07/08/25, 08:36

  25. Oliver Hader merged patch set 6 07/08/25, 08:39