concept object in category security

appears as: objects, objects, n object, object, object, The object
Secure by Design

This is an excerpt from Manning's book Secure by Design.

When developing software, you constantly make decisions on how to write the code that solves the problems at hand. You decide what syntax to use, what constructs and algorithms to apply, how to structure the code, and how to steer the flow of execution. If you’re using an object-oriented approach, you’ll make decisions on what your object model should look like and the interactions between the objects within that model. If you’re applying a functional style of programming, you’ll make decisions on what behavior to pass in as functions, making sure you’re creating pure functions without side effects.7  All these decisions can be viewed as part of the design process.

The Customer object in listing 4.1 shows two interesting details. First, all fields are initialized through setter methods, which implies that internal state is allowed to change after object creation. This is problematic because you never know when the object is properly initialized. The other observation is that each method is marked as synchronized to prevent concurrent field modification, which in turn can lead to thread contention (when threads are forced to wait for another thread to release one or more locks before executing).

Figure 5.5 Remodeling the User object, causing sensitive data to leak

figure05-05.eps
Listing 6.1 Account class with no-arg construction and setter initialization
public class Account {
    private AccountNumber number;    #1  
    private LegalPerson owner;    #1  
    private Percentage interest;    #1  
    private Money creditLimit;    #2  
    private AccountNumber fallbackAccount;    #2  

    public Account() {}    #3  

    public AccountNumber getNumber() {
        return number;
    }

    public void setNumber(AccountNumber number) {    #4  
        this.number = number;
    }

    public LegalPerson getOwner() {
        return owner;
    }

    public void setOwner(LegalPerson owner) {    #4  
        this.owner = owner;
    }
    ...
}

class AccountService {

    void openAccount() {
        Account account= new Account();    #5  
        account.setNumber(number);    #6  
        account.setOwner(accountowner);    #6  
        account.setInterest(interest);    #6  
        account.setCreditLimit(limit);    #7  
        ...
    }
}

#1   Mandatory
#2   Optional
#3   In Java, if you leave out the constructor, you’ll get a public no-arg constructor.
#4   Clients must remember to call these methods before the object is ready to use.
#5   An inconsistent, newly created object needs setter-based initialization.
#6   Client code required to set all mandatory fields, forgetting none
#7   Setting one optional field