Claude skill path traversal advanced security — SA-SKL-PATHTRAVERSAL2
Most MCP skill developers know to block ../ sequences in file paths, but basic string filtering is defeated by four encoding and filesystem tricks that bypass naive checks. SA-SKL-PATHTRAVERSAL2-001 through PATHTRAVERSAL2-004 cover URL-encoded sequences, null byte injection, Windows backslash traversal, and symlink escape — the four patterns that survive a str.replace('../', '') or includes('..') guard.
Why basic ../ filtering is not enough for MCP skill file access
Path traversal remains one of the most exploited vulnerability classes in file-serving MCP skills because the fix looks deceptively simple: check for .. in the input and reject it. The problem is that this check is performed on a string that may not yet represent what the filesystem will ultimately interpret. URL encoding, null byte injection, platform-specific path separators, and symlinks are all mechanisms by which an attacker can supply a string that passes a ../ check but resolves to an arbitrary filesystem path at read time. The only robust defense is to resolve the final path first and then check that it falls within the allowed base directory — no string-level filter can substitute for this post-resolution containment check.
SA-SKL-PATHTRAVERSAL2 in scope: PATHTRAVERSAL2-001 (URL-encoded %2e%2e%2f bypass), PATHTRAVERSAL2-002 (null byte injection), PATHTRAVERSAL2-003 (Windows backslash traversal), PATHTRAVERSAL2-004 (symlink escape). All four survive a naive includes('..') or replace('../', '') guard and are detected by SkillAudit's path handling static analysis and dynamic traversal probe suite.
PATHTRAVERSAL2-001 — URL-encoded %2e%2e%2f bypasses string filter
When a skill accepts a file path from a URL query parameter or request body and checks the raw string for ../, an attacker can supply the percent-encoded equivalent %2e%2e%2f — which the string check never matches. When path.join() or fs.readFile() subsequently processes the input, the Node.js URL handling or the operating system decodes the percent sequence and resolves the traversal. Double-encoding (%252e%252e%252f, where %25 is a literal %) survives a single decodeURIComponent call, leaving %2e%2e%2f in the string after the first decode — requiring a second decode before the check.
// VULNERABLE: checks raw string for '../' before decoding
app.get('/files', (req, res) => {
const filename = req.query.name; // e.g. "%2e%2e%2fetc%2fpasswd"
// String check on the still-encoded input — finds no '..'
if (filename.includes('..')) {
return res.status(400).send('invalid path');
}
// path.join decodes percent sequences via the OS or Node internals
// resolves to: /var/skill/files/../../etc/passwd => /etc/passwd
const fullPath = path.join('/var/skill/files', filename);
res.sendFile(fullPath);
});
// FIXED: decode fully before checking, then verify post-resolution containment
const path = require('path');
app.get('/files', (req, res) => {
let filename = req.query.name;
// Decode repeatedly until stable (handles double/triple encoding)
let decoded = filename;
try {
let prev;
do {
prev = decoded;
decoded = decodeURIComponent(decoded);
} while (decoded !== prev);
} catch {
return res.status(400).send('invalid encoding');
}
// After full decode, resolve the absolute path
const allowedBase = path.resolve('/var/skill/files');
const resolved = path.resolve(allowedBase, decoded);
// Post-resolution containment check — the ONLY reliable guard
if (!resolved.startsWith(allowedBase + path.sep) &&
resolved !== allowedBase) {
return res.status(403).send('forbidden');
}
res.sendFile(resolved);
});
../ before URL-decoding; percent-encoded sequences %2e%2e%2f or double-encoded %252e%252e%252f pass the guard and are decoded by the filesystem layer, resolving to arbitrary paths outside the intended sandbox. Fix: decode input fully (loop decodeURIComponent until stable) before any validation; then use path.resolve() and verify the result starts with the allowed base directory — never use string-level includes('..') as the sole guard.PATHTRAVERSAL2-002 — Null byte injection terminates path extension check
Some MCP skills validate that a user-supplied filename ends with an allowed extension (e.g. .html, .json) before serving the file. An attacker can append a null byte followed by the expected extension: ../../etc/passwd\x00.html. The JavaScript endsWith('.html') check sees the .html suffix and passes. However, in older Node.js versions, certain native addons, FFI calls to C libraries, and some image processing or PDF rendering libraries that accept file paths as C strings, the null byte terminates the string — the library receives ../../etc/passwd and reads that file. The pattern also applies when paths are passed to child_process.execFile or native database drivers that internally use C-string file path parameters.
// VULNERABLE: extension check passes; null byte terminates path in native calls
async function serveTemplate(req, res) {
const name = req.params.name;
// name = "../../etc/passwd\x00.html"
// endsWith sees the '.html' suffix — check passes
if (!name.endsWith('.html')) {
return res.status(400).send('only .html allowed');
}
// Sharp (native addon) receives path; C-layer truncates at \x00
// actually reads: ../../etc/passwd
const img = await sharp(path.join('/var/skill/templates', name))
.resize(800)
.toBuffer();
res.type('png').send(img);
}
// FIXED: reject null bytes explicitly; use resolved path for all calls
const path = require('path');
async function serveTemplate(req, res) {
const name = req.params.name;
// Reject null bytes before any further processing
if (name.includes('\x00')) {
return res.status(400).send('invalid filename');
}
// Allowlist extension check on clean input
if (!name.endsWith('.html')) {
return res.status(400).send('only .html allowed');
}
// Resolve and verify containment BEFORE passing to any native library
const allowedBase = path.resolve('/var/skill/templates');
const resolved = path.resolve(allowedBase, name);
if (!resolved.startsWith(allowedBase + path.sep) &&
resolved !== allowedBase) {
return res.status(403).send('forbidden');
}
// Pass only the fully resolved absolute path — no user string reaches native code
const img = await sharp(resolved).resize(800).toBuffer();
res.type('png').send(img);
}
endsWith() but does not reject null bytes in the input; a filename like ../../etc/passwd\x00.html passes the extension check while C-level fs calls, native addons (sharp, canvas), FFI bindings, and child_process.execFile file-path arguments truncate the path at the null byte and access the unintended file. Fix: reject inputs containing \x00 as the first validation step; pass only fully resolved absolute paths (not raw user strings) to any native library or child process.PATHTRAVERSAL2-003 — Windows UNC path via backslash on cross-platform deployment
MCP skills deployed in containerised Linux environments may use a path filter that only checks for forward-slash sequences (../) without considering Windows backslash separators. If the same skill codebase is later deployed on a Windows host, or if the Node.js path.join() call normalises backslashes on a Windows system, the input ..\..\etc\passwd bypasses the forward-slash filter entirely. Additionally, an attacker on any platform can supply a Windows UNC path (\\server\share\secret) or an absolute Windows drive path (C:\Windows\system32\config\SAM) that bypasses a relative-traversal check while resolving to a privileged location. Cross-platform Node.js skills that use path.join() (which uses OS-native separators) rather than path.posix.join() are especially exposed.
// VULNERABLE: only checks forward-slash sequences; backslash passes on Windows
app.get('/report', (req, res) => {
const name = req.query.file; // e.g. "..\..\Windows\System32\config\SAM"
// Filter only catches forward-slash traversal
if (name.includes('../')) {
return res.status(400).send('invalid');
}
// path.join on Windows resolves backslash — traverses out of reports/
const fullPath = path.join('C:\\skill\\reports', name);
res.sendFile(fullPath);
// Also: 'C:\secret' absolute path injection bypasses relative check entirely
});
// FIXED: normalise separators first; use path.resolve + containment check
const path = require('path');
app.get('/report', (req, res) => {
let name = req.query.file;
// Reject null bytes
if (name.includes('\x00')) return res.status(400).send('invalid');
// Normalise backslashes to forward slash before any check
name = name.replace(/\\/g, '/');
// Reject absolute paths (Windows drive letters, UNC paths, leading /)
if (/^([a-zA-Z]:|\\\|\/\/|\/)/i.test(name)) {
return res.status(400).send('absolute paths not allowed');
}
// Resolve to absolute path and verify containment
const allowedBase = path.resolve('/var/skill/reports');
const resolved = path.resolve(allowedBase, name);
if (!resolved.startsWith(allowedBase + path.sep) &&
resolved !== allowedBase) {
return res.status(403).send('forbidden');
}
res.sendFile(resolved);
});
../ forward-slash sequences; Windows backslash traversal (..\..\ ), absolute drive paths (C:\), and UNC paths (\\server\share) bypass the filter and are resolved by path.join() on Windows hosts or cross-platform deployments; also an issue when user input reaches child processes or native libs on any platform. Fix: normalise all backslashes to forward slash before validation; reject inputs matching absolute path patterns; use path.resolve() and post-resolution containment check as the final authoritative guard.PATHTRAVERSAL2-004 — Symlink escape from directory sandbox
When an MCP skill allows users to upload files to a sandboxed directory (e.g. /var/skill/uploads/<userId>/) and later reads back files from that directory, an attacker can upload a symbolic link that points to a path outside the sandbox. The upload path check passes because the symlink file itself resides inside the allowed directory. When a subsequent read request accesses the symlink by name, fs.readFile() follows the symlink transparently and reads the target — which may be /etc/shadow, a service's private key, or another user's data. The attack requires write access to the upload directory (which is the intended feature) and exploits the fact that path containment checks on the symlink path do not account for the symlink's target.
// VULNERABLE: containment check passes on symlink path; readFile follows the link
app.post('/upload', async (req, res) => {
const dest = path.join('/var/skill/uploads', req.user.id, req.body.filename);
await fs.promises.writeFile(dest, req.body.content);
// Attacker writes a symlink: uploads/userId/evil -> ../../../etc/shadow
res.json({ ok: true });
});
app.get('/read', async (req, res) => {
const filePath = path.resolve('/var/skill/uploads', req.user.id, req.query.name);
const base = path.resolve('/var/skill/uploads', req.user.id);
// Containment check passes — filePath IS inside base
if (!filePath.startsWith(base + path.sep)) {
return res.status(403).send('forbidden');
}
// fs.readFile silently follows the symlink, reads /etc/shadow
const data = await fs.promises.readFile(filePath, 'utf8');
res.send(data);
});
// FIXED: resolve symlinks with realpath before containment check (approach A),
// or detect symlinks with lstat and reject them (approach B)
const fs = require('fs');
const path = require('path');
app.get('/read', async (req, res) => {
const requested = path.resolve(
'/var/skill/uploads', req.user.id, req.query.name
);
const base = path.resolve('/var/skill/uploads', req.user.id);
// Initial containment check (catches basic traversal in the path string)
if (!requested.startsWith(base + path.sep) &&
requested !== base) {
return res.status(403).send('forbidden');
}
// Approach A: resolve ALL symlinks to their real path, then re-check containment
let real;
try {
real = await fs.promises.realpath(requested);
} catch {
return res.status(404).send('not found');
}
if (!real.startsWith(base + path.sep) && real !== base) {
return res.status(403).send('forbidden'); // symlink pointed outside sandbox
}
// Approach B (alternative or additional layer): reject symlinks outright
const stat = await fs.promises.lstat(requested); // lstat does NOT follow symlinks
if (stat.isSymbolicLink()) {
return res.status(403).send('symlinks not permitted');
}
const data = await fs.promises.readFile(real, 'utf8');
res.send(data);
});
../../../etc/shadow or any other privileged path passes the containment check on the symlink's own path, but fs.readFile() follows the symlink and reads the target outside the sandbox. Fix: use fs.promises.realpath() to resolve the final path after following all symlinks, then re-verify containment against the allowed base; optionally also use fs.promises.lstat() to detect and reject symlinks outright as defense-in-depth.SkillAudit detection summary — SA-SKL-PATHTRAVERSAL2
| Finding | Severity | Root Cause | Fix |
|---|---|---|---|
| PATHTRAVERSAL2-001 | HIGH | String check on raw (undecoded) input; %2e%2e%2f and double-encoded variants bypass includes('..') | Loop decodeURIComponent until stable before any check; use path.resolve() + startsWith(allowedBase) |
| PATHTRAVERSAL2-002 | HIGH | Null byte \x00 in filename passes JS endsWith('.ext') but terminates path in C-level native addons and FFI calls | Reject inputs containing \x00 first; pass only fully resolved absolute paths to native libraries |
| PATHTRAVERSAL2-003 | MEDIUM | Forward-slash-only filter; Windows backslash, absolute drive, and UNC paths bypass check on cross-platform deployments | Normalise backslashes to /; reject absolute path patterns; use path.resolve() + containment check |
| PATHTRAVERSAL2-004 | CRITICAL | Containment check on symlink path, not symlink target; fs.readFile() follows symlink outside sandbox | fs.promises.realpath() before containment re-check; optionally lstat() to reject symlinks outright |
Related patterns: For basic ../ path traversal without encoding tricks, see SA-SKL-PATHTRAVERSAL. For arbitrary file read via XXE that bypasses path checks entirely, see SA-SKL-XXE. For SQL injection patterns that can read filesystem files via LOAD_FILE(), see SA-SKL-SQLINJ. For server-side request forgery that can reach internal file URIs, see SA-SKL-SSRF.