// Whitelist of allowed mime types and extensions const ALLOWED_MIME = ['image/jpeg', 'image/png', 'application/pdf']; const MAX_SIZE = 2 * 1024 * 1024; // 2MB
| Traditional Approach | Vulnerability | Gunner Project Mitigation | |----------------------|---------------|----------------------------| | Trust Content-Type header | Attacker sends image/jpeg with PHP code | Re-validate using fileinfo or magic database | | Block .php but allow .php3 or .phtml | Extension blacklisting is incomplete | Whitelist ONLY safe extensions ( .jpg , .pdf , .txt ) | | Store in /uploads/ | Direct access leads to RCE | Store outside webroot with a secure download proxy | Let’s walk through a practical implementation using the Gunner principles in a Node.js/Express application. Step 1: Install Dependencies npm init -y npm install express multer file-type crypto Step 2: Implement Gunner Middleware const express = require('express'); const multer = require('multer'); const fileTypeFromBuffer = require('file-type'); const crypto = require('crypto'); const app = express(); fileupload gunner project
async function gunnerInspect(req, res, next) // Whitelist of allowed mime types and extensions
const storage = multer.memoryStorage(); const upload = multer( storage, limits: fileSize: MAX_SIZE ); const multer = require('multer')
app.post('/upload', upload.single('file'), gunnerInspect, (req, res) => // Store safely outside webroot // Write to /secure_storage/ with 0600 permissions res.json( message: 'File uploaded securely', filename: req.safeFile.name ); );