Versions Compared

Key

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

...

Code Block
bgColor#FFcccc
public final class CreditCard implements Serializable {
  //private Private internal state
  private String credit_card;
  private static final String DEFAULT = "DEFAULT";

  public CreditCard() {
    //initialize Initialize credit_card to default value
    credit_card = DEFAULT;
  }

  //allows Allows callers to modify (private) internal state
  public void changeCC(String newCC) {
    if (credit_card.equals(newCC)) {
      // noNo change
      return;
    } else {  
      validateInput(newCC);
      credit_card = newCC;
    }
  }

  // readObject() correctly enforces checks during deserialization
  private void readObject(ObjectInputStream in)  throws IOException {
    in.defaultReadObject();
    // ifIf the deserialized name does not match the default value normally
    // created at construction time, duplicate the checks
    if (!DEFAULT.equals(credit_card)) {
      validateInput(credit_card);
    }
  }

  // allowsAllows callers to retrieve internal state
  public String getValue() {
    return somePublicValuecredit_card;
  }

  // writeObject() correctly enforces checks during serialization
  private void writeObject(ObjectOutputStream out)  throws IOException {
    out.writeObject(credit_card);
  }
}

...

Code Block
bgColor#ccccff
public final class SecureCreditCard implements Serializable {
  //private Private internal state
  private String credit_card;
  private static final String DEFAULT = "DEFAULT";

  public SecureCreditCard() {
    //initialize Initialize credit_card to default value
    credit_card = DEFAULT;
  }

  //allows callers to modify (private) internal state
  public void changeCC(String newCC) {
    if (credit_card.equals(newCC)) {
      // noNo change
      return;
    } else {
      // checkCheck permissions to modify credit_card
      performSecurityManagerCheck();
      validateInput(newCC);
      credit_card = newCC;
    }
  }

  // readObject() correctly enforces checks during deserialization
  private void readObject(ObjectInputStream in)  throws IOException {
    in.defaultReadObject();
    // ifIf the deserialized name does not match the default value normally
    // created at construction time, duplicate the checks
    if (!DEFAULT.equals(credit_card)) {
      performSecurityManagerCheck();
      validateInput(credit_card);
    }
  }

  // allowsAllows callers to retrieve internal state
  public String getValue() {
    // checkCheck permission to get value
    performSecurityManagerCheck();
    return somePublicValue;
  }

  // writeObject() correctly enforces checks during serialization
  private void writeObject(ObjectOutputStream out)  throws IOException {
    // duplicateDuplicate check from getValue()
    performSecurityManagerCheck();
    out.writeObject(credit_card);
  }
}

Refer to the guideline SEC36-J. Enforce security checks in code that performs sensitive operations to learn about implementing the performSecurityManagerCheck() method.

Risk Assessment

Allowing serialization or deserialization to bypass the Security Manager may result in sensitive data being exposed or modified.

...