From 9845000ccf3f6ba1f14ce15aef81140ad6ae9dbc Mon Sep 17 00:00:00 2001 From: Samika Perera Date: Sun, 15 Nov 2026 16:36:05 +1300 Subject: [PATCH 1/3] Pin that a refused upload names the file, not the server's temp path (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. --- tests/test_upload_refusal_hides_temp_path.py | 129 +++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 tests/test_upload_refusal_hides_temp_path.py diff --git a/tests/test_upload_refusal_hides_temp_path.py b/tests/test_upload_refusal_hides_temp_path.py new file mode 100644 index 0000000..bfb1745 --- /dev/null +++ b/tests/test_upload_refusal_hides_temp_path.py @@ -0,0 +1,129 @@ +"""A refused policy or business-context upload never shows the server's +temp file path. + +WHAT THIS PINS + /api/policy and /api/business-context save the upload to a temp file and + hand that path to the loader (analysis/policy.py, analysis/ + business_context.py), whose refusals for an unreadable file start with + the path: "C:\\Users\\\\AppData\\Local\\Temp\\tmpab12cd.json is not + valid JSON: ...". The endpoint returned that text as the 400 detail, so + the user saw a server folder, including the Windows user name, instead + of the name of the file they chose. Measured on main for four kinds of + bad file on both endpoints: invalid JSON, an integer of 5,000 digits, + nesting 100,000 deep, and bytes that are not UTF-8. + + The refusal must name the file the user uploaded and keep the loader's + reason, word for word, without the path. + +HOW THE PATH IS MADE RECOGNISABLE + tempfile.tempdir is pointed at a folder whose name is a canary and + contains a backslash, so the path is distinctive on every OS and its + repr() spelling (backslashes doubled) differs from its plain one, on + CI's Linux as well as on Windows. + +NO BATFISH, NO OLLAMA -- the loaders refuse before anything is staged. +""" + +import re +import tempfile + +import pytest +from fastapi.testclient import TestClient + +from web import main + +client = TestClient(main.app) + +CANARY = "netwise-temp-canary" + + +@pytest.fixture(autouse=True) +def canary_tempdir(tmp_path, monkeypatch): + """Every temp file the endpoints make lands under a canary folder.""" + folder = tmp_path / (CANARY + "\\x") + folder.mkdir(parents=True) + monkeypatch.setattr(tempfile, "tempdir", str(folder)) + return folder + + +def _assert_no_server_path(detail: str, folder) -> None: + for spelling in (str(folder), str(folder).replace("\\", "\\\\"), CANARY): + assert spelling not in detail, f"the server's temp folder is in the refusal: {detail!r}" + assert "AppData" not in detail, f"a Windows profile folder is in the refusal: {detail!r}" + assert "Users" not in detail, f"a user folder is in the refusal: {detail!r}" + assert not re.search(r"\b[A-Za-z]:[\\/]", detail), f"a drive-letter path is in the refusal: {detail!r}" + + +DEEP = b"[" * 100000 + b"]" * 100000 + +BAD_FILES = [ + ("/api/policy", "my-policy.json", b'{"policy_compliance": [ }', "is not valid JSON: line 1, column 25"), + ("/api/policy", "my-policy.json", b'{"policy_compliance": [{"number": ' + b"9" * 5000 + b"}]}", + "holds a value that cannot be read: Exceeds the limit (4300 digits)"), + ("/api/policy", "my-policy.json", DEEP, "is nested too deeply to be a policy"), + ("/api/policy", "my-policy.json", b'{"policy_compliance": [\xff]}', "is not UTF-8 text: byte 23"), + ("/api/business-context", "assets.json", b'[{"device": }', "is not valid JSON: line 1, column 13"), + ("/api/business-context", "assets.json", b'[{"device": "r", "tier": "critical", "n": ' + b"9" * 5000 + b"}]", + "holds a value that cannot be read: Exceeds the limit (4300 digits)"), + ("/api/business-context", "assets.json", DEEP, "is nested too deeply to be a business context"), + ("/api/business-context", "assets.json", b'[{"device": "\xff"}]', "is not UTF-8 text: byte 13"), +] + + +@pytest.mark.parametrize("url, name, body, reason", BAD_FILES, + ids=[f"{u.rsplit('/', 1)[1]}-{r.split(':')[0][:20]}" for u, _, _, r in BAD_FILES]) +def test_a_refusal_names_the_uploaded_file_not_the_temp_path(canary_tempdir, url, name, body, reason): + response = client.post(url, files={"file": (name, body, "application/json")}) + + assert response.status_code == 400, response.text + detail = response.json()["detail"] + _assert_no_server_path(detail, canary_tempdir) + assert f"'{name}'" in detail, f"the refusal does not name the uploaded file: {detail!r}" + assert reason in detail, f"the loader's reason was lost: {detail!r}" + + +@pytest.mark.parametrize("url, loader_name, error_name, name", [ + ("/api/policy", "load_policy_file", "PolicyError", "my-policy.json"), + ("/api/business-context", "load_business_context_file", "BusinessContextError", "assets.json"), +]) +def test_a_path_quoted_with_doubled_backslashes_is_hidden_too(canary_tempdir, monkeypatch, url, loader_name, + error_name, name): + """An OSError formats its filename with repr(), which doubles every + backslash (found by Arsh on #463 in change impact). A refusal quoting + the path that way must be cleaned too, in both spellings at once.""" + error_type = getattr(main, error_name) + + def refuse(path): + raise error_type(f"{path} could not be read: [Errno 13] Permission denied: {str(path)!r}") + + monkeypatch.setattr(main, loader_name, refuse) + response = client.post(url, files={"file": (name, b"[]", "application/json")}) + + assert response.status_code == 400, response.text + detail = response.json()["detail"] + _assert_no_server_path(detail, canary_tempdir) + assert detail.count(f"'{name}'") == 2, f"both quotes of the path should name the file: {detail!r}" + assert "[Errno 13] Permission denied" in detail, detail + + +def test_a_refusal_that_never_named_a_path_is_unchanged(): + """The control: a refusal with no path in it keeps exactly its words.""" + body = b'{"policy_compliance": [], "policy_compliance": []}' + response = client.post("/api/policy", files={"file": ("my-policy.json", body, "application/json")}) + + assert response.status_code == 400 + assert response.json()["detail"] == ( + "the key 'policy_compliance' appears twice in one object. JSON keeps only " + "the last, so the first would be silently ignored" + ) + + +@pytest.mark.parametrize("url, name, body", [ + ("/api/policy", "my-policy.json", b"{}"), + ("/api/business-context", "assets.json", b"[]"), +]) +def test_a_good_upload_is_unchanged(url, name, body): + response = client.post(url, files={"file": (name, body, "application/json")}) + + assert response.status_code == 200, response.text + assert response.json()["filename"] == name From 2e9d04f0882efec6074d984ca71efd3b26356d53 Mon Sep 17 00:00:00 2001 From: Samika Perera Date: Sun, 15 Nov 2026 16:37:49 +1300 Subject: [PATCH 2/3] Name the uploaded file, not the server's temp path, in upload refusals /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. --- web/main.py | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/web/main.py b/web/main.py index dda5f3f..481d02f 100644 --- a/web/main.py +++ b/web/main.py @@ -2040,6 +2040,39 @@ def delete_all_saved_scans() -> Dict[str, Any]: return {"deleted": deleted} +def _refusal_without_temp_path(message: str, temp_path: Path, display_name: str) -> str: + """A loader's refusal with our temp file's path replaced by the file the + user chose. + + WHY THIS EXISTS + The policy and business-context uploads are validated through a temp + file, and the loaders' refusals for an unreadable file start with + that path: "C:\\Users\\\\AppData\\Local\\Temp\\tmpab12cd.json is + not valid JSON: ...". Returned as is, the user saw a server folder, + Windows user name included, instead of their own file's name. + + THE EXACT PATH, NOT A PATTERN + Only spellings of THIS temp path are replaced, every occurrence: as + written, as repr() quotes it (an OSError doubles every backslash), + and with forward slashes. A pattern matching any path was rejected: + the loaders echo the user's own values ("unknown tier + 'D:\\\\shares\\\\gold'"), and a drive-path regex rewrote that text. + The analysis/ loaders are left alone, so the command line keeps + naming the file it was given. + + WHAT IT DOES NOT CATCH + 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. + """ + shown = f"'{display_name}'" + plain = str(temp_path) + doubled = plain.replace("\\", "\\\\") + for spelling in (repr(plain), doubled, plain, temp_path.as_posix()): + message = message.replace(spelling, shown) + return message + + @app.post("/api/policy") async def upload_policy(file: UploadFile) -> Dict[str, Any]: """Accept and validate a policy file, staging it only if it loads. @@ -2118,7 +2151,10 @@ async def upload_policy(file: UploadFile) -> Dict[str, Any]: staging.write(bytes(contents)) policy = load_policy_file(temp_path) except PolicyError as error: - raise HTTPException(status_code=400, detail=str(error)) from error + raise HTTPException( + status_code=400, + detail=_refusal_without_temp_path(str(error), temp_path, display_name), + ) from error finally: temp_path.unlink(missing_ok=True) @@ -2364,7 +2400,10 @@ async def upload_business_context(file: UploadFile) -> Dict[str, Any]: staging.write(bytes(contents)) context = load_business_context_file(temp_path) except BusinessContextError as error: - raise HTTPException(status_code=400, detail=str(error)) from error + raise HTTPException( + status_code=400, + detail=_refusal_without_temp_path(str(error), temp_path, display_name), + ) from error finally: temp_path.unlink(missing_ok=True) From d4c82d39f7dd65e48d149f4715f286cf57175e6a Mon Sep 17 00:00:00 2001 From: Samika Perera Date: Sun, 15 Nov 2026 16:38:34 +1300 Subject: [PATCH 3/3] Pin that a path named twice in one refusal is hidden both times 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. --- tests/test_upload_refusal_hides_temp_path.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_upload_refusal_hides_temp_path.py b/tests/test_upload_refusal_hides_temp_path.py index bfb1745..7d2f429 100644 --- a/tests/test_upload_refusal_hides_temp_path.py +++ b/tests/test_upload_refusal_hides_temp_path.py @@ -106,6 +106,22 @@ def refuse(path): assert "[Errno 13] Permission denied" in detail, detail +def test_a_path_named_twice_is_hidden_both_times(canary_tempdir, monkeypatch): + """Every occurrence is replaced, not only the first: a refusal can name + the file twice in the same spelling. Replacing only the first left the + second path in place and passed every other test here.""" + def refuse(path): + raise main.PolicyError(f"{path} is not valid JSON, so {path} was not loaded") + + monkeypatch.setattr(main, "load_policy_file", refuse) + response = client.post("/api/policy", files={"file": ("my-policy.json", b"{}", "application/json")}) + + assert response.status_code == 400, response.text + detail = response.json()["detail"] + _assert_no_server_path(detail, canary_tempdir) + assert detail == "'my-policy.json' is not valid JSON, so 'my-policy.json' was not loaded", detail + + def test_a_refusal_that_never_named_a_path_is_unchanged(): """The control: a refusal with no path in it keeps exactly its words.""" body = b'{"policy_compliance": [], "policy_compliance": []}'