diff --git a/backend/src/controllers/faqController.ts b/backend/src/controllers/faqController.ts index 9e959650..ea1d12f9 100644 --- a/backend/src/controllers/faqController.ts +++ b/backend/src/controllers/faqController.ts @@ -1,11 +1,7 @@ import { Request, Response } from 'express'; -import fs from 'fs'; -import path from 'path'; import { AuthRequest } from '../middleware/auth'; -import { ensureFaqUploadDir, FAQ_ALLOWED_MIMES } from '../middleware/faqUpload'; -import { recordUploadedFile, FILE_INTEGRITY_ROOTS } from '../services/fileIntegrityService'; +import { FAQ_ALLOWED_MIMES } from '../middleware/faqUpload'; import { removeUploadedFile } from '../utils/fileMagic'; -import { validateUploadedFile } from '../utils/uploadValidation'; import * as faqService from '../services/faqService'; function handleError(res: Response, e: unknown): void { @@ -76,13 +72,19 @@ export async function recordHelpful(req: AuthRequest, res: Response): Promise { try { - const filePath = faqService.readFaqFile(req.params.filename); - if (!filePath) { + const resolved = await faqService.resolvePublicFaqFile(req.params.filename); + if (!resolved) { res.status(404).json({ error: 'File not found' }); return; } + res.setHeader('Content-Type', resolved.mime); + res.setHeader( + 'Content-Disposition', + faqService.faqFileContentDisposition(resolved.downloadName, resolved.mime) + ); + res.setHeader('X-Content-Type-Options', 'nosniff'); res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin'); - res.sendFile(filePath); + res.sendFile(resolved.filePath); } catch (e) { handleError(res, e); } @@ -100,8 +102,9 @@ export async function downloadCategoryDocument(req: Request, res: Response): Pro res.setHeader('Content-Type', 'application/pdf'); res.setHeader( 'Content-Disposition', - `attachment; filename="${resolved.downloadName.replace(/"/g, '')}"` + faqService.faqFileContentDisposition(resolved.downloadName, 'application/pdf') ); + res.setHeader('X-Content-Type-Options', 'nosniff'); res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin'); res.sendFile(resolved.filePath); } catch (e) { @@ -207,23 +210,25 @@ export async function adminUploadMedia(req: AuthRequest, res: Response): Promise res.status(400).json({ error: 'No file uploaded' }); return; } - const filePath = path.join(ensureFaqUploadDir(), req.file.filename); try { - await validateUploadedFile(filePath, FAQ_ALLOWED_MIMES); + const saved = await faqService.finalizeFaqUpload({ + tempFilename: req.file.filename, + originalName: req.file.originalname, + kind: 'media', + uploadedByUserId: req.user?.userId ?? null, + allowedMimes: FAQ_ALLOWED_MIMES, + }); + res.status(201).json({ + url: saved.url, + filename: saved.filename, + mimeType: saved.mime, + originalName: saved.originalName, + }); } catch (e) { - removeUploadedFile(filePath); + removeUploadedFile(faqService.resolveFaqFilePath(req.file.filename)); const msg = e instanceof Error ? e.message : 'File validation failed'; - res.status(msg.includes('virus') ? 422 : 400).json({ error: msg }); - return; + res.status(msg.toLowerCase().includes('virus') ? 422 : 400).json({ error: msg }); } - const url = faqService.publicFaqFileUrl(req.file.filename); - await recordUploadedFile(FILE_INTEGRITY_ROOTS.faq, req.file.filename); - res.status(201).json({ - url, - filename: req.file.filename, - mimeType: req.file.mimetype, - originalName: req.file.originalname, - }); } catch (e) { handleError(res, e); } @@ -244,14 +249,15 @@ export async function adminUploadCategoryDocument(req: AuthRequest, res: Respons const item = await faqService.uploadCategoryDocument({ categoryId: id, lang, - storedFilename: req.file.filename, + tempFilename: req.file.filename, originalName: req.file.originalname, byteSize: req.file.size, + uploadedByUserId: req.user?.userId ?? null, }); res.status(201).json(item); } catch (e) { const msg = e instanceof Error ? e.message : 'Upload failed'; - if (msg.includes('virus')) { + if (msg.toLowerCase().includes('virus')) { res.status(422).json({ error: msg }); return; } diff --git a/backend/src/db/schema.sql b/backend/src/db/schema.sql index 4f2bba8a..d3184b75 100644 --- a/backend/src/db/schema.sql +++ b/backend/src/db/schema.sql @@ -3052,6 +3052,22 @@ INSERT INTO system_settings (key, value) VALUES ('faq_pdf_max_bytes', '8388608') ON CONFLICT (key) DO NOTHING; +-- ACL / legitimacy registry for FAQ uploads (media + category PDFs) +CREATE TABLE IF NOT EXISTS faq_uploaded_files ( + filename VARCHAR(255) PRIMARY KEY, + mime VARCHAR(128) NOT NULL, + original_name VARCHAR(255), + kind VARCHAR(32) NOT NULL DEFAULT 'media' + CHECK (kind IN ('media', 'category_pdf')), + category_id INTEGER REFERENCES faq_categories (id) ON DELETE SET NULL, + uploaded_by_user_id UUID REFERENCES users (id) ON DELETE SET NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + +CREATE INDEX IF NOT EXISTS idx_faq_uploaded_files_kind ON faq_uploaded_files (kind); + +CREATE INDEX IF NOT EXISTS idx_faq_uploaded_files_category ON faq_uploaded_files (category_id); + -- ============================================================================= -- Database tamper detection (direct SQL / bypass application) -- ============================================================================= diff --git a/backend/src/middleware/faqPdfUpload.ts b/backend/src/middleware/faqPdfUpload.ts index f38181b4..418cd428 100644 --- a/backend/src/middleware/faqPdfUpload.ts +++ b/backend/src/middleware/faqPdfUpload.ts @@ -14,7 +14,8 @@ const storage = multer.diskStorage({ cb(null, ensureFaqUploadDir()); }, filename(_req, _file, cb) { - cb(null, `${uuidv4()}.pdf`); + // Temporary name — finalized to uuid.pdf after magic-byte validation. + cb(null, `${uuidv4()}.upload`); }, }); diff --git a/backend/src/middleware/faqUpload.ts b/backend/src/middleware/faqUpload.ts index 8182ab5b..464b49cf 100644 --- a/backend/src/middleware/faqUpload.ts +++ b/backend/src/middleware/faqUpload.ts @@ -1,6 +1,6 @@ import multer from 'multer'; -import path from 'path'; import fs from 'fs'; +import path from 'path'; import { v4 as uuidv4 } from 'uuid'; const MAX_BYTES = 10 * 1024 * 1024; @@ -23,9 +23,9 @@ const storage = multer.diskStorage({ destination(_req, _file, cb) { cb(null, ensureFaqUploadDir()); }, - filename(_req, file, cb) { - const ext = path.extname(file.originalname).slice(0, 20); - cb(null, `${uuidv4()}${ext}`); + filename(_req, _file, cb) { + // Temporary name — controller renames to uuid + extension from detected MIME. + cb(null, `${uuidv4()}.upload`); }, }); diff --git a/backend/src/repositories/faqRepository.ts b/backend/src/repositories/faqRepository.ts index 88e72841..2b609199 100644 --- a/backend/src/repositories/faqRepository.ts +++ b/backend/src/repositories/faqRepository.ts @@ -365,3 +365,79 @@ export async function countEntriesInCategory(categoryId: number): Promise { + const rows = await query( + `INSERT INTO faq_uploaded_files ( + filename, mime, original_name, kind, category_id, uploaded_by_user_id + ) VALUES ($1, $2, $3, $4, $5, $6) + ON CONFLICT (filename) DO UPDATE SET + mime = EXCLUDED.mime, + original_name = EXCLUDED.original_name, + kind = EXCLUDED.kind, + category_id = EXCLUDED.category_id, + uploaded_by_user_id = COALESCE(EXCLUDED.uploaded_by_user_id, faq_uploaded_files.uploaded_by_user_id) + RETURNING *`, + [ + input.filename, + input.mime, + input.originalName ?? null, + input.kind, + input.categoryId ?? null, + input.uploadedByUserId ?? null, + ] + ); + return rows[0]!; +} + +export async function findUploadedFile(filename: string): Promise { + const rows = await query( + `SELECT * FROM faq_uploaded_files WHERE filename = $1`, + [filename] + ); + return rows[0] ?? null; +} + +export async function deleteUploadedFile(filename: string): Promise { + await query(`DELETE FROM faq_uploaded_files WHERE filename = $1`, [filename]); +} + +/** True if filename is still referenced as a category PDF pointer. */ +export async function isCategoryPdfFilename(filename: string): Promise { + const rows = await query<{ ok: number }[]>( + `SELECT 1 AS ok FROM faq_categories + WHERE pdf_fr_filename = $1 OR pdf_en_filename = $1 + LIMIT 1`, + [filename] + ); + return rows.length > 0; +} + +/** Legacy media: referenced from an FAQ answer HTML body. */ +export async function isReferencedInFaqAnswerHtml(filename: string): Promise { + const needle = `%/faq/files/${filename}%`; + const rows = await query<{ ok: number }[]>( + `SELECT 1 AS ok FROM faq_entries + WHERE answer_html ILIKE $1 + LIMIT 1`, + [needle] + ); + return rows.length > 0; +} diff --git a/backend/src/services/faqService.ts b/backend/src/services/faqService.ts index d4ff621f..a8dcde71 100644 --- a/backend/src/services/faqService.ts +++ b/backend/src/services/faqService.ts @@ -1,6 +1,7 @@ import path from 'path'; import fs from 'fs'; import crypto from 'crypto'; +import { v4 as uuidv4 } from 'uuid'; import { Request } from 'express'; import * as repo from '../repositories/faqRepository'; import { FaqCategoryRow, FaqDocumentLang } from '../models/faq.model'; @@ -11,7 +12,7 @@ import { } from '../middleware/faqPdfUpload'; import { sanitizeFaqHtml } from '../utils/sanitizeHtml'; import { validateUploadedFile } from '../utils/uploadValidation'; -import { removeUploadedFile } from '../utils/fileMagic'; +import { extensionForMime, removeUploadedFile } from '../utils/fileMagic'; import { recordUploadedFile, FILE_INTEGRITY_ROOTS } from './fileIntegrityService'; import { query } from '../db/connection'; @@ -125,6 +126,127 @@ export function resolveFaqFilePath(filename: string): string { return path.join(ensureFaqUploadDir(), path.basename(filename)); } +function safeFaqDisplayName(originalName: string | undefined, mime: string): string { + const ext = extensionForMime(mime); + const base = path + .basename(originalName || 'file') + .replace(/[^\w.\-()+ ]+/g, '_') + .slice(0, 180); + const withoutExt = base.replace(/\.[^.]+$/, '') || 'file'; + return `${withoutExt}${ext}`; +} + +/** + * Validate magic/ClamAV, rename to uuid+safeExt, register legitimacy row. + */ +export async function finalizeFaqUpload(input: { + tempFilename: string; + originalName?: string; + kind: 'media' | 'category_pdf'; + categoryId?: number | null; + uploadedByUserId?: string | null; + allowedMimes: Set; +}): Promise<{ filename: string; mime: string; originalName: string; url: string }> { + const tempPath = resolveFaqFilePath(input.tempFilename); + let detected: string; + try { + detected = await validateUploadedFile(tempPath, input.allowedMimes); + } catch (e) { + removeUploadedFile(tempPath); + throw e; + } + + const ext = extensionForMime(detected); + const finalName = `${uuidv4()}${ext}`; + const finalPath = resolveFaqFilePath(finalName); + fs.renameSync(tempPath, finalPath); + + const originalName = safeFaqDisplayName(input.originalName, detected); + await repo.insertUploadedFile({ + filename: finalName, + mime: detected, + originalName, + kind: input.kind, + categoryId: input.categoryId ?? null, + uploadedByUserId: input.uploadedByUserId ?? null, + }); + await recordUploadedFile(FILE_INTEGRITY_ROOTS.faq, finalName); + + return { + filename: finalName, + mime: detected, + originalName, + url: publicFaqFileUrl(finalName), + }; +} + +/** Content-Disposition for FAQ downloads (attachment for PDF, inline for media). */ +export function faqFileContentDisposition( + downloadName: string, + mime: string +): string { + const ascii = downloadName.replace(/[^\x20-\x7E]/g, '_').replace(/"/g, ''); + const encoded = encodeURIComponent(downloadName); + const disposition = mime === 'application/pdf' ? 'attachment' : 'inline'; + return `${disposition}; filename="${ascii}"; filename*=UTF-8''${encoded}`; +} + +/** + * Resolve a legitimate FAQ file for public download. + * Registry first; legacy fallback for category PDFs / answer HTML references. + */ +export async function resolvePublicFaqFile(filename: string): Promise<{ + filePath: string; + mime: string; + downloadName: string; +} | null> { + const safe = path.basename(filename); + if (!safe || safe === '.' || safe === '..') return null; + const filePath = resolveFaqFilePath(safe); + if (!fs.existsSync(filePath)) return null; + + const registered = await repo.findUploadedFile(safe); + if (registered) { + return { + filePath, + mime: registered.mime, + downloadName: registered.original_name || safe, + }; + } + + if (await repo.isCategoryPdfFilename(safe)) { + return { + filePath, + mime: 'application/pdf', + downloadName: safe.toLowerCase().endsWith('.pdf') ? safe : `${safe}.pdf`, + }; + } + + if (await repo.isReferencedInFaqAnswerHtml(safe)) { + const ext = path.extname(safe).toLowerCase(); + const mime = + ext === '.jpg' || ext === '.jpeg' + ? 'image/jpeg' + : ext === '.png' + ? 'image/png' + : ext === '.gif' + ? 'image/gif' + : ext === '.webp' + ? 'image/webp' + : ext === '.mp4' + ? 'video/mp4' + : ext === '.webm' + ? 'video/webm' + : ext === '.pdf' + ? 'application/pdf' + : null; + if (!mime) return null; + return { filePath, mime, downloadName: safe }; + } + + return null; +} + export async function getFaqPdfMaxBytes(): Promise { const rows = await query<{ value: string }[]>( `SELECT value FROM system_settings WHERE key = $1 LIMIT 1`, @@ -308,29 +430,31 @@ export function readFaqFile(filename: string): string | null { export async function uploadCategoryDocument(input: { categoryId: number; lang: FaqDocumentLang; - storedFilename: string; + tempFilename: string; originalName: string; byteSize: number; + uploadedByUserId?: string | null; }): Promise> { const cat = await repo.getCategoryById(input.categoryId); if (!cat) { - removeUploadedFile(resolveFaqFilePath(input.storedFilename)); + removeUploadedFile(resolveFaqFilePath(input.tempFilename)); throw new Error('Category not found'); } - const filePath = resolveFaqFilePath(input.storedFilename); const maxBytes = await getFaqPdfMaxBytes(); if (input.byteSize > maxBytes) { - removeUploadedFile(filePath); + removeUploadedFile(resolveFaqFilePath(input.tempFilename)); throw new Error(`PDF exceeds maximum size of ${maxBytes} bytes`); } - try { - await validateUploadedFile(filePath, FAQ_PDF_ALLOWED_MIMES); - } catch (e) { - removeUploadedFile(filePath); - throw e; - } + const saved = await finalizeFaqUpload({ + tempFilename: input.tempFilename, + originalName: input.originalName, + kind: 'category_pdf', + categoryId: input.categoryId, + uploadedByUserId: input.uploadedByUserId ?? null, + allowedMimes: FAQ_PDF_ALLOWED_MIMES, + }); const previous = input.lang === 'fr' ? cat.pdf_fr_filename : cat.pdf_en_filename; @@ -340,18 +464,18 @@ export async function uploadCategoryDocument(input: { const updated = await repo.setCategoryDocument( input.categoryId, input.lang, - path.basename(input.storedFilename), + saved.filename, originalName ); if (!updated) { - removeUploadedFile(filePath); + removeUploadedFile(resolveFaqFilePath(saved.filename)); + await repo.deleteUploadedFile(saved.filename); throw new Error('Category not found'); } - await recordUploadedFile(FILE_INTEGRITY_ROOTS.faq, path.basename(input.storedFilename)); - - if (previous && previous !== input.storedFilename) { + if (previous && previous !== saved.filename) { unlinkFaqFileQuiet(previous); + await repo.deleteUploadedFile(previous).catch(() => undefined); } return mapCategory(updated); @@ -360,7 +484,10 @@ export async function uploadCategoryDocument(input: { export async function deleteCategoryDocument(categoryId: number, lang: FaqDocumentLang) { const result = await repo.clearCategoryDocument(categoryId, lang); if (!result) throw new Error('Category not found'); - unlinkFaqFileQuiet(result.removedFilename); + if (result.removedFilename) { + unlinkFaqFileQuiet(result.removedFilename); + await repo.deleteUploadedFile(result.removedFilename).catch(() => undefined); + } return mapCategory(result.row); } diff --git a/backend/src/tests/faqCategoryDocuments.test.ts b/backend/src/tests/faqCategoryDocuments.test.ts index 6c7709dc..03b2c64e 100644 --- a/backend/src/tests/faqCategoryDocuments.test.ts +++ b/backend/src/tests/faqCategoryDocuments.test.ts @@ -5,11 +5,13 @@ import os from 'os'; import path from 'path'; import { buildFaqCategoryGuideFilename, + faqFileContentDisposition, parseFaqDocumentLang, sanitizeDownloadFilename, } from '../services/faqService'; -import { assertAllowedFileMagic, detectMimeFromBuffer } from '../utils/fileMagic'; +import { assertAllowedFileMagic, detectMimeFromBuffer, extensionForMime } from '../utils/fileMagic'; import { FAQ_PDF_ALLOWED_MIMES } from '../middleware/faqPdfUpload'; +import { FAQ_ALLOWED_MIMES } from '../middleware/faqUpload'; describe('faq category document helpers', () => { test('parseFaqDocumentLang accepts fr and en only', () => { @@ -37,6 +39,18 @@ describe('faq category document helpers', () => { assert.equal(FAQ_PDF_ALLOWED_MIMES.size, 1); assert.ok(FAQ_PDF_ALLOWED_MIMES.has('application/pdf')); }); + + test('media allowlist excludes HTML and forces safe extensions', () => { + assert.ok(FAQ_ALLOWED_MIMES.has('image/png')); + assert.equal(FAQ_ALLOWED_MIMES.has('text/html'), false); + assert.equal(extensionForMime('image/png'), '.png'); + assert.equal(extensionForMime('application/pdf'), '.pdf'); + }); + + test('faqFileContentDisposition attaches PDFs and inlines media', () => { + assert.match(faqFileContentDisposition('guide.pdf', 'application/pdf'), /^attachment;/); + assert.match(faqFileContentDisposition('shot.png', 'image/png'), /^inline;/); + }); }); describe('faq PDF magic-byte validation', () => {