File size: 631 Bytes
7fc3c0c
967ce5c
7fc3c0c
 
 
 
 
 
 
 
 
 
 
967ce5c
 
 
7fc3c0c
 
967ce5c
 
 
 
 
 
7fc3c0c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { AuditLog } from "../models/AuditLog.js";
import { extractFeature } 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 value = await extractFeature(log);

  if (isAnomaly(value)) {
    log.isAnomaly = true;
    await log.save();

    emitAdminAlert({
      type: "ANOMALY",
      ip: log.ip,
      value,
    });
  }
}