Security Unit and Integration Tests
User A cannot access User B’s invoice; expired tokens and unsigned webhooks are rejected; non-admins cannot call admin operations.
Frame the problem
Security starts with a concrete asset, attacker capability and trust crossing.
Why the system fails
Tests cover owner success but omit non-owner, expired, unsigned, cross-tenant and over-limit cases.
The important question is not “what is Security Unit and Integration Tests?” but “which assumption let untrusted data or an over-scoped identity cross test principal/input → protected operation?” Trace the decision at the boundary, then constrain what can happen after the first control fails.
Design the control in layers
Start with the control closest to the interpretation or privilege boundary: Write negative tests for every authorization boundary Then add a control that reduces blast radius and telemetry that proves the decision was enforced.
The resulting design is not labelled secure. Record the identified controls, the known failure paths, the remaining exposure, and the evidence you would need during an incident.
| Prevent | Detect | Recover |
|---|---|---|
| Write negative tests for every authorization boundary · Test expired/revoked credentials and malformed signed messages · Exercise alternate API paths and batch operations | CI failure when an invariant changes | Contain the affected identity or component, scope impact from audit evidence, and preserve a regression test. |
Key points
- Asset: Explicit security invariants at every privileged boundary.
- Boundary: Test principal/input → protected operation
- Primary control: Write negative tests for every authorization boundary
- Detection signal: CI failure when an invariant changes
- Always ask what limits damage when the primary control fails.
Boundary control exercise
This lesson uses the shared boundary-control exercise.
Follow the attack
Safe conceptual simulation: capability → missing control → crossed boundary → asset impact.
- 1Attacker starts with: A future code change that accidentally removes or bypasses a control.
- 2Tests cover owner success but omit non-owner, expired, unsigned, cross-tenant and over-limit cases.
- 3The weak or missing boundary control is crossed: Test principal/input → protected operation
- 4Impact: A refactor silently reopens a known attack path.
- A refactor silently reopens a known attack path.
Defend, detect, recover
One prevention is a single point of security failure. Layer it and make failure observable.
- • Write negative tests for every authorization boundary
- • Test expired/revoked credentials and malformed signed messages
- • Exercise alternate API paths and batch operations
- • CI failure when an invariant changes
- • Contain the affected identity or component.
- • Scope access from audit evidence.
- • Fix the boundary and add a regression test.
- • Misconfiguration and new access paths can bypass the intended control.
- • A privileged insider or compromised control plane may still reach the asset.