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)