Objects, libraries and code written by others

8 min 5 sections 5 practice questions 8 sources citedISC2 objective 8.2

Modern software is assembled from parts. Some parts the team designs itself, as objects that each look after their own data. Most parts were written by strangers and are borrowed whole, as libraries. Both habits shape security: the first decides how far a mistake can spread inside a program, and the second decides whose mistakes the program inherits. No programming background is assumed.

By the end of this unit you will be able to:

  • Explain class, object, method and message with an everyday example
  • Tell apart encapsulation, abstraction, inheritance and polymorphism
  • Explain why high cohesion and low coupling are wanted
  • Tell apart a library, a software development kit and a framework
  • Choose the controls that manage the risk of borrowed code

A logging library inside almost everything

In December 2021 the United States cyber agency CISA was responding to active, widespread exploitation of a flaw in Apache Log4j. Log4j is a library: a piece of ready-made code that other programs use to keep a record of what they do. CISA described it as very broadly used in consumer and enterprise services, websites and applications.

The flaw, known as Log4Shell, let a remote attacker with no account take control of an affected system. On 17 December 2021 CISA issued an emergency directive to federal agencies. Its guidance told every organisation to work out which of its products contained Log4j, and to include software written in house and systems not facing the internet.

Most of those organisations had never chosen Log4j. How did it come to be inside their systems, and what record would have answered CISA's question in an hour?

Classes and objects: a template and the things made from it

Many programming languages are object-oriented: the program is built from objects. An object is a bundle of data together with the operations that are allowed on that data.

A class is the template from which objects are made. It says what data every object of that kind holds and what each can do. One object made from a class is also called an instance of it.

A bank's account class

A bank's program has a class called Account. The class says that every account holds a balance and an owner, and can do three things: pay in, withdraw and report its balance.

Mrs Okafor's account and Mr Lindqvist's account are two objects of that class. They hold different balances. They behave by the same rules, because both were made from the one template.

The things an object can do are its methods. A method is the code that runs to carry out one of an object's operations, such as withdraw. Objects ask each other to act by sending a message: a request from one object to another to run one of its methods. A cash machine object sends the message withdraw 50 to an account object and waits for the answer.

Encapsulation, abstraction, inheritance and polymorphism

Four properties of objects are examined by name.

Encapsulation
An object's data is sealed inside it and can be reached only through its methods. Nothing outside the account can set the balance directly. It must ask to withdraw, and the withdraw method checks the funds first. The same idea is called information hiding or data hiding.
Abstraction
Showing only what matters for the purpose and leaving out the rest. The cash machine needs to know that an account can withdraw. It does not how the ledger is stored.
Inheritance
A new class takes on the data and methods of an existing class and adds its own. A SavingsAccount class inherits everything from Account and adds an interest rate. The existing class is the parent, or superclass. The new one is the child, or subclass.
Polymorphism
Different classes answer the same message in their own way. Send month end to a savings account and it adds interest. Send it to a loan account and it charges interest. The sender does not need to know which kind it is talking to.

Each has a security consequence. Encapsulation protects , because every change to the data passes through code that can check it. Inheritance spreads whatever the parent contains: a flaw in the parent class is a flaw in every child, and a correction to the parent corrects them all.

Common misconception

“Encapsulated data is protected from attackers, as encrypted data is.”

Encapsulation is a rule inside the program about which code may touch which data. It stops programming mistakes from spreading. It does not hide the data from someone who can read the computer's memory or the database.

High cohesion and low coupling

A module is one self-contained part of a program. Two measures describe how well a program has been divided into modules.

Cohesion and coupling
MeasureWhat it describesWhat is wantedWhy
CohesionHow closely the tasks inside one module belong togetherHighA module that does one job is easy to understand, test and review
CouplingHow much one module depends on the insides of anotherLowA change or a fault in one module does not spread to the others

A restaurant kitchen

The pastry station makes only desserts: high cohesion. It needs nothing from the grill station except a clean plate handed over at the pass: low coupling. If the grill catches fire, pastry carries on.

A kitchen where every cook does a little of everything and reaches into every fridge has low cohesion and high coupling. One mistake travels everywhere.

High cohesion, low coupling. Each module does one job and leans on the others as little as possible.

Libraries, kits and frameworks told apart

Teams rarely write common functions themselves. They borrow them, in three forms.

Three kinds of borrowed code
KindWhat it isWho is in chargeEveryday comparison
LibraryReady-made code for one job, such as , drawing charts or reading a file formatThe team's code calls the library when it needs itA power drill taken from the shelf
Software development kit (SDK)A supplier's bundle of libraries, tools and instructions for building software that works with its productThe team's code calls the kitThe fitting kit sold with a kitchen
FrameworkA reusable skeleton of a whole application that the team extends with its own partsThe framework calls the team's codeA house frame that the builder fills in

Any outside component a program needs is a dependency. Libraries use other libraries, so a program also has transitive dependencies: the dependencies of its dependencies. A team may choose forty libraries and ship four hundred. That is how Log4j reached organisations that had never heard of it.

A flaw in a library is a flaw in every product that uses it

Borrowed code runs with the same access as the code around it. The organisation that ships a product answers for all of it, whoever wrote each part. Six controls manage the .

  • Take components only from approved sources, through an internal store that holds vetted copies.
  • Review a library before first use: who maintains it, how actively, and under what licence.
  • Pin the version. The build records the exact version of each component, with a of it, and never fetches whatever is newest.
  • Keep an of every component and version in every release, including the transitive ones. The formal name is a .
  • Watch published vulnerability lists for those components, and update when one is affected.
  • Remove components that are no longer used.

Two attacks aim at the moment a build fetches its components. In typosquatting the attacker publishes a malicious package whose name is one slip of the keyboard from a popular one. In dependency confusion the attacker publishes a public package with the same name as an organisation's private one, so that the build fetches the attacker's copy. Approved sources, pinned versions and hash checks defeat both.

Common misconception

“A flaw in a third-party library is for the library's author to deal with.”

The author can publish a corrected version. Only the organisation that shipped the old version can find where it is running and replace it. Until it does, its own product is vulnerable.

A customer cannot replace a library inside a product that a supplier maintains. It raises the flaw with the supplier, under the agreements in their contract, and asks for the supplier's fix or a statement of whether the product is exposed. Meanwhile it applies a , a temporary safeguard such as the supplier's workaround or cutting the product off from the network.

A question, worked through

A charity's donation website is built from about forty open-source libraries. A critical flaw is announced in one widely used library. The trustees ask whether the site is affected. It takes the web team three days of searching to answer, because nobody knows which libraries, or which versions, each release contains. The security lead is asked what to put in place so the next announcement can be answered quickly.

What should be put in place FIRST?

  1. An inventory of every component and version in each release
  2. A rule that developers use only code written by the charity
  3. Automatic updating of every library to its newest version
  4. A penetration test of the donation site every six months
Decide on your answer, then open the reasoning
  1. The question asks what comes first, and the stated problem is that nobody knows what the site contains.
  2. Rule out b. Writing everything in house is beyond a charity's means, and home-made versions of common functions tend to hold more flaws than well-used libraries.
  3. Rule out d. A penetration test is an authorised attack on the finished site. It finds some flaws on the day. It does not list the components, so it cannot answer whether an announced flaw is present.
  4. Between a and c: automatic updating sounds like the cure, but it fetches code nobody has reviewed, which is the opening typosquatting and compromised releases use. It still leaves the team unable to say what is running.
  5. An inventory answers the trustees' question in minutes, and every later control depends on it. Updates, monitoring and removal all start from knowing what is there. Option a.

Answer: A. An inventory of every component and version in each release

Choosing between good answers

A question asks which design property keeps a fault in one part of a program from spreading
choose low coupling. High cohesion is the answer when the point is that each module does one job.
A scenario says data can be changed only through an object's own operations
that is encapsulation. A child class gaining a parent's behaviour is inheritance, and one message answered differently by different classes is polymorphism.
A flaw is announced in a third-party component
the organisation that shipped the component must find and replace it. Waiting for the author is not an answer.
Several component controls are offered and the question asks what comes first
choose the inventory. Nothing can be monitored, updated or removed until it is known to be there.

Check your understanding

5 questions on this unit, easiest first. Confirm an answer to see the reasoning for it, then the right answer, the other options and the rule to reuse. Your answers are kept in this browser and feed readiness and review.

These questions are drafts that no named reviewer has approved yet.

Question 1 of 5

Match each term about borrowed code to its description.

Choose the match for each entry. Every match has to be right.

What to remember

  • A class is a template. An object, or instance, is one thing made from it. Objects act through methods and ask each other to act by sending messages.
  • Encapsulation seals data behind methods. Abstraction shows only what matters. Inheritance passes a parent's data and methods to a child. Polymorphism lets classes answer one message differently.
  • High cohesion and low coupling are wanted: each module does one job and depends little on the others.
  • Code calls a library or a kit. A framework calls the team's code.
  • A flaw in a borrowed component is a flaw in the product. Control it with approved sources, review, pinned versions, an inventory, monitoring and removal.
  • Typosquatting uses a look-alike name. Dependency confusion uses the same name as a private package.

Say it from memory

  1. What is a library, why is it a security concern, and which controls manage it?

    A library is ready-made code, usually written by others, that a program reuses. It runs with the program's own access, so its flaws become the product's flaws, and it brings its own dependencies with it. Controls: approved sources, review before first use, pinned and hashed versions, an inventory of every component, monitoring for known vulnerabilities with prompt updates, and removal of unused components.

    Read this part again

  2. What are a class and an object, what do encapsulation, inheritance and polymorphism mean, and which cohesion and coupling are wanted?

    A class is a template and an object is one instance made from it. Encapsulation seals data behind the object's methods. Inheritance passes a parent class's data and methods to a child, flaws included. Polymorphism lets different classes answer one message in their own way. High cohesion and low coupling are wanted, so a fault stays in one module.

    Read this part again

  3. What are a method and a message in object-oriented programming?

    A method is the code that runs to carry out one of an object's operations, such as withdraw. A message is a request from one object to another to run one of its methods, as when a cash machine object sends withdraw 50 to an account object.

    Read this part again

  4. How do a library, a software development kit and a framework differ, and what are transitive dependencies?

    A library is ready-made code for one job, which the team's code calls. An SDK is a supplier's bundle of libraries, tools and instructions for building software that works with its product. A framework is a reusable skeleton of a whole application that calls the team's code. Transitive dependencies are the dependencies of a program's dependencies.

    Read this part again

  5. How do typosquatting and dependency confusion differ, and what defeats both?

    In typosquatting the attacker publishes a malicious package whose name is one slip of the keyboard from a popular one. In dependency confusion the attacker publishes a public package with the same name as an organisation's private one. Approved sources, pinned versions and hash checks defeat both.

    Read this part again

Sources