[PR] Prevent path traversal in static file routes (#912) - #919
Open
lackas wants to merge 1 commit into
Open
Conversation
static_route and static_route_exts interpolate the {fname:path} URL
segment straight into FileResponse(f'{static_path}/{fname}...'). The path
converter matches '..', so a request like /%2e%2e/%2e%2e/secret.gz
resolves outside static_path and serves any file the process can read.
Add _static_fpath, which resolves the requested path against static_path
and raises 404 when it escapes, and route both static handlers through
it. Nested paths and an explicit static_path above the app dir still
work; only '..' within the request path is rejected.
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issue
Fixes #912.
Proposed Changes
static_routeandstatic_route_extsbuild the served path by interpolating the{fname:path}URL segment straight intoFileResponse(f'{static_path}/{fname}…'). Since thepathconverter matches.., a request such as/%2e%2e/%2e%2e/secret.gzresolves outsidestatic_pathand serves any file the worker process can read (confirmed on 0.14.11 — an encoded/%2e%2e/secret.mdreturns a sibling file that exists outside the static dir).This adds a small helper
_static_fpath, which resolves the requested path againststatic_pathand raisesHTTPException(404)when it escapes; both static handlers now route through it. Legitimate use is unchanged — nested paths, and an explicitstatic_pathabove the app dir likestatic_path='../..'(as used in the existing tests), still work; only..within the request path is rejected.This does not change the separate discussion in #918 about the default
static_path='.'serving the app root; it only closes the traversal escape.Types of changes
Checklist
00_coresuite has one pre-existing, unrelated failure — the<title>Default</title>assertion — which also fails on unpatchedmainin a clean environment.)Additional Information
The containment check uses
os.path.realpath(resolving symlinks), matching Starlette's ownStaticFilesbehavior (follow_symlink=False) — this also closes a symlink-based escape where a link insidestatic_pathpoints outside it. If you'd prefer to preserve symlink-following,os.path.abspath+normpathwould also close the traversal; happy to switch.