From 9295dc883b5e1e4e2222afff0d3d4e8b70379304 Mon Sep 17 00:00:00 2001 From: madara88645 <163588475+madara88645@users.noreply.github.com> Date: Sun, 13 Sep 2026 22:56:13 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITICAL]?= =?UTF-8?q?=20Fix=20arbitrary=20sensitive=20file=20upload=20bypass?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🚨 Severity: CRITICAL 💡 Vulnerability: The `normalize_uploaded_filename` utility checked for sensitive names (e.g. `.env`, `.git`) but was missing checks for some keys (e.g., `id_rsa`, `.pem`) that were present in the centralized `_contains_sensitive_segment`. This allowed these files to be uploaded. 🎯 Impact: Attackers could upload arbitrary sensitive files such as SSH keys or PEM certificates that bypass basic checks but might be exploited elsewhere. 🔧 Fix: Replaced inline subset checks with a call to the robust `_contains_sensitive_segment` utility. ✅ Verification: Ran unit tests and ensured that `id_rsa` and `test.pem` are blocked correctly. --- .jules/sentinel.md | 4 ++++ app/utils/security.py | 16 +++++++--------- tests/test_security.py | 2 ++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 593cbbd..59ec027 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -77,3 +77,7 @@ **Vulnerability:** The `ExplainRequest`, `ChatRequest`, and `GhostNarrateRequest` Pydantic models lacked LLM input sanitization (`sanitize_llm_input`) for the string list parameters (`callers`, `callees`, `neighbors`, `context_nodes`). These fields were only validated for maximum length. Without applying standard regex filtering via `sanitize_llm_input`, an attacker could potentially execute a prompt injection attack through these context node identifiers when they are serialized into backend LLM prompts. **Learning:** Pydantic validators (`@field_validator`) for complex types (like lists of strings) must iterate and individually apply sanitization filters to every element, just as scalar string fields do. Relying solely on `max_length` prevents DoS but does not prevent logic/injection attacks if special characters remain unsanitized. **Prevention:** Always apply the standard `sanitize_llm_input` function to every string passed into LLM prompts, even if those strings are nested inside list or dictionary parameters. +## 2024-05-31 - [Normalize uploaded filename uses unified sensitivity check] +**Vulnerability:** The `normalize_uploaded_filename` utility checked for sensitive names (e.g. `.env`, `.git`) but was missing checks for some keys (e.g., `id_rsa`, `.pem`) that were present in the centralized `_contains_sensitive_segment`. This allowed these files to be uploaded. +**Learning:** Defense mechanisms (like sensitive file blocklists) that have a robust centralized check (`_contains_sensitive_segment`) should be utilized everywhere paths are parsed to prevent discrepancy between code paths. +**Prevention:** Unify validation logic by replacing inline subset checks with the robust `_contains_sensitive_segment` utility. diff --git a/app/utils/security.py b/app/utils/security.py index 0ddbcb2..42b6964 100644 --- a/app/utils/security.py +++ b/app/utils/security.py @@ -101,16 +101,14 @@ def normalize_uploaded_filename(raw_name: str | None) -> str: if ".." in parts: raise HTTPException(status_code=400, detail=f"Unsafe upload path: {raw_name}") - sensitive_names = {".env", ".git", ".ssh", ".aws", ".npmrc", ".pypirc", ".netrc"} - for part in parts: - part_lower = part.lower() - if part_lower.startswith(".env") or part_lower in sensitive_names: - raise HTTPException( - status_code=400, - detail=f"Sensitive hidden file or directory not allowed: {part}", - ) - safe_rel = "/".join(parts) + + if _contains_sensitive_segment(safe_rel): + raise HTTPException( + status_code=400, + detail=f"Sensitive hidden file or directory not allowed in path: {raw_name}", + ) + if os.path.isabs(safe_rel): raise HTTPException(status_code=400, detail=f"Unsafe upload path: {raw_name}") diff --git a/tests/test_security.py b/tests/test_security.py index 8fafca6..2d575cc 100644 --- a/tests/test_security.py +++ b/tests/test_security.py @@ -138,6 +138,8 @@ def test_normalize_uploaded_filename_blocks_sensitive_hidden_names_case_insensit ".NPMRC", ".PyPiRc", ".NETRC", + "id_rsa", + "test.pem", ]: with pytest.raises(HTTPException) as exc_info: normalize_uploaded_filename(raw_name)