Section 10.43

Transitive dependency

Definition

A transitive dependency is a dependency that one part of a system has on another part through one or more intermediate parts. A part A depends on a part B when a change in B can change A’s behavior, or a property on which A relies. If A depends on B and B depends on C, then A depends transitively on C, even though A never refers to C. In software packaging, the transitive dependencies of a program are the dependencies of its dependencies, at any depth.

The idea

A program uses a library, that library uses a second library, and the second library has a defect. Neither the program’s code nor its list of direct dependencies mentions the second library, yet the defective code runs inside the program. In December 2021 a flaw in Apache Log4j 2, a widely used logging library for the Java language, became public under the name Log4Shell and the Common Vulnerabilities and Exposures (CVE) identifier CVE-2021-44228 [500]. Many organizations first had to find out whether their programs contained Log4j at all, because it often arrived only as a dependency of another library.

Dependencies are not limited to packages. A shared parser, the code that turns received bytes into fields (a deserialization §4.29 step), passes its output to consumers, and other components use what those consumers produce. A change at the parser flows downstream like water in a river, past every later point, whether or not those points changed. The picture stops at harm: a change that reaches a component may or may not alter the property that the component relies on, so each dependency is judged for the property it carries.

How it works

Model the system as a dependency graph: one node per component, and an arrow from A to B whenever A depends directly on B. Components can be packages, files, functions, data formats, configuration values or hardware. Direct dependencies arise in several ways.

Two questions are answered by following arrows.

  1. What does A depend on? Start at A and follow arrows forward. Every node reached is a direct or transitive dependency of A.

  2. What can a change to C affect? Start at C and follow arrows backward. Every node reached is a direct or transitive dependent of C: a component that may need rechecking when C changes.

A breadth-first search §14.18, which visits every node one arrow away, then every node two arrows away, and so on, answers either question in time proportional to the number of nodes plus the number of arrows. It marks visited nodes, so a cycle of dependencies cannot make it loop forever.

Reaching a node is necessary for an effect, not sufficient. For each dependent, ask which property of its dependency it relies on, and whether the change altered that property. A consumer that relies on a parser’s length limit is affected when the limit changes. A consumer that relies only on the order of the fields is not.

Package managers, such as npm for the JavaScript language and Cargo for the Rust language, build this graph from each package’s list of direct dependencies and pick a version of every transitive dependency. A lock file records the picked versions so that the next build uses the same ones. Without it, a transitive dependency can change version between two builds although no direct dependency changed.

Worked example

A parser P reads messages whose payload is at most 16 bytes long. Consumer A copies each payload into a 16-byte buffer. Consumer B computes a checksum §4.32 over each payload, whatever its length. A network service S hands every request to A. Writing X→YX \to Y for “X depends directly on Y”, the arrows are S→AS \to A, A→PA \to P and B→PB \to P.

The accepted input language of P, the set of all inputs that P accepts, consists of the messages whose payload length is 0 to 16 bytes. A developer raises P’s limit to 32 bytes. Neither A, nor B, nor S changes.

Following arrows backward from P reaches A and B in one step, and S in two steps, through A. So A, B and S are the dependents to examine.

Checking P alone, even with every possible input, establishes nothing about S. The defect appears only where P’s new output meets A’s old assumption.

Stated precisely

Let DD be the direct-dependency relation: xDyx \mathrel{D} y means that component xx depends directly on component yy. Its transitive closure D+D^{+} is defined by: xD+yx \mathrel{D^{+}} y if and only if there is a chain x=c0Dc1D…Dck=yx = c_0 \mathrel{D} c_1 \mathrel{D} \dots \mathrel{D} c_k = y of k≥1k \ge 1 steps. Component yy is a transitive dependency of xx when such a chain exists with k≥2k \ge 2 steps.

If the graph contains every real dependency, a change to yy can affect xx only if x=yx = y or xD+yx \mathrel{D^{+}} y. The condition is necessary, not sufficient: the effect occurs only if the change alters a property that every link of some chain passes on.

For the inputs that a consumer processes, let L(P)L(P) be the set of inputs that parser PP accepts, and let LAL_A be the set of inputs that consumer AA’s own checks accept (all inputs, if AA makes no checks of its own). If AA receives its input only through PP, the inputs that AA processes form the set L(P)∩LAL(P) \cap L_A, where ∩\cap is set intersection. Changing L(P)L(P) changes what AA processes unless LAL_A excludes every input that the change added.

Limits and common mistakes

A graph built only from import and include statements misses dependencies through data formats, configuration, shared memory, callbacks and code loaded while the program runs. Such a graph shows fewer dependencies than exist, and a change analysis based on it can miss affected components.

Following every arrow without asking which property it carries overstates the effect: a change usually reaches many components but alters a property that only a few of them rely on.

In a diamond dependency, two dependencies of one program both use a third library but need different versions of it. A package manager that installs only one version may give one of the two a version it was never tested with.

A software bill of materials (SBOM), a machine-readable list of every component in a product including its transitive dependencies, records the nodes of the graph but not which property each consumer relies on. It helps find affected components, not judge harm.

Questions answered

Does changing a shared parser affect every consumer’s accepted input language?

Yes, for every consumer that relies on the parser alone to filter its input; a consumer whose own checks reject every newly accepted input keeps its old accepted input language. The accepted input language of a component is the set of inputs it processes instead of rejecting. A consumer that receives its input only through the parser processes exactly the inputs that both the parser and the consumer’s own checks accept. When the parser starts to accept new inputs, those inputs reach every such consumer whether or not the consumer’s code changed, and the change also reaches every component that depends on those consumers.

Whether the new inputs do harm depends on the property each consumer relies on. If a parser’s payload limit rises from 16 to 32 bytes, a consumer that copies payloads into a 16-byte buffer now writes up to 32−16=1632 - 16 = 16 bytes past its end, and a service that calls that consumer is affected through it. A consumer that handles payloads of any length is not harmed. The change must therefore be followed to every direct and transitive consumer, and each consumer’s assumption checked against the new set of accepted inputs.

Related entries: Incremental validation (§10.42), Compiler correctness (§3.40), Deserialization (§4.29), Firmware build profile (§6.44), Buffer overflow (§4.4). Further reading: [500].