Skip to content

False positive CT_CONSTRUCTOR_THROW on canonical "Compliant Solution" #2710

@garydgregory

Description

@garydgregory

The bug description for CT_CONSTRUCTOR_THROW at https://spotbugs.readthedocs.io/en/stable/bugDescriptions.html#ct-be-wary-of-letting-constructors-throw-exceptions-ct-constructor-throw says:

CT: Be wary of letting constructors throw exceptions. (CT_CONSTRUCTOR_THROW)[](https://spotbugs.readthedocs.io/en/stable/bugDescriptions.html#ct-be-wary-of-letting-constructors-throw-exceptions-ct-constructor-throw)
Classes that throw exceptions in their constructors are vulnerable to Finalizer attacks

A finalizer attack can be prevented, by declaring the class final, using an empty finalizer declared as final, or by a clever use of a private constructor.

See [SEI CERT Rule OBJ-11](https://wiki.sei.cmu.edu/confluence/display/java/OBJ11-J.+Be+wary+of+letting+constructors+throw+exceptions) for more information.

The link https://wiki.sei.cmu.edu/confluence/display/java/OBJ11-J.+Be+wary+of+letting+constructors+throw+exceptions provides the compliant solution:

public class BankOperations {
    public BankOperations() {
      this(performSSNVerification());
    }
   
    private BankOperations(boolean secure) {
      // secure is always true
      // Constructor without any security checks
    }
   
    private static boolean performSSNVerification() {
      // Returns true if data entered is valid, else throws a SecurityException
      // Assume that the attacker just enters invalid SSN, so this method always throws the exception
      throw new SecurityException("Invalid SSN!");
    }
   
    // ...remainder of BankOperations class definition
  }

which blows up SpotBugs checks:

[ERROR] Medium: Exception thrown in class org.apache.commons.io.BankOperations at new org.apache.commons.io.BankOperations() will leave the constructor. The object under construction remains partially initialized and may be vulnerable to Finalizer attacks. [org.apache.commons.io.BankOperations, org.apache.commons.io.BankOperations] At BankOperations.java:[line 5]At BankOperations.java:[line 5] CT_CONSTRUCTOR_THROW

This example reflects what I see in Commons IO when I try to address 64 such issues with a compliant solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions