Skip to content

Java: Require related values in composite PathSanitizer guards - #23290

Open
owen-mc wants to merge 2 commits into
github:mainfrom
owen-mc:copilot/fix-path-sanitizer-related-guards
Open

owen-mc wants to merge 2 commits into
github:mainfrom
owen-mc:copilot/fix-path-sanitizer-related-guards

Conversation

@owen-mc

@owen-mc owen-mc commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Require composite path-injection sanitizers (allowedPrefixGuard, dotDotCheckGuard, and blockListGuard) to connect the expression being sanitized and the expression checked by the dominating complementary guard via local taint flow, preventing unrelated prefix or '..' checks in the same method from acting as sanitizers.

Also strip casts and Kotlin not-null assertions via getUnderlyingExpr() in the StringSubstringMatch branches of BlockListGuard and PathTraversalGuard.

Fixes #23281.

Require composite path-injection sanitizers (allowedPrefixGuard,
dotDotCheckGuard, and blockListGuard) to connect the expression being
sanitized and the expression checked by the dominating complementary
guard via local taint flow, preventing unrelated prefix or '..' checks
in the same method from acting as sanitizers.

Also strip casts and Kotlin not-null assertions via getUnderlyingExpr()
in the StringSubstringMatch branches of BlockListGuard and
PathTraversalGuard.

Fixes github#23281.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@owen-mc
owen-mc marked this pull request as ready for review November 27, 2026 16:21
@owen-mc
owen-mc requested a review from a team as a code owner November 27, 2026 16:21
@owen-mc
owen-mc requested review from a team and an adaptive review from Copilot November 27, 2026 16:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The sanitizer still suppresses path-traversal alerts after lossy conversions and introduces false positives for check-preserving conversions, with regression coverage missing both cases.

2 open issues
What changed in this PR

This PR tightens shared Java/Kotlin path-injection sanitizers so unrelated checks no longer suppress alerts, addressing #23281.

Changes:

  • Require related values across complementary prefix, blocklist, and traversal guards.
  • Unwrap casts and Kotlin not-null assertions in substring checks.
  • Add regression coverage, updated query expectations, and an analysis change note.
File Description
java/​ql/​test/​query-tests/​security/​CWE-022/​semmle/​tests/​TaintedPath.java Adds an unrelated-prefix regression case.
java/​ql/​test/​query-tests/​security/​CWE-022/​semmle/​tests/​TaintedPath.expected Records the new alert and flow details.
java/​ql/​test/​library-tests/​pathsanitizer/​TestKt.kt Adds Kotlin cases for unrelated and derived values.
java/​ql/​test/​library-tests/​pathsanitizer/​Test.java Adds Java cases for guard relationships and wrappers.
java/​ql/​lib/​semmle/​code/​java/​security/​PathSanitizer.qll Connects complementary guards through local flow and unwraps checked expressions.
java/​ql/​lib/​change-notes/​2026-11-27-path-sanitizer-related-guards.md Documents the analysis change and potential additional alerts.

Review effort: Max (adaptive)

Allow localFlowToPathGuard to step backwards across lossless String/File/Path representation conversions so precomputed derived paths remain sanitized when paired with a guard on a converted form.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The sanitizer can mark paths as safe despite unchecked attacker-controlled components or checks on lossy-derived values, causing real path traversal vulnerabilities to be missed.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Missing path conversion edges break related-guard recognition

java/​ql/​lib/​semmle/​code/​java/​security/​PathSanitizer.qll:99

Safe paths can now be reported when the complementary guard uses FileSystem.getPath, Path.toAbsolutePath, or File.getAbsolutePath, because these taint-only conversions are missing from the backward relation. For example, precomputing checked = FileSystems.getDefault().getPath(source) and target = source + "/file" before checking checked.startsWith("/safe") && !target.contains("..") no longer sanitizes target. Include these conversions and add regression cases, keeping the single-string/empty-varargs restriction for getPath.

Medium severity Missing File constructor edge loses related-value sanitization

java/​ql/​lib/​semmle/​code/​java/​security/​PathSanitizer.qll:135

Precomputing File file = new File(dir, name) before !name.contains("..") && file.toPath().startsWith(dir.toPath()) now loses sanitization and can produce a false alert. The child-to-File edge exists only in FileConstructorChildArgumentStep, which FlowToPathGuard::hasFlow does not follow. Include that edge in the forward relation and add a precomputed-constructor regression case.

2 resolved since last review

Review effort: Max (adaptive)

This branch has not been deployed

No deployments
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.

Java: false negative in java/path-injection when prefix and .. checks apply to different values

2 participants