Skip to content

Java: Path Injection FP fix - #413

Open
Chanel (chanel-y) wants to merge 7 commits into
mainfrom
java-pathinjection-fp-fix
Open

Chanel (chanel-y) wants to merge 7 commits into
mainfrom
java-pathinjection-fp-fix

Conversation

@chanel-y

@chanel-y Chanel (chanel-y) commented Dec 3, 2026 •

Copy link
Copy Markdown

Summary

False-positive java/path-injection alerts where static helper methods already validate that a canonical file path stays inside an allowed directory. The query entry point is unchanged; the correction is in the shared Java PathSanitizer.qll library.

The two patterns below are simplified versions of the reported validation helpers. In both, the allowed directory is trusted, callers use the returned file only when it is non-null, and canonicalization failures prevent the helper from returning a file.

False-positive patterns

1. The file's direct parent must equal the allowed directory

static File child(String base, String name) throws IOException {
    File directory = new File(base).getCanonicalFile();
    File candidate = new File(directory, name).getCanonicalFile();
    if (!directory.equals(candidate.getParentFile()))
        return null;
    return candidate;
}

File file = child("/srv/data", userInput);
if (file != null)
    consume(file); // Previously reported as path injection.

Why this is a false positive: the user controls the name, but the helper returns the file only if its canonical parent is exactly the canonical allowed directory. A path that escapes that directory, including through .., fails the check. Canonicalization alone is not the protection; the subsequent parent-equality check is.

How the query changes: recognize a successful File.equals comparison between the canonical directory and the canonical candidate's parent, provided that comparison controls the return of the same candidate. Treat that validated return as a path-injection sanitizer.

2. Walking the parents must find the allowed directory

static File descendant(String path, String base) throws IOException {
    File directory = new File(base).getCanonicalFile();
    File candidate = new File(path).getCanonicalFile();
    for (File parent = candidate.getParentFile();
         parent != null;
         parent = parent.getParentFile()) {
        if (directory.equals(parent))
            return candidate;
    }
    return null;
}

File file = descendant(userInput, "/srv/data");
if (file != null)
    consume(file); // Previously reported as path injection.

Why this is a false positive: the helper returns the canonical candidate only after finding the allowed directory among its parents. This permits files in nested subdirectories but rejects paths outside the allowed directory.

How the query changes: recognize the ancestor-walking loop and its successful equality check. The analysis verifies that every input to the loop's parent variable is either the candidate's initial parent or the previous parent's parent, and that equality controls the return of the unchanged candidate. This prevents an unrelated or unchecked value from satisfying the modeled containment check.

Chanel (chanel-y) and others added 7 commits December 1, 2026 13:56
Add safe and adversarial helper fixtures, recording the current two false-positive diagnostics before updating containment recognition.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b
Recognize successful canonical parent equality before returning the same candidate. Preserve the allowed-directory parameter's taint at helper calls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b
Remove the two pre-fix false-positive diagnostics after canonical parent validation is recognized. Unsafe cases retain their inline taint expectations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b
Add safe and adversarial parent-walking helpers, recording four false-positive and four missed-taint diagnostics before adding ancestor-loop recognition.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b
Require all loop SSA-phi inputs to be candidate-parent initialization or self-parent update, with successful equality controlling return of the unchanged candidate. Preserve directory taint for generic calls and method references.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b
Remove the four false-positive and four missed-taint diagnostics after ancestor-loop recognition and directory-taint propagation are updated. Unsafe cases and the documented pre-existing lexical-prefix gap retain their inline expectations.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b
Remove change notes and retain the reported helper patterns with closely related unsafe cases. Focused canonical-containment tests pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 07dbce6b-e7a3-48b2-a656-bddbcb83db5b

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant