🛡️ Sentinel: [security improvement]#98
Conversation
…afeStaticFiles Modified `SafeStaticFiles` middleware in `src/audioformation/server/app.py`: - Added check for `None` path to avoid TypeError. - Wrapped path parsing logic in a `try...except` block to ensure exceptions are securely caught and converted to 400 Bad Request `HTTPException`. - Enhanced the restricted file check to reject any hidden files (starting with `.`) instead of just `.env`. 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 enhances the security and robustness of the static file serving middleware by implementing explicit null checks for paths and wrapping path normalization in a try-except block to prevent unhandled exceptions. It also broadens the security check from specifically targeting '.env' files to all files starting with a dot and updates the project's documentation and dev dependencies. Feedback indicates that the current hidden file check is insufficient as it only validates the filename; a more comprehensive check is suggested to block access to files within hidden directories.
| if "00_config" in p.parts or p.name.startswith(".") or ".git" in p.parts: | ||
| raise HTTPException( | ||
| status_code=403, detail="Access denied to sensitive resource" | ||
| ) |
There was a problem hiding this comment.
The check p.name.startswith(".") only validates the final component of the path. This means that while a hidden file like .env is blocked, a file inside a hidden directory (e.g., .ssh/id_rsa) would be allowed because the filename id_rsa does not start with a dot.
To ensure all hidden resources are blocked, you should check if any part of the path starts with a dot. This also makes the explicit ".git" in p.parts check redundant as it is covered by the more general hidden path check.
| if "00_config" in p.parts or p.name.startswith(".") or ".git" in p.parts: | |
| raise HTTPException( | |
| status_code=403, detail="Access denied to sensitive resource" | |
| ) | |
| if "00_config" in p.parts or any(part.startswith(".") for part in p.parts): | |
| raise HTTPException( | |
| status_code=403, detail="Access denied to sensitive resource" | |
| ) |
🚨 Severity: MEDIUM
💡 Vulnerability: Unhandled Exception / Fail-Open condition in FastAPI custom StaticFiles routing. The original middleware did not check for
Nonepaths and could raise a 500 TypeError during path parsing. Additionally, only.envfiles were explicitly blocked, leaving other hidden/sensitive files potentially exposed.🎯 Impact: An attacker or malformed request could trigger an unhandled 500 error, potentially disrupting service or leaking stack traces if debugging is enabled. Other sensitive dotfiles might have been exposed.
🔧 Fix: Added a strict
path is Nonecheck. Wrapped string parsing and Path logic in atry...exceptblock that safely raises a 400 Bad RequestHTTPException. Changed.name.startswith(".env")to.startswith(".")to comprehensively block hidden files.✅ Verification: Ran
test_safestatic.pywith mocked unhandled states and verified a 400 response is generated instead of a PythonTypeError.PR created automatically by Jules for task 16271295522019933416 started by @socialawy