Spaces:
Running
Running
4SEND Team commited on
Commit ·
3fd4e79
1
Parent(s): 9540766
security: strip metadata from all media (video/audio ffmpeg, gif/svg/ico/bmp)
Browse filesSync routes/upload.js + add ffmpeg-static dep from 4SEND-SOURCE 2.0.
Keeps existing HF deployment files (built client, Dockerfile) intact.
Files changed (3)
- package-lock.jsonAdded
- package.json+1
- routes/upload.js+79-29
17 unmodified lines18192021222317 unmodified lines"express": "^4.18.2","express-rate-limit": "^7.1.5","express-session": "^1.19.0","firebase-admin": "^12.0.0","jsonwebtoken": "^9.0.2","mongoose": "^8.0.0",17 unmodified lines1819202122232417 unmodified lines"express": "^4.18.2","express-rate-limit": "^7.1.5","express-session": "^1.19.0","ffmpeg-static": "^5.3.0","firebase-admin": "^12.0.0","jsonwebtoken": "^9.0.2","mongoose": "^8.0.0",
11 unmodified lines12131415161726 unmodified lines4445464748495053 unmodified lines10410510610710810911011111211311411511611711811912012112212351 unmodified lines17517617717817918018118218318418518618718818919019119219319411 unmodified linesconst { uploadLimiter } = require('../constants/limits');const { st, detectLangReq } = require('../constants/translations');const { verifyUploadedFile } = require('../utils/file');const uploadDir = path.join(os.tmpdir(), '4send_uploads');if (!fs.existsSync(uploadDir)) {26 unmodified lines};const allowedExts = Object.values(extList).flat();const imgExts = extList.img;const sharpExts = ['.jpg', '.jpeg', '.png', '.webp', '.tiff', '.tif', '.heic', '.heif'];const blockedCloudinaryExts = ['.apk', '.app', '.bat', '.bin', '.cmd', '.com', '.command', '.cpl', '.csh', '.exe', '.gadget', '.inf1', '.ins', '.inx', '.ipa', '.isu', '.job', '.jse', '.ksh', '.lnk', '.msc', '.msi', '.msp', '.mst', '.osx', '.out', '.paf', '.pif', '.prg', '.ps1', '.reg', '.rgs', '.run', '.sct', '.shb', '.shs', '.u3p', '.vb', '.vbe', '.vbs', '.vbscript', '.workflow', '.ws', '.wsf'];53 unmodified lines}}if (sharpExts.includes(ext)) {try {const tempPath = safePath + '_clean.webp';await sharp(safePath, { limitInputPixels: 50000000 }).rotate().resize({ width: 1920, height: 1920, fit: 'inside', withoutEnlargement: true }).webp({ quality: 80 }).toFile(tempPath);await fsPromises.unlink(safePath);finalPath = tempPath;} catch (sharpErr) {if (fs.existsSync(safePath)) await fsPromises.unlink(safePath);return res.status(400).json({ error: st('Неподдерживаемый тип медиа', detectLangReq(req)) });}}const uploadOpts = {51 unmodified lines}}if (sharpExts.includes(ext)) {try {const tempPath = safePath + '_clean.webp';await sharp(safePath, { limitInputPixels: 50000000 }).rotate().resize({ width: 1920, height: 1920, fit: 'inside', withoutEnlargement: true }).webp({ quality: 80 }).toFile(tempPath);await fsPromises.unlink(safePath);finalPath = tempPath;} catch {if (fs.existsSync(safePath)) await fsPromises.unlink(safePath);continue;}}const uploadOpts = {11 unmodified lines121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848526 unmodified lines11211311411511611711853 unmodified lines17217317417517617717817918018118251 unmodified lines23423523623723823924024124224324411 unmodified linesconst { uploadLimiter } = require('../constants/limits');const { st, detectLangReq } = require('../constants/translations');const { verifyUploadedFile } = require('../utils/file');const { spawn } = require('child_process');const ffmpegPath = (() => { try { return require('ffmpeg-static'); } catch { return null; } })();const runFfmpegStrip = (input, output) => new Promise((resolve, reject) => {if (!ffmpegPath) return reject(new Error('ffmpeg unavailable'));const args = ['-y', '-i', input, '-map_metadata', '-1', '-map_chapters', '-1', '-fflags', '+bitexact', '-c', 'copy', output];const cp = spawn(ffmpegPath, args);let err = '';cp.stderr.on('data', (d) => { err += d; });cp.on('error', reject);cp.on('close', (code) => code === 0 ? resolve() : reject(new Error(err || ('ffmpeg exit ' + code))));});const sanitizeSvg = async (inputPath) => {const s = await fs.promises.readFile(inputPath, 'utf8');const cleaned = s.replace(/<metadata[\s\S]*?<\/metadata>/gi, '').replace(/<script[\s\S]*?<\/script>/gi, '').replace(/<\?xml-stylesheet[\s\S]*?\?>/gi, '').replace(/\son\w+\s*=\s*("(?:[^"]*)"|'(?:[^']*)')/gi, '').replace(/(?:href|xlink:href)\s*=\s*("javascript:[^"]*"|'javascript:[^']*')/gi, '');await fs.promises.writeFile(inputPath, cleaned);};const processMedia = async (safePath, ext) => {if (sharpExts.includes(ext)) {const tempPath = safePath + '_clean.webp';await sharp(safePath, { limitInputPixels: 50000000 }).rotate().resize({ width: 1920, height: 1920, fit: 'inside', withoutEnlargement: true }).webp({ quality: 80 }).toFile(tempPath);await fs.promises.unlink(safePath);return tempPath;}if (ext === '.gif') {if (ffmpegPath) {const tempPath = safePath + '_clean.gif';try {await runFfmpegStrip(safePath, tempPath);await fs.promises.unlink(safePath);return tempPath;} catch {if (fs.existsSync(tempPath)) await fs.promises.unlink(tempPath).catch(() => {});}}return safePath;}if (ext === '.svg') {try { await sanitizeSvg(safePath); } catch {}return safePath;}if (extList.audio.includes(ext) || extList.video.includes(ext)) {if (ffmpegPath) {const tempPath = safePath + '_clean' + ext;try {await runFfmpegStrip(safePath, tempPath);await fs.promises.unlink(safePath);return tempPath;} catch {if (fs.existsSync(tempPath)) await fs.promises.unlink(tempPath).catch(() => {});}}return safePath;}return safePath;};const uploadDir = path.join(os.tmpdir(), '4send_uploads');if (!fs.existsSync(uploadDir)) {26 unmodified lines};const allowedExts = Object.values(extList).flat();const imgExts = extList.img;const sharpExts = ['.jpg', '.jpeg', '.png', '.webp', '.tiff', '.tif', '.heic', '.heif', '.ico', '.bmp'];const blockedCloudinaryExts = ['.apk', '.app', '.bat', '.bin', '.cmd', '.com', '.command', '.cpl', '.csh', '.exe', '.gadget', '.inf1', '.ins', '.inx', '.ipa', '.isu', '.job', '.jse', '.ksh', '.lnk', '.msc', '.msi', '.msp', '.mst', '.osx', '.out', '.paf', '.pif', '.prg', '.ps1', '.reg', '.rgs', '.run', '.sct', '.shb', '.shs', '.u3p', '.vb', '.vbe', '.vbs', '.vbscript', '.workflow', '.ws', '.wsf'];53 unmodified lines}}try {finalPath = await processMedia(safePath, ext);} catch (procErr) {if (fs.existsSync(safePath)) await fsPromises.unlink(safePath);return res.status(400).json({ error: st('Неподдерживаемый тип медиа', detectLangReq(req)) });}const uploadOpts = {51 unmodified lines}}try {finalPath = await processMedia(safePath, ext);} catch {if (fs.existsSync(safePath)) await fsPromises.unlink(safePath);continue;}const uploadOpts = {
package-lock.jsonAdded
package.json+1
routes/upload.js+79-29