[NEW QUERY] Add pathlib.Path.resolve() and is_relative_to() as path injection sanitizers#21
Draft
[NEW QUERY] Add pathlib.Path.resolve() and is_relative_to() as path injection sanitizers#21
Conversation
…itizers Extend the standard py/path-injection query to recognize: - pathlib.Path.resolve() as Path::PathNormalization::Range - pathlib.Path.is_relative_to() as Path::SafeAccessCheck::Range This enables the two-state flow analysis (NotNormalized → NormalizedUnchecked) to correctly handle the resolve() + is_relative_to() sanitization pattern, preventing false positives when both are used together. Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> Agent-Logs-Url: https://github.com/testing-felickz/codeql-development-template/sessions/933a78be-b382-4058-bda3-08e9eac28cb0
- Rename resolveAttr to resolveMethodAccess for clarity - Add documentation comment on the checks predicate explaining branch=true logic Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> Agent-Logs-Url: https://github.com/testing-felickz/codeql-development-template/sessions/933a78be-b382-4058-bda3-08e9eac28cb0
Copilot
AI
changed the title
[WIP] Fix CodeQL misunderstanding of sanitized dataflow in py-path-injection
[NEW QUERY] Add pathlib.Path.resolve() and is_relative_to() as path injection sanitizers
Mar 25, 2026
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.
📝 Query Information
python/detect-unsanitized-rglob-path-traversal🎯 Description
What This Query Detects
The standard
py/path-injectionquery does not recognizepathlib.Path.resolve()as path normalization orpathlib.Path.is_relative_to()as a safe access check — despite theiros.pathequivalents (realpath,startswith) already being modeled. This causes false positives when the idiomatic pathlib sanitization pattern is used.This PR extends the standard library's two-state flow machine (
NotNormalized→NormalizedUnchecked) via its OO extension points:PathlibResolveCallextendsPath::PathNormalization::Range(analogous toOsPathRealpathCallinStdlib.qll:~1065)IsRelativeToCallextendsPath::SafeAccessCheck::Range(analogous toStartswithCallinStdlib.qll:~5090)Both sanitizers must be applied together (normalize then check) for the flow to be blocked — matching the standard library's intended design.
Example Vulnerable Code
Example Safe Code
Upstream re-bundling note
CodeQL is extensible via OO here — no custom query would be needed if these two classes were added directly to
semmle/python/frameworks/Stdlib.qllin the standard library. The custom query exists only because the standardpy/path-injectiondoesn't import our extensions.🧪 Testing
Seven scenarios tested: unsanitized rglob, sanitized rglob (resolve+is_relative_to), resolve-only, check-only, sanitized open, unsanitized open, and existing
realpath+startswithregression.📋 Checklist
.mdand.qhelp)🔗 References
OsPathRealpathCallandStartswithCallinStdlib.qllNote: This query was developed using Test-Driven Development methodology.
📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.