After improving the security for the files uploaded
This commit is contained in:
parent
919f1e87b3
commit
7ec1fdad28
@ -84,3 +84,9 @@ EMAIL_TRANSPORT=console
|
||||
|
||||
# Local Docker: skip mandatory admin 2FA (production should leave this unset or true)
|
||||
REQUIRE_ADMIN_2FA=false
|
||||
|
||||
# ClamAV (optional locally). Production VPS: CLAMAV_ENABLED=true + docker compose --profile clamav
|
||||
# CLAMAV_ENABLED=false
|
||||
# CLAMAV_HOST=clamav
|
||||
# CLAMAV_PORT=3310
|
||||
# CLAMAV_TIMEOUT_MS=30000
|
||||
|
||||
@ -107,8 +107,10 @@ WORKER_ID=worker-1
|
||||
# SENTRY_DSN=https://...@sentry.io/...
|
||||
# SENTRY_TRACES_SAMPLE_RATE=0.1
|
||||
|
||||
# ClamAV virus scan on uploads (optional — requires clamd)
|
||||
# CLAMAV_ENABLED=false
|
||||
# ClamAV virus scan on uploads (requires clamd).
|
||||
# Production (NODE_ENV=production): enabled by default unless CLAMAV_ENABLED=false.
|
||||
# Development: disabled unless CLAMAV_ENABLED=true.
|
||||
# CLAMAV_ENABLED=true
|
||||
# CLAMAV_HOST=127.0.0.1
|
||||
# CLAMAV_PORT=3310
|
||||
# CLAMAV_TIMEOUT_MS=30000
|
||||
|
||||
@ -297,7 +297,13 @@ export const config = {
|
||||
),
|
||||
},
|
||||
clamav: {
|
||||
enabled: process.env.CLAMAV_ENABLED === 'true',
|
||||
// Production: on by default. Explicit CLAMAV_ENABLED=false disables; =true forces on in any env.
|
||||
enabled: (() => {
|
||||
const raw = (process.env.CLAMAV_ENABLED || '').trim().toLowerCase();
|
||||
if (raw === 'true' || raw === '1' || raw === 'yes') return true;
|
||||
if (raw === 'false' || raw === '0' || raw === 'no') return false;
|
||||
return (process.env.NODE_ENV || 'development') === 'production';
|
||||
})(),
|
||||
host: process.env.CLAMAV_HOST || '127.0.0.1',
|
||||
port: parseInt(process.env.CLAMAV_PORT || '3310', 10),
|
||||
timeoutMs: readEnvInt('CLAMAV_TIMEOUT_MS', 30_000, 5000, 120_000),
|
||||
|
||||
@ -1,13 +1,9 @@
|
||||
import { Response } from 'express';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { AuthRequest } from '../middleware/auth';
|
||||
import { readVisitorToken } from '../middleware/optionalAuth';
|
||||
import { SUPPORT_ALLOWED_MIMES } from '../middleware/supportUpload';
|
||||
import { removeUploadedFile } from '../utils/fileMagic';
|
||||
import { validateUploadedFile } from '../utils/uploadValidation';
|
||||
import { verifyToken } from '../services/authService';
|
||||
import * as supportService from '../services/supportService';
|
||||
import { recordUploadedFile, FILE_INTEGRITY_ROOTS } from '../services/fileIntegrityService';
|
||||
|
||||
function buildCtx(req: AuthRequest) {
|
||||
return {
|
||||
@ -155,22 +151,23 @@ export async function uploadCurrentAttachment(req: AuthRequest, res: Response):
|
||||
res.status(400).json({ error: 'No file uploaded' });
|
||||
return;
|
||||
}
|
||||
const filePath = supportService.resolveUploadPath(req.file.filename);
|
||||
const ctx = buildCtx(req);
|
||||
try {
|
||||
await validateUploadedFile(filePath, SUPPORT_ALLOWED_MIMES);
|
||||
const saved = await supportService.finalizeSupportUpload({
|
||||
tempFilename: req.file.filename,
|
||||
originalName: req.file.originalname,
|
||||
ctx,
|
||||
});
|
||||
res.status(201).json({
|
||||
attachmentUrl: saved.url,
|
||||
attachmentName: saved.originalName,
|
||||
attachmentMime: saved.mime,
|
||||
});
|
||||
} catch (e) {
|
||||
removeUploadedFile(filePath);
|
||||
removeUploadedFile(supportService.resolveUploadPath(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 = supportService.publicAttachmentUrl(req.file.filename);
|
||||
await recordUploadedFile(FILE_INTEGRITY_ROOTS.support, req.file.filename);
|
||||
res.status(201).json({
|
||||
attachmentUrl: url,
|
||||
attachmentName: req.file.originalname,
|
||||
attachmentMime: req.file.mimetype,
|
||||
});
|
||||
} catch (e) {
|
||||
handleError(res, e);
|
||||
}
|
||||
@ -182,23 +179,29 @@ export async function uploadAttachment(req: AuthRequest, res: Response): Promise
|
||||
res.status(400).json({ error: 'No file uploaded' });
|
||||
return;
|
||||
}
|
||||
const filePath = supportService.resolveUploadPath(req.file.filename);
|
||||
const ctx = buildCtx(req);
|
||||
try {
|
||||
await validateUploadedFile(filePath, SUPPORT_ALLOWED_MIMES);
|
||||
await supportService.assertConversationAccess(req.params.id, ctx);
|
||||
const saved = await supportService.finalizeSupportUpload({
|
||||
tempFilename: req.file.filename,
|
||||
originalName: req.file.originalname,
|
||||
conversationId: req.params.id,
|
||||
ctx,
|
||||
});
|
||||
res.status(201).json({
|
||||
attachmentUrl: saved.url,
|
||||
attachmentName: saved.originalName,
|
||||
attachmentMime: saved.mime,
|
||||
});
|
||||
} catch (e) {
|
||||
removeUploadedFile(filePath);
|
||||
removeUploadedFile(supportService.resolveUploadPath(req.file.filename));
|
||||
const msg = e instanceof Error ? e.message : 'File validation failed';
|
||||
res.status(msg.includes('virus') ? 422 : 400).json({ error: msg });
|
||||
return;
|
||||
if (msg === 'Forbidden' || msg === 'Conversation not found') {
|
||||
handleError(res, e);
|
||||
return;
|
||||
}
|
||||
res.status(msg.toLowerCase().includes('virus') ? 422 : 400).json({ error: msg });
|
||||
}
|
||||
await supportService.assertConversationAccess(req.params.id, buildCtx(req));
|
||||
const url = supportService.publicAttachmentUrl(req.file.filename);
|
||||
await recordUploadedFile(FILE_INTEGRITY_ROOTS.support, req.file.filename);
|
||||
res.status(201).json({
|
||||
attachmentUrl: url,
|
||||
attachmentName: req.file.originalname,
|
||||
attachmentMime: req.file.mimetype,
|
||||
});
|
||||
} catch (e) {
|
||||
handleError(res, e);
|
||||
}
|
||||
@ -206,14 +209,36 @@ export async function uploadAttachment(req: AuthRequest, res: Response): Promise
|
||||
|
||||
export async function getAttachment(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const filePath = supportService.resolveUploadPath(req.params.filename);
|
||||
if (!fs.existsSync(filePath)) {
|
||||
res.status(404).json({ error: 'File not found' });
|
||||
// <img src> cannot send Authorization — accept accessToken query as fallback.
|
||||
if (!req.user) {
|
||||
const q = req.query.accessToken;
|
||||
if (typeof q === 'string' && q.trim()) {
|
||||
try {
|
||||
req.user = await verifyToken(q.trim());
|
||||
} catch {
|
||||
res.status(401).json({ error: 'Invalid or expired token' });
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ctx = buildCtx(req);
|
||||
if (!ctx.userId && !ctx.visitorToken && !supportService.isStaffRole(ctx.role)) {
|
||||
res.status(401).json({ error: 'Authentication required' });
|
||||
return;
|
||||
}
|
||||
|
||||
const access = await supportService.assertSupportFileDownloadAccess(
|
||||
req.params.filename,
|
||||
ctx
|
||||
);
|
||||
const downloadName = access.originalName || req.params.filename;
|
||||
res.setHeader('Content-Type', access.mime);
|
||||
res.setHeader('Content-Disposition', supportService.attachmentContentDisposition(downloadName));
|
||||
res.setHeader('X-Content-Type-Options', 'nosniff');
|
||||
// Helmet defaults to CORP same-origin; the web app is on another port/origin.
|
||||
res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
|
||||
res.sendFile(filePath);
|
||||
res.sendFile(access.filePath);
|
||||
} catch (e) {
|
||||
handleError(res, e);
|
||||
}
|
||||
@ -315,22 +340,24 @@ export async function adminUploadAttachment(req: AuthRequest, res: Response): Pr
|
||||
res.status(400).json({ error: 'No file uploaded' });
|
||||
return;
|
||||
}
|
||||
const filePath = supportService.resolveUploadPath(req.file.filename);
|
||||
const ctx = buildCtx(req);
|
||||
try {
|
||||
await validateUploadedFile(filePath, SUPPORT_ALLOWED_MIMES);
|
||||
const saved = await supportService.finalizeSupportUpload({
|
||||
tempFilename: req.file.filename,
|
||||
originalName: req.file.originalname,
|
||||
conversationId: req.params.id,
|
||||
ctx,
|
||||
});
|
||||
res.status(201).json({
|
||||
attachmentUrl: saved.url,
|
||||
attachmentName: saved.originalName,
|
||||
attachmentMime: saved.mime,
|
||||
});
|
||||
} catch (e) {
|
||||
removeUploadedFile(filePath);
|
||||
removeUploadedFile(supportService.resolveUploadPath(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 = supportService.publicAttachmentUrl(req.file.filename);
|
||||
await recordUploadedFile(FILE_INTEGRITY_ROOTS.support, req.file.filename);
|
||||
res.status(201).json({
|
||||
attachmentUrl: url,
|
||||
attachmentName: req.file.originalname,
|
||||
attachmentMime: req.file.mimetype,
|
||||
});
|
||||
} catch (e) {
|
||||
handleError(res, e);
|
||||
}
|
||||
|
||||
@ -2928,6 +2928,23 @@ CREATE INDEX IF NOT EXISTS idx_support_msg_unread ON support_messages (conversat
|
||||
WHERE
|
||||
read_at IS NULL;
|
||||
|
||||
-- ACL registry for support chat uploads (authz on download)
|
||||
CREATE TABLE IF NOT EXISTS support_uploaded_files (
|
||||
filename VARCHAR(255) PRIMARY KEY,
|
||||
mime VARCHAR(128) NOT NULL,
|
||||
original_name VARCHAR(255),
|
||||
conversation_id UUID REFERENCES support_conversations (id) ON DELETE SET NULL,
|
||||
uploaded_by_user_id UUID REFERENCES users (id) ON DELETE SET NULL,
|
||||
visitor_token VARCHAR(128),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_support_uploaded_files_conv ON support_uploaded_files (conversation_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_support_uploaded_files_user ON support_uploaded_files (uploaded_by_user_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_support_uploaded_files_visitor ON support_uploaded_files (visitor_token);
|
||||
|
||||
-- FAQ (categories + entries, staff-managed)
|
||||
CREATE TABLE IF NOT EXISTS faq_categories (
|
||||
id SERIAL PRIMARY KEY,
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import multer from 'multer';
|
||||
import path from 'path';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { ensureUploadDir } from '../services/supportService';
|
||||
|
||||
@ -19,9 +18,9 @@ const storage = multer.diskStorage({
|
||||
destination(_req, _file, cb) {
|
||||
cb(null, ensureUploadDir());
|
||||
},
|
||||
filename(_req, file, cb) {
|
||||
const ext = path.extname(file.originalname).slice(0, 20);
|
||||
cb(null, `${uuidv4()}${ext}`);
|
||||
filename(_req, _file, cb) {
|
||||
// Temporary name only — controller renames to uuid + extension from detected MIME.
|
||||
cb(null, `${uuidv4()}.upload`);
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -422,3 +422,76 @@ export async function mergeAllVisitorConversationsToUser(
|
||||
);
|
||||
return rows.length;
|
||||
}
|
||||
|
||||
export type SupportUploadedFileRow = {
|
||||
filename: string;
|
||||
mime: string;
|
||||
original_name: string | null;
|
||||
conversation_id: string | null;
|
||||
uploaded_by_user_id: string | null;
|
||||
visitor_token: string | null;
|
||||
created_at: Date;
|
||||
};
|
||||
|
||||
export async function insertUploadedFile(input: {
|
||||
filename: string;
|
||||
mime: string;
|
||||
originalName?: string | null;
|
||||
conversationId?: string | null;
|
||||
uploadedByUserId?: string | null;
|
||||
visitorToken?: string | null;
|
||||
}): Promise<SupportUploadedFileRow> {
|
||||
const rows = await query<SupportUploadedFileRow[]>(
|
||||
`INSERT INTO support_uploaded_files (
|
||||
filename, mime, original_name, conversation_id, uploaded_by_user_id, visitor_token
|
||||
) VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *`,
|
||||
[
|
||||
input.filename,
|
||||
input.mime,
|
||||
input.originalName ?? null,
|
||||
input.conversationId ?? null,
|
||||
input.uploadedByUserId ?? null,
|
||||
input.visitorToken ?? null,
|
||||
]
|
||||
);
|
||||
return rows[0]!;
|
||||
}
|
||||
|
||||
export async function findUploadedFile(filename: string): Promise<SupportUploadedFileRow | null> {
|
||||
const rows = await query<SupportUploadedFileRow[]>(
|
||||
`SELECT * FROM support_uploaded_files WHERE filename = $1`,
|
||||
[filename]
|
||||
);
|
||||
return rows[0] ?? null;
|
||||
}
|
||||
|
||||
export async function bindUploadedFileConversation(
|
||||
filename: string,
|
||||
conversationId: string
|
||||
): Promise<void> {
|
||||
await query(
|
||||
`UPDATE support_uploaded_files
|
||||
SET conversation_id = COALESCE(conversation_id, $2)
|
||||
WHERE filename = $1`,
|
||||
[filename, conversationId]
|
||||
);
|
||||
}
|
||||
|
||||
export async function findMessageByAttachmentFilename(
|
||||
filename: string
|
||||
): Promise<{ conversation_id: string } | null> {
|
||||
const suffix = `%/support/files/${filename}`;
|
||||
const rows = await query<{ conversation_id: string }[]>(
|
||||
`SELECT conversation_id FROM support_messages
|
||||
WHERE attachment_url IS NOT NULL
|
||||
AND (
|
||||
attachment_url = $1
|
||||
OR attachment_url LIKE $2
|
||||
)
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1`,
|
||||
[`/api/v1/support/files/${filename}`, suffix]
|
||||
);
|
||||
return rows[0] ?? null;
|
||||
}
|
||||
|
||||
@ -222,7 +222,11 @@ router.post(
|
||||
);
|
||||
|
||||
/** Support messaging — guests (visitor token) and optional authenticated users. */
|
||||
router.get('/support/files/:filename', supportController.getAttachment);
|
||||
router.get(
|
||||
'/support/files/:filename',
|
||||
optionalAuthMiddleware,
|
||||
supportController.getAttachment
|
||||
);
|
||||
router.post('/support/conversations/current', optionalAuthMiddleware, supportController.getOrCreateConversation);
|
||||
router.post(
|
||||
'/support/conversations/current/messages',
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { decrypt } from '../utils/crypto';
|
||||
import * as repo from '../repositories/supportRepository';
|
||||
import {
|
||||
@ -19,6 +20,10 @@ import {
|
||||
resolveSupportMessageActions,
|
||||
type SupportMessageActionFlags,
|
||||
} from './supportMessagePolicy';
|
||||
import { extensionForMime, removeUploadedFile } from '../utils/fileMagic';
|
||||
import { validateUploadedFile } from '../utils/uploadValidation';
|
||||
import { SUPPORT_ALLOWED_MIMES } from '../middleware/supportUpload';
|
||||
import { recordUploadedFile, FILE_INTEGRITY_ROOTS } from './fileIntegrityService';
|
||||
|
||||
const STAFF_ROLES = new Set(['admin', 'moderator', 'super_admin']);
|
||||
|
||||
@ -273,6 +278,17 @@ export async function sendMessage(
|
||||
throw new Error('Message body or attachment is required');
|
||||
}
|
||||
|
||||
let attachmentUrl = input.attachmentUrl ?? null;
|
||||
let attachmentName = input.attachmentName ?? null;
|
||||
let attachmentMime = input.attachmentMime ?? null;
|
||||
if (attachmentUrl) {
|
||||
const meta = await assertOwnedSupportAttachment(attachmentUrl, ctx);
|
||||
attachmentUrl = publicAttachmentUrl(meta.filename);
|
||||
attachmentName = attachmentName || meta.original_name || meta.filename;
|
||||
attachmentMime = meta.mime;
|
||||
await repo.bindUploadedFileConversation(meta.filename, conversationId);
|
||||
}
|
||||
|
||||
let senderType: SupportSenderType;
|
||||
let senderUserId: string | null = null;
|
||||
|
||||
@ -291,12 +307,12 @@ export async function sendMessage(
|
||||
senderType,
|
||||
senderUserId,
|
||||
body,
|
||||
attachmentUrl: input.attachmentUrl ?? null,
|
||||
attachmentName: input.attachmentName ?? null,
|
||||
attachmentMime: input.attachmentMime ?? null,
|
||||
attachmentUrl,
|
||||
attachmentName,
|
||||
attachmentMime,
|
||||
});
|
||||
|
||||
const preview = previewForMessage(body, input.attachmentName ?? null);
|
||||
const preview = previewForMessage(body, attachmentName);
|
||||
const staffSent = senderType === 'staff';
|
||||
await repo.touchConversationLastMessage(
|
||||
conversationId,
|
||||
@ -513,6 +529,164 @@ export function publicAttachmentUrl(storedName: string): string {
|
||||
return `/api/v1/support/files/${encodeURIComponent(storedName)}`;
|
||||
}
|
||||
|
||||
const SUPPORT_FILE_RE = /^\/api\/v1\/support\/files\/([^/?#]+)$/i;
|
||||
|
||||
/** Extract basename from a support attachment URL (relative or absolute). */
|
||||
export function parseSupportAttachmentFilename(url: string): string | null {
|
||||
try {
|
||||
const pathOnly = url.startsWith('http') ? new URL(url).pathname : url.split('?')[0] ?? url;
|
||||
const m = pathOnly.match(SUPPORT_FILE_RE);
|
||||
if (!m?.[1]) return null;
|
||||
const name = path.basename(decodeURIComponent(m[1]));
|
||||
if (!name || name === '.' || name === '..') return null;
|
||||
return name;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function safeDisplayName(originalName: string | undefined, mime: string): string {
|
||||
const base = path.basename(originalName || 'attachment').replace(/[^\w.\-()+ ]+/g, '_').slice(0, 180);
|
||||
const ext = extensionForMime(mime);
|
||||
const withoutExt = base.replace(/\.[^.]+$/, '') || 'attachment';
|
||||
return `${withoutExt}${ext}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate magic/ClamAV, rename to uuid+safeExt, register ACL row.
|
||||
*/
|
||||
export async function finalizeSupportUpload(input: {
|
||||
tempFilename: string;
|
||||
originalName?: string;
|
||||
conversationId?: string | null;
|
||||
ctx: SupportAccessContext;
|
||||
}): Promise<{ filename: string; mime: string; originalName: string; url: string }> {
|
||||
const tempPath = resolveUploadPath(input.tempFilename);
|
||||
let detected: string;
|
||||
try {
|
||||
detected = await validateUploadedFile(tempPath, SUPPORT_ALLOWED_MIMES);
|
||||
} catch (e) {
|
||||
removeUploadedFile(tempPath);
|
||||
throw e;
|
||||
}
|
||||
|
||||
const ext = extensionForMime(detected);
|
||||
const finalName = `${uuidv4()}${ext}`;
|
||||
const finalPath = resolveUploadPath(finalName);
|
||||
fs.renameSync(tempPath, finalPath);
|
||||
|
||||
const originalName = safeDisplayName(input.originalName, detected);
|
||||
await repo.insertUploadedFile({
|
||||
filename: finalName,
|
||||
mime: detected,
|
||||
originalName,
|
||||
conversationId: input.conversationId ?? null,
|
||||
uploadedByUserId: input.ctx.userId ?? null,
|
||||
visitorToken: input.ctx.visitorToken ?? null,
|
||||
});
|
||||
await recordUploadedFile(FILE_INTEGRITY_ROOTS.support, finalName);
|
||||
|
||||
return {
|
||||
filename: finalName,
|
||||
mime: detected,
|
||||
originalName,
|
||||
url: publicAttachmentUrl(finalName),
|
||||
};
|
||||
}
|
||||
|
||||
async function assertOwnedSupportAttachment(url: string, ctx: SupportAccessContext) {
|
||||
const filename = parseSupportAttachmentFilename(url);
|
||||
if (!filename) throw new Error('Invalid attachment URL');
|
||||
const row = await repo.findUploadedFile(filename);
|
||||
if (!row) throw new Error('Attachment not found');
|
||||
if (isStaffRole(ctx.role)) return row;
|
||||
if (ctx.userId && row.uploaded_by_user_id === ctx.userId) return row;
|
||||
if (ctx.visitorToken && row.visitor_token === ctx.visitorToken) return row;
|
||||
if (row.conversation_id) {
|
||||
await assertConversationAccess(row.conversation_id, ctx);
|
||||
return row;
|
||||
}
|
||||
throw new Error('Forbidden');
|
||||
}
|
||||
|
||||
/** Authorize download of a stored support file. */
|
||||
export async function assertSupportFileDownloadAccess(
|
||||
filename: string,
|
||||
ctx: SupportAccessContext
|
||||
): Promise<{ mime: string; originalName: string | null; filePath: string }> {
|
||||
const safe = path.basename(filename);
|
||||
const filePath = resolveUploadPath(safe);
|
||||
if (!fs.existsSync(filePath)) throw new Error('Not found');
|
||||
|
||||
if (isStaffRole(ctx.role)) {
|
||||
const row = await repo.findUploadedFile(safe);
|
||||
return {
|
||||
mime: row?.mime || mimeFromFilename(safe),
|
||||
originalName: row?.original_name ?? null,
|
||||
filePath,
|
||||
};
|
||||
}
|
||||
|
||||
const row = await repo.findUploadedFile(safe);
|
||||
if (row) {
|
||||
if (ctx.userId && row.uploaded_by_user_id === ctx.userId) {
|
||||
return { mime: row.mime, originalName: row.original_name, filePath };
|
||||
}
|
||||
if (ctx.visitorToken && row.visitor_token === ctx.visitorToken) {
|
||||
return { mime: row.mime, originalName: row.original_name, filePath };
|
||||
}
|
||||
if (row.conversation_id) {
|
||||
await assertConversationAccess(row.conversation_id, ctx);
|
||||
return { mime: row.mime, originalName: row.original_name, filePath };
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy files uploaded before ACL registry: allow if referenced by an accessible message.
|
||||
const msg = await repo.findMessageByAttachmentFilename(safe);
|
||||
if (msg) {
|
||||
await assertConversationAccess(msg.conversation_id, ctx);
|
||||
return {
|
||||
mime: mimeFromFilename(safe),
|
||||
originalName: null,
|
||||
filePath,
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error('Forbidden');
|
||||
}
|
||||
|
||||
function mimeFromFilename(filename: string): string {
|
||||
const ext = path.extname(filename).toLowerCase();
|
||||
switch (ext) {
|
||||
case '.jpg':
|
||||
case '.jpeg':
|
||||
return 'image/jpeg';
|
||||
case '.png':
|
||||
return 'image/png';
|
||||
case '.gif':
|
||||
return 'image/gif';
|
||||
case '.webp':
|
||||
return 'image/webp';
|
||||
case '.pdf':
|
||||
return 'application/pdf';
|
||||
case '.txt':
|
||||
return 'text/plain';
|
||||
case '.doc':
|
||||
return 'application/msword';
|
||||
case '.docx':
|
||||
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
||||
default:
|
||||
return 'application/octet-stream';
|
||||
}
|
||||
}
|
||||
|
||||
/** Content-Disposition filename* / filename header value. */
|
||||
export function attachmentContentDisposition(downloadName: string): string {
|
||||
const ascii = downloadName.replace(/[^\x20-\x7E]/g, '_').replace(/"/g, '');
|
||||
const encoded = encodeURIComponent(downloadName);
|
||||
return `attachment; filename="${ascii}"; filename*=UTF-8''${encoded}`;
|
||||
}
|
||||
|
||||
/** Links all anonymous visitor conversations to a newly registered or logged-in user. */
|
||||
export async function mergeVisitorSupportForUser(
|
||||
userId: string,
|
||||
|
||||
59
backend/src/tests/supportUploadHardening.test.ts
Normal file
59
backend/src/tests/supportUploadHardening.test.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
assertAllowedFileMagic,
|
||||
detectMimeFromBuffer,
|
||||
extensionForMime,
|
||||
} from '../utils/fileMagic';
|
||||
import {
|
||||
attachmentContentDisposition,
|
||||
parseSupportAttachmentFilename,
|
||||
} from '../services/supportService';
|
||||
|
||||
test('extensionForMime maps detected types to safe extensions', () => {
|
||||
assert.equal(extensionForMime('image/jpeg'), '.jpg');
|
||||
assert.equal(extensionForMime('application/pdf'), '.pdf');
|
||||
assert.equal(extensionForMime('text/plain'), '.txt');
|
||||
assert.equal(
|
||||
extensionForMime('application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
|
||||
'.docx'
|
||||
);
|
||||
});
|
||||
|
||||
test('text/plain magic rejects HTML/SVG looking content', () => {
|
||||
assert.equal(detectMimeFromBuffer(Buffer.from('hello world')), 'text/plain');
|
||||
assert.equal(detectMimeFromBuffer(Buffer.from('<!DOCTYPE html><html>')), null);
|
||||
assert.equal(detectMimeFromBuffer(Buffer.from('<svg xmlns="http://www.w3.org/2000/svg">')), null);
|
||||
assert.equal(detectMimeFromBuffer(Buffer.from('note\n<script>alert(1)</script>')), null);
|
||||
});
|
||||
|
||||
test('assertAllowedFileMagic returns detected mime', () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'support-magic-'));
|
||||
const filePath = path.join(dir, 'x.upload');
|
||||
fs.writeFileSync(filePath, Buffer.from('%PDF-1.4\n'));
|
||||
const mime = assertAllowedFileMagic(filePath, new Set(['application/pdf']));
|
||||
assert.equal(mime, 'application/pdf');
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test('parseSupportAttachmentFilename accepts only support file URLs', () => {
|
||||
assert.equal(
|
||||
parseSupportAttachmentFilename('/api/v1/support/files/abc-123.pdf'),
|
||||
'abc-123.pdf'
|
||||
);
|
||||
assert.equal(
|
||||
parseSupportAttachmentFilename('https://tloto.net/api/v1/support/files/abc-123.pdf?x=1'),
|
||||
'abc-123.pdf'
|
||||
);
|
||||
assert.equal(parseSupportAttachmentFilename('/api/v1/faq/files/abc.pdf'), null);
|
||||
assert.equal(parseSupportAttachmentFilename('https://evil.test/steal.pdf'), null);
|
||||
});
|
||||
|
||||
test('attachmentContentDisposition forces download', () => {
|
||||
const v = attachmentContentDisposition('guide compte.pdf');
|
||||
assert.match(v, /^attachment;/);
|
||||
assert.match(v, /filename=/);
|
||||
});
|
||||
@ -24,7 +24,18 @@ const RULES: MagicRule[] = [
|
||||
if (byte === 0) return false;
|
||||
if (byte < 9 || (byte > 13 && byte < 32 && byte !== 27)) return false;
|
||||
}
|
||||
return sample.length > 0;
|
||||
if (sample.length === 0) return false;
|
||||
// Reject HTML/SVG-looking payloads that could be served as active content.
|
||||
const text = sample.toString('utf8').trimStart().toLowerCase();
|
||||
if (
|
||||
text.startsWith('<!doctype html') ||
|
||||
text.startsWith('<html') ||
|
||||
text.startsWith('<svg') ||
|
||||
text.includes('<script')
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
},
|
||||
{
|
||||
@ -46,21 +57,55 @@ export function detectMimeFromBuffer(buf: Buffer): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Ensures on-disk upload content matches an allowed MIME (magic bytes, not client header). */
|
||||
export function assertAllowedFileMagic(filePath: string, allowedMimes: Set<string>): void {
|
||||
/** Safe file extension for a detected MIME (never trust the client filename). */
|
||||
export function extensionForMime(mime: string): string {
|
||||
switch (mime) {
|
||||
case 'image/jpeg':
|
||||
return '.jpg';
|
||||
case 'image/png':
|
||||
return '.png';
|
||||
case 'image/gif':
|
||||
return '.gif';
|
||||
case 'image/webp':
|
||||
return '.webp';
|
||||
case 'application/pdf':
|
||||
return '.pdf';
|
||||
case 'text/plain':
|
||||
return '.txt';
|
||||
case 'application/msword':
|
||||
return '.doc';
|
||||
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
|
||||
return '.docx';
|
||||
case 'video/mp4':
|
||||
return '.mp4';
|
||||
case 'video/webm':
|
||||
return '.webm';
|
||||
default:
|
||||
return '.bin';
|
||||
}
|
||||
}
|
||||
|
||||
/** Detect MIME from an on-disk file header. */
|
||||
export function detectMimeFromFile(filePath: string): string | null {
|
||||
const fd = fs.openSync(filePath, 'r');
|
||||
try {
|
||||
const buf = Buffer.alloc(512);
|
||||
const read = fs.readSync(fd, buf, 0, buf.length, 0);
|
||||
const detected = detectMimeFromBuffer(buf.subarray(0, read));
|
||||
if (!detected || !allowedMimes.has(detected)) {
|
||||
throw new Error('File content does not match an allowed type');
|
||||
}
|
||||
return detectMimeFromBuffer(buf.subarray(0, read));
|
||||
} finally {
|
||||
fs.closeSync(fd);
|
||||
}
|
||||
}
|
||||
|
||||
/** Ensures on-disk upload content matches an allowed MIME (magic bytes, not client header). */
|
||||
export function assertAllowedFileMagic(filePath: string, allowedMimes: Set<string>): string {
|
||||
const detected = detectMimeFromFile(filePath);
|
||||
if (!detected || !allowedMimes.has(detected)) {
|
||||
throw new Error('File content does not match an allowed type');
|
||||
}
|
||||
return detected;
|
||||
}
|
||||
|
||||
/** Removes an uploaded file after failed validation. */
|
||||
export function removeUploadedFile(filePath: string): void {
|
||||
try {
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
import { assertAllowedFileMagic } from './fileMagic';
|
||||
import { scanUploadedFile } from '../services/virusScanService';
|
||||
|
||||
/** Validates upload magic bytes and optional ClamAV scan. */
|
||||
export async function validateUploadedFile(filePath: string, allowedMimes: Set<string>): Promise<void> {
|
||||
assertAllowedFileMagic(filePath, allowedMimes);
|
||||
/** Validates upload magic bytes and optional ClamAV scan. Returns detected MIME. */
|
||||
export async function validateUploadedFile(
|
||||
filePath: string,
|
||||
allowedMimes: Set<string>
|
||||
): Promise<string> {
|
||||
const detected = assertAllowedFileMagic(filePath, allowedMimes);
|
||||
await scanUploadedFile(filePath);
|
||||
return detected;
|
||||
}
|
||||
|
||||
@ -94,36 +94,49 @@ Le backend scanne les uploads **FAQ** et **Support** via le protocole **INSTREAM
|
||||
|
||||
### 2.1 Activer le service Docker
|
||||
|
||||
Le service `clamav` est défini dans `docker-compose.yml` avec le profil **`clamav`** (opt-in).
|
||||
Le service `clamav` est défini dans `docker-compose.yml` avec le profil **`clamav`**.
|
||||
|
||||
Les commandes VPS Phase B démarrent **déjà** ClamAV avec la stack :
|
||||
|
||||
```bash
|
||||
# Démarrer ClamAV + mettre à jour le backend
|
||||
docker compose --profile clamav up -d clamav
|
||||
docker compose up -d backend
|
||||
sudo npm run phase-b:up:https # HTTPS + clamav
|
||||
sudo npm run phase-b:up # HTTP + clamav
|
||||
sudo npm run phase-b:up:ssl # SSL + clamav
|
||||
sudo npm run phase-b:init:https # migrate + HTTPS + clamav
|
||||
```
|
||||
|
||||
Pas besoin d’une commande ClamAV séparée.
|
||||
|
||||
Le conteneur `clamav` :
|
||||
|
||||
- Télécharge les signatures virales au premier démarrage (`freshclam`) — **2 à 10 minutes**
|
||||
- Expose le port **3310** uniquement sur le réseau interne `prediqo-net` (pas exposé sur l’hôte)
|
||||
|
||||
### 2.2 Variables d’environnement
|
||||
> Au premier boot, attendez que clamd soit prêt avant de tester les uploads (`docker compose logs -f clamav`).
|
||||
|
||||
Dans le `.env` racine :
|
||||
### 2.2 Variables d’environnement (`.env` VPS)
|
||||
|
||||
À ajouter / vérifier dans le `.env` **racine** du VPS (même fichier que `DATABASE_URL`, `JWT_SECRET`, etc.) :
|
||||
|
||||
```env
|
||||
# ClamAV — scan antivirus des uploads Support / FAQ
|
||||
CLAMAV_ENABLED=true
|
||||
CLAMAV_HOST=clamav
|
||||
CLAMAV_PORT=3310
|
||||
CLAMAV_TIMEOUT_MS=60000
|
||||
```
|
||||
|
||||
`CLAMAV_HOST=clamav` correspond au **nom du service** Docker sur `prediqo-net`.
|
||||
| Variable | Valeur VPS | Pourquoi |
|
||||
|----------|------------|----------|
|
||||
| `CLAMAV_ENABLED` | `true` | Force le scan (sinon déjà on si `NODE_ENV=production`) |
|
||||
| `CLAMAV_HOST` | `clamav` | Nom du **service Docker** sur `prediqo-net` (pas `127.0.0.1`) |
|
||||
| `CLAMAV_PORT` | `3310` | Port clamd interne |
|
||||
| `CLAMAV_TIMEOUT_MS` | `60000` | Délai confortable pendant le 1er `freshclam` |
|
||||
|
||||
Puis redémarrez l’API :
|
||||
Puis un seul redémarrage de stack :
|
||||
|
||||
```bash
|
||||
docker compose up -d backend
|
||||
sudo npm run phase-b:up:https
|
||||
```
|
||||
|
||||
### 2.3 Vérification
|
||||
@ -197,8 +210,8 @@ Le canal sécurité fonctionne dès que Redis et `backend-ws` tournent — indé
|
||||
[ ] VITE_SENTRY_DSN dans .env racine
|
||||
[ ] Rebuild prediqo-web après ajout VITE_SENTRY_DSN
|
||||
[ ] Test issue Sentry backend (5xx)
|
||||
[ ] docker compose --profile clamav up -d clamav
|
||||
[ ] CLAMAV_ENABLED=true, CLAMAV_HOST=clamav
|
||||
[ ] `.env` VPS : CLAMAV_ENABLED=true, CLAMAV_HOST=clamav, CLAMAV_PORT=3310, CLAMAV_TIMEOUT_MS=60000
|
||||
[ ] `sudo npm run phase-b:up:https` (démarre aussi le profil clamav)
|
||||
[ ] Redémarrage backend
|
||||
[ ] Test upload EICAR → 422
|
||||
[ ] npm run db:migrate (si pas fait depuis P4/P5)
|
||||
|
||||
@ -89,7 +89,8 @@ x-backend-common: &backend-common
|
||||
SENTRY_DSN: ${SENTRY_DSN:-}
|
||||
SENTRY_TRACES_SAMPLE_RATE: ${SENTRY_TRACES_SAMPLE_RATE:-0.1}
|
||||
SENTRY_RELEASE: ${SENTRY_RELEASE:-}
|
||||
CLAMAV_ENABLED: ${CLAMAV_ENABLED:-false}
|
||||
# Empty = auto (on in production NODE_ENV). Set true/false to force.
|
||||
CLAMAV_ENABLED: ${CLAMAV_ENABLED:-}
|
||||
CLAMAV_HOST: ${CLAMAV_HOST:-clamav}
|
||||
CLAMAV_PORT: ${CLAMAV_PORT:-3310}
|
||||
CLAMAV_TIMEOUT_MS: ${CLAMAV_TIMEOUT_MS:-30000}
|
||||
|
||||
16
package.json
16
package.json
@ -3,20 +3,20 @@
|
||||
"private": true,
|
||||
"description": "Tloto monorepo — Phase B Docker Compose helpers",
|
||||
"scripts": {
|
||||
"phase-b:init": "docker compose run --rm --no-deps --build backend node dist/db/migrate.js && docker compose --profile http up -d --build",
|
||||
"phase-b:init:https": "docker compose run --rm --no-deps --build backend node dist/db/migrate.js && docker compose --profile https-dns up -d --build",
|
||||
"phase-b:up": "docker compose --profile http up -d --build && docker restart vps_gateway_nginx >/dev/null 2>&1 || true",
|
||||
"phase-b:init": "docker compose run --rm --no-deps --build backend node dist/db/migrate.js && docker compose --profile http --profile clamav up -d --build",
|
||||
"phase-b:init:https": "docker compose run --rm --no-deps --build backend node dist/db/migrate.js && docker compose --profile https-dns --profile clamav up -d --build",
|
||||
"phase-b:up": "docker compose --profile http --profile clamav up -d --build && docker restart vps_gateway_nginx >/dev/null 2>&1 || true",
|
||||
"docker:local:up": "docker compose --env-file .env.docker --profile http up -d --build",
|
||||
"docker:local:init": "docker compose --env-file .env.docker run --rm --no-deps --build backend node dist/db/migrate.js && docker compose --env-file .env.docker --profile http up -d --build",
|
||||
"docker:local:down": "docker compose --env-file .env.docker --profile http down",
|
||||
"docker:local:migrate": "docker compose --env-file .env.docker run --rm --no-deps backend node dist/db/migrate.js",
|
||||
"docker:local:logs": "docker compose --env-file .env.docker logs -f nginx backend backend-engine backend-ws backend-worker-bets redis prediqo-web",
|
||||
"docker:local:status": "docker compose --env-file .env.docker ps",
|
||||
"phase-b:up:https": "docker compose --profile https-dns up -d --build && docker restart vps_gateway_nginx >/dev/null 2>&1 || true",
|
||||
"phase-b:up:ssl": "docker compose --profile ssl up -d --build && docker restart vps_gateway_nginx >/dev/null 2>&1 || true",
|
||||
"phase-b:down": "docker compose down",
|
||||
"phase-b:logs": "docker compose logs -f nginx-https-dns nginx backend backend-engine backend-ws backend-worker-bets redis prediqo-web",
|
||||
"phase-b:restart": "docker compose --profile https-dns restart nginx-https-dns backend backend-engine backend-ws backend-worker-bets prediqo-web",
|
||||
"phase-b:up:https": "docker compose --profile https-dns --profile clamav up -d --build && docker restart vps_gateway_nginx >/dev/null 2>&1 || true",
|
||||
"phase-b:up:ssl": "docker compose --profile ssl --profile clamav up -d --build && docker restart vps_gateway_nginx >/dev/null 2>&1 || true",
|
||||
"phase-b:down": "docker compose --profile clamav down",
|
||||
"phase-b:logs": "docker compose logs -f nginx-https-dns nginx backend backend-engine backend-ws backend-worker-bets redis prediqo-web clamav",
|
||||
"phase-b:restart": "docker compose --profile https-dns --profile clamav restart nginx-https-dns backend backend-engine backend-ws backend-worker-bets prediqo-web clamav",
|
||||
"phase-b:status": "docker compose ps",
|
||||
"gateway:reload": "docker restart vps_gateway_nginx",
|
||||
"monitoring:up": "docker compose --profile monitoring up -d",
|
||||
|
||||
@ -185,4 +185,16 @@ class SupportRepository {
|
||||
final base = _api.baseUrl.replaceAll(RegExp(r'/$'), '');
|
||||
return '$base$relativeUrl';
|
||||
}
|
||||
|
||||
/// Headers so [Image.network] / download requests pass support auth.
|
||||
Future<Map<String, String>> attachmentRequestHeaders() async {
|
||||
final headers = <String, String>{
|
||||
'X-Support-Visitor-Token': await visitorToken(),
|
||||
};
|
||||
final token = await _api.readAccessToken();
|
||||
if (token != null && token.isNotEmpty) {
|
||||
headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
}
|
||||
|
||||
@ -745,10 +745,23 @@ class _MessageBubble extends StatelessWidget {
|
||||
errorBuilder: (_, __, ___) => errorChild,
|
||||
);
|
||||
}
|
||||
return Image.network(
|
||||
repo.attachmentFullUrl(url),
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => errorChild,
|
||||
return FutureBuilder<Map<String, String>>(
|
||||
future: repo.attachmentRequestHeaders(),
|
||||
builder: (context, snap) {
|
||||
if (!snap.hasData) {
|
||||
return const SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
);
|
||||
}
|
||||
return Image.network(
|
||||
repo.attachmentFullUrl(url),
|
||||
headers: snap.data,
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => errorChild,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -177,10 +177,22 @@ export async function adminSupportUnreadCount() {
|
||||
return res.data.unreadCount;
|
||||
}
|
||||
|
||||
/** Absolute support attachment URL with auth query params (needed for <img>/<a>). */
|
||||
export function attachmentFullUrl(relativeUrl: string): string {
|
||||
const base = api.defaults.baseURL ?? '';
|
||||
if (relativeUrl.startsWith('http')) return relativeUrl;
|
||||
return `${base.replace(/\/$/, '')}${relativeUrl}`;
|
||||
const raw = relativeUrl.startsWith('http')
|
||||
? relativeUrl
|
||||
: `${base.replace(/\/$/, '')}${relativeUrl}`;
|
||||
try {
|
||||
const url = new URL(raw, typeof window !== 'undefined' ? window.location.origin : 'http://localhost');
|
||||
const visitor = supportVisitorHeaders()['X-Support-Visitor-Token'];
|
||||
if (visitor) url.searchParams.set('visitorToken', visitor);
|
||||
const accessToken = readAuth().accessToken;
|
||||
if (accessToken) url.searchParams.set('accessToken', accessToken);
|
||||
return url.toString();
|
||||
} catch {
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
const IMAGE_EXT = /\.(jpe?g|png|gif|webp|bmp|heic)$/i;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user