Skip to content

False positive: injava/path-injection,Path.toRealPath() not recognized as sanitizer #21651

@MarkLee131

Description

@MarkLee131

Description of the false positive

PathNormalizeSanitizer recognizes Path.normalize() and File.getCanonicalPath()/getCanonicalFile(), but not Path.toRealPath().

toRealPath() is strictly stronger than normalize() (resolves .., resolves symlinks, verifies existence). It is the NIO.2 equivalent of getCanonicalPath(). CERT FIO16-J lists them as interchangeable for path traversal prevention.

One-line fix: add "toRealPath" alongside "normalize" in PathNormalizeSanitizer.

Code samples or links to source code

Path base = Paths.get("/safe/dir").toRealPath();
Path resolved = base.resolve(userInput).toRealPath();
if (resolved.startsWith(base + File.separator)) {
    // Still flagged as path injection despite toRealPath() normalization
    Files.readAllBytes(resolved);
}

The same pattern with .normalize() instead of .toRealPath() is correctly recognized as safe.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions