concept failure in category security

appears as: failure, Failure, failures, failure, failures
Secure by Design

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

Data integrity involves the consistency of data during its entire life cycle; data availability ensures data is obtainable at the expected level of performance in a system.1  Both concepts are essential to understanding the root cause of the webshop problems. For example, failure to retrieve data is an availability problem that often boils down to code that prevents parallel or concurrent access. Similarly, identifying code that allows modification is the place to start when analyzing integrity issues. Table 4.2 shows the webshop problems categorized as availability and integrity issues.

Using exceptions to represent failures in domain logic is a common practice, but another equally common approach is to not use exceptions at all. This approach starts with the design mindset that failures are a natural and expected outcome of anything we do. Because exceptions represent something exceptional (the name kind of gives it away) and failures are expected outcomes, it doesn’t make sense to model them as exceptions. Instead, a failure should be modeled as a possible result of a performed operation in the same way a success is. By designing failures as unexceptional outcomes, you can avoid several of the problems that come from using exceptions—including ambiguity between domain and technical exceptions, and inadvertently leaking sensitive information.

If you look at the logic you implement in an application, it quickly becomes obvious that it’s not only about happy cases. When you execute a method, you have an intention of performing a specific action. Performing that action can almost always have multiple outcomes. At the very least, it can succeed or it can fail. In this section, you’ll learn how to gain more security by designing failures without using exceptions.

To explain this design approach, let’s illustrate the difference between designing failures as exceptions versus designing them as expected outcomes. We’ll do this by solving the same task using the two different approaches. The task is to implement a system to transfer money between bank accounts. In the current domain of banking, a money transfer can have two possible outcomes: either the transaction is performed or it fails due to insufficient funds (figure 9.2).

If you redesign the transfer method from listing 9.8 so that insufficient funds are an expected outcome, you’ll have something like that in listing 9.9. This new method won’t throw any exceptions as part of the logical flow. Instead, it returns the result of the transfer operation. The result can either be a success or a failure, and in the case of a failure, it’s possible for the calling code to find out what type of failure it was by inspecting the result. If there aren’t enough funds, an INSUFFICIENT_FUNDS failure is returned. Otherwise, the method continues and will try to execute the transfer via the executeTransfer(amount, toAccount) method. The executeTransfer method also returns a result that can either be a success or a failure due to difficulties in executing the transfer. When the executeTransfer method finishes, the money transfer either succeeds or fails, with a failure message indicating the reason for failure.

Listing 9.9 Expected results not modeled as exceptional
import static Result.Failure.INSUFFICIENT_FUNDS;
import static Result.success;
import static org.apache.commons.lang3.Validate.notNull;
 
public final class Account {
 
  public Result transfer(final Amount amount,
                         final Account toAccount) {
     notNull(amount);
     notNull(toAccount);
 
     if (balance().isLessThan(amount)) {    #1  
        return INSUFFICIENT_FUNDS.failure();    #2  
     }
 
     return executeTransfer(amount, toAccount);    #3  
  }
 
  public Amount balance() {
     return calculateBalance();
  }
 
  // ...
}
 
-------------------------------------------------
 
import java.util.Optional;
 
public final class Result {
 
   public enum Failure {
      INSUFFICIENT_FUNDS,    #4  
      SERVICE_NOT_AVAILABLE;
 
      public Result failure() {
         return new Result(this);
      }
   }
 
   public static Result success() {
      return new Result(null);
   }
 
   private final Failure failure;
 
   private Result(final Failure failure) {
      this.failure = failure;
   }
 
   public boolean isFailure() {
      return failure != null;
   }
 
   public boolean isSuccess() {
      return !isFailure();
   }
 
   public Optional<Failure> failure() {
      return Optional.ofNullable(failure);
   }
}

#1   Checks whether there are sufficient funds
#2   Returns the failure as a result instead of throwing an exception
#3   Returns the result of calling the underlying systems to execute the transfer
#4   Different types of possible failures