Claude MCP skill file upload security — file type validation bypass and polyglot files — SA-SKL-UPLOAD15
File type validation for uploaded skill assets — icons, screenshots, documentation attachments — is harder to get right than it looks. Magic byte checkers can be defeated with polyglot files. Content-Type headers are attacker-controlled. Image re-encoding sanitizers skip SVG because they can't process it. ZIP manifests lie about MIME types. SA-SKL-UPLOAD15 covers the four file type validation bypass patterns SkillAudit finds in MCP skill registry asset upload pipelines, and the correct defences for each.
file-type package checks first N magic bytes; attacker crafts ZIP+JPEG polyglot where JPEG magic bytes at start pass the check but ZIP content is extracted by backend; attacker uploads ZIP archive disguised as an imageContent-Type header accepted as ground truth for file type; attacker sends Content-Type: image/jpeg for a PHP script or HTML file; backend stores with JPEG MIME and serves it inline in browser context<script> tags is stored and served with Content-Type: image/svg+xml; XSS in browserpackage.json or custom manifest) declares an executable as an image in the assets[] array; backend derives MIME from manifest declaration and serves with Content-Disposition: inlineUPLOAD15-001 — ZIP+JPEG polyglot magic byte bypass
The file-type npm package detects file types by reading the first few bytes of the file buffer — the "magic bytes" or file signature. JPEG files start with FF D8 FF. ZIP files start with 50 4B 03 04. A polyglot file is crafted so that it is simultaneously valid as two different file formats: the JPEG magic bytes appear first to satisfy file-type, but the file is also a valid ZIP archive that the backend extracts. When the backend subsequently extracts the "image," it unpacks an arbitrary ZIP containing malicious content.
// UPLOAD15-001 — VULNERABLE
// file-type checks magic bytes at upload; extraction happens after the check passes.
// Polyglot file has JPEG magic bytes at the start but is a valid ZIP.
import { fileTypeFromBuffer } from 'file-type';
app.post('/api/skills/:skillId/assets', authenticate, upload.single('file'), async (req, res) => {
const buffer = req.file.buffer;
// VULNERABLE: checks magic bytes at the start of the buffer
const type = await fileTypeFromBuffer(buffer);
// type.mime === 'image/jpeg' — polyglot passes this check
if (!type || !['image/jpeg', 'image/png', 'image/gif'].includes(type.mime)) {
return res.status(400).json({ error: 'Invalid file type' });
}
// Store the file and later process it as a ZIP for asset extraction
await storeAsset(req.params.skillId, buffer);
// If backend later calls extractZip(buffer) — polyglot is extracted as ZIP
res.json({ success: true });
});
// UPLOAD15-001 — FIXED
// Check file-type AFTER any extraction step, not before.
// For images: use Sharp to re-encode — re-encoding destroys the ZIP structure.
import { fileTypeFromBuffer } from 'file-type';
import sharp from 'sharp';
app.post('/api/skills/:skillId/assets', authenticate, upload.single('file'), async (req, res) => {
const buffer = req.file.buffer;
// Fix 1: re-encode through Sharp BEFORE storing — Sharp cannot process a ZIP
// If the buffer is not a valid image, Sharp throws — attacker's polyglot rejected
let sanitisedBuffer;
try {
sanitisedBuffer = await sharp(buffer)
.resize({ width: 512, height: 512, fit: 'inside', withoutEnlargement: true })
.png()
.toBuffer();
} catch {
return res.status(400).json({ error: 'Invalid image file' });
}
// Fix 2: check file-type AFTER re-encoding — now guaranteed to be a real PNG
const type = await fileTypeFromBuffer(sanitisedBuffer);
if (!type || type.mime !== 'image/png') {
return res.status(400).json({ error: 'Image processing failed' });
}
await storeAsset(req.params.skillId, sanitisedBuffer);
res.json({ success: true });
});
The polyglot attack chain: The attacker crafts a file that starts with FF D8 FF E0 (JPEG magic) but contains a valid ZIP central directory at an offset after the JPEG header. The file-type check sees JPEG and passes. If the registry has a backend step that processes uploaded skill packages as ZIPs (to extract README, icons, or config files), that extraction step sees a valid ZIP and unpacks the attacker's payload — potentially including a package.json with malicious postinstall scripts or executable files. The fix is to validate type after the processing step, not before.
UPLOAD15-002 — Content-Type accepted as file type truth
The HTTP Content-Type header on a multipart upload is set by the client's browser or the attacker's script. It is entirely attacker-controlled — no browser or HTTP spec constrains it to match the actual file bytes. Any backend that uses req.file.mimetype (which reflects the client-supplied Content-Type) as the authoritative source for file type validation is accepting attacker-controlled input as a security decision.
// UPLOAD15-002 — VULNERABLE
// multer's req.file.mimetype reflects the client-supplied Content-Type header.
// Attacker sends Content-Type: image/jpeg for an HTML file containing XSS.
import multer from 'multer';
const upload = multer({ storage: multer.memoryStorage() });
app.post('/api/skills/:skillId/icon', authenticate, upload.single('icon'), (req, res) => {
// VULNERABLE: req.file.mimetype is client-controlled — attacker sends image/jpeg
const allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!allowedTypes.includes(req.file.mimetype)) {
return res.status(400).json({ error: 'Invalid file type' });
}
// Stored with MIME from client; served as image/jpeg inline in browser
storeIcon(req.params.skillId, req.file.buffer, req.file.mimetype);
res.json({ success: true });
// Attacker uploads HTML file with Content-Type: image/jpeg
// Browser serves it inline → XSS in registry origin
});
// UPLOAD15-002 — FIXED
// Ignore Content-Type header entirely for file type determination.
// Use file-type on the actual buffer bytes; re-derive MIME server-side.
import { fileTypeFromBuffer } from 'file-type';
import multer from 'multer';
import sharp from 'sharp';
const upload = multer({
storage: multer.memoryStorage(),
limits: { fileSize: 2 * 1024 * 1024 }, // 2MB limit
});
app.post('/api/skills/:skillId/icon', authenticate, upload.single('icon'), async (req, res) => {
const buffer = req.file.buffer;
// Fix 1: ignore req.file.mimetype — derive from bytes instead
const type = await fileTypeFromBuffer(buffer);
const allowedTypes = new Set(['image/jpeg', 'image/png', 'image/gif']);
if (!type || !allowedTypes.has(type.mime)) {
return res.status(400).json({ error: 'Invalid image file' });
}
// Fix 2: re-encode through Sharp to strip non-image content
const sanitisedBuffer = await sharp(buffer).png().toBuffer();
// Fix 3: store with server-derived MIME, never client-supplied
await storeIcon(req.params.skillId, sanitisedBuffer, 'image/png');
res.json({ success: true });
});
UPLOAD15-003 — SVG sanitization gap in Sharp pipeline
Sharp is an excellent image processing library for raster formats — it re-encodes images through pixel-level processing, stripping any embedded payloads in EXIF data or file structure. But Sharp cannot process SVG files: SVG is an XML-based vector format, and Sharp's processing pipeline is designed for raster images. Some registries add an explicit if (type.mime === 'image/svg+xml') return next() skip for SVG to avoid the Sharp processing error — inadvertently creating a sanitization gap. SVG containing <script> tags, onload attributes, or href="javascript:..." links executes JavaScript in the browser when the SVG is served with Content-Type: image/svg+xml.
// UPLOAD15-003 — VULNERABLE
// Sharp pipeline skips SVG; SVG with embedded JavaScript passes through.
app.post('/api/skills/:skillId/icon', authenticate, upload.single('icon'), async (req, res) => {
const buffer = req.file.buffer;
const type = await fileTypeFromBuffer(buffer);
if (!type || !['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'].includes(type.mime)) {
return res.status(400).json({ error: 'Invalid file type' });
}
let sanitisedBuffer = buffer;
// VULNERABLE: SVG explicitly skipped — Sharp cannot process vector graphics
if (type.mime !== 'image/svg+xml') {
sanitisedBuffer = await sharp(buffer).png().toBuffer();
}
// SVG stored as-is; served with Content-Type: image/svg+xml
// Browser executes