Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

An absolute path may sometimes contain aliases, shadows, symbolic links and shortcuts as opposed to canonical paths, which refer to actual files or directories that these point to. The process of canonicalizing file names makes it safer to verify a path, directory, or file name by making it easier to compare names.

Noncompliant Code Example

Wiki Markup
This noncompliant code example accepts the file path as a command line argument. Let {{argv\[1\]}} be the string {{java}}, where {{/tmp/java}} is a symbolic link that points to another file in some directory of the local file system. On POSIX based systems, the {{getAbsolutePath()}} method includes {{/tmp/java}} (name of the symbolic link) in the path that it returns. An adversary who can create symbolic links can cause the program to operate on the wrong file.

...

Code Block
bgColor#FFcccc
public static void main(String[] args) {
  File f = new File("/tmp/" + args[0]);
  String absPath = f.getAbsolutePath();

  if(!absPath.equals("/tmp/somefile")) {  // Validation
    throw new IllegalArgumentException();
  }		  
}

Compliant Solution

This compliant solution uses the getCanonicalPath() method, introduced in Java 2, because it resolves the aliases, shortcuts or symbolic links consistently, across all platforms. The value of the alias is not included in the returned value. Moreover, relative references like the double period (..) are also removed so that the input is reduced to a canonicalized form before validation is carried out. The getCanonicalPath() method throws a security exception when used within applets as it reveals too much information about the host machine. The getCanonicalFile() method behaves like getCanonicalPath() but returns a new File object instead of a String.

Code Block
bgColor#ccccff
public static void main(String[] args) throws IOException {
  File f = new File("/tmp/" + args[0]);
  String canonicalPath = f.getCanonicalPath();
 
  if(!canonicalPath.equals("/tmp/somefile")) {  // Validation
    throw new IllegalArgumentException();
  }
}

Risk Assessment

Using path names from untrusted sources without first canonicalizing the filenames may result in operations being carried out on the wrong files.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

FIO00- J

medium

unlikely

medium

P4

L3

Automated Detection

TODO

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Other Languages

This rule appears in the C Secure Coding Standard as FIO02-C. Canonicalize path names originating from untrusted sources.

This rule appears in the C++ Secure Coding Standard as FIO02-CPP. Canonicalize path names originating from untrusted sources.

References

Wiki Markup
\[[API 06|AA. Java References#API 06]\] [method getCanonicalPath()|http://java.sun.com/javase/6/docs/api/java/io/File.html#getCanonicalPath()]
\[[API 06|AA. Java References#API 06]\] [method getCanonicalFile()|http://java.sun.com/javase/6/docs/api/java/io/File.html#getCanonicalFile()]
\[[Harold 99|AA. Java References#Harold 99]\]
\[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 171|http://cwe.mitre.org/data/definitions/171.html] "Cleansing, Canonicalization, and Comparison Errors", [CWE ID 647|http://cwe.mitre.org/data/definitions/647.html] "Use of Non-Canonical URL Paths for Authorization Decisions"

...