Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds granular symlink control for static file serving in Sanic, addressing security concerns around symlinks that point outside the static root directory. Two new parameters (follow_external_symlink_files and follow_external_symlink_dirs) provide fine-grained control over whether file symlinks and directory symlinks pointing outside the root should be served.
Key Changes:
- New security parameters default to
Falseto prevent serving external symlinks by default - Updated path resolution logic to detect and block/allow external symlinks based on configuration
- Directory listings now respect symlink policies and hide disallowed external symlinks
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
tests/test_static_directory.py |
Adds comprehensive test coverage for symlink behavior including internal symlinks, external file/directory symlinks, broken symlinks, and directory listing visibility under various permission combinations |
tests/test_static.py |
Updates existing tests to enable follow_external_symlink_files=True where needed since the symlink fixture creates external symlinks that are now blocked by default |
sanic/models/futures.py |
Adds follow_external_symlink_files and follow_external_symlink_dirs fields to the FutureStatic tuple to store configuration |
sanic/mixins/static.py |
Implements core symlink detection and access control logic in _get_file_path, adds new parameters to static() method and passes them through the handler chain |
sanic/handlers/directory.py |
Adds filtering logic to _iter_files to hide external symlinks from directory listings based on the new parameters, includes helper function _is_path_within_root for security checks |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
* Initial plan * Fix symlink type detection to correctly distinguish file vs directory symlinks Co-authored-by: ahopkins <166269+ahopkins@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ahopkins <166269+ahopkins@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3117 +/- ##
=============================================
+ Coverage 87.869% 87.957% +0.087%
=============================================
Files 105 105
Lines 8079 8113 +34
Branches 1281 1287 +6
=============================================
+ Hits 7099 7136 +37
+ Misses 672 671 -1
+ Partials 308 306 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Closes #3073
Sanic now provides granular control over symlinks in static file serving with two new parameters:
Examples
Secure defaults (block external symlinks):
Allow file symlinks only:
Allow directory symlinks only:
Behavior Notes