fix(files): prevent path traversal in /api/files endpoint#233
Open
sebastiondev wants to merge 1 commit into
Open
fix(files): prevent path traversal in /api/files endpoint#233sebastiondev wants to merge 1 commit into
sebastiondev wants to merge 1 commit into
Conversation
Resolve the requested path and verify it lives strictly inside one of the allowed directories, instead of comparing the unresolved relative path with string prefixes. This blocks inputs containing '..' segments or symlinks that escape the intended sandbox (CWE-22).
|
|
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.
Summary
The
GET /api/files/{file_path:path}endpoint inapi/routers/files.pyis vulnerable to path traversal (CWE-22). An unauthenticated remote attacker can read arbitrary files that the server process can access (e.g./etc/passwd, application config with API keys, private SSH keys) by supplying..segments in the path parameter.Vulnerability details
api/routers/files.py@router.get("/{file_path:path}")→ mounted at/api/files/...viaapp.include_router(files_router, prefix=api_config.api_prefix)inapi/app.pydependencies=, and no app-wide auth middleware is installed. The server binds0.0.0.0by default.Root cause
The previous containment check compared the raw, unresolved path against the allow-listed prefixes as a string:
For input
output/../../../etc/passwd:full_pathstarts withoutput/, so the first allow-list check passes.abs_path = Path.cwd() / "output/../../../etc/passwd"—pathlibdoes not normalize..segments.abs_path.relative_to(Path.cwd())returnsPosixPath('output/../../../etc/passwd'), whose string form starts withoutput/, so the second check passes as well.FileResponse(abs_path)then hands the path to the OS, which resolves the traversal and reads the file outside the sandbox.Fix
Resolve the path first, then verify it is strictly contained within one of the allow-listed directories using
pathlib.Path.parents(not string prefix matching):resolve()collapses..segments and symlinks before the containment check, so any path that escapes the intended directories is rejected with 403 beforeFileResponseis called. Theparents-based comparison also avoids the classic/tmp/output_evilvs/tmp/outputprefix-collision bug thatstartswithwould allow.Proof of concept
Start the app locally (default bind is
0.0.0.0:8080) and request a traversal payload:Legitimate requests to files under
output/,workflows/,templates/,bgm/,data/bgm/,data/templates/,resources/continue to work.Testing
%2e%2e%2f...) are also rejected — FastAPI/Starlette decodes them before the handler sees the value, so they go through the sameresolve()check.Adversarial review
Before submitting we tried to disprove this. We checked whether the router or app installs any auth dependency that would gate the endpoint (
grep -n 'dependencies=\|include_router' api/app.py api/routers/files.py) — none is present, and the defaultapi_config.api_prefixis/api, so the route really is reachable unauthenticated. We also confirmed FastAPI's{file_path:path}converter does not strip..segments, and thatPath.cwd() / "output/../../../etc/passwd"is not normalized bypathlibwithout an explicitresolve()— matching the observed bypass of the originalstartswithcheck.Scope
Single-file change, only touches the
get_filehandler; no API surface changes, no new dependencies.Discovered by the Sebastion AI GitHub App.