Audit context building

From RedHatProductSecurity/prodsec-skills · View interactive version

Audit context building is an AI agent skill from the GitHub repository RedHatProductSecurity/prodsec-skills. Enables ultra-granular, line-by-line code analysis to build deep architectural context before vulnerability or bug finding.

How to use this skill

Skill file

Deep Context Builder Skill (Ultra-Granular Pure Context Mode)

1. Purpose

This skill governs how Claude thinks during the context-building phase of an audit.

When active, Claude will:

This skill defines a structured analysis format (see Example: Function Micro-Analysis below) and runs before the vulnerability-hunting phase.


2. When to Use This Skill

Use when:

Do not use for:


3. How This Skill Behaves

When active, Claude will:

Goal: deep, accurate understanding, not conclusions.


Rationalizations (Do Not Skip)

| Rationalization | Why It's Wrong | Required Action |

|-----------------|----------------|-----------------|

| "I get the gist" | Gist-level understanding misses edge cases | Line-by-line analysis required |

| "This function is simple" | Simple functions compose into complex bugs | Apply 5 Whys anyway |

| "I'll remember this invariant" | You won't. Context degrades. | Write it down explicitly |

| "External call is probably fine" | External = adversarial until proven otherwise | Jump into code or model as hostile |

| "I can skip this helper" | Helpers contain assumptions that propagate | Trace the full call chain |

| "This is taking too long" | Rushed context = hallucinated vulnerabilities later | Slow is fast |


4. Phase 1 — Initial Orientation (Bottom-Up Scan)

Before deep analysis, Claude performs a minimal mapping:

  1. Identify major modules/files/contracts.
  2. Note obvious public/external entrypoints.
  3. Identify likely actors (users, owners, relayers, oracles, other contracts).
  4. Identify important storage variables, dicts, state structs, or cells.
  5. Build a preliminary structure without assuming behavior.

This establishes anchors for detailed analysis.


5. Phase 2 — Ultra-Granular Function Analysis (Default Mode)

Every non-trivial function receives full micro analysis.

5.1 Per-Function Microstructure Checklist

For each function:

  1. Purpose
  1. Inputs & Assumptions
  1. Outputs & Effects
  1. Block-by-Block / Line-by-Line Analysis

For each logical block:

Apply per-block:


5.2 Cross-Function & External Flow Analysis

(Full Integration of Jump-Into-External-Code Rule)

When encountering calls, continue the same micro-first analysis across boundaries.

Internal Calls

caller → callee → return → caller.

External Calls — Two Cases

Case A — External Call to a Contract Whose Code Exists in the Codebase

Treat as an internal call:

Case B — External Call Without Available Code (True External / Black Box)

Analyze as adversarial:

Continuity Rule

Treat the entire call chain as one continuous execution flow.

Never reset context.

All invariants, assumptions, and data dependencies must propagate across calls.


5.3 Complete Analysis Example

See ## Inlined: Function Micro-Analysis Example at the end of this document for a full walkthrough (DEX swap), demonstrating depth and structure required for all analyzed functions.


5.4 Output Requirements

When performing ultra-granular analysis, structure output per ## Inlined: Output Requirements at the end of this document (mandatory sections and quality thresholds).


5.5 Completeness Checklist

Before concluding micro-analysis of a function, verify against ## Inlined: Completeness Checklist at the end of this document.


6. Phase 3 — Global System Understanding

After sufficient micro-analysis:

  1. State & Invariant Reconstruction
  1. Workflow Reconstruction
  1. Trust Boundary Mapping
  1. Complexity & Fragility Clustering

These clusters help guide the vulnerability-hunting phase.


7. Stability & Consistency Rules

(Anti-Hallucination, Anti-Contradiction)

Claude must:

When contradicted:

Summarize core:

Use:

instead of:

Connect new insights to previous state, flows, and invariants to maintain global coherence.


8. Delegating Focused Analysis

Split dense or complex work across separate focused sessions or reviewers when helpful:

Each focused pass should follow the full microstructure checklist, cross-function flow rules, and quality thresholds in this skill, and enforce the pure-context-building constraint (no vulnerability findings).

Delegated work must:

(See upstream Trail of Bits prodsec-skills for companion agent/plugin wiring if you use that distribution.)


9. Relationship to Other Phases

This skill runs before:

It exists solely to build:


10. Non-Goals

While active, Claude should NOT:

This is pure context building only.


Inlined: Output Requirements

Output Requirements

When performing ultra-granular analysis, structure output following the Per-Function Microstructure Checklist format demonstrated in ## Inlined: Function Micro-Analysis Example (later in this document).


Required Structure

For EACH analyzed function, output MUST include:

1. Purpose (mandatory)

2. Inputs & Assumptions (mandatory)

3. Outputs & Effects (mandatory)

4. Block-by-Block Analysis (mandatory)

For EACH logical code block, document:

Minimum standards:

5. Cross-Function Dependencies (mandatory)


Quality Thresholds

A complete micro-analysis MUST identify:


Format Consistency


Inlined: Completeness Checklist

Completeness Checklist

Before concluding micro-analysis of a function, verify:


Structural Completeness


Content Depth


Continuity & Integration


Anti-Hallucination Verification


Completeness Signal

Analysis is complete when:

  1. All checklist items above are satisfied

<!-- skillsaw-disable-next-line content-placeholder-text -->

  1. No remaining "TODO: analyze X" or "unclear Y" items
  2. Full call chain analyzed (for internal calls, jumped into and analyzed)
  3. All identified risks have mitigation analysis or acknowledged as unresolved

Inlined: Function Micro-Analysis Example

Function Micro-Analysis Example

This example demonstrates a complete micro-analysis following the Per-Function Microstructure Checklist.


Target: `swap(address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut, uint256 deadline)` in Router.sol

Purpose:

Enables users to swap one token for another through a liquidity pool. Core trading operation in a DEX that:

This is a critical financial primitive affecting pool solvency, user fund safety, and protocol fee collection.


Inputs & Assumptions:

Parameters:

Implicit Inputs:

Preconditions:

Trust Assumptions:


Outputs & Effects:

Returns:

State Writes:

External Interactions:

Events Emitted:

Postconditions:


Block-by-Block Analysis:

// L90: Deadline validation (modifier: ensure(deadline))
modifier ensure(uint256 deadline) {
    require(block.timestamp <= deadline, "Expired");
    _;
}

// L92-94: Input validation
require(amountIn > 0, "Invalid input amount");
require(minAmountOut > 0, "Invalid minimum output");
require(tokenIn != tokenOut, "Identical tokens");

// L98-99: Pool resolution
address pair = pairs[tokenIn][tokenOut];
require(pair != address(0), "Pool does not exist");

// L102-103: Reserve reads
(uint112 reserveIn, uint112 reserveOut) = getReserves(pair, tokenIn, tokenOut);
require(reserveIn > 0 && reserveOut > 0, "Insufficient liquidity");

// L108-109: Fee application
uint256 amountInWithFee = amountIn * 997;
uint256 numerator = amountInWithFee * reserveOut;

// L110-111: Output calculation (constant product formula)
uint256 denominator = (reserveIn * 1000) + amountInWithFee;
uint256 amountOut = numerator / denominator;

// L115: Slippage protection enforcement
require(amountOut >= minAmountOut, "Slippage exceeded");

// L118: Input token transfer (pull pattern)
IERC20(tokenIn).transferFrom(msg.sender, pair, amountIn);

// L122: Output token transfer (push pattern)
IERC20(tokenOut).transfer(msg.sender, amountOut);

// L125-126: Reserve synchronization
reserves[pair].reserve0 = uint112(reserveIn + amountIn);
reserves[pair].reserve1 = uint112(reserveOut - amountOut);

// L130: Event emission
emit Swap(msg.sender, tokenIn, tokenOut, amountIn, amountOut, block.timestamp);

Cross-Function Dependencies:

Internal Calls:

External Calls (Outbound):

Called By:

Shares State With:

Invariant Coupling:

Assumptions Propagated to Callers:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.