feat(0pt): record legitimate-interest stance — B2B contacts, company-linked only
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.
This commit is contained in:
@@ -22,6 +22,7 @@ import type {
|
||||
PiiIdentityKey,
|
||||
PiiErasureRequest,
|
||||
PiiAccessStatement,
|
||||
ProcessingPurpose,
|
||||
} from "../types/pii";
|
||||
import type { Source } from "../types/source";
|
||||
import { createAud0Client, type Aud0Client, type Aud0Event } from "../clients/aud0-client";
|
||||
@@ -44,6 +45,7 @@ export class PiiLegalBasisError extends Error {}
|
||||
export class PiiMinimisationError extends Error {}
|
||||
export class PiiRetentionError extends Error {}
|
||||
export class PiiRobotsRateError extends Error {}
|
||||
export class PiiEntityLinkageError extends Error {}
|
||||
|
||||
export class PiiGateService {
|
||||
private policies: Map<string, PiiPolicy> = new Map();
|
||||
@@ -80,6 +82,21 @@ export class PiiGateService {
|
||||
throw new Error("field_allowlist must contain at least one field (data minimisation)");
|
||||
}
|
||||
|
||||
// Recorded decision (Simon, 2026-08-31): legitimate interest is accepted for
|
||||
// BUSINESS CONTACTS — personal data only in correspondence with a company/org
|
||||
// entity. Enforce that posture so "B2B-kontakt, företagskopplad" cannot be
|
||||
// bypassed by a lazily-written policy.
|
||||
const purpose: ProcessingPurpose = input.purpose ?? "business_contact";
|
||||
const entityLinkedOnly = input.entity_linked_only ?? input.legal_basis === "legitimate_interest";
|
||||
if (input.legal_basis === "legitimate_interest") {
|
||||
if (purpose !== "business_contact") {
|
||||
throw new Error("legitimate_interest is only accepted with purpose=business_contact (recorded decision 2026-08-31)");
|
||||
}
|
||||
if (!entityLinkedOnly) {
|
||||
throw new Error("legitimate_interest requires entity_linked_only=true — personal data must be bound to a company/org entity");
|
||||
}
|
||||
}
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const policy: PiiPolicy = {
|
||||
source_id: input.source_id,
|
||||
@@ -87,6 +104,8 @@ export class PiiGateService {
|
||||
project_id: input.project_id,
|
||||
touches_personal_data: true,
|
||||
legal_basis: input.legal_basis,
|
||||
purpose,
|
||||
entity_linked_only: entityLinkedOnly,
|
||||
basis_ref: input.basis_ref,
|
||||
retention_days: input.retention_days,
|
||||
field_allowlist: [...input.field_allowlist],
|
||||
@@ -108,7 +127,13 @@ export class PiiGateService {
|
||||
resource_type: "source_pii_policy",
|
||||
resource_id: input.source_id,
|
||||
risk_level: "L3",
|
||||
payload: { legal_basis: input.legal_basis, retention_days: input.retention_days, field_count: input.field_allowlist.length },
|
||||
payload: {
|
||||
legal_basis: input.legal_basis,
|
||||
purpose,
|
||||
entity_linked_only: entityLinkedOnly,
|
||||
retention_days: input.retention_days,
|
||||
field_count: input.field_allowlist.length,
|
||||
},
|
||||
});
|
||||
return policy;
|
||||
}
|
||||
@@ -192,14 +217,26 @@ export class PiiGateService {
|
||||
subjectId: string;
|
||||
field: string;
|
||||
artifactRef?: string;
|
||||
/** Company/org entity the value is bound to (orgnr/rgs/LEI). REQUIRED when the policy is entity_linked_only. */
|
||||
entityId?: string;
|
||||
}): Promise<PiiIdentityKey> {
|
||||
await this.assertCanStore(input.source, [input.field], input.tenantId);
|
||||
const policy = this.policies.get(input.source.source_id)!;
|
||||
|
||||
if (policy.entity_linked_only && !input.entityId) {
|
||||
throw new PiiEntityLinkageError(
|
||||
DECISION_TEMPLATE.replace("%s", input.source.source_id).replace(
|
||||
"%s",
|
||||
"policy requires entity linkage (business contact, recorded decision 2026-08-31) — pass entityId (orgnr/rgs/LEI); personal data may only be stored in correspondence with a company/org entity",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
const key: PiiIdentityKey = {
|
||||
key_hash: hash256(`${sourceIdSalt(input.source.source_id)}:${input.identityKey}`),
|
||||
subject_hash: hash256(input.subjectId),
|
||||
entity_hash: input.entityId ? hash256(normaliseEntity(input.entityId)) : null,
|
||||
source_id: input.source.source_id,
|
||||
tenant_id: input.tenantId,
|
||||
field: input.field,
|
||||
@@ -220,7 +257,12 @@ export class PiiGateService {
|
||||
resource_type: "pii_identity_key",
|
||||
resource_id: key.key_hash,
|
||||
risk_level: "L3",
|
||||
payload: { source_id: input.source.source_id, field: input.field, expires_at: key.expires_at },
|
||||
payload: {
|
||||
source_id: input.source.source_id,
|
||||
field: input.field,
|
||||
expires_at: key.expires_at,
|
||||
entity_linked: key.entity_hash !== null,
|
||||
},
|
||||
});
|
||||
return key;
|
||||
}
|
||||
@@ -318,6 +360,11 @@ function sourceIdSalt(sourceId: string): string {
|
||||
return `0pt-pii-v1:${sourceId}`;
|
||||
}
|
||||
|
||||
/** Normalise an org/rgs nummer or LEI to canonical digits/letters before hashing. */
|
||||
function normaliseEntity(entityId: string): string {
|
||||
return entityId.replace(/[^0-9A-Za-z]/g, "").toUpperCase();
|
||||
}
|
||||
|
||||
/** sha256 hex digest — never the plaintext. */
|
||||
export function hash256(value: string): string {
|
||||
return createHash("sha256").update(value, "utf8").digest("hex");
|
||||
|
||||
Reference in New Issue
Block a user