Claude MCP skill algorithmic complexity and ReDoS via regex denial of service in input validation — SA-SKL-RATELIMIT12
Regular expression denial of service (ReDoS) and algorithmic complexity attacks exploit the fact that some input validation code has worst-case time complexity that grows exponentially or polynomially with crafted input size. A single HTTP request carrying a carefully constructed string — a URL with repeated segments, a deeply nested JSON object, an exhaustive GraphQL query, or a crafted semver range string — can saturate the Node.js event loop for seconds or minutes, making the MCP skill unresponsive to all other requests. Unlike CPU-based DDoS, ReDoS attacks require no botnet: a single client sending a single request can knock an unprotected single-threaded Node.js server offline. SA-SKL-RATELIMIT12 documents four input validation patterns that introduce algorithmic complexity vulnerabilities in MCP skill infrastructure.
isURL() with backtracking-vulnerable regex on user-supplied URL — O(2^n) worst case with crafted repeated-segment input; a 100-character crafted URL causes multi-second event loop block; fix: use new URL() constructor for validation, avoid complex regex on user-controlled URL stringsJSON.parse() on unbounded-depth nested object from request body — 10,000 nested brackets [[[[...]]]] causes call stack overflow and process crash; fix: use ajv with maxDepth option or a depth-counting JSON Reviver before JSON.parse()graphql-depth-limit (max depth 7) + graphql-query-complexity with request rejectionsemver.satisfies(version, range) with attacker-controlled range string on semver version below 7.5.2 (CVE-2022-25883) — O(n^2) regex on crafted range causes event loop saturation; fix: pin semver ≥7.5.2 or validate range string length (≤100 chars) before calling satisfies()RATELIMIT12-001 — isURL() regex backtracking on crafted URL input
Express validator's isURL() validator uses a complex regular expression to validate URL structure. Some versions of this regex exhibit catastrophic backtracking on specially crafted inputs where the regex engine must explore an exponential number of match paths before concluding the input is invalid. A URL like https://aaaaaaaaaa.aaaaaaaaaa.aaaaaaaaaa.aaaaaaaaaa.aaaaaaaaaa! with repeated dot-separated segments followed by an invalid terminator can cause the regex engine to backtrack O(2^n) times, saturating the Node.js event loop for seconds.
// VULNERABLE — RATELIMIT12-001
// Express validator isURL() with backtracking-vulnerable regex on user URL input.
// Attacker sends: skillUrl = "https://" + "a.".repeat(50) + "a!"
// isURL() regex backtracks exponentially — event loop blocked for 2-10 seconds.
import { body, validationResult } from 'express-validator';
app.post('/api/skills/register', [
// VULNERABLE: isURL() uses complex regex — backtracking on crafted input
body('skillUrl').isURL(),
], async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(400).json({ errors: errors.array() });
await registerSkill(req.body.skillUrl);
res.json({ ok: true });
});
// FIXED — RATELIMIT12-001
// Fix: Use new URL() constructor — O(n) parser, no backtracking, throws on invalid URL.
// URL constructor validates scheme, host, path structure without regex backtracking.
function safeIsUrl(str) {
if (typeof str !== 'string' || str.length > 2048) return false;
try {
const url = new URL(str);
return url.protocol === 'https:' || url.protocol === 'http:';
} catch {
return false;
}
}
app.post('/api/skills/register', async (req, res) => {
const { skillUrl } = req.body;
if (!safeIsUrl(skillUrl)) {
return res.status(400).json({ error: 'Invalid skill URL' });
}
await registerSkill(skillUrl);
res.json({ ok: true });
});
Detection: SkillAudit's static analyzer detects isURL() calls on user-controlled fields and flags them for review. It also checks the installed express-validator version against known ReDoS-fixed releases.
RATELIMIT12-002 — JSON.parse() on unbounded-depth nested object
Node.js's built-in JSON.parse() is implemented in C++ and handles deeply nested JSON by recursing into the parser call stack. On most Node.js versions, nesting beyond approximately 10,000 levels causes a call stack overflow, crashing the Node.js process (or in newer versions, throwing a maximum call stack error that propagates as an unhandled exception). The crafted input is trivially small: a string of 10,000 opening brackets [ followed by 10,000 closing brackets ] is 20,000 bytes — well under typical request size limits — but crashes the process when parsed.
// VULNERABLE — RATELIMIT12-002
// express.json() calls JSON.parse() internally. No body size limit protects against
// deeply nested JSON — size limit is in bytes, not nesting depth.
// Attacker sends: Content-Type: application/json, Body: "[".repeat(10000) + "]".repeat(10000)
// Node.js JSON.parse crashes with Maximum call stack size exceeded.
app.use(express.json({ limit: '1mb' })); // VULNERABLE: byte limit doesn't prevent depth attack
app.post('/api/audits/batch', async (req, res) => {
// JSON.parse already called by middleware — crash happens before reaching here
const audits = req.body;
res.json({ count: audits.length });
});
// FIXED — RATELIMIT12-002
// Fix 1: Use a depth-aware JSON parser (safe-json-parse, secure-json-parse) that limits nesting.
// Fix 2: Validate nesting depth with a lightweight pre-check before JSON.parse().
// Fix 3: Use secure-json-parse which wraps JSON.parse with depth + prototype-pollution protection.
import sjp from 'secure-json-parse';
app.use(express.text({ type: 'application/json', limit: '1mb' }));
app.post('/api/audits/batch', async (req, res) => {
let body;
try {
// FIX: secure-json-parse limits nesting depth (default 20) — throws on deeper input
body = sjp.parse(req.body, null, { protoAction: 'error', constructorAction: 'error' });
} catch (e) {
return res.status(400).json({ error: 'Invalid JSON: ' + e.message });
}
const audits = Array.isArray(body) ? body : [body];
res.json({ count: audits.length });
});
RATELIMIT12-003 — GraphQL query without depth or complexity limits
GraphQL's recursive query structure makes it inherently vulnerable to denial of service via deeply nested or broadly fanned queries. A query that nests audits { skill { audits { skill { audits ... } } } } 20 levels deep can resolve to millions of database queries. Even without database access, the query complexity analysis itself can saturate the event loop if performed naively. A query with 100 levels of nesting or a "broadening fan" that selects 100 fields per level, each with their own resolvers, is enough to bring down an unprotected GraphQL endpoint.
// VULNERABLE — RATELIMIT12-003
// GraphQL endpoint with no depth or complexity limits.
// Attacker sends a 50-level nested query — resolver chain exhausts heap memory.
import { graphql, buildSchema } from 'graphql';
import { createHandler } from 'graphql-http/lib/use/express';
// VULNERABLE: schema has circular relationships — audits have skills, skills have audits
const schema = buildSchema(`
type Audit { id: ID, score: Int, skill: Skill }
type Skill { id: ID, name: String, audits: [Audit] }
type Query { audit(id: ID!): Audit }
`);
// VULNERABLE: no depth limit on the handler
app.use('/graphql', createHandler({ schema, rootValue: resolvers }));
// FIXED — RATELIMIT12-003
// Fix 1: graphql-depth-limit — rejects queries deeper than N levels.
// Fix 2: graphql-query-complexity — assigns cost to each field resolver, rejects expensive queries.
// Fix 3: Disable introspection in production (reduces attack surface for schema mining).
import depthLimit from 'graphql-depth-limit';
import { createComplexityLimitRule } from 'graphql-validation-complexity';
const complexityRule = createComplexityLimitRule(1000, {
onCost: (cost) => console.log('Query cost:', cost),
formatErrorMessage: (cost) => `Query complexity ${cost} exceeds maximum of 1000`,
});
app.use('/graphql', createHandler({
schema,
rootValue: resolvers,
validationRules: [
depthLimit(7), // FIX 1: reject queries nested deeper than 7 levels
complexityRule, // FIX 2: reject queries exceeding complexity budget
],
}));
RATELIMIT12-004 — semver.satisfies() ReDoS via CVE-2022-25883
The semver npm package is widely used in MCP skill version compatibility checks. Versions below 7.5.2 contain CVE-2022-25883: the range parsing regex used by semver.satisfies() exhibits O(n^2) backtracking on crafted range strings like 1.2.3 - 1.2.3 - 1.2.3 - 1.2.3... repeated many times. If the range argument comes from user input (e.g., a client-specified version requirement for a skill installation check), an attacker can craft a 1,000-character range string that blocks the event loop for seconds.
// VULNERABLE — RATELIMIT12-004
// semver.satisfies() on user-supplied range — semver <7.5.2 — CVE-2022-25883.
// Attacker sends: versionRange = "1.2.3 - 1.2.3 - ".repeat(60) + "1.2.3"
// satisfies() regex backtracks O(n^2) — event loop blocked.
import semver from 'semver'; // version 7.3.8
app.get('/api/skills/compatible', async (req, res) => {
const { version, range } = req.query;
// VULNERABLE: range is from query parameter — attacker controlled
const isCompatible = semver.satisfies(version, range);
res.json({ compatible: isCompatible });
});
// FIXED — RATELIMIT12-004
// Fix 1: Upgrade semver to >=7.5.2 (patches CVE-2022-25883).
// Fix 2: Validate range string length before calling satisfies() — reject strings >100 chars.
// Fix 3: Validate range matches a simple semver range pattern before calling satisfies().
import semver from 'semver'; // upgraded to 7.5.2+
const MAX_RANGE_LENGTH = 100;
const SAFE_RANGE_RE = /^[~^><= \d.*x|,&-]+$/; // Allow only semver range characters
app.get('/api/skills/compatible', async (req, res) => {
const { version, range } = req.query;
// FIX 2 + 3: Validate range before passing to semver
if (!range || range.length > MAX_RANGE_LENGTH || !SAFE_RANGE_RE.test(range)) {
return res.status(400).json({ error: 'Invalid version range' });
}
if (!semver.valid(version) && !semver.validRange(version)) {
return res.status(400).json({ error: 'Invalid version string' });
}
const isCompatible = semver.satisfies(version, range);
res.json({ compatible: isCompatible });
});
Detection: SkillAudit checks package.json for semver versions below 7.5.2 and scans for semver.satisfies() / semver.validRange() call sites where the range argument comes from request parameters or user-controlled data. It also scans for GraphQL endpoints missing validation rules and JSON.parse() calls on untrusted request bodies without depth limiting.
Summary — SA-SKL-RATELIMIT12 at a glance
| Pattern | Root cause | Fix | Severity |
|---|---|---|---|
| RATELIMIT12-001 | isURL() regex with catastrophic backtracking on crafted URL; O(2^n) worst case; event loop blocked for seconds | Use new URL() constructor (O(n) parser, no backtracking); cap input length at 2048 chars | High |
| RATELIMIT12-002 | JSON.parse() on unbounded-depth nested JSON; 10k nested brackets crashes Node.js via call stack overflow | Use secure-json-parse with depth limit; pre-check nesting count before parsing | High |
| RATELIMIT12-003 | GraphQL endpoint without depth/complexity validation rules; 100-level nested query exhausts heap and blocks event loop | graphql-depth-limit(7) + complexity rule (max 1000); disable introspection in production | High |
| RATELIMIT12-004 | semver.satisfies() on user-controlled range string; CVE-2022-25883 O(n^2) regex; semver <7.5.2 | Upgrade semver ≥7.5.2; validate range length ≤100 chars; check with allowlist regex before satisfies() | Medium |
Does your MCP skill's input validation introduce ReDoS or algorithmic complexity vulnerabilities?
SkillAudit scans for backtracking-vulnerable regex validators, unbounded JSON nesting, GraphQL endpoints without depth limits, and outdated semver versions with known ReDoS CVEs — before a single crafted request takes your skill offline.
Audit your MCP skill →