Skip to content

Commit

Permalink
Java: fix handling of '^' and '.' in replace cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Jami Cogswell authored and Jami Cogswell committed Feb 27, 2025
1 parent ca19531 commit 80c4f1a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 5 additions & 5 deletions java/ql/lib/semmle/code/java/security/PathSanitizer.qll
Original file line number Diff line number Diff line change
Expand Up @@ -418,13 +418,13 @@ private class ReplaceDirectoryCharactersSanitizer extends MethodCall {
mc.getArgument(1).(CompileTimeConstantExpr).getStringValue() = ["", "_", "-"] and
(
// replaceAll with single call
target.getStringValue().matches("[%]") and
target.getStringValue().matches("[%\\.%]%") and
target.getStringValue().matches("[%/%]%") and
target.getStringValue().matches("[%\\\\%]%")
not target.getStringValue().matches("%[^%]%") and
target.getStringValue().matches("[%.%]") and
target.getStringValue().matches("[%/%]") and
target.getStringValue().matches("[%\\\\%]")
or
target.getStringValue().matches("%|%") and
target.getStringValue().matches("%" + ["\\.\\.", "[\\.][\\.]", "\\."] + "%") and
target.getStringValue().matches("%" + ["\\.\\.", "[.][.]", "\\."] + "%") and
target.getStringValue().matches("%/%") and
target.getStringValue().matches("%\\\\%")
or
Expand Down
5 changes: 5 additions & 0 deletions java/ql/test/library-tests/pathsanitizer/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,11 @@ public void directoryCharsSanitizer() throws Exception {
source = source.replaceAll("[\\./\\\\]", "");
sink(source); // Safe
}
{
String source = (String) source();
source = source.replaceAll("[^\\.\\\\/]", "");
sink(source); // $ hasTaintFlow
}
// `replaceAll` with regex
{
String source = (String) source();
Expand Down

0 comments on commit 80c4f1a

Please # to comment.