Fixes #20236: Improve file naming and upload handling #20315
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #20236
Summary
Silently normalize the on‑disk filename derived from image attachments to prevent nested directories and path traversal, while preserving the user‑entered
namein the database/UI unchanged.What’s changed
upload_to:C:\fakepath\photo.jpg) and extract the real uploaded extension.namewhen present; sanitize via Django’s filename utility.{object_type.model}_{object_id}and validate the final relative path withvalidate_file_name(..., allow_relative_path=True).bmp,gif,jpeg,jpg,png,webp); otherwise save without an extension.IndexErrorif older files don’t follow the expected underscore pattern.Behavior before
namecould create subdirectories and triggerIndexErrorin code that assumes a fixed filename layout.Behavior after
image-attachments/with a flat, safe basename.namefield is not altered; only the on‑disk filename is normalized.Notes for reviewers
namefield or forms.