Defense-in-Depth Validation
What it does
When you fix a bug caused by invalid data, adding one validation check feels
sufficient — but that single check gets bypassed by other code paths,
refactoring, or mocks. This skill says: validate at EVERY layer the data passes
through, so the bug is structurally impossible instead of merely patched.
- Single validation = "we fixed the bug"
- Multiple layers = "we made the bug impossible"
When to use
- You just fixed (or are about to fix) a bug rooted in invalid/empty/malformed input.
- A value reached somewhere dangerous (wrong dir, wrong env, null where required).
- You catch yourself adding ONE
if (!x) throw and calling it done.
- Hardening code paths that mocks or tests can sneak bad data through.
The four layers
Add a check at each layer the data crosses. Different layers catch different cases.
- Entry point validation — reject obviously invalid input at the API
boundary (empty, missing, wrong type, path doesn't exist / isn't a dir).
Catches most bugs.
- Business logic validation — ensure the data makes sense for THIS
operation (e.g.
projectDir required before initializing a workspace).
Catches edge cases that bypass entry checks.
- Environment guards — refuse dangerous operations in specific contexts.
e.g. in tests, refuse
git init outside a temp dir:if (process.env.NODE_ENV === 'test') {
const normalized = normalize(resolve(directory));
const tmp = normalize(resolve(tmpdir()));
if (!normalized.startsWith(tmp)) {
throw new Error(`Refusing git init outside temp dir during tests: ${directory}`);
}
}
- Debug instrumentation — capture context for forensics when other layers
fail. Log the value,
cwd, and a stack trace right before the risky call:logger.debug('About to git init', { directory, cwd: process.cwd(), stack: new Error().stack });
How to apply
- Trace the data flow — where does the bad value originate, where is it used?
- Map every checkpoint — list each point the data passes through.
- Add validation at each layer — entry, business, environment, debug.
- Test each layer — try to bypass layer 1 and verify layer 2 catches it;
exercise the mock/edge-case path that motivated the fix.
Worked example
Bug: empty projectDir caused git init to run in the source-code directory.
Data flow: test setup passes '' → Project.create(name, '') →
WorkspaceManager.createWorkspace('') → git init runs in process.cwd().
Layers added:
- L1:
Project.create() validates not-empty / exists / is-directory / writable.
- L2:
WorkspaceManager validates projectDir not empty.
- L3:
WorktreeManager refuses git init outside tmpdir during tests.
- L4: stack-trace log before
git init.
Result: all tests passed; bug impossible to reproduce. Each layer caught cases
the others missed — different code paths bypassed entry validation, mocks
bypassed business checks, platform edge cases needed the env guard, and the log
exposed structural misuse.
Gotcha
Don't stop at one validation point. The temptation is to add a single check and
move on — but that's the failure mode this skill exists to prevent. All layers
are necessary; each catches what the others let through.
1---2name: defense-in-depth3description: "Use when fixing a bug caused by invalid/empty/malformed data and a single validation check feels enough — add validation at EVERY layer (entry point, business logic, environment guard, debug logging) so the bug becomes structurally impossible, not just patched."4version: 1.0.05license: MIT6tags: [validation, defense-in-depth, security, bug-fix, input-validation, guards, hardening, layered-validation]7source: https://github.com/obra/superpowers/tree/main/skills/defense-in-depth8derived_from: awesomeclaude9metadata:10 source: https://skillmd.com/skills/aashutosh396/defense-in-depth11---1213# Defense-in-Depth Validation1415## What it does1617When you fix a bug caused by invalid data, adding one validation check feels18sufficient — but that single check gets bypassed by other code paths,19refactoring, or mocks. This skill says: validate at EVERY layer the data passes20through, so the bug is structurally impossible instead of merely patched.2122- Single validation = "we fixed the bug"23- Multiple layers = "we made the bug impossible"2425## When to use2627- You just fixed (or are about to fix) a bug rooted in invalid/empty/malformed input.28- A value reached somewhere dangerous (wrong dir, wrong env, null where required).29- You catch yourself adding ONE `if (!x) throw` and calling it done.30- Hardening code paths that mocks or tests can sneak bad data through.3132## The four layers3334Add a check at each layer the data crosses. Different layers catch different cases.35361. **Entry point validation** — reject obviously invalid input at the API37 boundary (empty, missing, wrong type, path doesn't exist / isn't a dir).38 Catches most bugs.392. **Business logic validation** — ensure the data makes sense for THIS40 operation (e.g. `projectDir` required before initializing a workspace).41 Catches edge cases that bypass entry checks.423. **Environment guards** — refuse dangerous operations in specific contexts.43 e.g. in tests, refuse `git init` outside a temp dir:44 ```ts45 if (process.env.NODE_ENV === 'test') {46 const normalized = normalize(resolve(directory));47 const tmp = normalize(resolve(tmpdir()));48 if (!normalized.startsWith(tmp)) {49 throw new Error(`Refusing git init outside temp dir during tests: ${directory}`);50 }51 }52 ```534. **Debug instrumentation** — capture context for forensics when other layers54 fail. Log the value, `cwd`, and a stack trace right before the risky call:55 ```ts56 logger.debug('About to git init', { directory, cwd: process.cwd(), stack: new Error().stack });57 ```5859## How to apply60611. **Trace the data flow** — where does the bad value originate, where is it used?622. **Map every checkpoint** — list each point the data passes through.633. **Add validation at each layer** — entry, business, environment, debug.644. **Test each layer** — try to bypass layer 1 and verify layer 2 catches it;65 exercise the mock/edge-case path that motivated the fix.6667## Worked example6869Bug: empty `projectDir` caused `git init` to run in the source-code directory.7071Data flow: test setup passes `''` → `Project.create(name, '')` →72`WorkspaceManager.createWorkspace('')` → `git init` runs in `process.cwd()`.7374Layers added:75- L1: `Project.create()` validates not-empty / exists / is-directory / writable.76- L2: `WorkspaceManager` validates `projectDir` not empty.77- L3: `WorktreeManager` refuses `git init` outside tmpdir during tests.78- L4: stack-trace log before `git init`.7980Result: all tests passed; bug impossible to reproduce. Each layer caught cases81the others missed — different code paths bypassed entry validation, mocks82bypassed business checks, platform edge cases needed the env guard, and the log83exposed structural misuse.8485## Gotcha8687Don't stop at one validation point. The temptation is to add a single check and88move on — but that's the failure mode this skill exists to prevent. All layers89are necessary; each catches what the others let through.