@wardenjs/ssr-upload
v0.2.0
Published
File upload validation for Fetch-API servers: type detection by magic bytes, safe file names and a multipart decorator.
Maintainers
Readme
@wardenjs/ssr-upload
Part of warden's server track: security controls as decorators for any Fetch-API handler (Next.js route handlers, Hono, Bun, Deno, Workers). Here SSR means server side, not server-side rendering.
File upload checks by content, not by what the client claims.
npm i @wardenjs/ssr @wardenjs/ssr-uploadimport { upload } from '@wardenjs/ssr-upload';
export const POST = chain()
.use(session.required())
.use(upload({ allowedTypes: ['image/png', 'image/jpeg'], maxBytes: 2 * 1024 * 1024 }))
.handle(async (_r, { upload }) => {
await storage.put(upload.storageName, upload.bytes, { contentType: upload.mime });
return Response.json({ name: upload.displayName });
});- Type from magic bytes (PNG, JPEG, WebP, GIF, PDF). Claimed MIME and extension must agree with it.
- No SVG: it is a document that runs script.
- Polyglots with script markers are rejected.
storageNameis random;displayNamehas paths, control and bidi characters removed.- Capped multipart read; 413 or 422
upload_rejected. checkUploadfor files that arrive some other way.
The stronger half is architectural: store outside the web root, serve from a
separate cookieless origin with Content-Disposition: attachment, re-encode
images.
