Skip to content

[PR] Prevent path traversal in static file routes (#912) - #919

Open
lackas wants to merge 1 commit into
AnswerDotAI:mainfrom
lackas:fix-static-path-traversal
Open

[PR] Prevent path traversal in static file routes (#912)#919
lackas wants to merge 1 commit into
AnswerDotAI:mainfrom
lackas:fix-static-path-traversal

Conversation

@lackas

@lackas lackas commented Aug 14, 2026

Copy link
Copy Markdown

Related Issue

Fixes #912.

Proposed Changes

static_route and static_route_exts interpolate the {fname:path} URL segment straight into FileResponse(f'{static_path}/{fname}…'). The path converter matches .., so /%2e%2e/secret.md resolves outside static_path and serves any file the worker process can read.

New helper _static_fpath resolves the request against static_path and raises HTTPException(404) if it escapes; both handlers route through it. The containment check is the one Starlette's own StaticFiles.lookup_path uses (realpath + commonpath), so a symlink inside static_path pointing outside it is also rejected.

Nested paths and an explicit static_path above the app dir (e.g. static_path='../..', as in the existing tests) are unaffected. Only .. within the request path is rejected.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed (nbdev_test --path nbs/api/00_core.ipynb). Both new assertions fail on unpatched main.
  • I am aware that this is an nbdev project, and I have edited, cleaned, and synced the source notebooks instead of editing .py or .md files directly.

@lackas
lackas force-pushed the fix-static-path-traversal branch from e2a3703 to 409f355 Compare August 27, 2026 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] static_route_exts / static_route allow directory traversal via fname path segments

1 participant