Skip to content

Commit 49d37c5

Browse files
Jami CogswellJami Cogswell
Jami Cogswell
authored and
Jami Cogswell
committed
Java: fix replacement char check and add tests
1 parent 3083360 commit 49d37c5

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ private predicate replacesDirectoryCharactersWithSingleReplaceAll(
419419
) {
420420
exists(CompileTimeConstantExpr target, string targetValue |
421421
isReplaceAllTarget(replaceAllCall, target) and
422-
target.getStringValue() = targetValue
422+
target.getStringValue() = targetValue and
423+
replaceAllCall.getArgument(1).(CompileTimeConstantExpr).getStringValue() = getAReplacementChar()
423424
|
424425
not targetValue.matches("%[^%]%") and
425426
targetValue.matches("[%.%]") and
@@ -460,6 +461,7 @@ private predicate replacesDirectoryCharactersWithDoubleReplaceOrReplaceAll(
460461
rc2.getQualifier() = rc1 and
461462
target1.getStringValue() = targetValue1 and
462463
target2.getStringValue() = targetValue2 and
464+
rc1.getArgument(1).(CompileTimeConstantExpr).getStringValue() = getAReplacementChar() and
463465
rc2.getArgument(1).(CompileTimeConstantExpr).getStringValue() = getAReplacementChar() and
464466
// make sure the calls replace different characters
465467
targetValue2 != targetValue1 and

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -716,14 +716,20 @@ public void directoryCharsSanitizer() throws Exception {
716716
}
717717
{
718718
String source = (String) source();
719-
source = source.replaceAll("\\.|[/\\\\]", "");
719+
source = source.replaceAll("\\.|[/\\\\]", "-");
720720
sink(source); // Safe
721721
}
722722
{
723723
String source = (String) source();
724-
source = source.replaceAll("[.][.]|[/\\\\]", "");
724+
source = source.replaceAll("[.][.]|[/\\\\]", "_");
725725
sink(source); // Safe
726726
}
727+
{
728+
String source = (String) source();
729+
// test a not-accepted replacement character
730+
source = source.replaceAll("[.][.]|[/\\\\]", "/");
731+
sink(source); // $ hasTaintFlow
732+
}
727733
{
728734
String source = (String) source();
729735
source = source.replaceAll(".|[/\\\\]", "");
@@ -761,6 +767,24 @@ public void directoryCharsSanitizer() throws Exception {
761767
source = source.replaceAll("\\.", "").replaceAll("/", "");
762768
sink(source); // Safe
763769
}
770+
{
771+
String source = (String) source();
772+
// test a not-accepted replacement character in each call
773+
source = source.replaceAll("\\.", "/").replaceAll("/", ".");
774+
sink(source); // $ hasTaintFlow
775+
}
776+
{
777+
String source = (String) source();
778+
// test a not-accepted replacement character in first call
779+
source = source.replaceAll("\\.", "/").replaceAll("/", "-");
780+
sink(source); // $ hasTaintFlow
781+
}
782+
{
783+
String source = (String) source();
784+
// test a not-accepted replacement character in second call
785+
source = source.replaceAll("\\.", "_").replaceAll("/", ".");
786+
sink(source); // $ hasTaintFlow
787+
}
764788
{
765789
String source = (String) source();
766790
source = source.replaceAll("\\.", "").replaceAll("/", "").replaceAll("\\\\", "");

0 commit comments

Comments
 (0)