Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: File constructor path sanitizer #18504

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
22 changes: 22 additions & 0 deletions java/ql/lib/semmle/code/java/security/PathSanitizer.qll
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,25 @@ private class FileGetNameSanitizer extends PathInjectionSanitizer {
)
}
}

/**
* A sanitizer that considers the second argument to a `File` constructor safe
* if it is checked for `..` components (`PathTraversalGuard`) or if any internal
* `..` components are removed from it (`PathNormalizeSanitizer`).
*/
class FileConstructorSanitizer extends PathInjectionSanitizer {
FileConstructorSanitizer() {
exists(ConstructorCall constrCall, Argument arg, Expr guard |
constrCall.getConstructedType() instanceof TypeFile and
arg = constrCall.getArgument(1) and
(
guard
.(PathTraversalGuard)
.controls(arg.getBasicBlock(), guard.(PathTraversalGuard).getBranch())
or
TaintTracking::localExprTaint(guard.(PathNormalizeSanitizer), arg)
) and
this.asExpr() = constrCall
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,38 @@ public void sendUserFileGood4(Socket sock, String user) throws IOException {
fileLine = fileReader.readLine();
}
}

public void sendUserFileGood5(Socket sock, String user) throws IOException {
BufferedReader filenameReader =
new BufferedReader(new InputStreamReader(sock.getInputStream(), "UTF-8"));
String filename = filenameReader.readLine();
File f1 = new File("safe/file.txt");
// GOOD: ensure that the path does not contain ".." and is used as the
// second argument to a `File` constructor
if (!filename.contains("..")) {
File f2 = new File(f1, filename);
f2.exists();

// Only sanitize `f2`; `filename` is still tainted
BufferedReader fileReader = new BufferedReader(new FileReader(filename)); // $ hasTaintFlow
}
}

public void sendUserFileGood6(Socket sock, String user) throws IOException {
BufferedReader filenameReader =
new BufferedReader(new InputStreamReader(sock.getInputStream(), "UTF-8"));
String filename = filenameReader.readLine();
File f1 = new File("safe/file.txt");

// GOOD: ensure that the path is normalized and is then used as the
// second argument to a `File` constructor
Path normalizedFilename = Paths.get(filename).normalize().toAbsolutePath();
String normalizedFilenameStr = normalizedFilename.toString();
File f2 = new File(f1, normalizedFilenameStr);
f2.exists();

// Only sanitize `f2`; `filename` and `normalizedFilenameStr` are still tainted
BufferedReader fileReader = new BufferedReader(new FileReader(filename)); // $ hasTaintFlow
BufferedReader fileReader2 = new BufferedReader(new FileReader(normalizedFilenameStr)); // $ hasTaintFlow
}
}