Versions Compared

Key

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

Validate method parameters arguments to ensure that they fall within the bounds of the method's intended design. This practice ensures that operations on the method's parameters yield valid results. Failure to validate method parameters arguments can result in incorrect calculations, runtime exceptions, violation of class invariants, and inconsistent object state.

Redundant testing of parameters arguments by both the caller and the callee is a style of defensive programming that is largely discredited within the programming community, in part for reasons of performance. Instead, normal practice requires validation on only one side of each interface.

Caller validation of parameters arguments can result in faster code , because the caller may be aware of invariants that prevent invalid values from being passed. Conversely, callee validation of parameters arguments encapsulates the validation code in a single location, reducing the size of the code and raising the likelihood that the validation checks are performed consistently and correctly.

If a method receives data from Methods that receive arguments across a trust boundary , that method must perform callee validation of its parameter their arguments for safety and security reasons. This precaution applies to all public methods of a library. Other methods, including private methods, should validate arguments that are both untrusted and unvalidated when those arguments may propagate from a public method via its arguments.

When defensive copying is necessary, make the defensive copies before parameter argument validation, and validate the copies rather than the original parameters. See guideline SER07arguments (see SER06-J. Make defensive copies of private mutable components during deserialization for additional information).

Noncompliant Code Example

In this noncompliant code example, setState() and useState() fail to validate their parametersarguments. A malicious caller could pass an invalid state to the library, consequently corrupting it the library and exposing a vulnerability.

Code Block
bgColor#FFcccc

private Object myState = null;

// Sets some internal state in the library 
void setfilesetState(Object state) {
  myState = state;
}

// Performs some action using the filestate passed earlier 
void useState() {
  // Perform some action here 
}

Such vulnerabilities are particularly severe when the internal state references contains or refers to sensitive or system-critical data.

Compliant Solution

This compliant solution both validates the method parameters arguments and also verifies the internal state before use. This practice promotes consistency in program execution and reduces the potential for vulnerabilities.

Code Block
bgColor#ccccff

private Object myState = null;

// Sets some internal state in the library 
void setfilesetState(Object state) {
  if (state == null) {
    // Handle null state 
  }

  // Defensive copy here when state is mutable

  if (isInvalidState(state)) {
    // Handle invalid state 
  }
  myState = state;
}

// Performs some action using the state passed earlier 
void useState() {
  if (myState == null) {
    // Handle no state (e.g., null) condition
  }
  // ...
}

Exceptions

MET01MET00-J-EX0: Parameter Argument validation inside a method may be omitted when the stated contract of a method requires that the caller must validate arguments passed to the method. In this case, the validation must be performed by the caller for all invocations of the method.

MET01MET00-J-EX1: Parameter Argument validation may be omitted for parameters arguments whose type adequately constrains the state of the parameterargument. This constraint should be clearly documented in the code.

This exception may include parameters apply to arguments whose values (as permitted by their type) are not necessarily valid , but are still correctly handled by the functionmethod. In the following code, no explicit validation is done of the arguments x and y are not validated even though their product might not be a valid int. The code is safe as because it adequately handles all int values for x and y.

Code Block
bgColor#ccccff

public int product(int x, int y) {
  long result = (long) x * y;
  if (result < Integer.MIN_VALUE || result > Integer.MAX_VALUE) {
    // handleHandle error
  }
  return (int) result;
}

MET01MET00-J-EX2: Complete validation of all parameters arguments of all methods may introduce added cost and complexity that exceeds its value for all but the most critical code. See, for example, NUM00-J. Detect or prevent integer overflow exception NUM00-EX2. In such cases, consider parameter argument validation at API boundaries, especially those that may involve interaction with untrusted code.

Risk Assessment

Failure to validate method parameters arguments can result in inconsistent computations, runtime exceptions, and control flow vulnerabilities.

Guideline Rule

Severity

Likelihood

Remediation Cost

Priority

Level

MET01MET00-J

high High

likely Likely

high High

P9

L1 L2

Related

...

Guidelines

ISO/IEC TR 24772:2010

Argument Passing to Library Functions [TRJ]

Bibliography

[Bloch 2008]

Item 38, "Check Parameters for Validity"

 

...

Image Added Image Added Image Added

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

Bibliography

Wiki Markup
\[[Bloch 2008|AA. Bibliography#Bloch 08]\] Item 38: Check parameters for validity

05. Methods (MET)      05. Methods (MET)      MET02-J. Never use assertions to validate method parameters