Don't Use Uploaded File Names as Storage Paths
uploads a file named ../../report.pdf, does not mean the server should save the file under that name. The filename originates from the user and constitutes inpu
If you accept file uploads, you will eventually receive a file named ../../report.pdf. This guide is for developers who store user uploads, handle document translation, or build pipelines around object storage. It explains why the name a user gives a file should not decide where the server saves it, and how to separate the two.
You will learn how to keep the display name for the interface while the server assigns its own internal identifier. You will also see why filename cleanup alone does not identify a file, and how the same boundary applies to object storage such as R2. The last section shows how file identity helps when a translation workflow retries, downloads, or proofreads a document.
Should an uploaded file name become its storage path? The short answer
No. Keep the user's name for display only, and let the server choose an internal ID and storage suffix.
| Your situation | What to do | Why |
|---|---|---|
The user uploads a file named ../../report.pdf | Treat the name as input data and do not use it as a disk path | The filename comes from the user, while the disk path falls within the system's permission boundary |
| You need to show the original name in the interface | Store the display name separately from the storage name | The display name can stay as the user typed it, while the server controls the storage name |
| Two users upload files with the same display name | Assign each upload its own internal ID | Identical names should not overwrite each other |
| You choose the file extension for storage | Derive the suffix from your allowed list | Copying the user's extension trusts input you have not verified |
| You use R2 or another object store | Do not concatenate tenant strings directly into shared prefixes | Object keys still affect isolation, overwriting, and public URLs |
Why a file name is not a storage path
A user who uploads a file named ../../report.pdf has not told the server where to save it. The filename originates from the user and constitutes input data. The disk path, on the other hand, falls within the system's permission boundary.
Mixing these two concepts can easily lead to:
- Path traversal, where the saved file escapes the intended folder
- Overwriting, where files with identical names replace each other
- Platform inconsistencies, where Windows and Linux handle paths differently
Separate the display name from the storage name
Keep the display name for the interface
The display name keeps the original name so users can recognise their file. It is used only for showing information on screen.
Assign an internal identifier
The server gives each upload its own internal identifier. The storage name is built from that identifier, not from the user's text.
Derive the suffix from an allowed list
The storage suffix should come from the server's allowed list. Do not simply copy the extension the user provided.
The example below only constructs a path. It does not create any directories or files.
from pathlib import Path
import re
def storage_path(root, internal_id, extension):
if not re.fullmatch(r"[a-f0-9]{32}", internal_id):
raise ValueError("invalid internal id")
if extension not in {".pdf", ".docx", ".udoc"}:
raise ValueError("unsupported extension")
base = Path(root).resolve()
candidate = (base / (internal_id + extension)).resolve()
if candidate.parent != base:
raise ValueError("outside storage root")
return candidate
root = Path.cwd() / "synthetic-uploads"
identifier = "a" * 32
p = storage_path(root, identifier, ".pdf")
assert p.parent == root.resolve()
assert p.name == identifier + ".pdf"
for bad in ["../../report", "A" * 32, "a" * 31, "a" * 32 + "/x"]:
try:
storage_path(root, bad, ".pdf")
except ValueError:
pass
else:
raise AssertionError(bad)
Note: This example is a demonstration. Keep the original copies of important files, and check results against the boundaries and acceptance criteria in your own documentation before processing them.
The example assumes that the server has already generated the internal ID. It is not a secure file-writing implementation against arbitrary disk-based attacks:
- A race condition can still occur between the check and the actual open.
- Parent directory permissions must be controlled separately.
- Symbolic-link policies must be controlled separately.
- A unique ID does not mean you can ignore your "prohibit overwriting" policy.
For path handling in Python, see the Python pathlib documentation.
Cleaning up a file name does not identify the file
Removing dangerous characters from a name does not tell you what the file is or how to store it safely. Each problem belongs at a different layer.
| Input phenomenon | Where it should be handled | Shortcomings |
|---|---|---|
| Name contains a path separator | Not used as a storage path | Merely removing the slash is insufficient |
| Extension is PDF | Content detection and parsing | The extension can be forged |
| Display name contains HTML | Escaping at interface output | Cannot be solved by changing the disk name |
| Identical display name | Isolation by internal ID | Should not overwrite the previous upload |
Object storage has the same boundary
R2 and other object stores do not fully reproduce the semantics of traditional directory traversal. Object keys still shape isolation, overwriting, and public URLs, so the same caution applies:
- Do not directly concatenate tenant-provided strings into shared prefixes.
- Do not let the client decide authorisation based only on key names.
Give files an identity before translation starts
Files should have an identity even before they enter the translation process. In OmniDoc (Product Entry), you can keep familiar file names on screen while the system uses independent task IDs and file IDs internally.
This clear identity helps you:
- Match failed retries to the correct upload
- Download the right output for each task
- Track subsequent proofreading without confusing files
- Avoid mistaking another person's file with the same name for the current result
🎯 Which approach fits you?
If you accept user uploads
Store every upload under a server-generated internal ID, and keep the user's name only for display.
If you use object storage such as R2
Build object keys from controlled values, and never let tenant strings define shared prefixes.
If you run a document translation workflow
Give each task and file its own ID, so retries, downloads, and proofreading stay tied to the right document.
❓ FAQ
1. Can I just remove slashes from the uploaded file name?
No. Removing the path separator is insufficient, and the name still should not be used as a storage path.
2. Is checking the file extension enough to trust a PDF?
No. The extension can be forged, so content detection and parsing should handle file identification.
3. Does a unique internal ID mean I can allow overwriting?
No. A unique ID does not mean you can ignore your prohibit-overwriting policy.
4. Does object storage avoid path traversal problems?
Not entirely. R2 and other object stores do not fully capture traditional directory traversal, but object keys still affect isolation, overwriting, and public URLs.
📝 Summary
An uploaded file name is user input, not a storage location. Keep the display name for the interface, assign an internal ID, and derive the storage suffix from an allowed list.
Try it now: upload a document and get it translated with the layout intact


