🛡️ Sentinel: [HIGH] Fix Unhandled Exception and Secure Failure in SafeStaticFiles#85
Conversation
Co-authored-by: socialawy <24765060+socialawy@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Code Review
This pull request addresses a vulnerability in the SafeStaticFiles middleware where malformed paths caused unhandled exceptions. The implementation now wraps path normalization in a try...except block to ensure the application fails securely with a 400 Bad Request status. Review feedback recommends strengthening the .env file check to inspect all path segments rather than just the leaf node and refactoring the provided test script into a comprehensive test suite with proper assertions for various response codes.
| if "00_config" in p.parts or p.name.startswith(".env") or ".git" in p.parts: | ||
| raise HTTPException( | ||
| status_code=403, detail="Access denied to sensitive resource" | ||
| ) |
There was a problem hiding this comment.
The current security check for .env files only inspects the leaf component of the path (p.name). If a directory were named .env (e.g., .env/config), this check would be bypassed because the leaf name would be config. To be consistent with the checks for 00_config and .git, you should verify if any segment of the path starts with .env.
| if "00_config" in p.parts or p.name.startswith(".env") or ".git" in p.parts: | |
| raise HTTPException( | |
| status_code=403, detail="Access denied to sensitive resource" | |
| ) | |
| if "00_config" in p.parts or ".git" in p.parts or any(part.startswith(".env") for part in p.parts): | |
| raise HTTPException( | |
| status_code=403, detail="Access denied to sensitive resource" | |
| ) |
| response = client.get("/test_server_routes.py") | ||
| print("Response status:", response.status_code) | ||
| # Try accessing via 'None' path, this happens if a request goes through differently? |
There was a problem hiding this comment.
This test script is incomplete and does not effectively verify the security fix. It lacks assertions, meaning it won't fail in a CI environment even if the middleware logic is broken. Additionally, it contains a dangling comment/TODO on line 14. Consider refactoring this into a proper test suite that asserts successful access to allowed files (200 OK), blocked access to sensitive paths (403 Forbidden), and handling of malformed paths (400 Bad Request).
Understood. Acknowledging that this work is now obsolete as it has been superseded by #90. Stopping work on this task. |
🚨 Severity: HIGH
💡 Vulnerability: The
SafeStaticFilesmiddleware crashed with anAttributeErrorwhen accessing paths, because.lower()was incorrectly called on aPathobject. This could be triggered by accessing paths through the static file routes, causing unhandled 500 errors and potentially bypassing or masking the security mechanism.🎯 Impact: Denial of Service (DoS) on static file serving routes, and unpredictable behavior for security controls.
🔧 Fix: Corrected the path parsing logic to perform
.lower()on the string representation (Path(str(path).lower())). Wrapped the validation logic in atry...exceptblock to "fail closed" by raising a 400 Bad RequestHTTPExceptionon malformed paths, rather than crashing.✅ Verification: Ran
uv run python test_app.pyand full test suite viauv run pytest. The static file routes now properly reject sensitive requests or malformed inputs with 403/400 instead of 500 errors.PR created automatically by Jules for task 12386900327120662075 started by @socialawy