Java: recognize Path.toRealPath() as path normalization sanitizer#21652
Open
MarkLee131 wants to merge 1 commit intogithub:mainfrom
Open
Java: recognize Path.toRealPath() as path normalization sanitizer#21652MarkLee131 wants to merge 1 commit intogithub:mainfrom
MarkLee131 wants to merge 1 commit intogithub:mainfrom
Conversation
PathNormalizeSanitizer recognized Path.normalize() and File.getCanonicalPath()/getCanonicalFile(), but not Path.toRealPath(). toRealPath() is strictly stronger than normalize() (resolves symlinks and verifies file existence in addition to normalizing ".." components), and is functionally equivalent to File.getCanonicalPath() for the NIO.2 API. CERT FIO16-J and OWASP both recommend it for path traversal defense. This adds toRealPath to PathNormalizeSanitizer alongside normalize, reducing false positives for code using idiomatic NIO.2 path handling.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Java path-traversal sanitization logic so CodeQL’s java/path-injection (and related) analysis treats Path.toRealPath() as a path-normalization sanitizer, aligning it with existing handling of Path.normalize() and File.getCanonicalPath() to reduce false positives for idiomatic NIO.2 code.
Changes:
- Extend
PathNormalizeSanitizerto recognizePath.toRealPath()as a sanitizer. - Add a new “good” path-handling test case that uses
toRealPath()+ prefix checking. - Add a change-note documenting the analysis improvement.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| java/ql/lib/semmle/code/java/security/PathSanitizer.qll | Treat Path.toRealPath() as a path normalization sanitizer in the path traversal guard/sanitizer logic. |
| java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.java | Add a new safe (GOOD) test variant using toRealPath() to ensure no new false positives. |
| java/ql/lib/change-notes/2026-04-04-path-injection-torealpath.md | Document the new sanitizer recognition for java/path-injection and java/zipslip. |
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.
Fix #21651:
This adds toRealPath to PathNormalizeSanitizer alongside normalize, reducing false positives for code using idiomatic NIO.2 path handling.