Files
EventLens-Public/frontend/src/redact.test.ts
T
2026-11-20 12:20:18 +00:00

133 lines
4.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { redactAnalysis } from "./redact";
import type { Analysis } from "./types";
const source: Analysis = {
source: "Security.evtx",
incidents: [
{
id: "i1",
rule_id: "windows.auth.failed-logon",
start: "2026-09-09T10:00:00Z",
end: "2026-09-09T10:00:01Z",
title: "Failed login",
category: "authentication",
severity: "warning",
reasons: ["Connection from 192.168.1.15"],
evidence_event_ids: ["1"],
original_count: 1,
compressed_count: 1,
},
],
events: [
{
id: "1",
timestamp: "2026-09-09T10:00:00Z",
provider: "Security",
event_id: 4625,
level: "warning",
category: "authentication",
summary: "C:\\Users\\jens\\tool.exe contacted admin@example.com at 192.168.1.15",
computer: "DEMO-PC",
fields: {
"EventData.TargetUserName": "jens",
"EventData.IpAddress": "10.0.0.5",
"EventData.ProcessName": "C:\\Windows\\System32\\cmd.exe",
},
},
],
};
describe("redaction", () => {
it("redacts sensitive event values without mutating the source", () => {
const result = redactAnalysis(source);
const event = result.analysis.events[0];
expect(event.computer).toMatch(/^\[REDACTED_HOST_\d+\]$/);
expect(event.fields?.["EventData.TargetUserName"]).toMatch(/^\[REDACTED_IDENTITY_\d+\]$/);
expect(event.fields?.["EventData.IpAddress"]).toMatch(/^\[REDACTED_IP_\d+\]$/);
expect(event.fields?.["EventData.ProcessName"]).toMatch(/^\[REDACTED_PATH_\d+\]$/);
expect(event.summary).toMatch(/\[REDACTED_EMAIL_\d+\]/);
expect(event.summary).toMatch(/\[REDACTED_IP_\d+\]/);
expect(source.events[0].computer).toBe("DEMO-PC");
expect(result.summary.total).toBeGreaterThanOrEqual(6);
});
it("propagates identities and redacts Windows/UNC paths and IPv6 in free-form text", () => {
const sensitive: Analysis = {
source: "private.evtx",
incidents: [
{
id: "incident-1",
rule_id: "windows.auth.failed-logon",
start: "2026-09-09T10:00:00Z",
end: "2026-09-09T10:00:01Z",
title: "Failed sign-in for jens",
category: "authentication",
severity: "warning",
reasons: ["jens used C:\\Program Files\\SecretTool\\agent.exe from 2001:db8::42"],
evidence_event_ids: ["1"],
original_count: 1,
compressed_count: 1,
},
],
events: [
{
id: "1",
timestamp: "2026-09-09T10:00:00Z",
provider: "Security",
event_id: 4625,
level: "warning",
category: "authentication",
summary:
"Failed sign-in for DOMAIN\\jens at C:\\Program Files\\SecretTool\\agent.exe from 2001:db8::42, fe80::1 and ::ffff:192.0.2.1; SID S-1-5-21-123-456-789-1001; machine BUILD01$; UNC \\\\server\\Share With Space\\agent.exe",
computer: "DEMO-PC",
fields: {
"EventData.TargetUserName": "jens",
"EventData.SubjectUserName": "DOMAIN\\jens",
"EventData.ProcessName": "C:\\Program Files\\SecretTool\\agent.exe",
"EventData.UncPath": "\\\\server\\Share With Space\\agent.exe",
"EventData.MachineAccount": "DOMAIN\\BUILD01$",
"EventData.Sid": "S-1-5-21-123-456-789-1001",
},
},
],
};
const result = redactAnalysis(sensitive);
const serialized = JSON.stringify(result.analysis);
expect(serialized).not.toContain("jens");
expect(serialized).not.toContain("Program Files\\SecretTool");
expect(serialized).not.toContain("Share With Space");
expect(serialized).not.toContain("2001:db8::42");
expect(serialized).not.toContain("fe80::1");
expect(serialized).not.toContain("192.0.2.1");
expect(serialized).not.toContain("BUILD01$");
expect(serialized).not.toContain("S-1-5-21");
expect(result.analysis.incidents[0].title).toMatch(/\[REDACTED_IDENTITY_\d+\]/);
expect(result.summary.paths).toBeGreaterThan(0);
expect(result.summary.ips).toBeGreaterThanOrEqual(3);
});
it("propagates source-field values into free-form redaction", () => {
const sourceField: Analysis = {
source: "private.evtx",
incidents: [],
events: [
{
id: "1",
timestamp: null,
provider: "Security",
event_id: 1,
level: "information",
category: "other",
summary: "Observed source Collector-A",
computer: "",
fields: { "EventData.SourceName": "Collector-A" },
},
],
};
const result = redactAnalysis(sourceField);
expect(result.analysis.events[0].summary).toMatch(/\[REDACTED_SOURCE_\d+\]/);
expect(result.summary.sources).toBeGreaterThan(1);
});
});