Skip to content

Fix code scanning path injection alerts in media endpoint#1607

Merged
romanlutz merged 1 commit intomicrosoft:mainfrom
romanlutz:fix/code-scanning-path-injection
Apr 14, 2026
Merged

Fix code scanning path injection alerts in media endpoint#1607
romanlutz merged 1 commit intomicrosoft:mainfrom
romanlutz:fix/code-scanning-path-injection

Conversation

@romanlutz
Copy link
Copy Markdown
Contributor

Summary

Refactor the /api/media\ endpoint to resolve 3 CodeQL \py/path-injection\ alerts (#19, #20, #21).

Changes

  • Extract path validation into a dedicated _validate_media_path\ helper
  • Use \os.path.realpath\ + \str.startswith\ sanitization pattern (the standard pattern recognized by CodeQL's path injection analysis) instead of \pathlib.resolve\ + \is_relative_to\
  • Use \os.path\ operations for file checks and \FileResponse\ construction so the validated path flows through consistently

Security model (unchanged)

All existing guards are preserved:

  1. Path must resolve under the configured
    esults_path\
  2. Must be in an allowed subdirectory (\prompt-memory-entries\ or \seed-prompt-entries)
  3. Must have an allowed file extension (media types only)

All 12 existing tests pass without modification.

Refactor serve_media_async to use os.path.realpath + str.startswith
sanitization pattern (recognized by CodeQL py/path-injection) instead
of pathlib resolve + is_relative_to. Extract validation into a dedicated
_validate_media_path helper.

Resolves code scanning alerts microsoft#19, microsoft#20, microsoft#21.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@romanlutz romanlutz merged commit 10e7542 into microsoft:main Apr 14, 2026
38 checks passed
@romanlutz romanlutz deleted the fix/code-scanning-path-injection branch April 14, 2026 13:49
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.

2 participants