Versions Compared

Key

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

...

This noncompliant code example contains a TOCTOU vulnerability. Because cookie is a mutable input, an attacker can cause it to expire between the initial check (the hasExpired() call) and the actual use (the doLogic() call).

...

This compliant solution avoids the TOCTOU vulnerability by copying the mutable input and performing all operations on the copy. Consequently, an attacker's changes to the mutable input cannot affect the copy. Acceptable techniques include using a copy constructor or implementing the java.lang.Cloneable interface and declaring a public public clone() method (for classes not declared final). In cases such as HttpCookie where the mutable class is declared final—that is, it cannot provide an accessible copy method — perform method—perform a manual copy of the object state within the caller (see OBJ04-J. Provide mutable classes with copy functionality to safely allow passing instances to untrusted code for more information). Note that any input validation must be performed on the copy rather than on the original object.

...

Secure Coding Guidelines for the Java Programming LanguageSE, Version 35.0

Guideline 6-2 / MUTABLE-2. : Create copies of mutable outputsoutput values

Bibliography

[Bloch 2008]

Item 39, "Make Defensive Copies When Needed"

[Pugh 2009]

"Returning References to Internal Mutable State"

...