-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit.test.ts
More file actions
214 lines (200 loc) · 8.49 KB
/
Copy pathaudit.test.ts
File metadata and controls
214 lines (200 loc) · 8.49 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import assert from "node:assert/strict";
import test from "node:test";
import { POST } from "../app/api/audit/route";
import { analyzeDataset } from "../lib/audit-engine";
import { LIMITS } from "../lib/constants";
import { runFixedRegressionSuite } from "../lib/regression-suite";
import {
parseSeededDemoDataset,
SEEDED_DEMO_METADATA,
SEEDED_DEMO_ROWS,
} from "../lib/demo";
import { createAuditReport } from "../lib/report";
import type { AuditReport, DatasetMetadata, DatasetRow } from "../lib/types";
const governed: DatasetMetadata = {
datasetName: "Clean synthetic control",
provenance: "Generated in the unit test with no external source data.",
license: "CC0-1.0",
purpose: "Regression control",
labelColumn: "label",
splitColumn: "split",
idColumn: "id",
};
const cleanRows: DatasetRow[] = Array.from({ length: 10 }, (_, index) => ({
id: `R-${index}`,
feature: `unit_${index} zone_${index + 20} signal_${index + 50}`,
label: index % 2 ? "review" : "clear",
split: index < 8 ? "train" : "test",
}));
test("clean governed control passes all configured detectors", () => {
const result = analyzeDataset(cleanRows, governed);
assert.equal(result.status, "PASS");
assert.deepEqual(result.findings, []);
assert.equal(result.summary.rowCount, 10);
});
test("fixed demo raises every requested detector family", () => {
const result = analyzeDataset(SEEDED_DEMO_ROWS, SEEDED_DEMO_METADATA);
const types = new Set<string>(result.findings.map((finding) => finding.type));
assert.equal(result.status, "BLOCK");
for (const expected of [
"pii",
"exact-duplicate",
"near-duplicate",
"split-leakage",
"conflicting-label",
"class-imbalance",
"missing-provenance",
"missing-license",
]) {
assert.ok(types.has(expected), `missing ${expected}`);
}
const serialized = JSON.stringify(result);
assert.doesNotMatch(serialized, /amira\.student@example\.com|900101-13-5678|192\.0\.2\.44/);
assert.doesNotMatch(serialized, /addresss/);
assert.match(serialized, /Potential email addresses/);
assert.match(serialized, /Potential IPv4 addresses/);
assert.match(serialized, /\[email redacted\]/);
});
test("built-in demo survives browser parsing and request schema validation", async () => {
const parsed = parseSeededDemoDataset();
const expectedSchema = [...parsed.columns].sort();
for (const row of parsed.rows) {
assert.deepEqual(Object.keys(row).sort(), expectedSchema);
}
const response = await POST(new Request("http://localhost/api/audit", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ rows: parsed.rows, metadata: SEEDED_DEMO_METADATA }),
}));
const report = await response.json() as AuditReport;
assert.equal(response.status, 200);
assert.equal(report.status, "BLOCK");
assert.equal(report.summary.rowCount, SEEDED_DEMO_ROWS.length);
assert.equal(report.summary.piiCellCount, 4);
assert.equal(report.summary.exactDuplicatePairs, 1);
assert.equal(report.summary.leakagePairs, 1);
assert.equal(report.summary.conflictingGroups, 1);
});
test("report hashes are reproducible while generation timestamps may differ", async () => {
const first = await createAuditReport(cleanRows, governed);
const second = await createAuditReport(cleanRows, governed);
assert.equal(first.datasetHash, second.datasetHash);
assert.equal(first.configurationHash, second.configurationHash);
assert.equal(first.reportHash, second.reportHash);
assert.match(first.datasetHash, /^[a-f0-9]{64}$/);
assert.match(first.dataCardMarkdown, /not a legal-compliance engine/i);
});
test("operator metadata is inert text in generated Markdown", async () => {
const report = await createAuditReport(cleanRows, {
...governed,
datasetName: ")",
purpose: "[open me](https://attacker.example/purpose)",
provenance: "<img src=x onerror=alert(1)>",
license: "https://attacker.example/license",
});
const markdown = report.dataCardMarkdown;
assert.doesNotMatch(markdown, /!\[[^\]]*\]\([^)]*\)/u);
assert.doesNotMatch(markdown, /\[[^\]]+\]\([^)]*\)/u);
assert.doesNotMatch(markdown, /<(?:img|script)\b/iu);
assert.doesNotMatch(markdown, /(?:javascript|https?):\/\//iu);
assert.match(markdown, /;
});
test("fixed regression suite matches every maintained expected finding", async () => {
const suite = await runFixedRegressionSuite();
assert.equal(suite.id, "dtg-fixed-regression-v1");
assert.equal(suite.suiteVersion, "1.0.0");
assert.match(suite.inputArtifactHash, /^[a-f0-9]{64}$/);
assert.match(suite.goldArtifactHash, /^[a-f0-9]{64}$/);
assert.equal(suite.byCategory.length, 11);
assert.equal(suite.cleanRows, 10);
assert.equal(suite.expectedFindings, 15);
assert.equal(suite.matchedExpectedFindings, 15);
assert.equal(suite.unexpectedFindings, 0);
assert.equal(suite.missedExpectedFindings, 0);
assert.equal(suite.cleanRowsFlagged, 0);
});
test("serialized reports never echo dataset label or split scalar values", async () => {
const labelsAndSplits: DatasetRow[] = [
{
id: "V-1",
feature: "same-feature",
label: "class-owner@example.com",
split: "train-owner@example.com",
},
{
id: "V-2",
feature: "same-feature",
label: "class-owner@example.com",
split: "test-owner@example.com",
},
];
const report = await createAuditReport(labelsAndSplits, governed);
const serialized = JSON.stringify(report);
assert.doesNotMatch(
serialized,
/class-owner@example\.com|train-owner@example\.com|test-owner@example\.com/,
);
assert.deepEqual(report.summary.labelDistribution, [{ category: "class-01", count: 2 }]);
assert.equal(report.summary.leakagePairs, 1);
});
test("numeric Malaysian phone and NRIC-like scalars are scanned without flagging scores", () => {
const result = analyzeDataset(
[{
id: "N-1",
phone: 60123456789,
nric: 900101135678,
score: 0.973,
label: "clear",
split: "train",
}],
governed,
);
const pii = result.findings.filter((item) => item.type === "pii");
const columns = new Set(pii.flatMap((item) => item.evidence.map((entry) => entry.column)));
assert.ok(columns.has("phone"));
assert.ok(columns.has("nric"));
assert.ok(!columns.has("score"));
assert.equal(result.summary.piiCellCount, 2);
});
test("hostile label strings cannot corrupt counts or appear in summaries", () => {
const rows: DatasetRow[] = Array.from({ length: 10 }, (_, index) => ({
id: `H-${index}`,
feature: `hostile_${index}`,
label: index < 8 ? "constructor" : index === 8 ? "__proto__" : "clear",
split: "train",
}));
const result = analyzeDataset(rows, governed);
assert.deepEqual(result.summary.labelDistribution, [
{ category: "class-01", count: 8 },
{ category: "class-02", count: 1 },
{ category: "class-03", count: 1 },
]);
assert.ok(result.findings.some((item) => item.type === "class-imbalance"));
assert.doesNotMatch(JSON.stringify(result.summary), /constructor|__proto__/);
});
test("1,000 high-token rows stay inside deterministic near-duplicate work budgets", () => {
const common = Array.from({ length: 63 }, (_, index) => `c${index.toString(36)}`).join(" ");
const tail = Array.from({ length: 329 }, () => "z").join(" ");
const rows: DatasetRow[] = Array.from({ length: 1_000 }, (_, index) => ({
id: `S-${index}`,
feature: `${common} r${index.toString(36)} ${tail}`,
label: "clear",
split: "train",
}));
const requestBytes = new TextEncoder().encode(JSON.stringify({ rows, metadata: governed })).byteLength;
assert.ok(requestBytes <= LIMITS.requestBytes, `fixture is ${requestBytes} bytes`);
const started = performance.now();
const result = analyzeDataset(rows, governed);
const elapsed = performance.now() - started;
const finding = result.findings.find((item) => item.type === "near-duplicate");
assert.ok(finding);
assert.equal(result.summary.nearDuplicateComparisons, LIMITS.pairComparisons);
assert.equal(result.summary.nearDuplicateComparisonLimit, LIMITS.pairComparisons);
assert.equal(result.summary.nearDuplicatePairs, LIMITS.pairComparisons);
assert.equal(result.summary.nearDuplicatePairScanCapped, true);
assert.equal(result.summary.nearDuplicateTokenLimit, LIMITS.nearDuplicateTokenMatchesPerRow);
assert.equal(result.summary.nearDuplicateRowsAtTokenLimit, 1_000);
assert.equal(finding.evidence.length, 6);
assert.ok(JSON.stringify(result).length < 25_000);
assert.ok(elapsed < 10_000, `budgeted stress scan took ${elapsed.toFixed(0)} ms`);
});