Skip to content

Name the uploaded file, not the server temp path, in upload refusals - #475

Merged
patelankeet2 merged 3 commits into
mainfrom
fix/refuse-messages-hide-temp-path
Nov 19, 2026
Merged

patelankeet2 merged 3 commits into
mainfrom
fix/refuse-messages-hide-temp-path

Conversation

@SamikaPerera

Copy link
Copy Markdown
Collaborator

Closes #474. web/ is mine, so this stays in web/main.py and leaves the loaders in analysis/ alone.

What changed

  • web/main.py:2043 adds _refusal_without_temp_path(message, temp_path, display_name). It replaces every spelling of this upload's temp path in a loader's refusal with the quoted name of the file the user uploaded: as written, as repr() quotes it (an OSError doubles every backslash), and with forward slashes. Every other word of the message is kept.
  • web/main.py:2156 (/api/policy) and :2405 (/api/business-context) use it in place of detail=str(error).

Before and after

Measured with the real app and TEMP and TMP pointed at an empty folder. The temp path is written as <temp path>.

endpoint bad file before after
/api/policy invalid JSON <temp path> is not valid JSON: line 1, column 25: Expecting value 'mine.json' is not valid JSON: line 1, column 25: Expecting value
/api/policy a 5,000-digit integer <temp path> holds a value that cannot be read: ... 'mine.json' holds a value that cannot be read: ...
/api/policy nested 100,000 deep <temp path> is nested too deeply to be a policy 'mine.json' is nested too deeply to be a policy
/api/policy not UTF-8 <temp path> is not UTF-8 text: byte 23 cannot be decoded. ... 'mine.json' is not UTF-8 text: byte 23 cannot be decoded. ...
/api/business-context the same four <temp path> ... 'ctx.json' ..., same words
either duplicate key, unknown section, unknown tier no path unchanged

Why the exact path and not a pattern

Three options were measured:

  1. Replace the exact temp path. Chosen. It touches only text the web layer itself introduced.
  2. A regex for any path. Rejected. The loaders echo the user's own values back. Uploading a context with tier D:\shares\gold gives unknown tier 'D:\\shares\\gold', and a drive-path regex rewrote that, swallowing the closing '. too.
  3. Change what the loader is given. Rejected.
    • A file named after the upload still sits in a temp folder, so the folder still shows.
    • A relative path needs os.chdir, which is process-wide and unsafe with concurrent requests.
    • Parsing the bytes in web/ would duplicate the loaders' messages, which the comment above the policy upload explicitly avoids.

Tests, red then green on purpose

tests/test_upload_refusal_hides_temp_path.py, 14 tests:

  • the four bad files on both endpoints: no temp folder, no AppData, no Users, no drive-letter path, and the detail keeps the uploaded name and the loader's reason
  • a refusal quoting the path with doubled backslashes, on both endpoints
  • a refusal naming the path twice
  • controls: a refusal that never had a path keeps its exact words, and a good upload of each kind is unchanged

The temp folder is a canary whose name contains a backslash, so the doubled spelling differs from the plain one on Linux as well as on Windows.

The commits are in order on purpose:

  1. Tests only. 10 fail on main (the temp folder is in the refusal), and the 3 controls pass.
  2. The fix. All pass.
  3. One more test, from mutation testing (below).

Mutation check

Each mutant ran in a scratch copy, one at a time:

mutant result
remove the replacement killed, 11 fail
replace only the first occurrence survived the first 13 tests; killed by the third commit's test
apply on /api/policy only killed, the 5 business-context cases fail
do not handle doubled backslashes killed, the 2 doubled-backslash cases fail

Counts

tests/test_upload_refusal_hides_temp_path.py     14 passed
full suite, Batfish running                      2340 passed, 13 skipped
ruff check --select E4,E7,E9,F (changed files)   All checks passed
merges cleanly with #470 (e7c3a69): its tests and the upload tests, 147 passed
merges cleanly with #467 (5087466): the loader and upload tests, 206 passed

What this does not change

  • The loaders in analysis/: the command line still names the real file it was given.
  • /api/upload: it already names the uploaded file and never showed a path.
  • A different spelling of the same location, such as a resolved or 8.3 short path, or the temp folder quoted without the file name. Neither appears in any refusal today.
  • /api/ask and /api/policy/draft, which pass describe_error() text that can name the session's config folder. That is not reached by a bad upload and is noted on the issue for a separate look.

…(red)

/api/policy and /api/business-context validate the upload through a temp
file, and the loaders' refusals for an unreadable file start with that
path. Measured on main, four kinds of bad file on each endpoint (invalid
JSON, a 5,000-digit integer, nesting 100,000 deep, bytes that are not
UTF-8) put the full temp path, Windows user folder included, into the 400
detail.

These tests assert the detail names the uploaded file, keeps the loader's
reason, and contains no temp folder, no AppData or Users folder and no
drive-letter path. A second test covers a path quoted with doubled
backslashes, as an OSError prints it. Two controls show a refusal with no
path and a good upload are unchanged.

Red on purpose: 10 fail on main, the 3 controls pass. The fix is the next
commit.
/api/policy and /api/business-context now pass a loader's refusal through
_refusal_without_temp_path(), which replaces every spelling of this
upload's temp path (as written, as repr() quotes it with doubled
backslashes, and with forward slashes) by the quoted name of the file the
user uploaded. Every other word of the message is kept.

Only the exact temp path is replaced. A pattern matching any path was
measured and rejected: the loaders echo the user's own values, and a
drive-path regex rewrote "unknown tier 'D:\shares\gold'". The loaders
in analysis/ are unchanged, so the command line still names the file it
was given.

The 10 tests from the previous commit now pass, with the 3 controls.
Mutation testing the previous commit: replacing only the first occurrence
of each spelling passed every test, because each test named the path once
per spelling. This test has a refusal naming the same path twice and
asserts the exact message, so that mutant now fails.

@shubhamkataria2005 shubhamkataria2005 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving d4c82d3. Same leak as my #462, same cure: replace only this exact path, and you've thought through all its spellings.

Checked:

  • Merged onto current main (ee769b4): 2340 passed, 13 skipped, ruff clean on CI's selection.
  • Both endpoints are pinned: unwrapping /api/policy fails 6 and unwrapping /api/business-context fails 5. Replacing only the plain spelling fails 2.
  • Echoing the filename is safe: display_name is reduced to its basename, and showPolicyMessage/showBusinessContextMessage set textContent, never innerHTML, so a hostile filename is shown as text.
  • Rejecting a drive-path regex was right. Your D:\\shares\\gold tier example is exactly why.

One gap, not blocking: the same one Arsh found on my #463. Dropping the doubled, the repr() or the as_posix() spelling, one at a time, survives all 14 tests. On Linux (CI, and these tests) a temp path has no backslashes, so those spellings are identical to the plain one, and "a refusal quoting the path with doubled backslashes" doesn't actually contain a doubled backslash there. Arsh's trick on #463 fixes that: put a backslash in a folder name you control (e.g. a tmp_path / "up\\load" temp directory), so the doubled form differs on every OS. Then the Windows case you designed for is tested where CI runs.

@patelankeet2
patelankeet2 merged commit 48e2db2 into main Nov 19, 2026
2 checks passed
@patelankeet2
patelankeet2 deleted the fix/refuse-messages-hide-temp-path branch November 19, 2026 01:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refusal messages for a bad policy or business-context upload show the server temp path

3 participants