File size: 618 Bytes
7fc3c0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { AuditLog } from "../models/AuditLog.js";
import { extractFeatures } from "../security/featureExtractor.js";
import { isAnomaly } from "../security/anomalyEngine.js";
import { emitAdminAlert } from "../ws/adminWs.js";

export async function securityAudit(req, payload) {
  const log = await AuditLog.create({
    ...payload,
    userId: req.user?.id,
    ip: req.ip,
    userAgent: req.headers["user-agent"],
  });

  const features = await extractFeatures(log);
  if (isAnomaly(features)) {
    log.isAnomaly = true;
    await log.save();
    emitAdminAlert({ type: "ML_ANOMALY", action: log.action });
  }
}