After improving the security for the files uploaded1
This commit is contained in:
parent
7ec1fdad28
commit
f2cad66cd2
@ -1,11 +1,7 @@
|
|||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import fs from 'fs';
|
|
||||||
import path from 'path';
|
|
||||||
import { AuthRequest } from '../middleware/auth';
|
import { AuthRequest } from '../middleware/auth';
|
||||||
import { ensureFaqUploadDir, FAQ_ALLOWED_MIMES } from '../middleware/faqUpload';
|
import { FAQ_ALLOWED_MIMES } from '../middleware/faqUpload';
|
||||||
import { recordUploadedFile, FILE_INTEGRITY_ROOTS } from '../services/fileIntegrityService';
|
|
||||||
import { removeUploadedFile } from '../utils/fileMagic';
|
import { removeUploadedFile } from '../utils/fileMagic';
|
||||||
import { validateUploadedFile } from '../utils/uploadValidation';
|
|
||||||
import * as faqService from '../services/faqService';
|
import * as faqService from '../services/faqService';
|
||||||
|
|
||||||
function handleError(res: Response, e: unknown): void {
|
function handleError(res: Response, e: unknown): void {
|
||||||
@ -76,13 +72,19 @@ export async function recordHelpful(req: AuthRequest, res: Response): Promise<vo
|
|||||||
|
|
||||||
export async function getFaqFile(req: Request, res: Response): Promise<void> {
|
export async function getFaqFile(req: Request, res: Response): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const filePath = faqService.readFaqFile(req.params.filename);
|
const resolved = await faqService.resolvePublicFaqFile(req.params.filename);
|
||||||
if (!filePath) {
|
if (!resolved) {
|
||||||
res.status(404).json({ error: 'File not found' });
|
res.status(404).json({ error: 'File not found' });
|
||||||
return;
|
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.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
|
||||||
res.sendFile(filePath);
|
res.sendFile(resolved.filePath);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
handleError(res, 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-Type', 'application/pdf');
|
||||||
res.setHeader(
|
res.setHeader(
|
||||||
'Content-Disposition',
|
'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.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
|
||||||
res.sendFile(resolved.filePath);
|
res.sendFile(resolved.filePath);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -207,23 +210,25 @@ export async function adminUploadMedia(req: AuthRequest, res: Response): Promise
|
|||||||
res.status(400).json({ error: 'No file uploaded' });
|
res.status(400).json({ error: 'No file uploaded' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const filePath = path.join(ensureFaqUploadDir(), req.file.filename);
|
|
||||||
try {
|
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) {
|
} catch (e) {
|
||||||
removeUploadedFile(filePath);
|
removeUploadedFile(faqService.resolveFaqFilePath(req.file.filename));
|
||||||
const msg = e instanceof Error ? e.message : 'File validation failed';
|
const msg = e instanceof Error ? e.message : 'File validation failed';
|
||||||
res.status(msg.includes('virus') ? 422 : 400).json({ error: msg });
|
res.status(msg.toLowerCase().includes('virus') ? 422 : 400).json({ error: msg });
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
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) {
|
} catch (e) {
|
||||||
handleError(res, e);
|
handleError(res, e);
|
||||||
}
|
}
|
||||||
@ -244,14 +249,15 @@ export async function adminUploadCategoryDocument(req: AuthRequest, res: Respons
|
|||||||
const item = await faqService.uploadCategoryDocument({
|
const item = await faqService.uploadCategoryDocument({
|
||||||
categoryId: id,
|
categoryId: id,
|
||||||
lang,
|
lang,
|
||||||
storedFilename: req.file.filename,
|
tempFilename: req.file.filename,
|
||||||
originalName: req.file.originalname,
|
originalName: req.file.originalname,
|
||||||
byteSize: req.file.size,
|
byteSize: req.file.size,
|
||||||
|
uploadedByUserId: req.user?.userId ?? null,
|
||||||
});
|
});
|
||||||
res.status(201).json(item);
|
res.status(201).json(item);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const msg = e instanceof Error ? e.message : 'Upload failed';
|
const msg = e instanceof Error ? e.message : 'Upload failed';
|
||||||
if (msg.includes('virus')) {
|
if (msg.toLowerCase().includes('virus')) {
|
||||||
res.status(422).json({ error: msg });
|
res.status(422).json({ error: msg });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3052,6 +3052,22 @@ INSERT INTO
|
|||||||
system_settings (key, value)
|
system_settings (key, value)
|
||||||
VALUES ('faq_pdf_max_bytes', '8388608') ON CONFLICT (key) DO NOTHING;
|
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)
|
-- Database tamper detection (direct SQL / bypass application)
|
||||||
-- =============================================================================
|
-- =============================================================================
|
||||||
|
|||||||
@ -14,7 +14,8 @@ const storage = multer.diskStorage({
|
|||||||
cb(null, ensureFaqUploadDir());
|
cb(null, ensureFaqUploadDir());
|
||||||
},
|
},
|
||||||
filename(_req, _file, cb) {
|
filename(_req, _file, cb) {
|
||||||
cb(null, `${uuidv4()}.pdf`);
|
// Temporary name — finalized to uuid.pdf after magic-byte validation.
|
||||||
|
cb(null, `${uuidv4()}.upload`);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import multer from 'multer';
|
import multer from 'multer';
|
||||||
import path from 'path';
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
const MAX_BYTES = 10 * 1024 * 1024;
|
const MAX_BYTES = 10 * 1024 * 1024;
|
||||||
@ -23,9 +23,9 @@ const storage = multer.diskStorage({
|
|||||||
destination(_req, _file, cb) {
|
destination(_req, _file, cb) {
|
||||||
cb(null, ensureFaqUploadDir());
|
cb(null, ensureFaqUploadDir());
|
||||||
},
|
},
|
||||||
filename(_req, file, cb) {
|
filename(_req, _file, cb) {
|
||||||
const ext = path.extname(file.originalname).slice(0, 20);
|
// Temporary name — controller renames to uuid + extension from detected MIME.
|
||||||
cb(null, `${uuidv4()}${ext}`);
|
cb(null, `${uuidv4()}.upload`);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -365,3 +365,79 @@ export async function countEntriesInCategory(categoryId: number): Promise<number
|
|||||||
);
|
);
|
||||||
return Number(rows[0]?.count ?? 0);
|
return Number(rows[0]?.count ?? 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type FaqUploadedFileRow = {
|
||||||
|
filename: string;
|
||||||
|
mime: string;
|
||||||
|
original_name: string | null;
|
||||||
|
kind: 'media' | 'category_pdf';
|
||||||
|
category_id: number | null;
|
||||||
|
uploaded_by_user_id: string | null;
|
||||||
|
created_at: Date;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function insertUploadedFile(input: {
|
||||||
|
filename: string;
|
||||||
|
mime: string;
|
||||||
|
originalName?: string | null;
|
||||||
|
kind: 'media' | 'category_pdf';
|
||||||
|
categoryId?: number | null;
|
||||||
|
uploadedByUserId?: string | null;
|
||||||
|
}): Promise<FaqUploadedFileRow> {
|
||||||
|
const rows = await query<FaqUploadedFileRow[]>(
|
||||||
|
`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<FaqUploadedFileRow | null> {
|
||||||
|
const rows = await query<FaqUploadedFileRow[]>(
|
||||||
|
`SELECT * FROM faq_uploaded_files WHERE filename = $1`,
|
||||||
|
[filename]
|
||||||
|
);
|
||||||
|
return rows[0] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteUploadedFile(filename: string): Promise<void> {
|
||||||
|
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<boolean> {
|
||||||
|
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<boolean> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { Request } from 'express';
|
import { Request } from 'express';
|
||||||
import * as repo from '../repositories/faqRepository';
|
import * as repo from '../repositories/faqRepository';
|
||||||
import { FaqCategoryRow, FaqDocumentLang } from '../models/faq.model';
|
import { FaqCategoryRow, FaqDocumentLang } from '../models/faq.model';
|
||||||
@ -11,7 +12,7 @@ import {
|
|||||||
} from '../middleware/faqPdfUpload';
|
} from '../middleware/faqPdfUpload';
|
||||||
import { sanitizeFaqHtml } from '../utils/sanitizeHtml';
|
import { sanitizeFaqHtml } from '../utils/sanitizeHtml';
|
||||||
import { validateUploadedFile } from '../utils/uploadValidation';
|
import { validateUploadedFile } from '../utils/uploadValidation';
|
||||||
import { removeUploadedFile } from '../utils/fileMagic';
|
import { extensionForMime, removeUploadedFile } from '../utils/fileMagic';
|
||||||
import { recordUploadedFile, FILE_INTEGRITY_ROOTS } from './fileIntegrityService';
|
import { recordUploadedFile, FILE_INTEGRITY_ROOTS } from './fileIntegrityService';
|
||||||
import { query } from '../db/connection';
|
import { query } from '../db/connection';
|
||||||
|
|
||||||
@ -125,6 +126,127 @@ export function resolveFaqFilePath(filename: string): string {
|
|||||||
return path.join(ensureFaqUploadDir(), path.basename(filename));
|
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<string>;
|
||||||
|
}): 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<number> {
|
export async function getFaqPdfMaxBytes(): Promise<number> {
|
||||||
const rows = await query<{ value: string }[]>(
|
const rows = await query<{ value: string }[]>(
|
||||||
`SELECT value FROM system_settings WHERE key = $1 LIMIT 1`,
|
`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: {
|
export async function uploadCategoryDocument(input: {
|
||||||
categoryId: number;
|
categoryId: number;
|
||||||
lang: FaqDocumentLang;
|
lang: FaqDocumentLang;
|
||||||
storedFilename: string;
|
tempFilename: string;
|
||||||
originalName: string;
|
originalName: string;
|
||||||
byteSize: number;
|
byteSize: number;
|
||||||
|
uploadedByUserId?: string | null;
|
||||||
}): Promise<ReturnType<typeof mapCategory>> {
|
}): Promise<ReturnType<typeof mapCategory>> {
|
||||||
const cat = await repo.getCategoryById(input.categoryId);
|
const cat = await repo.getCategoryById(input.categoryId);
|
||||||
if (!cat) {
|
if (!cat) {
|
||||||
removeUploadedFile(resolveFaqFilePath(input.storedFilename));
|
removeUploadedFile(resolveFaqFilePath(input.tempFilename));
|
||||||
throw new Error('Category not found');
|
throw new Error('Category not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const filePath = resolveFaqFilePath(input.storedFilename);
|
|
||||||
const maxBytes = await getFaqPdfMaxBytes();
|
const maxBytes = await getFaqPdfMaxBytes();
|
||||||
if (input.byteSize > maxBytes) {
|
if (input.byteSize > maxBytes) {
|
||||||
removeUploadedFile(filePath);
|
removeUploadedFile(resolveFaqFilePath(input.tempFilename));
|
||||||
throw new Error(`PDF exceeds maximum size of ${maxBytes} bytes`);
|
throw new Error(`PDF exceeds maximum size of ${maxBytes} bytes`);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
const saved = await finalizeFaqUpload({
|
||||||
await validateUploadedFile(filePath, FAQ_PDF_ALLOWED_MIMES);
|
tempFilename: input.tempFilename,
|
||||||
} catch (e) {
|
originalName: input.originalName,
|
||||||
removeUploadedFile(filePath);
|
kind: 'category_pdf',
|
||||||
throw e;
|
categoryId: input.categoryId,
|
||||||
}
|
uploadedByUserId: input.uploadedByUserId ?? null,
|
||||||
|
allowedMimes: FAQ_PDF_ALLOWED_MIMES,
|
||||||
|
});
|
||||||
|
|
||||||
const previous =
|
const previous =
|
||||||
input.lang === 'fr' ? cat.pdf_fr_filename : cat.pdf_en_filename;
|
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(
|
const updated = await repo.setCategoryDocument(
|
||||||
input.categoryId,
|
input.categoryId,
|
||||||
input.lang,
|
input.lang,
|
||||||
path.basename(input.storedFilename),
|
saved.filename,
|
||||||
originalName
|
originalName
|
||||||
);
|
);
|
||||||
if (!updated) {
|
if (!updated) {
|
||||||
removeUploadedFile(filePath);
|
removeUploadedFile(resolveFaqFilePath(saved.filename));
|
||||||
|
await repo.deleteUploadedFile(saved.filename);
|
||||||
throw new Error('Category not found');
|
throw new Error('Category not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
await recordUploadedFile(FILE_INTEGRITY_ROOTS.faq, path.basename(input.storedFilename));
|
if (previous && previous !== saved.filename) {
|
||||||
|
|
||||||
if (previous && previous !== input.storedFilename) {
|
|
||||||
unlinkFaqFileQuiet(previous);
|
unlinkFaqFileQuiet(previous);
|
||||||
|
await repo.deleteUploadedFile(previous).catch(() => undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
return mapCategory(updated);
|
return mapCategory(updated);
|
||||||
@ -360,7 +484,10 @@ export async function uploadCategoryDocument(input: {
|
|||||||
export async function deleteCategoryDocument(categoryId: number, lang: FaqDocumentLang) {
|
export async function deleteCategoryDocument(categoryId: number, lang: FaqDocumentLang) {
|
||||||
const result = await repo.clearCategoryDocument(categoryId, lang);
|
const result = await repo.clearCategoryDocument(categoryId, lang);
|
||||||
if (!result) throw new Error('Category not found');
|
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);
|
return mapCategory(result.row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,11 +5,13 @@ import os from 'os';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import {
|
import {
|
||||||
buildFaqCategoryGuideFilename,
|
buildFaqCategoryGuideFilename,
|
||||||
|
faqFileContentDisposition,
|
||||||
parseFaqDocumentLang,
|
parseFaqDocumentLang,
|
||||||
sanitizeDownloadFilename,
|
sanitizeDownloadFilename,
|
||||||
} from '../services/faqService';
|
} 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_PDF_ALLOWED_MIMES } from '../middleware/faqPdfUpload';
|
||||||
|
import { FAQ_ALLOWED_MIMES } from '../middleware/faqUpload';
|
||||||
|
|
||||||
describe('faq category document helpers', () => {
|
describe('faq category document helpers', () => {
|
||||||
test('parseFaqDocumentLang accepts fr and en only', () => {
|
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.equal(FAQ_PDF_ALLOWED_MIMES.size, 1);
|
||||||
assert.ok(FAQ_PDF_ALLOWED_MIMES.has('application/pdf'));
|
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', () => {
|
describe('faq PDF magic-byte validation', () => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user