Filenames that become commands

An upload’s original name is data. When that name is placed in a command line, a script, or an environment variable the program executes, the name is no longer a label.

Lesson 63 of 69 · 14 min read · practice only with authorization

Diagram

From an uploaded name to a process argument

Follow the path. One example at a time.

Step 1 of 4

Upload

The browser sends a filename. You only needed a label for the person who uploaded it.

Illustration: the display name is not the stored name
display_name = "scan.png"
# Show this name to the person.
# Do not use it as the path you execute.
What changes the outcome
Disk

The file is saved under the client’s name.

Later

A job interpolates that name into a command.

Result

The name can change what the job runs.

The example on the open step is one illustration, or the fix you can paste into an application you own.

What this page is made of

4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.

  1. Display name versus stored name

    What fails
    The person wants to see the name they chose. The disk and the process do not need that string.
    Who is affected
    Users who see a wrong label if you drop it, and everyone who shares the process account if you execute it.
    What you would notice
    One column used both as the path and as the label in the UI.
    What you change
    Split them. Generate the path. Encode the label when you render it.
  2. Scripts and environment

    What fails
    A filename in an environment variable or a generated shell script is still a command if something executes that script.
    Who is affected
    The account that runs the script.
    What you would notice
    A job template that quotes a name into a script file “for logging” and then runs the file.
    What you change
    Pass arguments in an array from the scheduler. Do not generate shell scripts from upload metadata.
  3. The directory listing

    What fails
    Serving a directory of original names also creates traversal and encoding bugs next door. Generated names avoid that class.
    Who is affected
    Other tenants who share the directory.
    What you would notice
    An upload folder that is also a static file route.
    What you change
    Keep scratch private. Serve files through a handler that looks up the id and the owner.
  4. What review looks for

    What fails
    Search for the original filename variable in any exec, spawn, or script template.
    Who is affected
    The owner who has to rebuild a host if the process could write outside scratch.
    What you would notice
    A ticket that says the filename is safe because the UI shows it as text.
    What you change
    The UI being text does not constrain the job. Change the job.

A filename is a label until you execute it

Uploads arrive with a name the person’s computer chose. That string is useful in the interface and dangerous in a path or a command. The safe split is boring: generate the name on disk, store the original string in a column you treat as text, and render it encoded. The job that converts or scans the file receives the generated path only.

Teams skip the split because one string is easier to pass around. Six months later a background job interpolates it. The feature that “only displays a name” becomes a process argument without anyone thinking they added command execution. The data model is what prevents that.

Generated scripts are command lines

Writing a shell script that includes the filename, then executing the script, is a shell, even if the first version only used the script for logging. Schedulers, makefiles, and “run this uploaded tool” admin features do this. Pass an argument vector from the scheduler. Keep the script, if you need one, as a constant file in the repo.

Environment variables are not a safer channel. If any script expands them without care, the variable is the command. Put identifiers in arguments you built in code, not in an environment inherited from the request.

What you would notice

Look at the upload handler and the job side by side. If the same string is the on-disk path, the process argument, and the HTML label, the feature is one refactor away from a bad day and may already be there. Also look at admin tools that reprocess “by filename.”

The people affected are whoever shares the application user. A display bug is a separate issue: encode the label so it cannot become HTML. That encoding does not make the string safe to execute. They are different contexts. Fix both.

What you change

Add the stored-name column. Migrate by generating names and moving files, not by trusting the old paths in new code. Point every exec at the scratch path helper. Delete any template that string-builds a command. Serve downloads through an authorized id lookup so the private scratch directory is not a static route.

Test that the argument vector contains the generated name and does not contain the display name. That assertion is the regression you want. It stays inside a unit test with files you created.

Check yourself

Recognition and the fix. Nothing here is a procedure.

1 / 3

A converter is invoked with the shell off, but the path is the original upload filename, which the client chose. What can still fail?

Sibling techniques

Pages at the same depth, plus a lab and a glossary card for this one.

  1. MapOS command injection: when input reaches a shellHow features that call system tools — ping, image conversion, PDF generation, archive handling — pass user input to a shell, how to recognize it in code and in an authorized test with a timing probe, and how argument arrays and libraries remove the shell.
  1. 01Argument injection without a shellSkipping the shell is necessary and not sufficient.

Practice

  1. LabFile upload validationNine uploads hit a profile picture endpoint: two real photos and seven files that should never be stored as sent. Turn on checks until every one is handled.

Glossary

  1. CardCommand injectionUser input is concatenated into a shell command, so the server runs instructions the developer did not plan.