feat(0pt): PII-Gate GDPR-guardrail for personal-data sources (fail-closed)
PiiGateService makes any future personal-data source (ratsit.se and other directory domains) technically GDPR-OK before the first byte is stored: - recorded legal basis (art. 6) with basis_ref evidence, no policy => fail-closed - data minimisation via field allowlist (art. 5(1)(c)) - storage limitation + enforceRetention purge (art. 5(1)(e)) - sha256-only identity keys + log redaction (art. 25/32) - accessStatement / erase subject-rights surfaces (art. 15/17) - robots/rate confirmation required for web+api sources; no stealth/bypass - audit trail via AUD0 at risk L3 (0pt.pii_*) - gate enabled by default in SourceQueryService 10 new tests; full suite 166 pass | 1 skip; typecheck (blocking gate) clean.
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
import { describe, it, expect, vi } from "vitest";
|
||||
import { PiiGateService } from "@/src/services/pii-gate-service";
|
||||
import { SourceService } from "@/src/services/source-service";
|
||||
import { SourceQueryService } from "@/src/services/source-query-service";
|
||||
import type { FabricOneLakeConnectionConfig } from "@/src/adapters/fabric-onelake-adapter";
|
||||
|
||||
/** Minimal config — never reached (the pii gate fires first). */
|
||||
const EMPTY_CFG = {} as FabricOneLakeConnectionConfig;
|
||||
|
||||
describe("PiiGateService — GDPR guardrail (fail-closed)", () => {
|
||||
const ratsit = {
|
||||
source_id: "src_ratsit_1",
|
||||
tenant_id: "t1",
|
||||
project_id: "p1",
|
||||
source_type: "web" as const,
|
||||
name: "ratsit.se",
|
||||
status: "active" as const,
|
||||
created_at: new Date().toISOString(),
|
||||
};
|
||||
const company = {
|
||||
source_id: "src_company_1",
|
||||
tenant_id: "t1",
|
||||
project_id: "p1",
|
||||
source_type: "api" as const,
|
||||
name: "allabolag.se",
|
||||
status: "active" as const,
|
||||
created_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
it("classifies known directory domains as personal-data sources", () => {
|
||||
const gate = new PiiGateService();
|
||||
expect(gate.classifySourcePersonal(ratsit)).toBe(true);
|
||||
expect(gate.classifySourcePersonal({ ...ratsit, name: "www.ratsit.se" })).toBe(true);
|
||||
expect(gate.classifySourcePersonal(company)).toBe(false);
|
||||
expect(gate.classifySourcePersonal({ ...company, touches_personal_data: true })).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects processing a personal-data source with no recorded lawful basis", async () => {
|
||||
const gate = new PiiGateService();
|
||||
await expect(gate.assertCanStore(ratsit, [], "t1")).rejects.toThrowError(/no PiiPolicy/);
|
||||
});
|
||||
|
||||
it("rejects half-legal policies (missing basis_ref, zero retention, empty allowlist)", async () => {
|
||||
const gate = new PiiGateService();
|
||||
await expect(
|
||||
gate.createPiiPolicy({ source_id: "s1", tenant_id: "t1", project_id: "p1", legal_basis: "legitimate_interest", basis_ref: " ", retention_days: 90, field_allowlist: ["address"] }),
|
||||
).rejects.toThrow(/basis_ref is required/);
|
||||
await expect(
|
||||
gate.createPiiPolicy({ source_id: "s1", tenant_id: "t1", project_id: "p1", legal_basis: "consent", basis_ref: "consent-record-1", retention_days: -1, field_allowlist: ["address"] }),
|
||||
).rejects.toThrow(/retention_days/);
|
||||
await expect(
|
||||
gate.createPiiPolicy({ source_id: "s1", tenant_id: "t1", project_id: "p1", legal_basis: "contract", basis_ref: "clause-9", retention_days: 90, field_allowlist: [] }),
|
||||
).rejects.toThrow(/data minimisation/);
|
||||
});
|
||||
|
||||
it("blocks out-of-allowlist fields (minimisation) and unconfirmed web sources", async () => {
|
||||
const gate = new PiiGateService();
|
||||
await gate.createPiiPolicy({
|
||||
source_id: ratsit.source_id, tenant_id: "t1", project_id: "p1",
|
||||
legal_basis: "consent", basis_ref: "consent-record-42",
|
||||
retention_days: 90, field_allowlist: ["name", "address"],
|
||||
});
|
||||
await expect(gate.assertCanStore(ratsit, ["name", "phone"], "t1")).rejects.toThrowError(/outside the allowlist/);
|
||||
await expect(gate.assertCanStore(ratsit, ["name"], "t1")).rejects.toThrowError(/robots.txt \/ rate-limit not confirmed/);
|
||||
|
||||
const ratsitConfirmed = { ...ratsit, source_id: "src_ratsit_confirmed" };
|
||||
await gate.createPiiPolicy({
|
||||
source_id: ratsitConfirmed.source_id, tenant_id: "t1", project_id: "p1",
|
||||
legal_basis: "consent", basis_ref: "consent-record-42",
|
||||
retention_days: 90, field_allowlist: ["name", "address"], robots_and_rate_confirmed: true,
|
||||
});
|
||||
await expect(gate.assertCanStore(ratsitConfirmed, ["name"], "t1")).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("forbids any storage when retention_days = 0", async () => {
|
||||
const gate = new PiiGateService();
|
||||
await gate.createPiiPolicy({
|
||||
source_id: "src_drop_1", tenant_id: "t1", project_id: "p1",
|
||||
legal_basis: "legitimate_interest", basis_ref: "lia-2026-001",
|
||||
retention_days: 0, field_allowlist: ["name"], robots_and_rate_confirmed: true,
|
||||
});
|
||||
await expect(
|
||||
gate.assertCanStore({ ...company, source_id: "src_drop_1", name: "ratsit.se" }, ["name"], "t1"),
|
||||
).rejects.toThrowError(/forbids retention/);
|
||||
});
|
||||
|
||||
it("stores ruled bases and enables access/erasure without plaintext identity", async () => {
|
||||
const gate = new PiiGateService();
|
||||
await gate.createPiiPolicy({
|
||||
source_id: ratsit.source_id, tenant_id: "t1", project_id: "p1",
|
||||
legal_basis: "legal_obligation", basis_ref: "prop-§5",
|
||||
retention_days: 30, field_allowlist: ["street_address"], robots_and_rate_confirmed: true,
|
||||
});
|
||||
const key = await gate.recordStored({
|
||||
source: ratsit, tenantId: "t1",
|
||||
identityKey: "20250101-1234", subjectId: "subj-77", field: "street_address", artifactRef: "st0re/dataset-a",
|
||||
});
|
||||
|
||||
expect(key.key_hash).toMatch(/^[0-9a-f]{64}$/);
|
||||
expect(key.key_hash).not.toContain("20250101");
|
||||
|
||||
const statement = await gate.accessStatement(key.subject_hash);
|
||||
expect(statement.rows).toHaveLength(1);
|
||||
expect(statement.rows[0].field).toBe("street_address");
|
||||
|
||||
const erased = await gate.erase({ subject_hash: key.subject_hash, reason: "deleted_by_subject" });
|
||||
expect(erased).toBe(1);
|
||||
expect((await gate.accessStatement(key.subject_hash)).rows).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("retention worker purges rows past their deadline", async () => {
|
||||
const gate = new PiiGateService();
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-08-01T00:00:00Z"));
|
||||
await gate.createPiiPolicy({
|
||||
source_id: ratsit.source_id, tenant_id: "t1", project_id: "p1",
|
||||
legal_basis: "legitimate_interest", basis_ref: "lia-2026-002",
|
||||
retention_days: 30, field_allowlist: ["street_address"], robots_and_rate_confirmed: true,
|
||||
});
|
||||
await gate.recordStored({ source: ratsit, tenantId: "t1", identityKey: "k-1", subjectId: "s-1", field: "street_address" });
|
||||
|
||||
vi.setSystemTime(new Date("2026-09-15T00:00:00Z"));
|
||||
const { purged } = await gate.enforceRetention(new Date("2026-09-15T00:00:00Z"));
|
||||
vi.useRealTimers();
|
||||
expect(purged).toBe(1);
|
||||
});
|
||||
|
||||
it("redacts personnummer, e-mail and phone from log lines", () => {
|
||||
const gate = new PiiGateService();
|
||||
const out = gate.redact(
|
||||
"subject=202501011234 res=" +
|
||||
'evt=19720101-1234 [email protected] tel=070-123 45 67 sök=ok',
|
||||
);
|
||||
expect(out).not.toMatch(/\d{8}-\d{4}/);
|
||||
expect(out).toContain("[PERSONNUMMER]");
|
||||
expect(out).toContain("[EMAIL]");
|
||||
expect(out).toContain("[TELEFON]");
|
||||
});
|
||||
|
||||
it("SourceQueryService blocks a personal-data source by default (fail-closed)", async () => {
|
||||
const sourceService = new SourceService();
|
||||
await sourceService.createSource({ ...ratsit });
|
||||
|
||||
const service = new SourceQueryService({ sourceService });
|
||||
await expect(
|
||||
service.querySource({
|
||||
source_id: ratsit.source_id,
|
||||
query: "SELECT 1",
|
||||
tenant_id: "t1", project_id: "p1",
|
||||
connection_config: EMPTY_CFG,
|
||||
}),
|
||||
).rejects.toThrowError(/no PiiPolicy/);
|
||||
});
|
||||
|
||||
it("SourceQueryService passes non-personal sources straight through to capability checks", async () => {
|
||||
const sourceService = new SourceService();
|
||||
await sourceService.createSource({
|
||||
source_id: "src_safe_1", tenant_id: "t1", project_id: "p1",
|
||||
source_type: "web", name: "example.org", status: "active",
|
||||
});
|
||||
|
||||
const service = new SourceQueryService({ sourceService });
|
||||
// Gate passes; the sync path then correctly rejects non-database sources.
|
||||
await expect(
|
||||
service.querySource({
|
||||
source_id: "src_safe_1",
|
||||
query: "SELECT 1",
|
||||
tenant_id: "t1", project_id: "p1",
|
||||
connection_config: EMPTY_CFG,
|
||||
}),
|
||||
).rejects.toThrowError(/source_type "web"/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user