Skip to content

Commit 0d2e9ae

Browse files
Jami CogswellJami Cogswell
Jami Cogswell
authored and
Jami Cogswell
committed
Java: fix 'matches' false branch
1 parent 49d37c5 commit 0d2e9ae

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

java/ql/lib/semmle/code/java/security/PathSanitizer.qll

+5-4
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,9 @@ private predicate isMatchesCall(StringMatchesCall matchesCall, Expr checkedExpr,
497497
target.getStringValue() = targetValue and
498498
checkedExpr = matchesCall.getQualifier()
499499
|
500-
targetValue.matches(["[%]*", "[%]+", "[%]{%}"]) and
501500
(
502501
// Allow anything except `.`, '/', '\'
502+
targetValue.matches(["[%]*", "[%]+", "[%]{%}"]) and
503503
(
504504
// Note: we do not account for when '.', '/', '\' are inside a character range
505505
not targetValue.matches("[%" + [".", "/", "\\\\\\\\"] + "%]%") and
@@ -512,9 +512,10 @@ private predicate isMatchesCall(StringMatchesCall matchesCall, Expr checkedExpr,
512512
branch = true
513513
or
514514
// Disallow `.`, '/', '\'
515-
targetValue.matches("[%.%]%") and
516-
targetValue.matches("[%/%]%") and
517-
targetValue.matches("[%\\\\\\\\%]%") and
515+
targetValue.matches([".*[%].*", ".+[%].+"]) and
516+
targetValue.matches("%[%.%]%") and
517+
targetValue.matches("%[%/%]%") and
518+
targetValue.matches("%[%\\\\\\\\%]%") and
518519
not targetValue.matches("%[^%]%") and
519520
branch = false
520521
)

java/ql/test/library-tests/pathsanitizer/Test.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -684,16 +684,33 @@ public void directoryCharsSanitizer() throws Exception {
684684
// branch = false
685685
{
686686
String source = (String) source();
687+
if (source.matches(".*[\\./\\\\].*")) {
688+
sink(source); // $ hasTaintFlow
689+
} else {
690+
sink(source); // Safe
691+
}
692+
}
693+
{
694+
String source = (String) source();
695+
if (source.matches(".+[\\./\\\\].+")) {
696+
sink(source); // $ hasTaintFlow
697+
} else {
698+
sink(source); // Safe
699+
}
700+
}
701+
{
702+
String source = (String) source();
703+
// does not match whole string
687704
if (source.matches("[\\./\\\\]+")) {
688705
sink(source); // $ hasTaintFlow
689706
} else {
690-
sink(source); // $ Safe
707+
sink(source); // $ hasTaintFlow
691708
}
692709
}
693710
{
694711
String source = (String) source();
695712
// not a complete sanitizer since it doesn't protect against absolute path injection
696-
if (source.matches("[\\.]+")) {
713+
if (source.matches(".+[\\.].+")) {
697714
sink(source); // $ hasTaintFlow
698715
} else {
699716
sink(source); // $ hasTaintFlow

0 commit comments

Comments
 (0)