Content-Disposition header attacks in Claude MCP skill file download endpoints — SA-SKL-CONTENTDISPOSITION
File download endpoints in MCP skills are a common source of response-header injection, stored XSS, and MIME-based attacks. The root cause is insufficient sanitization of user-supplied filename values and incorrect reliance on Content-Type alone to control browser rendering behavior. SA-SKL-CONTENTDISPOSITION covers four patterns: CRLF injection via filename, missing attachment directive, MIME sniffing without nosniff, and path traversal in filename stored to disk.
Why Content-Disposition is a security-critical header
The Content-Disposition response header controls whether the browser renders the response body in-window (inline) or saves it to disk (attachment). For user-generated content, this distinction is the difference between XSS and a safe file download. An HTML file served with Content-Disposition: inline and no explicit Content-Type will be rendered by the browser in the context of the skill's origin — giving any attacker who can upload HTML files a persistent XSS vector. The header is also used to supply the suggested filename for downloads via the filename parameter, and that parameter is frequently constructed from user input without sanitization, creating both CRLF header injection and path traversal risks.
SA-SKL-CONTENTDISPOSITION in scope: CONTENTDISPOSITION-001 (CRLF injection via filename), CONTENTDISPOSITION-002 (missing attachment directive), CONTENTDISPOSITION-003 (MIME sniff without nosniff), CONTENTDISPOSITION-004 (path traversal in filename stored to disk). Each pattern includes SkillAudit detection rules and correct fixes.
CONTENTDISPOSITION-001 — CRLF injection via filename parameter
HTTP headers are separated by carriage return + line feed (\r\n). If a user-supplied filename is embedded into the Content-Disposition header value without stripping \r and \n characters, an attacker can terminate the current header and inject arbitrary headers into the response. Injecting Content-Type: text/html after a \r\n in the filename causes the browser to render the response as HTML regardless of the original Content-Type, enabling XSS. Injecting a second \r\n followed by a blank line and then HTML content achieves HTTP response splitting — serving a crafted response body from the skill's origin. The fix is to strip all CRLF characters from the filename and to use the RFC 6266 filename*=UTF-8'' percent-encoded form for non-ASCII filenames, which is immune to CRLF injection because percent-encoding escapes control characters.
// VULNERABLE — user filename embedded in header without CRLF stripping
app.get('/api/download/:fileId', async (req, res) => {
const record = await db.getFile(req.params.fileId);
// Attacker uploads file with name: "report.pdf\r\nContent-Type: text/html\r\n\r\n<script>alert(1)</script>"
// Resulting header:
// Content-Disposition: attachment; filename="report.pdf
// Content-Type: text/html
//
// <script>alert(1)</script>"
res.setHeader('Content-Disposition', `attachment; filename="${record.originalName}"`);
res.setHeader('Content-Type', 'application/pdf');
res.sendFile(record.storagePath);
});
// FIXED — strip CRLF; use RFC 6266 filename* for Unicode; sanitize to ASCII safe chars
import path from 'path';
function safeContentDisposition(originalName: string): string {
// Strip all CRLF and null bytes from filename
const stripped = originalName.replace(/[\r\n\x00]/g, '');
// ASCII-safe portion: allow only printable ASCII except double-quote and backslash
const asciiSafe = stripped.replace(/[^\x20-\x7E]|["\\]/g, '_');
// Limit length to 255 characters
const limited = asciiSafe.slice(0, 255);
// RFC 6266: filename* uses UTF-8 percent-encoding — safe for all characters
const encoded = encodeURIComponent(stripped.slice(0, 255));
return `attachment; filename="${limited}"; filename*=UTF-8''${encoded}`;
}
app.get('/api/download/:fileId', async (req, res) => {
const record = await db.getFile(req.params.fileId);
res.setHeader('Content-Disposition', safeContentDisposition(record.originalName));
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.sendFile(record.storagePath);
});
Content-Disposition header without stripping CRLF characters allow response-header injection — an attacker can inject Content-Type: text/html and an HTML body to achieve XSS on the skill's origin; fix by stripping \r, \n, and \x00 from filename before header construction, and by using the RFC 6266 filename*=UTF-8'' percent-encoded form for all non-ASCII characters. Detection: SkillAudit flags template literal or string concatenation patterns that embed a DB-sourced or request-sourced filename directly into a Content-Disposition header value without an intervening sanitization function.CONTENTDISPOSITION-002 — Missing attachment directive enables browser-rendered XSS
When a skill serves user-generated content files (HTML exports, SVG diagrams, Markdown-to-HTML renders, skill output previews) without a Content-Disposition: attachment header, the browser renders the file inline using the skill's origin. If the file contains any JavaScript (or SVG with inline scripts, or HTML with event handlers), that script executes in the skill's security context — full access to the skill's cookies, localStorage, and DOM. This is stored XSS via file upload: the attacker uploads a crafted HTML file, shares the download URL, and any user who clicks the link executes the attacker's script on the skill's origin. The fix is to always set Content-Disposition: attachment for any user-generated file content, regardless of the file type or intended use.
// VULNERABLE — user-generated files served inline; SVG/HTML renders in skill origin
app.get('/uploads/:filename', async (req, res) => {
const record = await db.getFileByName(req.params.filename);
// For SVG files: no Content-Disposition header → browser renders SVG inline
// SVG with <script> tag executes in skillaudit.dev origin context
res.setHeader('Content-Type', record.mimeType); // e.g. 'image/svg+xml'
// No Content-Disposition: attachment
res.sendFile(record.storagePath);
});
// FIXED — all user-generated content served with Content-Disposition: attachment
app.get('/uploads/:filename', async (req, res) => {
const record = await db.getFileByName(req.params.filename);
if (!record) return res.status(404).end();
// Always attachment for user-generated content — never allow inline rendering
res.setHeader('Content-Disposition', safeContentDisposition(record.originalName));
// For images that must display inline (thumbnails, avatars):
// Serve from a separate sandbox domain (e.g., uploads.skills-cdn.com) with
// no session cookies and a strict CSP that blocks scripts:
// Content-Security-Policy: default-src 'none'; img-src 'self'
// Force octet-stream regardless of original MIME — prevents browser using MIME
// to decide rendering even if Content-Disposition is somehow stripped by proxy
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.sendFile(record.storagePath);
});
Content-Disposition: attachment for user-generated content allow the browser to render HTML, SVG, and other scriptable formats inline within the skill's origin — enabling stored XSS via crafted upload; fix by always setting Content-Disposition: attachment for all user-generated file responses, and by serving user content from a separate sandboxed domain for cases where inline display is required. Detection: SkillAudit flags route handlers that call res.sendFile() or res.send() with a DB-sourced file path where the response does not include a Content-Disposition: attachment header.CONTENTDISPOSITION-003 — MIME sniffing without X-Content-Type-Options: nosniff
Even when a skill sets Content-Type: application/octet-stream on a download, some browsers (Internet Explorer, and legacy versions of Chrome) perform MIME sniffing: they inspect the first N bytes of the response body and override the declared Content-Type if the content looks like HTML or a script. If a user uploads a file containing <html><script> in its first 256 bytes, a MIME-sniffing browser may render it as HTML regardless of the declared MIME type. The X-Content-Type-Options: nosniff response header instructs all major browsers to trust the declared Content-Type and skip MIME sniffing. This header should be set on every response from a skill endpoint that serves user-generated content, not just download endpoints.
// VULNERABLE — application/octet-stream without nosniff; MIME sniff renders HTML
app.get('/api/download/:fileId', async (req, res) => {
const record = await db.getFile(req.params.fileId);
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', `attachment; filename="${record.originalName}"`);
// No X-Content-Type-Options: nosniff
// IE/legacy Chrome: sniff first bytes, detect HTML → render inline on skill origin
res.sendFile(record.storagePath);
});
// FIXED — X-Content-Type-Options: nosniff on all user-content responses
// Best applied globally via Express middleware:
import express from 'express';
const app = express();
// Apply nosniff globally — safe for all routes
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
next();
});
// Or via helmet:
import helmet from 'helmet';
app.use(helmet()); // Includes nosniff by default
// Per-route for download endpoints (belt-and-suspenders):
app.get('/api/download/:fileId', async (req, res) => {
const record = await db.getFile(req.params.fileId);
res.setHeader('Content-Type', 'application/octet-stream');
res.setHeader('Content-Disposition', safeContentDisposition(record.originalName));
res.setHeader('X-Content-Type-Options', 'nosniff');
// Also set cache-control to prevent caching of user-specific content
res.setHeader('Cache-Control', 'private, no-store');
res.sendFile(record.storagePath);
});
X-Content-Type-Options: nosniff allow MIME-sniffing browsers to render user-uploaded content as HTML on the skill's origin regardless of the declared Content-Type; fix by setting X-Content-Type-Options: nosniff globally via middleware (or helmet()) on all skill responses. Detection: SkillAudit flags skills that serve user-generated file content without the nosniff header present in either a global middleware chain or the specific route handler.CONTENTDISPOSITION-004 — Path traversal in filename field stored to disk
When a skill stores uploaded files using the user-supplied filename as the disk path — or when a download endpoint reads the filename from a database column that was populated from user input and uses it to construct a file path — an attacker can supply a filename containing path traversal sequences like ../../.env or ../../etc/passwd. If the skill constructs a file path as path.join(uploadDir, filename) without first applying path.basename(), the traversal escapes the upload directory. The path.join() function in Node.js does not normalize traversals relative to the base path — it simply resolves the resulting path, which may be outside the intended directory. The fix is to use path.basename(filename) to strip all directory components before constructing any storage path from user-supplied input.
// VULNERABLE — path traversal in filename escapes upload directory
app.post('/api/upload', upload.single('file'), async (req, res) => {
// Attacker supplies filename: "../../.env"
// path.join('/var/uploads/', '../../.env') → '/var/.env' — overwrites .env file!
// Or: "../../etc/cron.d/backdoor" on Linux systems
const finalPath = path.join('/var/uploads/', req.file.originalname);
await fs.rename(req.file.path, finalPath);
res.json({ path: finalPath });
});
// FIXED — path.basename() strips all directory components before path construction
import path from 'path';
import { randomUUID } from 'crypto';
app.post('/api/upload', upload.single('file'), async (req, res) => {
// path.basename('../../.env') → '.env' — traversal stripped
// path.basename('../etc/cron.d/backdoor') → 'backdoor' — only leaf name kept
const safeName = path.basename(req.file.originalname);
// Reject filenames that are still suspicious after basename:
// Hidden files (Unix), Windows device names, overly long names
if (
safeName.startsWith('.') || // Hidden files
/^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/i.test(safeName) || // Windows device names
safeName.length > 255 || // Filesystem limit
/[<>:"/\\|?*\x00-\x1F]/.test(safeName) // Characters invalid on Windows/Linux
) {
return res.status(400).json({ error: 'Invalid filename' });
}
// Use UUID storage key — original name stored in DB only for display
const storageKey = `${randomUUID()}${path.extname(safeName).toLowerCase()}`;
const finalPath = path.join('/var/uploads/', storageKey);
await fs.rename(req.file.path, finalPath);
await db.createUpload({ storageKey, originalName: safeName, userId: req.user.id });
res.json({ id: storageKey });
});
path.basename() allow path traversal attacks that overwrite arbitrary files on the server including .env, cron files, and application code; fix by applying path.basename() to any user-supplied filename before constructing any storage path, and by using UUID-based storage keys to store files so the original filename is never used as a path component. Detection: SkillAudit flags path.join(), path.resolve(), or string concatenation patterns that incorporate req.file.originalname, req.body.filename, or any DB column sourced from user input directly into a filesystem path.