Skip to content

Question: False positive in Path traversal - Java #14922

@chmodxxx

Description

@chmodxxx

Hello, I'm facing a false positive in path traversal with the following code :

 public String doSomething(ArgType arg1, SinkType sink1) {
        if (!Util.isValidFilename(sink1.getFilename())) {
            throw new ServiceException();
        }
        Path testPath = Paths.get(sink1.getFilename());

isValidFilename definition :

    static boolean isValidFilename(String filename) {
        if (filename.contains("/") || filename.contains("\\")) {
            return false;
        }
        return true;
    }
}

basically the isValidFilename method doesn't cut the flow when it reaches Util.isValidFilename(sink1.getFilename()). Do you know how I can add some customizations to PathSanitizer so that it takes into consideration this case.
I have tried things like

class ContainsSanitizer extends PathInjectionSanitizer {
  ContainsSanitizer() {
     exists(MethodCall ma| 
      
      ma.getMethod().getName() = "contains" and ma = this.asExpr() and isDisallowedWord(ma.getArgument(0)) )

  }
}

this doesn't work as well.

Appreciate any pointers

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    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