concept ISBN in category security

appears as: n ISBN, ISBN, ISBN, An ISBN
Secure by Design

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

Different things are represented in different ways in the domain, some explicit and some implicit. For example, the model contains order as a concept, and an order has a monetary value. An order also consists of order lines, each with a book and a quantity. A book has a title, an ISBN, and a price. Order, order line, and book are explicit concepts in the domain. Quantity, title, ISBN, and price are implicit concepts, represented by integers, strings, and so forth. Rephrased, a quantity is an integer without restrictions. Looking at it this way, it seems strange that order, order line, and book are all well elaborated, but quantity is left as an integer without consideration. How did it become this way?

Imagine you’re building book-managing software that uses ISBNs to identify books. After a while, you realize you need a way to identify and handle books that haven’t received an ISBN yet. One approach would be to redefine the term ISBN to not only represent real ISBN numbers, but also to include internally assigned identifiers, perhaps using a magic prefix or something similar to distinguish them from the real ISBNs. To avoid the possible confusion that comes with redefining an ISO standard, you could instead introduce a new term, BookId, that would contain either an ISBN or an UnpublishedBookNumber (figure 5.3). BookId is what identifies a book, and UnpublishedBookNumber is the internally assigned identifier.

Figure 5.3 Introducing new terms instead of redefining existing ones

figure05-03.eps

By introducing two new terms, BookId and UnpublishedBookNumber, you’re able to keep the exact and well-known definition of ISBN, while at the same time meeting the needs of your business domain.

Listing 5.5 Order entity that does a lot in its addItem method
import org.apache.commons.lang3.Validate;

class Order {
   private BookRepository bookCatalog;
   private ArrayList<Object> items;
   private boolean paid = false;
   Inventory inventory;

   public void addItem(String isbn, int qty) {
      if(this.paid == false) {    #1  
         notNull(isbn);                              
         isTrue(isbn.length() == 10);    #2  
         isTrue(isbn.matches("[0-9X]*"));    #2  
         isTrue(isbn.matches("[0-9]{9}[0-9X]"));    #2  
         Book book = bookCatalog.findByISBN(isbn);    #3  
         if(inventory.avaliableBooks(isbn) >= qty)){    #4  
            items.add(new OrderLine(book, qty));    #5  
         }
      }
   }
   ...
}

class ShoppingFlow {

    void handleOrderAdd() {    #6  
        ...
        String isbnText = ...
        int qty = Integer.parseInt(qtyText);
        ...    #7  
        order.addItem(isbn, qty);
       ...

    }

}

#1   Checks that the order isn’t closed
#2   Validates ISBN format
#3   Uses validated ISBN to search for book
#4   Checks that there are enough books left for sale
#5   Adds the quantity of books to the order
#6   Calls the order entity to update it
#7   Did we validate or not? Should we have?

What about the mistake? There are two. First, the code doesn’t verify the checksum of the ISBN. That’s a minor flaw and most probably harmless. But second, the code doesn’t check for negative quantities—the very flaw that led to the massive loss of money in the chapter 2 case study. If you noticed, good spotting, indeed. If you didn’t, don’t feel too bad about it. It’s hard to spot these kinds of mistakes in a codebase you’re shown like this. (In the case study, there was also a restriction that the quantity must be a maximum of 240 books because of restrictions from the storehouse and logistics. That check is also lacking in this code.)

Finding mistakes gets even trickier with a more realistic codebase. A realistic codebase needs to take even more aspects into account. For example, it needs to do error handling, something this example glosses over. A realistic addItem would throw ItemCannotBeAddedException and InvalidISBNException when appropriate. It would also include a for loop for finding if the ISBN was already in the order to avoid several OrderLines with the same ISBN. To make even more of a mess, the code would be riddled with outdated functionality, like last year’s promotional campaign where you received one cookbook for free if you bought three books on home furnishing. The code that was added then is no longer used and hasn’t been removed. The same goes for the code for the Christmas campaign. There would also be the VIP customer cases with a discount on express shipping, and free shipping under certain circumstances would be patched in there somewhere in the mess. The list goes on. In a large and hairy method like that, it wouldn’t be strange if you didn’t notice a missing range check.