Versions Compared

Key

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

Propagating the content of exceptions without performing explicit filtering is often associated with information leakageresults in information leaks and lets an attacker build the attack surface. An attacker may craft input parameters such that underlying structures and mechanisms may get exposed inadvertently. Information leakage leaks can result from both the exception message text and the type of exception. For example, with FileNotFoundException, the message reveals the file system layout while the type conveys the absence of the file.

Wiki Markup
This guideline extends equally to both server side applications as well as clients. Adversaries can glean sensitive information from not only vulnerable web servers but also innocent users who use vulnerable web browsers. In 2004, Schoenefeld  \[[Schoenefeld 04|AA. Java References#Schoenefeld 04]\] discovered an instance in the Opera v7.54 web browser, wherein an attacker could use the {{sun.security.krb5.Credentials}} class in an applet as an oracle to "retrieve the name of the currently logged in user and parse his home directory from the information which is provided by the thrown {{java.security.AccessControlException}}."

Noncompliant Code Example

This example demonstrates code that accepts a file name as an input argument, on which operations are to be performed. An attacker can gain insights on the underlying file system structure by repeatedly passing different paths to fictitious files. When a file is not found, the FileInputStream constructor throws a FileNotFoundException. Other risks such as revelation of the user's home directory and as a result the user name, also manifest themselves.

This example also violates the condition that user supplied input must never occur in file names or as constituting elements of file paths. This can lead Failure to do this can leave the code vulnerable to a brute force attack allowing that allows the attacker to enumerate valid file names on a system by constantly monitoring the inputs that generate a system defined sanitized message.

Code Block
bgColor#FFcccc

import java.io.FileInputStream;
import java.io.FileNotFoundException;

class ExceptionExample {
  public static void main(String[] args) throws FileNotFoundException {
    FileInputStream fis = new FileInputStream("c:\\" + args[0]);
  }
}

...

To overcome the problem, the exception must be caught while taking special care to sanitize the message before propagating it to the caller. In cases where the exception type itself can reveal too much information, consider throwing a different exception altogether (with a different message, or possibly a higher level exception, referred to as exception translation). The MyExceptionReporter class described in EXC05-J. Use a class dedicated to reporting exceptions is a good choice, as exemplified in this compliant solution highlights.

Notice how Throwable is caught instead of specific exceptions. This is a departure from commonly suggested best practices, but is critical in cases where runtime exceptions or errors can reveal sensitive information. Moreover, this solution overcomes the issue of the brute force attack described earlier by accepting a denumerable set of file name choices with the aid help of a switch-case clause. The Consequently, the actual file names and paths are as a result shielded from the user of the application.

...

While following this guideline, make sure that security exceptions such as java.security.AccessControlException and java.lang.SecurityException are not swallowed or masked in the process. This can lead to far more pernicious effects such as missed security event log entries. The MyExceptionReporter class prescribes a logging method to deal with this condition.

...

Exceptions may inadvertently reveal sensitive information unless care is taken to limit the information displayed as the result of an exceptiondisclosure.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXC01- J

medium

probable

high

P4

L3

...