diff --git a/backend/src/repositories/securityAlertRepository.ts b/backend/src/repositories/securityAlertRepository.ts index f3e530c1..2635f814 100644 --- a/backend/src/repositories/securityAlertRepository.ts +++ b/backend/src/repositories/securityAlertRepository.ts @@ -66,6 +66,25 @@ export async function findById(id: string): Promise { return rows[0] ?? null; } +/** True if an open FIM alert already exists for the same root/path/issue (stops alert storms). */ +export async function hasOpenFileIntegrityAlert(input: { + rootKey: string; + relativePath: string; + issue: string; +}): Promise { + const rows = await query<{ id: string }[]>( + `SELECT id FROM security_alerts + WHERE status = 'open' + AND alert_type = 'file_integrity_violation' + AND metadata->>'rootKey' = $1 + AND metadata->>'relativePath' = $2 + AND metadata->>'issue' = $3 + LIMIT 1`, + [input.rootKey, input.relativePath, input.issue] + ); + return Boolean(rows[0]); +} + export async function updateAlertStatus( id: string, status: SecurityAlertStatus, diff --git a/backend/src/services/fileIntegrityService.ts b/backend/src/services/fileIntegrityService.ts index 7ed8804f..42b22f54 100644 --- a/backend/src/services/fileIntegrityService.ts +++ b/backend/src/services/fileIntegrityService.ts @@ -7,6 +7,7 @@ import crypto from 'crypto'; import fs from 'fs'; import path from 'path'; import * as fileIntegrityRepository from '../repositories/fileIntegrityRepository'; +import * as securityAlertRepository from '../repositories/securityAlertRepository'; import * as auditLogRepository from '../repositories/auditLogRepository'; import { raiseAlert } from './securityService'; import { ensureFaqUploadDir } from '../middleware/faqUpload'; @@ -134,18 +135,32 @@ export async function runFileIntegrityCheck(): Promise { allIssues.push(...suspicious); for (const issue of suspicious) { - await auditLogRepository.create({ - action: 'file_integrity_violation', - resourceType: 'file', - metadata: issue, - }); - await raiseAlert({ - severity: 'high', - alertType: 'file_integrity_violation', - title: `Upload file integrity violation (${issue.issue})`, - details: `${issue.rootKey}/${issue.relativePath} β€” expected baseline mismatch or deletion.`, - metadata: issue, + const alreadyOpen = await securityAlertRepository.hasOpenFileIntegrityAlert({ + rootKey: issue.rootKey, + relativePath: issue.relativePath, + issue: issue.issue, }); + + if (!alreadyOpen) { + await auditLogRepository.create({ + action: 'file_integrity_violation', + resourceType: 'file', + metadata: issue, + }); + await raiseAlert({ + severity: 'high', + alertType: 'file_integrity_violation', + title: `Upload file integrity violation (${issue.issue})`, + details: `${issue.rootKey}/${issue.relativePath} β€” expected baseline mismatch or deletion.`, + metadata: issue, + }); + } + + // File is gone: drop baseline so the 5‑min monitor does not re-alert forever. + // (Common when upload dir is ephemeral Docker storage without a volume.) + if (issue.issue === 'deleted') { + await fileIntegrityRepository.deleteSnapshot(issue.rootKey, issue.relativePath); + } } } diff --git a/docker-compose.yml b/docker-compose.yml index 6b5b22ac..080cb67a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -217,6 +217,9 @@ services: ENGINE_ENABLED: "false" WS_ENABLED: "false" command: ["sh", "-c", "if [ \"${RUN_MIGRATE_ON_START:-0}\" = \"1\" ]; then node dist/db/migrate.js || exit 1; fi; exec node dist/apps/api.js"] + # Persist support/FAQ uploads β€” without this, FIM baselines outlive files and spam "deleted" alerts. + volumes: + - upload_data:/app/data expose: - "4000" healthcheck: @@ -482,5 +485,6 @@ networks: volumes: postgres_data: redis_data: + upload_data: letsencrypt: certbot-www: