Skip to content

Java: recognize Path.toRealPath() as path normalization sanitizer#21652

Open
MarkLee131 wants to merge 1 commit intogithub:mainfrom
MarkLee131:fix/path-injection-torealpath
Open

Java: recognize Path.toRealPath() as path normalization sanitizer#21652
MarkLee131 wants to merge 1 commit intogithub:mainfrom
MarkLee131:fix/path-injection-torealpath

Conversation

@MarkLee131
Copy link
Copy Markdown
Contributor

Fix #21651:

This adds toRealPath to PathNormalizeSanitizer alongside normalize, reducing false positives for code using idiomatic NIO.2 path handling.

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.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 PathNormalizeSanitizer to recognize Path.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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive: injava/path-injection,Path.toRealPath() not recognized as sanitizer

2 participants