Fix Improper Assets Management in Blitz.js
Blitz.js, inheriting Next.js architecture, serves everything in the /public directory as static assets. Improper asset management occurs when developers treat this folder as a general storage bin, inadvertently leaking .env files, Prisma schemas, or internal documentation. If it's in /public, it's public. Period. An attacker won't bother with complex injections if you're serving your database credentials or source maps on a silver platter.
The Vulnerable Pattern
// Directory Structure: // app/ // public/ // ├── images/ // ├── .env.production <-- CRITICAL LEAK: Exposed to / .env.production // ├── schema.prisma <-- LEAK: Database architecture exposed // └── internal_api.pdf <-- LEAK: Internal docs
// blitz.config.ts const config = { // No headers or security middleware protecting the public folder // Everything inside /public is served with a 200 OK by default }; export default config;
The Secure Implementation
To remediate improper asset management in Blitz.js: First, audit the /public directory and purge any non-static, sensitive assets. Use .gitignore and .dockerignore to ensure secrets never hit the deployment build. Second, leverage the 'headers' configuration in blitz.config.ts to enforce strict CSP and prevent MIME-type sniffing. Third, for assets that require authorization, do not store them in /public; instead, serve them via an API route or a protected Blitz Mutation/Query that verifies the user's session before streaming the file buffer.
// 1. Move all sensitive files to the root directory and update .gitignore // .gitignore // .env* // schema.prisma// 2. Implement a custom middleware to restrict access to specific static paths if necessary // src/middleware.ts import { blitzMiddleware } from ”./blitz-server”
export default blitzMiddleware(async (req, res, next) => { const { pathname } = new URL(req.url || "", “http://localhost”) if (pathname.startsWith(“/protected-assets/”) && !req.session.userId) { res.statusCode = 403 res.end(“Access Denied”) return } return next() })
// 3. Set Security Headers in blitz.config.ts const config = { async headers() { return [ { source: ”/(.*)”, headers: [ { key: “X-Content-Type-Options”, value: “nosniff” }, { key: “Content-Security-Policy”, value: “default-src ‘self’;” } ], }, ] }, };
Your Blitz.js API
might be exposed to Improper Assets Management
74% of Blitz.js apps fail this check. Hackers use automated scanners to find this specific flaw. Check your codebase before they do.
Free Tier • No Credit Card • Instant Report
Verified by Ghost Labs Security Team
This content is continuously validated by our automated security engine and reviewed by our research team. Ghost Labs analyzes over 500+ vulnerability patterns across 40+ frameworks to provide up-to-date remediation strategies.