User-triggered code paths have infinite-loop guards
Why it matters
Recursive functions without base cases or depth limits crash on cyclic data structures and blow the call stack on deeply nested inputs. CWE-674 (Uncontrolled Recursion) and CWE-835 (Loop with Unreachable Exit Condition) both apply. In a user-facing API, an attacker who can control a tree or graph structure — JSON, AST, category hierarchy, file system path — can trigger a stack overflow that crashes the Node process for all users. Even without adversarial input, an AI-generated recursive function that processes database-fetched hierarchical data will eventually encounter a self-referencing node in production and take down the server.
Severity rationale
Medium because recursion without a guard requires either user-controlled input or corrupt data to trigger, but the crash affects all users on the same process when it fires.
Remediation
Add a depth parameter with a hard limit to every recursive function. Throw an explicit error rather than returning silently, so the guard failure is visible in logs.
const MAX_DEPTH = 100
function processNode(node: TreeNode, depth = 0): ProcessedNode {
if (depth > MAX_DEPTH) {
throw new Error(`processNode: max depth ${MAX_DEPTH} exceeded — possible cycle`)
}
return {
...node,
children: node.children.map(child => processNode(child, depth + 1)),
}
}
For graph traversal (not trees), add a visited: Set<string> parameter and check visited.has(node.id) before recursing. This catches cycles that a depth limit alone would miss.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Hidden Cost Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- cwe · CWE-674 — Uncontrolled Recursion
- cwe · CWE-835 — Loop with Unreachable Exit Condition
- iso-25010:2011 · reliability
Taxons
History
- 2026-04-18·v1.0.0·Initial import from ai-slop-cost-bombs·automated