concept list in category security

This is an excerpt from Manning's book Secure by Design.
Imagine yourself setting up a typical software project. You assemble a team of developers, testers, and domain experts and start outlining the key requirements. With input from stakeholders, you come up with a list of important attributes: performance, security, maintainability, and usability. As with many software projects, quality takes top priority, time to market is of the essence, and you need to stay within budget. You decide to be proactive and add security features to your backlog, and some of the other team members come up with a good list of security libraries you can use in your code. After the initial planning, you get the project up and running and start implementing features and business functionality. The team is motivated and delivers features at a good pace.
Even if a class is well designed so as not to leak mutable objects, there’s one tricky area: collections such as lists or sets. For example, an
Orderobject of an online bookstore might have a list of order lines, where each line describes a book being bought together with the quantity. On the inside, this is stored as a data field,List<OrderLine> orderItems. For such collections, we’ve seen a few pitfalls that we’d like to share.To start with, you obviously must not expose the list to the outside. Making the field
orderItemspublic would allow anyone to replace the list arbitrarily. Neither should you use a setter method likevoid setOrderItems(List<OrderLine> orderItems)that does the same thing. Instead of exporting the collection to the outside and letting the client work on the list, you want to encapsulate what is done inside the entity.For example, to add items to the list, you should have a
void addOrderItem(OrderLine orderItem)method. If you want to know the total quantity of items in the order, you don’t give the client the list to do the sum; instead, you capture the calculation inside anint nrItems()method. Think of the entity as a gravity well for functionality and let computations move into the entity. The result over time is a much greater consistency of business rules, as well as greater integrity of data. You might even come to the point where there’s no need to expose the list at all because everything that works on that list is now inside the entity abstraction.If there’s still a legitimate need for the rest of the code to work on the list of order items, then you need to share it in some form; for example, by providing a method
List<OrderItem> orderItems(). But now you’re back to the same problem as before. The list is a mutable object, and there’s nothing that stops a client from obtaining a reference to the order item list and then modifying it by adding new order items or removing existing items. In the following listing, you can see howvoid addFreeShipping(Order)works directly on the order item list.Listing 6.12 Violating integrity by working on the list from outside
void addFreeShipping(Order order) { if(order.value().greaterThan(FREE_SHIPPING_LIMIT) { List<OrderLine> orderlines = order.orderItems(); orderlines.add( #1 new OrderLine(SHIPPING_VOUCHER, 1)); } } #1 Adds directly to the list, without the consent of the Order objectIn this example, the method
orderItemsgives out a reference to the list where order items are stored. The client directly changes this list, and theOrderobject doesn’t get a chance to control the change. This is definitely a security loophole, and something we see quite often.The way to secure this design is to ensure that when an entity returns its data, it should be a nonmodifiable copy. For data fields of primitive types, this isn’t a problem. In the previous example of
boolean isPaid(), the value returned is a copy of the boolean value in the data field. The receiver can do whatever it likes without affecting theOrder. To secure the design forList<OrderItem> orderItems(), you must ensure the copy that’s returned can’t be used to make changes to your internal list. You can clone the list in the same way as you did withDate, but for collections, there’s also a special trick using a read-only proxy.Starting with cloning, the usual way to copy collections isn’t by using
clonebut instead by using the so-called copy constructors. Every class in the collections library in Java has a constructor that takes another collection as an argument and creates a new collection with the same content. You can see in the following listing how this is used fororderItems, which returns a copy of the list of order items.Listing 6.13 Copy constructor to return a copy of the list of order items
class Order { private List<OrderLine> orderitems; #1 public List<OrderLine> getOrderItems() { return new ArrayList(orderitems); #2 } } #1 Collection that carries the internal state #2 The caller is handed a copy of the list.The caller of
orderItemsgets a copy of the collection, and any changes are made to that copy, not to the list insideOrder. The drawback of this approach is that the caller can still do operations on the list, thinking they should result in state changes. This can lead to hard-to-find bugs. But, as we mentioned, for collections there’s another neat trick available. In the handyCollectionsutility class, there are many useful static methods. One of them isstatic <T> List<T> unmodifiableList(List<? extends T> list)