Recorded decision (Simon 2026-08-31): personal data may be stored under legitimate interest ONLY as business contacts bound to a company/org entity. Gate enforces it: legitimate_interest requires purpose=business_contact and entity_linked_only=true; recordStored() requires entity_id (orgnr/rgs/LEI, hashed) and refuses person-only rows. +1 test; suite 167 pass | 1 skip; typecheck clean.
201 lines
9.3 KiB
TypeScript
201 lines
9.3 KiB
TypeScript
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("enforces the recorded decision: legitimate_interest = B2B business contact, company-linked only", async () => {
|
|
const gate = new PiiGateService();
|
|
await expect(
|
|
gate.createPiiPolicy({ source_id: "s_li_wrong", tenant_id: "t1", project_id: "p1", legal_basis: "legitimate_interest", basis_ref: "lia-2026-001", retention_days: 90, field_allowlist: ["email"], purpose: "consent_marketing" }),
|
|
).rejects.toThrowError(/only accepted with purpose=business_contact/);
|
|
await expect(
|
|
gate.createPiiPolicy({ source_id: "s_li_wrong2", tenant_id: "t1", project_id: "p1", legal_basis: "legitimate_interest", basis_ref: "lia-2026-001", retention_days: 90, field_allowlist: ["email"], entity_linked_only: false }),
|
|
).rejects.toThrowError(/entity_linked_only=true/);
|
|
|
|
await gate.createPiiPolicy({
|
|
source_id: "s_li_ok", tenant_id: "t1", project_id: "p1",
|
|
legal_basis: "legitimate_interest", basis_ref: "lia-2026-b2b-01",
|
|
retention_days: 365, field_allowlist: ["email", "phone"],
|
|
});
|
|
const p = await gate.getPiiPolicy("s_li_ok");
|
|
expect(p?.purpose).toBe("business_contact");
|
|
expect(p?.entity_linked_only).toBe(true);
|
|
|
|
// Business contact must be bound to a company — stray person-only rows are refused.
|
|
const liSource = { ...company, source_id: "s_li_ok" };
|
|
await expect(
|
|
gate.recordStored({ source: liSource, tenantId: "t1", identityKey: "k", subjectId: "s", field: "email" }),
|
|
).rejects.toThrowError(/entity linkage/);
|
|
const linked = await gate.recordStored({ source: liSource, tenantId: "t1", identityKey: "k", subjectId: "s", field: "email", entityId: "556677-8899" });
|
|
expect(linked.entity_hash).toMatch(/^[0-9a-f]{64}$/);
|
|
expect(linked.entity_hash).not.toContain("556677");
|
|
});
|
|
|
|
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", entityId: "5566778899" });
|
|
|
|
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 mail=simon@siax.io 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"/);
|
|
});
|
|
}); |