Testingsecurityunitandintegrationtests

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.

▶ Run the labFollow the failure

Frame the problem

Security starts with a concrete asset, attacker capability and trust crossing.

Asset
Explicit security invariants at every privileged boundary.
Attacker & capability
A future code change that accidentally removes or bypasses a control.
Trust boundary
Test principal/input → protected operation
AssetThreatAttack SurfaceTrust BoundaryVulnerabilityExploit PathImpactMitigationDefense in DepthResidual Risk

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.

PreventDetectRecover
Write negative tests for every authorization boundary · Test expired/revoked credentials and malformed signed messages · Exercise alternate API paths and batch operationsCI failure when an invariant changesContain 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.

Boundary control check
Untrusted input / identity
Trust boundary
Privileged asset
Prevention may fail silently.

Follow the attack

Safe conceptual simulation: capability → missing control → crossed boundary → asset impact.

  1. 1
    Attacker starts with: A future code change that accidentally removes or bypasses a control.
  2. 2
    Tests cover owner success but omit non-owner, expired, unsigned, cross-tenant and over-limit cases.
  3. 3
    The weak or missing boundary control is crossed: Test principal/input → protected operation
  4. 4
    Impact: A refactor silently reopens a known attack path.
Blast radius
  • 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.

Prevent
  • • Write negative tests for every authorization boundary
  • • Test expired/revoked credentials and malformed signed messages
  • • Exercise alternate API paths and batch operations
Detect
  • • CI failure when an invariant changes
Respond & recover
  • • Contain the affected identity or component.
  • • Scope access from audit evidence.
  • • Fix the boundary and add a regression test.
Residual risk
  • • Misconfiguration and new access paths can bypass the intended control.
  • • A privileged insider or compromised control plane may still reach the asset.

Misconceptions

Claim
“A single write negative tests for every authorization boundary control makes this safe.”
Reality
One control changes risk; it does not erase it. Design prevention, detection, recovery, and blast-radius limits together.