Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Added contextual path-normalization recognition for `org.apache.commons.io.FilenameUtils.normalize`, reducing path-injection false positives.
3 changes: 3 additions & 0 deletions java/ql/lib/semmle/code/java/security/PathSanitizer.qll
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ private class PathNormalizeSanitizer extends MethodCall {
or
t instanceof TypeFile and
this.getMethod().hasName(["getCanonicalPath", "getCanonicalFile"])
or
t.hasQualifiedName("org.apache.commons.io", "FilenameUtils") and
this.getMethod().hasName("normalize")
Comment on lines +296 to +297
Comment on lines +296 to +297
Comment on lines +296 to +297
Comment on lines +296 to +297
)
}
}
Expand Down
12 changes: 12 additions & 0 deletions java/ql/test/library-tests/pathsanitizer/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Paths;
import android.net.Uri;
import com.google.common.base.CharMatcher;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

public class Test {
Expand Down Expand Up @@ -623,6 +624,17 @@ public void fileConstructorSanitizer() throws Exception {
sink(source); // $ hasTaintFlow
sink(normalized); // $ hasTaintFlow
}
{
String source = (String) source();
String normalized = FilenameUtils.normalize(source);
String normalizedWithSeparator = FilenameUtils.normalize(source, true);
File f1 = new File("safe/file.txt");
File f2 = new File(f1, normalized);
File f3 = new File(f1, normalizedWithSeparator);
sink(f2); // Safe
sink(f3); // Safe
sink(normalized); // $ hasTaintFlow
}
}

private void directoryCharsValidation(String path) throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion java/ql/test/library-tests/pathsanitizer/options
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//semmle-extractor-options: --javac-args -cp ${testdir}/../../stubs/google-android-9.0.0:${testdir}/../../stubs/apache-commons-lang3-3.7:${testdir}/../../stubs/guava-30.0
//semmle-extractor-options: --javac-args -cp ${testdir}/../../stubs/google-android-9.0.0:${testdir}/../../stubs/apache-commons-lang3-3.7:${testdir}/../../stubs/guava-30.0:${testdir}/../../stubs/apache-commons-io-2.6
//codeql-extractor-kotlin-options: ${testdir}/../../stubs/google-android-9.0.0

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading