Skip to content

Java: fix @Pattern sanitization for java/path-injection#22119

Open
owen-mc wants to merge 4 commits into
github:mainfrom
owen-mc:java/fix-tainted-path-pattern-sanitization
Open

Java: fix @Pattern sanitization for java/path-injection#22119
owen-mc wants to merge 4 commits into
github:mainfrom
owen-mc:java/fix-tainted-path-pattern-sanitization

Conversation

@owen-mc

@owen-mc owen-mc commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

There was a bug in the way this was previously implemented for this query. This PR adds a test showing the bug, fixes the bug and the test, and then refactors how barrier nodes should be created for RegexMatchs to avoid the same bug happening in future.

@owen-mc owen-mc requested a review from a team as a code owner July 3, 2026 10:15
Copilot AI review requested due to automatic review settings July 3, 2026 10:15

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.

Pull request overview

This PR fixes a bug in the java/path-injection sanitizer modeling so that regex constraints expressed via @javax.validation.constraints.Pattern are treated as sanitizers, and extends the Java query tests accordingly.

Changes:

  • Update PathSanitizer.qll to handle @Pattern-based regex checks as sanitizers in the directory-characters sanitizer logic.
  • Add a new Spring controller test case exercising a vulnerable flow and a @Pattern-constrained variant, and extend the test classpath with a Bean Validation stub.
  • Update the expected results and add a change note documenting the analysis behavior change.
Show a summary per file
File Description
java/ql/test/query-tests/security/CWE-022/semmle/tests/TaintedPath.expected Updates expected results to include the new test case output.
java/ql/test/query-tests/security/CWE-022/semmle/tests/SanitizationTests2.java Adds a new test controller covering @Pattern sanitization behavior.
java/ql/test/query-tests/security/CWE-022/semmle/tests/options Adds the javax-validation-constraints stub to the test compilation classpath.
java/ql/lib/semmle/code/java/security/PathSanitizer.qll Adjusts sanitizer logic to recognize annotation-based regex checks appropriately.
java/ql/lib/change-notes/2026-07-03-fix-pattern-sanitizer-tainted-path.md Documents the analysis change in a new change note.

Review details

  • Files reviewed: 5/5 changed files
  • Comments generated: 3
  • Review effort level: Low

Comment thread java/ql/test/query-tests/security/CWE-022/semmle/tests/SanitizationTests2.java Outdated
Comment thread java/ql/test/query-tests/security/CWE-022/semmle/tests/SanitizationTests2.java Outdated
Comment thread java/ql/lib/change-notes/2026-07-03-fix-pattern-sanitizer-tainted-path.md Outdated
@owen-mc owen-mc force-pushed the java/fix-tainted-path-pattern-sanitization branch from a21389f to e5bd62d Compare July 3, 2026 10:35
Comment thread java/ql/lib/semmle/code/java/security/Sanitizers.qll Fixed
@owen-mc

owen-mc commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

DCA shows slower analysis time in apache__commons-math. However, when I download the database and run the three main queries which use these sanitizers (log injection, path injection, request forgery) I find that the first two run faster and the third slower, with no obvious causal connection to the contents of this PR.

* always present) and use `getABarrierNode` in place of a direct
* `DataFlow::BarrierGuard` instantiation.
*/
module RegexMatchBarrier<regexMatchGuardChecksSig/3 guardChecks> {

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.

This is poorly named, since it's not just about Regex barriers - it's a generalisation of DataFlow::BarrierGuard that also supports Annotations-as-Guards. Most of the time DataFlow::BarrierGuard will be invoked with just regular guards, but whenever it's invoked with annotations as well then this is what we want. Therefore I think it would be better to simply extend the functionality of DataFlow::BarrierGuard directly to also cover annotations, i.e. drop this module and add the annotation-guard-related disjunct directly in DataFlow::BarrierGuard in java/ql/lib/semmle/code/java/dataflow/internal/DataFlowUtil.qll.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, that is a lot better. I've force-pushed, since there isn't much point keeping the old way in the git history. The new code is in the commit "Refactor creating barriers with annotations".

@owen-mc owen-mc force-pushed the java/fix-tainted-path-pattern-sanitization branch from 5b2e0b6 to a812e4a Compare July 7, 2026 11:44
@owen-mc owen-mc requested a review from aschackmull July 7, 2026 11:48
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.

4 participants