[PR #20315] [MERGED] Fixes #20236: Improve file naming and upload handling #15886

Closed
opened 2025-12-30 00:24:36 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/20315
Author: @pheus
Created: 9/10/2025
Status: Merged
Merged: 9/13/2025
Merged by: @jnovinger

Base: mainHead: 20236-sanitize-image-attachment-name


📝 Commits (1)

  • 615cad3 fix(extras): Improve file naming and upload handling

📊 Changes

4 files changed (+273 additions, -22 deletions)

View changed files

📝 netbox/extras/models/models.py (+4 -2)
📝 netbox/extras/tests/test_models.py (+85 -7)
📝 netbox/extras/tests/test_utils.py (+142 -1)
📝 netbox/extras/utils.py (+42 -12)

📄 Description

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 name in the database/UI unchanged.

What’s changed

  • In upload_to:
    • Normalize browser paths (e.g. C:\fakepath\photo.jpg) and extract the real uploaded extension.
    • Use the user‑entered name when present; sanitize via Django’s filename utility.
    • Prefix with {object_type.model}_{object_id} and validate the final relative path with validate_file_name(..., allow_relative_path=True).
    • Preserve only known image extensions (bmp, gif, jpeg, jpg, png, webp); otherwise save without an extension.
  • (Hardening) Guard filename parsing to avoid IndexError if older files don’t follow the expected underscore pattern.

Behavior before

  • Supplying slashes in the name could create subdirectories and trigger IndexError in code that assumes a fixed filename layout.

Behavior after

  • Files are always stored under image-attachments/ with a flat, safe basename.
  • The value shown/stored in the name field is not altered; only the on‑disk filename is normalized.

Notes for reviewers

  • This follows the maintainer guidance to silently normalize rather than reject slashes.
  • No user‑visible changes to the name field or forms.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/20315 **Author:** [@pheus](https://github.com/pheus) **Created:** 9/10/2025 **Status:** ✅ Merged **Merged:** 9/13/2025 **Merged by:** [@jnovinger](https://github.com/jnovinger) **Base:** `main` ← **Head:** `20236-sanitize-image-attachment-name` --- ### 📝 Commits (1) - [`615cad3`](https://github.com/netbox-community/netbox/commit/615cad3fa7f82f97dc7ff1a6bd85c0a917be6bc5) fix(extras): Improve file naming and upload handling ### 📊 Changes **4 files changed** (+273 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `netbox/extras/models/models.py` (+4 -2) 📝 `netbox/extras/tests/test_models.py` (+85 -7) 📝 `netbox/extras/tests/test_utils.py` (+142 -1) 📝 `netbox/extras/utils.py` (+42 -12) </details> ### 📄 Description ### 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 `name`** in the database/UI unchanged. **What’s changed** - In `upload_to`: - Normalize browser paths (e.g. `C:\fakepath\photo.jpg`) and extract the real uploaded extension. - Use the user‑entered `name` when present; sanitize via Django’s filename utility. - Prefix with `{object_type.model}_{object_id}` and validate the final relative path with `validate_file_name(..., allow_relative_path=True)`. - Preserve only known image extensions (`bmp`, `gif`, `jpeg`, `jpg`, `png`, `webp`); otherwise save without an extension. - (Hardening) Guard filename parsing to avoid `IndexError` if older files don’t follow the expected underscore pattern. **Behavior before** - Supplying slashes in the `name` could create subdirectories and trigger `IndexError` in code that assumes a fixed filename layout. **Behavior after** - Files are always stored under `image-attachments/` with a flat, safe basename. - The value shown/stored in the `name` field is **not altered**; only the on‑disk filename is normalized. **Notes for reviewers** - This follows the maintainer guidance to **silently normalize** rather than reject slashes. - No user‑visible changes to the `name` field or forms. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:24:36 +01:00
adam closed this issue 2025-12-30 00:24:36 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15886