dpdp-kit 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/README.md +130 -8
- package/dist/access.d.ts +106 -0
- package/dist/audit.d.ts +3 -1
- package/dist/chunk-5TVA2TKG.js +37 -0
- package/dist/chunk-YOEAZX65.js +937 -0
- package/dist/cli.js +30 -10
- package/dist/config.d.ts +69 -0
- package/dist/data-access.d.ts +6 -2
- package/dist/dpdp.d.ts +146 -0
- package/dist/drizzle.d.ts +195 -5
- package/dist/drizzle.js +99 -7
- package/dist/errors.d.ts +1 -1
- package/dist/express.d.ts +17 -1
- package/dist/express.js +10 -2
- package/dist/fastify.d.ts +41 -0
- package/dist/fastify.js +25 -0
- package/dist/guard.d.ts +19 -0
- package/dist/hono.d.ts +16 -1
- package/dist/hono.js +11 -2
- package/dist/index.d.ts +6 -4
- package/dist/index.js +189 -103
- package/dist/nestjs.d.ts +65 -0
- package/dist/nestjs.js +41 -0
- package/dist/protect.d.ts +74 -0
- package/dist/protect.js +114 -0
- package/dist/retention.d.ts +1 -1
- package/dist/run-audit.d.ts +5 -0
- package/dist/safeguards.d.ts +23 -0
- package/dist/schedule.d.ts +37 -0
- package/dist/schedule.js +66 -0
- package/dist/schema-check.d.ts +5 -7
- package/dist/store.d.ts +8 -1
- package/package.json +35 -4
- package/dist/chunk-FGZAH2NF.js +0 -16
- package/dist/chunk-KVTUMHXO.js +0 -491
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DpdpError,
|
|
3
|
+
SeqConflictError
|
|
4
|
+
} from "./chunk-67Q5GFD6.js";
|
|
5
|
+
|
|
6
|
+
// src/duration.ts
|
|
7
|
+
var PATTERN = /^(\d+)(h|d|y)$/;
|
|
8
|
+
var HOUR_MS = 60 * 60 * 1e3;
|
|
9
|
+
var UNIT_MS = { h: HOUR_MS, d: 24 * HOUR_MS, y: 365 * 24 * HOUR_MS };
|
|
10
|
+
function isDuration(value) {
|
|
11
|
+
return typeof value === "string" && PATTERN.test(value);
|
|
12
|
+
}
|
|
13
|
+
function durationMs(value) {
|
|
14
|
+
const match = PATTERN.exec(value);
|
|
15
|
+
if (!match) throw new TypeError(`Not a duration: ${JSON.stringify(value)} (use e.g. "48h", "30d", "1y")`);
|
|
16
|
+
const [, amount, unit] = match;
|
|
17
|
+
return Number(amount) * UNIT_MS[unit];
|
|
18
|
+
}
|
|
19
|
+
function maxDuration(a, b) {
|
|
20
|
+
return durationMs(b) > durationMs(a) ? b : a;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/records.ts
|
|
24
|
+
var REQUEST_TYPES = ["access", "correction", "erasure", "grievance", "nomination"];
|
|
25
|
+
function isRunnable(task, { now, staleBefore, maxAttempts }) {
|
|
26
|
+
if (task.dueAt > now) return false;
|
|
27
|
+
if (task.status === "pending") return true;
|
|
28
|
+
if (task.status === "failed") return task.attempts < maxAttempts;
|
|
29
|
+
if (task.status === "running") return (task.claimedAt ?? "") < staleBefore;
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/store.ts
|
|
34
|
+
function isDpdpStore(store) {
|
|
35
|
+
const s = store;
|
|
36
|
+
return typeof s.insertRequest === "function" && typeof s.insertTasks === "function";
|
|
37
|
+
}
|
|
38
|
+
function memoryStore() {
|
|
39
|
+
const all = /* @__PURE__ */ new Map();
|
|
40
|
+
const requests = /* @__PURE__ */ new Map();
|
|
41
|
+
const tasks = /* @__PURE__ */ new Map();
|
|
42
|
+
const accessLog = [];
|
|
43
|
+
const byDue = (a, b) => a.dueAt < b.dueAt ? -1 : a.dueAt > b.dueAt ? 1 : 0;
|
|
44
|
+
return {
|
|
45
|
+
all,
|
|
46
|
+
requests,
|
|
47
|
+
tasks,
|
|
48
|
+
accessLog,
|
|
49
|
+
async insertAccess(entries) {
|
|
50
|
+
for (const e of entries) accessLog.push(structuredClone(e));
|
|
51
|
+
},
|
|
52
|
+
async listAccess(filter) {
|
|
53
|
+
const list = accessLog.filter(
|
|
54
|
+
(e) => (!filter.subjectId || e.subjectId === filter.subjectId) && (!filter.actor || e.actor === filter.actor) && (!filter.purpose || e.purpose === filter.purpose) && (!filter.from || e.at >= filter.from) && (!filter.to || e.at < filter.to)
|
|
55
|
+
).sort((a, b) => a.at < b.at ? -1 : a.at > b.at ? 1 : 0).map((e) => structuredClone(e));
|
|
56
|
+
return filter.limit === void 0 ? list : list.slice(0, filter.limit);
|
|
57
|
+
},
|
|
58
|
+
async pruneAccess(before) {
|
|
59
|
+
const keep = accessLog.filter((e) => e.at >= before);
|
|
60
|
+
const deleted = accessLog.length - keep.length;
|
|
61
|
+
accessLog.splice(0, accessLog.length, ...keep);
|
|
62
|
+
return deleted;
|
|
63
|
+
},
|
|
64
|
+
async insertRequest(request) {
|
|
65
|
+
requests.set(request.id, structuredClone(request));
|
|
66
|
+
},
|
|
67
|
+
async getRequest(id) {
|
|
68
|
+
const r = requests.get(id);
|
|
69
|
+
return r && structuredClone(r);
|
|
70
|
+
},
|
|
71
|
+
async updateRequest(request) {
|
|
72
|
+
requests.set(request.id, structuredClone(request));
|
|
73
|
+
},
|
|
74
|
+
async listRequests(filter) {
|
|
75
|
+
return [...requests.values()].filter((r) => (!filter.subjectId || r.subjectId === filter.subjectId) && (!filter.status || r.status === filter.status)).sort((a, b) => a.openedAt < b.openedAt ? -1 : 1).map((r) => structuredClone(r));
|
|
76
|
+
},
|
|
77
|
+
async insertTasks(list) {
|
|
78
|
+
for (const t of list) tasks.set(t.id, structuredClone(t));
|
|
79
|
+
},
|
|
80
|
+
async dueTasks({ limit, ...when }) {
|
|
81
|
+
return [...tasks.values()].filter((t) => isRunnable(t, when)).sort(byDue).slice(0, limit).map((t) => structuredClone(t));
|
|
82
|
+
},
|
|
83
|
+
async claimTask({ id, ...when }) {
|
|
84
|
+
const t = tasks.get(id);
|
|
85
|
+
if (!t || !isRunnable(t, when)) return false;
|
|
86
|
+
tasks.set(id, { ...t, status: "running", claimedAt: when.now });
|
|
87
|
+
return true;
|
|
88
|
+
},
|
|
89
|
+
async updateTask(task) {
|
|
90
|
+
tasks.set(task.id, structuredClone(task));
|
|
91
|
+
},
|
|
92
|
+
async getTask(id) {
|
|
93
|
+
const t = tasks.get(id);
|
|
94
|
+
return t && structuredClone(t);
|
|
95
|
+
},
|
|
96
|
+
async listTasks(filter) {
|
|
97
|
+
return [...tasks.values()].filter((t) => (!filter.subjectId || t.subjectId === filter.subjectId) && (!filter.status || t.status === filter.status)).sort(byDue).map((t) => structuredClone(t));
|
|
98
|
+
},
|
|
99
|
+
async lastEvent(subjectId) {
|
|
100
|
+
const list = all.get(subjectId);
|
|
101
|
+
return list?.[list.length - 1];
|
|
102
|
+
},
|
|
103
|
+
async insert(event) {
|
|
104
|
+
const list = all.get(event.subjectId) ?? [];
|
|
105
|
+
if (list.some((e) => e.seq === event.seq)) throw new SeqConflictError(event.subjectId, event.seq);
|
|
106
|
+
list.push(structuredClone(event));
|
|
107
|
+
list.sort((a, b) => a.seq - b.seq);
|
|
108
|
+
all.set(event.subjectId, list);
|
|
109
|
+
},
|
|
110
|
+
async events(subjectId) {
|
|
111
|
+
return structuredClone(all.get(subjectId) ?? []);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function grantedPurposes(events, until) {
|
|
116
|
+
const granted = /* @__PURE__ */ new Set();
|
|
117
|
+
for (const e of events) {
|
|
118
|
+
if (until !== void 0 && e.at > until) break;
|
|
119
|
+
for (const p of e.purposes) {
|
|
120
|
+
if (e.action === "grant") granted.add(p);
|
|
121
|
+
else granted.delete(p);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return granted;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/access.ts
|
|
128
|
+
import { randomBytes } from "crypto";
|
|
129
|
+
function isAccessLogStore(store) {
|
|
130
|
+
const s = store;
|
|
131
|
+
return typeof s.insertAccess === "function" && typeof s.listAccess === "function" && typeof s.pruneAccess === "function";
|
|
132
|
+
}
|
|
133
|
+
var sequence = 0;
|
|
134
|
+
function accessId(at) {
|
|
135
|
+
sequence = (sequence + 1) % 36 ** 4;
|
|
136
|
+
return `${Date.parse(at).toString(36).padStart(9, "0")}-${sequence.toString(36).padStart(4, "0")}-${randomBytes(6).toString("hex")}`;
|
|
137
|
+
}
|
|
138
|
+
var DEFAULT_ACCESS_LOG_RETENTION = "1y";
|
|
139
|
+
function resolveFields(config, keys) {
|
|
140
|
+
const fields = /* @__PURE__ */ new Set();
|
|
141
|
+
const unknown = [];
|
|
142
|
+
for (const key of keys) {
|
|
143
|
+
const [table, field, ...extra] = key.split(".");
|
|
144
|
+
const def = table ? config.data[table] : void 0;
|
|
145
|
+
if (!def || extra.length > 0) {
|
|
146
|
+
unknown.push(key);
|
|
147
|
+
} else if (field === void 0 || field === "*") {
|
|
148
|
+
for (const f of Object.keys(def.fields)) fields.add(`${table}.${f}`);
|
|
149
|
+
} else if (def.fields[field]) {
|
|
150
|
+
fields.add(key);
|
|
151
|
+
} else {
|
|
152
|
+
unknown.push(key);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return { fields: [...fields], unknown };
|
|
156
|
+
}
|
|
157
|
+
function fieldsOutsidePurpose(config, fields, purpose) {
|
|
158
|
+
return fields.filter((key) => {
|
|
159
|
+
const [table, field] = key.split(".");
|
|
160
|
+
const def = config.data[table]?.fields[field];
|
|
161
|
+
return !def || !def.purposes.includes(purpose);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
async function eachLimited(items, limit, fn) {
|
|
165
|
+
let next = 0;
|
|
166
|
+
const worker = async () => {
|
|
167
|
+
while (next < items.length) await fn(items[next++]);
|
|
168
|
+
};
|
|
169
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
170
|
+
}
|
|
171
|
+
async function reviewAccess(input) {
|
|
172
|
+
const { config, entries } = input;
|
|
173
|
+
const findings = [];
|
|
174
|
+
for (const e of entries) {
|
|
175
|
+
if (e.outOfPurpose && e.outOfPurpose.length > 0) {
|
|
176
|
+
findings.push({
|
|
177
|
+
kind: "out-of-purpose",
|
|
178
|
+
actor: e.actor,
|
|
179
|
+
subjectId: e.subjectId,
|
|
180
|
+
purpose: e.purpose,
|
|
181
|
+
entries: [e.id],
|
|
182
|
+
message: `${e.actor} accessed ${e.outOfPurpose.join(", ")} for "${e.purpose}", which those fields aren't held for.`
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const consentPurposes = new Set(Object.keys(config.purposes).filter((p) => config.purposes[p]?.basis === "consent"));
|
|
187
|
+
const needConsent = entries.filter((e) => consentPurposes.has(e.purpose));
|
|
188
|
+
const histories = /* @__PURE__ */ new Map();
|
|
189
|
+
await eachLimited([...new Set(needConsent.map((e) => e.subjectId))], 16, async (subjectId) => {
|
|
190
|
+
histories.set(subjectId, await input.history(subjectId));
|
|
191
|
+
});
|
|
192
|
+
for (const e of needConsent) {
|
|
193
|
+
if (!grantedPurposes(histories.get(e.subjectId) ?? [], e.at).has(e.purpose)) {
|
|
194
|
+
findings.push({
|
|
195
|
+
kind: "no-consent",
|
|
196
|
+
actor: e.actor,
|
|
197
|
+
subjectId: e.subjectId,
|
|
198
|
+
purpose: e.purpose,
|
|
199
|
+
entries: [e.id],
|
|
200
|
+
message: `${e.actor} accessed data for "${e.purpose}" when the person hadn't consented to it.`
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const windowMs = durationMs(input.window);
|
|
205
|
+
const ignored = new Set(input.ignoreActors ?? []);
|
|
206
|
+
const byActor = /* @__PURE__ */ new Map();
|
|
207
|
+
for (const e of entries) {
|
|
208
|
+
if (ignored.has(e.actor)) continue;
|
|
209
|
+
const list = byActor.get(e.actor);
|
|
210
|
+
if (list) list.push(e);
|
|
211
|
+
else byActor.set(e.actor, [e]);
|
|
212
|
+
}
|
|
213
|
+
for (const [actor, list] of byActor) {
|
|
214
|
+
list.sort((a, b) => a.at < b.at ? -1 : a.at > b.at ? 1 : 0);
|
|
215
|
+
const counts = /* @__PURE__ */ new Map();
|
|
216
|
+
let start = 0;
|
|
217
|
+
let peak = { subjects: 0, from: 0, to: 0 };
|
|
218
|
+
for (let end = 0; end < list.length; end++) {
|
|
219
|
+
const e = list[end];
|
|
220
|
+
counts.set(e.subjectId, (counts.get(e.subjectId) ?? 0) + 1);
|
|
221
|
+
while (Date.parse(e.at) - Date.parse(list[start].at) > windowMs) {
|
|
222
|
+
const s = list[start].subjectId;
|
|
223
|
+
const n = counts.get(s) - 1;
|
|
224
|
+
if (n === 0) counts.delete(s);
|
|
225
|
+
else counts.set(s, n);
|
|
226
|
+
start++;
|
|
227
|
+
}
|
|
228
|
+
if (counts.size > peak.subjects) peak = { subjects: counts.size, from: start, to: end };
|
|
229
|
+
}
|
|
230
|
+
if (peak.subjects > input.maxSubjects) {
|
|
231
|
+
findings.push({
|
|
232
|
+
kind: "bulk-access",
|
|
233
|
+
actor,
|
|
234
|
+
subjects: peak.subjects,
|
|
235
|
+
entries: list.slice(peak.from, peak.to + 1).map((e) => e.id),
|
|
236
|
+
message: `${actor} accessed the data of ${peak.subjects} people within ${input.window} (more than ${input.maxSubjects}).`
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return { from: input.from, to: input.to, scanned: entries.length, findings };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// src/personal.ts
|
|
244
|
+
var rule = (spec, category, confidence = "strong") => ({
|
|
245
|
+
tokens: spec.split(" "),
|
|
246
|
+
category,
|
|
247
|
+
confidence
|
|
248
|
+
});
|
|
249
|
+
var RULES = [
|
|
250
|
+
// device and online identifiers
|
|
251
|
+
rule("ip address", "device"),
|
|
252
|
+
rule("ip", "device"),
|
|
253
|
+
rule("imei", "device"),
|
|
254
|
+
rule("device id", "device"),
|
|
255
|
+
rule("user agent", "device"),
|
|
256
|
+
rule("fcm token", "device"),
|
|
257
|
+
rule("push token", "device"),
|
|
258
|
+
rule("advertising id", "device"),
|
|
259
|
+
rule("gaid", "device"),
|
|
260
|
+
rule("idfa", "device"),
|
|
261
|
+
// Indian and other government identifiers
|
|
262
|
+
rule("aadhaar", "identity"),
|
|
263
|
+
rule("aadhar", "identity"),
|
|
264
|
+
rule("uidai", "identity"),
|
|
265
|
+
rule("pan", "identity"),
|
|
266
|
+
rule("passport", "identity"),
|
|
267
|
+
rule("voter id", "identity"),
|
|
268
|
+
rule("epic", "identity"),
|
|
269
|
+
rule("driving licence", "identity"),
|
|
270
|
+
rule("driving license", "identity"),
|
|
271
|
+
rule("abha", "health"),
|
|
272
|
+
// dates of birth and names
|
|
273
|
+
rule("date of birth", "identity"),
|
|
274
|
+
rule("birth date", "identity"),
|
|
275
|
+
rule("birthdate", "identity"),
|
|
276
|
+
rule("birthday", "identity"),
|
|
277
|
+
rule("dob", "identity"),
|
|
278
|
+
rule("first name", "identity"),
|
|
279
|
+
rule("last name", "identity"),
|
|
280
|
+
rule("middle name", "identity"),
|
|
281
|
+
rule("full name", "identity"),
|
|
282
|
+
rule("surname", "identity"),
|
|
283
|
+
rule("father name", "identity"),
|
|
284
|
+
rule("mother name", "identity"),
|
|
285
|
+
rule("guardian name", "identity"),
|
|
286
|
+
rule("parent name", "identity"),
|
|
287
|
+
// contact
|
|
288
|
+
rule("phone", "contact"),
|
|
289
|
+
rule("mobile", "contact"),
|
|
290
|
+
rule("msisdn", "contact"),
|
|
291
|
+
rule("whatsapp", "contact"),
|
|
292
|
+
rule("email", "contact"),
|
|
293
|
+
rule("address", "contact"),
|
|
294
|
+
rule("pincode", "contact"),
|
|
295
|
+
rule("pin code", "contact"),
|
|
296
|
+
rule("postcode", "contact"),
|
|
297
|
+
rule("postal code", "contact"),
|
|
298
|
+
rule("zipcode", "contact"),
|
|
299
|
+
rule("zip code", "contact"),
|
|
300
|
+
// financial
|
|
301
|
+
rule("account number", "financial"),
|
|
302
|
+
rule("bank account", "financial"),
|
|
303
|
+
rule("ifsc", "financial"),
|
|
304
|
+
rule("upi", "financial"),
|
|
305
|
+
rule("vpa", "financial"),
|
|
306
|
+
rule("card number", "financial"),
|
|
307
|
+
rule("iban", "financial"),
|
|
308
|
+
rule("salary", "financial"),
|
|
309
|
+
rule("income", "financial"),
|
|
310
|
+
// health and biometric
|
|
311
|
+
rule("blood group", "health"),
|
|
312
|
+
rule("diagnosis", "health"),
|
|
313
|
+
rule("medical", "health"),
|
|
314
|
+
rule("allergy", "health"),
|
|
315
|
+
rule("allergies", "health"),
|
|
316
|
+
rule("prescription", "health"),
|
|
317
|
+
rule("fingerprint", "biometric"),
|
|
318
|
+
rule("biometric", "biometric"),
|
|
319
|
+
rule("face embedding", "biometric"),
|
|
320
|
+
// location
|
|
321
|
+
rule("latitude", "location"),
|
|
322
|
+
rule("longitude", "location"),
|
|
323
|
+
rule("lat", "location"),
|
|
324
|
+
rule("lng", "location"),
|
|
325
|
+
rule("gps", "location"),
|
|
326
|
+
// weak: personal in a people table, not in a products table
|
|
327
|
+
rule("name", "identity", "weak"),
|
|
328
|
+
rule("gender", "identity", "weak"),
|
|
329
|
+
rule("age", "identity", "weak"),
|
|
330
|
+
rule("photo", "identity", "weak"),
|
|
331
|
+
rule("avatar", "identity", "weak"),
|
|
332
|
+
rule("selfie", "biometric", "weak"),
|
|
333
|
+
rule("signature", "identity", "weak"),
|
|
334
|
+
rule("city", "location", "weak"),
|
|
335
|
+
rule("street", "contact", "weak"),
|
|
336
|
+
rule("location", "location", "weak")
|
|
337
|
+
];
|
|
338
|
+
var DESCRIPTIVE_ENDINGS = /* @__PURE__ */ new Set([
|
|
339
|
+
"verified",
|
|
340
|
+
"verification",
|
|
341
|
+
"confirmed",
|
|
342
|
+
"enabled",
|
|
343
|
+
"disabled",
|
|
344
|
+
"count",
|
|
345
|
+
"type",
|
|
346
|
+
"status",
|
|
347
|
+
"kind",
|
|
348
|
+
"flag",
|
|
349
|
+
"visible",
|
|
350
|
+
"public",
|
|
351
|
+
"at",
|
|
352
|
+
"sent",
|
|
353
|
+
"in",
|
|
354
|
+
"out",
|
|
355
|
+
"allowed",
|
|
356
|
+
"required"
|
|
357
|
+
]);
|
|
358
|
+
var ORGANISATION_WORDS = /* @__PURE__ */ new Set([
|
|
359
|
+
"company",
|
|
360
|
+
"business",
|
|
361
|
+
"org",
|
|
362
|
+
"organisation",
|
|
363
|
+
"organization",
|
|
364
|
+
"merchant",
|
|
365
|
+
"vendor",
|
|
366
|
+
"supplier",
|
|
367
|
+
"firm",
|
|
368
|
+
"branch",
|
|
369
|
+
"office",
|
|
370
|
+
"school",
|
|
371
|
+
"institute",
|
|
372
|
+
"college"
|
|
373
|
+
]);
|
|
374
|
+
function tokenize(name) {
|
|
375
|
+
return name.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
376
|
+
}
|
|
377
|
+
function containsRun(tokens, run) {
|
|
378
|
+
outer: for (let i = 0; i + run.length <= tokens.length; i++) {
|
|
379
|
+
for (let j = 0; j < run.length; j++) if (tokens[i + j] !== run[j]) continue outer;
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
function guessPersonal(columnName) {
|
|
385
|
+
const tokens = tokenize(columnName);
|
|
386
|
+
if (tokens.length === 0) return void 0;
|
|
387
|
+
const last = tokens[tokens.length - 1];
|
|
388
|
+
if (tokens.length > 1 && DESCRIPTIVE_ENDINGS.has(last)) return void 0;
|
|
389
|
+
const organisational = tokens.some((t) => ORGANISATION_WORDS.has(t));
|
|
390
|
+
for (const r of RULES) {
|
|
391
|
+
if (containsRun(tokens, r.tokens)) {
|
|
392
|
+
return {
|
|
393
|
+
category: r.category,
|
|
394
|
+
confidence: organisational ? "weak" : r.confidence,
|
|
395
|
+
matched: r.tokens.join(" ")
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return void 0;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// src/audit.ts
|
|
403
|
+
var ONE_YEAR_MS = durationMs("1y");
|
|
404
|
+
function checkRetention(retain, path, issues) {
|
|
405
|
+
if (retain === "while-active" || isDuration(retain)) return;
|
|
406
|
+
if (retain && typeof retain === "object" && "afterWithdrawal" in retain) {
|
|
407
|
+
if (!isDuration(retain.afterWithdrawal)) {
|
|
408
|
+
issues.push({
|
|
409
|
+
level: "error",
|
|
410
|
+
code: "invalid-duration",
|
|
411
|
+
path: `${path}.afterWithdrawal`,
|
|
412
|
+
message: `${JSON.stringify(retain.afterWithdrawal)} is not a duration. Use e.g. "0d", "30d", "1y".`
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
issues.push({
|
|
418
|
+
level: "error",
|
|
419
|
+
code: "invalid-retention",
|
|
420
|
+
path,
|
|
421
|
+
message: `Retention must be "while-active", a duration like "1y", or { afterWithdrawal: "30d" }.`
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
function checkPurposeList(purposes, known, used, path, issues) {
|
|
425
|
+
if (!purposes || purposes.length === 0) {
|
|
426
|
+
issues.push({
|
|
427
|
+
level: "error",
|
|
428
|
+
code: "field-without-purpose",
|
|
429
|
+
path: `${path}.purposes`,
|
|
430
|
+
message: "List at least one purpose. Personal data with no purpose must not be collected."
|
|
431
|
+
});
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
for (const p of purposes) {
|
|
435
|
+
used.add(p);
|
|
436
|
+
if (!known.has(p)) {
|
|
437
|
+
issues.push({
|
|
438
|
+
level: "error",
|
|
439
|
+
code: "unknown-purpose",
|
|
440
|
+
path: `${path}.purposes`,
|
|
441
|
+
message: `Unknown purpose ${JSON.stringify(p)}. Declare it under \`purposes\` or fix the spelling.`
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
function audit(config) {
|
|
447
|
+
const issues = [];
|
|
448
|
+
const purposes = config.purposes ?? {};
|
|
449
|
+
const known = new Set(Object.keys(purposes));
|
|
450
|
+
const used = /* @__PURE__ */ new Set();
|
|
451
|
+
if (!config.fiduciary?.name) {
|
|
452
|
+
issues.push({
|
|
453
|
+
level: "error",
|
|
454
|
+
code: "fiduciary-missing",
|
|
455
|
+
path: "fiduciary.name",
|
|
456
|
+
message: "Name the Data Fiduciary (your organisation). It appears on every notice."
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
if (!config.fiduciary?.grievanceUrl) {
|
|
460
|
+
issues.push({
|
|
461
|
+
level: "warning",
|
|
462
|
+
code: "grievance-missing",
|
|
463
|
+
path: "fiduciary.grievanceUrl",
|
|
464
|
+
message: "Add where people can raise a grievance. Section 8(10) requires a grievance mechanism, and Section 5(1)(ii) requires the notice to say how to use it."
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
if (known.size === 0) {
|
|
468
|
+
issues.push({ level: "error", code: "no-purposes", path: "purposes", message: "Declare at least one purpose." });
|
|
469
|
+
}
|
|
470
|
+
for (const [name, def] of Object.entries(purposes)) {
|
|
471
|
+
const path = `purposes.${name}`;
|
|
472
|
+
if (def.basis !== "consent" && def.basis !== "legitimate-use") {
|
|
473
|
+
issues.push({
|
|
474
|
+
level: "error",
|
|
475
|
+
code: "invalid-basis",
|
|
476
|
+
path: `${path}.basis`,
|
|
477
|
+
message: `Basis must be "consent" or "legitimate-use".`
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
checkRetention(def.retain, `${path}.retain`, issues);
|
|
481
|
+
if (def.basis === "legitimate-use" && typeof def.retain === "object") {
|
|
482
|
+
issues.push({
|
|
483
|
+
level: "error",
|
|
484
|
+
code: "invalid-retention",
|
|
485
|
+
path: `${path}.retain`,
|
|
486
|
+
message: 'afterWithdrawal only applies to consent purposes (Section 6(4)). Use "while-active" or a duration for a legitimate use.'
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
for (const [key, table] of Object.entries(config.data ?? {})) {
|
|
491
|
+
const path = `data.${key}`;
|
|
492
|
+
if (!table.subject) {
|
|
493
|
+
issues.push({
|
|
494
|
+
level: "error",
|
|
495
|
+
code: "missing-subject-column",
|
|
496
|
+
path: `${path}.subject`,
|
|
497
|
+
message: "Name the column that holds the person's id, so their rows can be found."
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
const fields = Object.entries(table.fields ?? {});
|
|
501
|
+
if (fields.length === 0) {
|
|
502
|
+
issues.push({
|
|
503
|
+
level: "warning",
|
|
504
|
+
code: "table-without-fields",
|
|
505
|
+
path: `${path}.fields`,
|
|
506
|
+
message: "No personal fields listed. Remove the table from the map, or list its fields."
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
for (const [field, def] of fields) {
|
|
510
|
+
checkPurposeList(def.purposes, known, used, `${path}.fields.${field}`, issues);
|
|
511
|
+
}
|
|
512
|
+
if (table.legalHold && !isDuration(table.legalHold.keep)) {
|
|
513
|
+
issues.push({
|
|
514
|
+
level: "error",
|
|
515
|
+
code: "invalid-duration",
|
|
516
|
+
path: `${path}.legalHold.keep`,
|
|
517
|
+
message: `${JSON.stringify(table.legalHold.keep)} is not a duration. Use e.g. "8y".`
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
for (const [name, processor] of Object.entries(config.processors ?? {})) {
|
|
522
|
+
checkPurposeList(processor.purposes, known, used, `processors.${name}`, issues);
|
|
523
|
+
}
|
|
524
|
+
for (const name of known) {
|
|
525
|
+
if (!used.has(name)) {
|
|
526
|
+
issues.push({
|
|
527
|
+
level: "warning",
|
|
528
|
+
code: "unused-purpose",
|
|
529
|
+
path: `purposes.${name}`,
|
|
530
|
+
message: "No field or processor uses this purpose. Remove it, or map the data it covers."
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
for (const entry of config.notPersonal ?? []) {
|
|
535
|
+
if (!/^[^.\s]+(\.[^.\s]+)*\.([^.\s]+|\*)$/.test(entry)) {
|
|
536
|
+
issues.push({
|
|
537
|
+
level: "error",
|
|
538
|
+
code: "invalid-not-personal",
|
|
539
|
+
path: "notPersonal",
|
|
540
|
+
message: `${JSON.stringify(entry)} should be "table.column" or "table.*".`
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
const within = config.rights?.responseWithin;
|
|
545
|
+
if (within !== void 0) {
|
|
546
|
+
const entries = typeof within === "string" ? [["", within]] : Object.entries(within);
|
|
547
|
+
for (const [type, value] of entries) {
|
|
548
|
+
const path = type ? `rights.responseWithin.${type}` : "rights.responseWithin";
|
|
549
|
+
if (type && !REQUEST_TYPES.includes(type)) {
|
|
550
|
+
issues.push({
|
|
551
|
+
level: "error",
|
|
552
|
+
code: "invalid-request-type",
|
|
553
|
+
path,
|
|
554
|
+
message: `Unknown request type. Use one of: ${REQUEST_TYPES.join(", ")}.`
|
|
555
|
+
});
|
|
556
|
+
} else if (!isDuration(value)) {
|
|
557
|
+
issues.push({ level: "error", code: "invalid-duration", path, message: `${JSON.stringify(value)} is not a duration. Use e.g. "30d".` });
|
|
558
|
+
} else if ((type === "" || type === "grievance") && durationMs(value) > durationMs("90d")) {
|
|
559
|
+
issues.push({
|
|
560
|
+
level: "error",
|
|
561
|
+
code: "grievance-response-too-long",
|
|
562
|
+
path,
|
|
563
|
+
message: 'Rule 14(3) of the DPDP Rules requires answering grievances within ninety days. Use at most "90d".'
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
const minimum = config.minimumRetention;
|
|
569
|
+
if (minimum === void 0) {
|
|
570
|
+
issues.push({
|
|
571
|
+
level: "warning",
|
|
572
|
+
code: "minimum-retention-missing",
|
|
573
|
+
path: "minimumRetention",
|
|
574
|
+
message: 'Set minimumRetention. Rule 8(3) of the DPDP Rules requires keeping personal data, traffic data and logs of each processing for at least one year from the processing ("1y").'
|
|
575
|
+
});
|
|
576
|
+
} else if (!isDuration(minimum)) {
|
|
577
|
+
issues.push({
|
|
578
|
+
level: "error",
|
|
579
|
+
code: "invalid-duration",
|
|
580
|
+
path: "minimumRetention",
|
|
581
|
+
message: `${JSON.stringify(minimum)} is not a duration. Use e.g. "1y".`
|
|
582
|
+
});
|
|
583
|
+
} else if (durationMs(minimum) < ONE_YEAR_MS) {
|
|
584
|
+
issues.push({
|
|
585
|
+
level: "error",
|
|
586
|
+
code: "minimum-retention-too-short",
|
|
587
|
+
path: "minimumRetention",
|
|
588
|
+
message: 'Rule 8(3) of the DPDP Rules requires keeping personal data, traffic data and logs of each processing for at least one year. Use at least "1y".'
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
checkSecurity(config, issues);
|
|
592
|
+
checkErase(config, issues);
|
|
593
|
+
return issues;
|
|
594
|
+
}
|
|
595
|
+
function checkErase(config, issues) {
|
|
596
|
+
for (const [key, table] of Object.entries(config.data ?? {})) {
|
|
597
|
+
for (const [field, def] of Object.entries(table.fields ?? {})) {
|
|
598
|
+
const erase = def.erase;
|
|
599
|
+
if (erase === void 0 || erase === "null" || erase === "redact") continue;
|
|
600
|
+
if (erase && typeof erase === "object" && "value" in erase) {
|
|
601
|
+
const value = erase.value;
|
|
602
|
+
if (value === null || ["string", "number", "boolean", "function"].includes(typeof value)) continue;
|
|
603
|
+
}
|
|
604
|
+
issues.push({
|
|
605
|
+
level: "error",
|
|
606
|
+
code: "invalid-erase",
|
|
607
|
+
path: `data.${key}.fields.${field}.erase`,
|
|
608
|
+
message: 'erase must be "null", "redact", or { value } with a string, number, boolean, null, or a function returning one.'
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
var HIGH_RISK_MATCHES = /* @__PURE__ */ new Set([
|
|
614
|
+
"aadhaar",
|
|
615
|
+
"aadhar",
|
|
616
|
+
"uidai",
|
|
617
|
+
"pan",
|
|
618
|
+
"passport",
|
|
619
|
+
"voter id",
|
|
620
|
+
"epic",
|
|
621
|
+
"driving licence",
|
|
622
|
+
"driving license",
|
|
623
|
+
"abha",
|
|
624
|
+
"account number",
|
|
625
|
+
"bank account",
|
|
626
|
+
"card number",
|
|
627
|
+
"iban"
|
|
628
|
+
]);
|
|
629
|
+
var HIGH_RISK_CATEGORIES = /* @__PURE__ */ new Set(["health", "biometric"]);
|
|
630
|
+
function isHighRisk(field, category) {
|
|
631
|
+
if (HIGH_RISK_CATEGORIES.has(category)) return true;
|
|
632
|
+
const guess = guessPersonal(field);
|
|
633
|
+
return guess !== void 0 && guess.confidence === "strong" && (HIGH_RISK_MATCHES.has(guess.matched) || HIGH_RISK_CATEGORIES.has(guess.category));
|
|
634
|
+
}
|
|
635
|
+
var SECURITY_MEASURES = [
|
|
636
|
+
["accessControl", "(b)", "how access to your computer resources is controlled (for example SSO and least-privilege roles)"],
|
|
637
|
+
["backups", "(d)", "how processing continues after a compromise or loss of data (for example tested backups)"],
|
|
638
|
+
["measures", "(g)", "the technical and organisational measures that make the safeguards work (for example training and access reviews)"]
|
|
639
|
+
];
|
|
640
|
+
function checkSecurity(config, issues) {
|
|
641
|
+
for (const [key, table] of Object.entries(config.data ?? {})) {
|
|
642
|
+
for (const [field, def] of Object.entries(table.fields ?? {})) {
|
|
643
|
+
if (!def.protect && isHighRisk(field, def.category)) {
|
|
644
|
+
issues.push({
|
|
645
|
+
level: "warning",
|
|
646
|
+
code: "unprotected-high-risk-field",
|
|
647
|
+
path: `data.${key}.fields.${field}.protect`,
|
|
648
|
+
message: `${field} looks like high-risk personal data. Rule 6(1)(a) lists encryption, obfuscation, masking and virtual tokens as safeguards: store it protected (see dpdp-kit/protect) and say how with protect: "encrypted" | "tokenised" | "masked" | "hashed".`
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
for (const [name, def] of Object.entries(config.processors ?? {})) {
|
|
654
|
+
if (!def.contract) {
|
|
655
|
+
issues.push({
|
|
656
|
+
level: "warning",
|
|
657
|
+
code: "processor-contract-missing",
|
|
658
|
+
path: `processors.${name}.contract`,
|
|
659
|
+
message: `Rule 6(1)(f) requires a provision for reasonable security safeguards in your contract with each Data Processor. Record where it is, e.g. contract: "DPA signed 2026-03-01, clause 7".`
|
|
660
|
+
});
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
for (const [key, clause, what] of SECURITY_MEASURES) {
|
|
664
|
+
if (!config.security?.[key]) {
|
|
665
|
+
issues.push({
|
|
666
|
+
level: "warning",
|
|
667
|
+
code: "security-measure-missing",
|
|
668
|
+
path: `security.${key}`,
|
|
669
|
+
message: `Rule 6(1)${clause} requires ${what}. Describe what you do in a sentence, as evidence.`
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
const keep = config.security?.accessLogRetention;
|
|
674
|
+
if (keep !== void 0 && !isDuration(keep)) {
|
|
675
|
+
issues.push({
|
|
676
|
+
level: "error",
|
|
677
|
+
code: "invalid-duration",
|
|
678
|
+
path: "security.accessLogRetention",
|
|
679
|
+
message: `${JSON.stringify(keep)} is not a duration. Use e.g. "1y".`
|
|
680
|
+
});
|
|
681
|
+
} else if (keep !== void 0 && durationMs(keep) < ONE_YEAR_MS) {
|
|
682
|
+
issues.push({
|
|
683
|
+
level: "error",
|
|
684
|
+
code: "access-log-retention-too-short",
|
|
685
|
+
path: "security.accessLogRetention",
|
|
686
|
+
message: 'Rule 6(1)(e) of the DPDP Rules requires keeping access logs for one year to detect and investigate unauthorised access. Use at least "1y".'
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
function hasErrors(issues) {
|
|
691
|
+
return issues.some((i) => i.level === "error");
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// src/config.ts
|
|
695
|
+
var REDACTED = "[erased]";
|
|
696
|
+
function defineConfig(config) {
|
|
697
|
+
return config;
|
|
698
|
+
}
|
|
699
|
+
function purposeNames(config) {
|
|
700
|
+
return Object.keys(config.purposes);
|
|
701
|
+
}
|
|
702
|
+
function erasedValue(erase, field) {
|
|
703
|
+
if (erase === void 0 || erase === "null") return null;
|
|
704
|
+
if (erase === "redact") return REDACTED;
|
|
705
|
+
const value = typeof erase.value === "function" ? erase.value(field) : erase.value;
|
|
706
|
+
if (value !== null && !["string", "number", "boolean"].includes(typeof value)) {
|
|
707
|
+
throw new DpdpError(
|
|
708
|
+
"invalid-erase",
|
|
709
|
+
`The erase value for ${field.table}.${field.field} must be a string, number, boolean or null; got ${value === void 0 ? "undefined (does the function return?)" : typeof value}`
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
return value;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// src/schema-check.ts
|
|
716
|
+
var OWN_TABLE_PREFIX = "dpdp_";
|
|
717
|
+
var matches = (column, name) => column.name === name || column.key === name;
|
|
718
|
+
function parseNotPersonal(entries) {
|
|
719
|
+
const columns = /* @__PURE__ */ new Set();
|
|
720
|
+
const tables = /* @__PURE__ */ new Set();
|
|
721
|
+
for (const entry of entries ?? []) {
|
|
722
|
+
const dot = entry.lastIndexOf(".");
|
|
723
|
+
if (dot <= 0) continue;
|
|
724
|
+
const table = entry.slice(0, dot);
|
|
725
|
+
const column = entry.slice(dot + 1);
|
|
726
|
+
if (column === "*") tables.add(table);
|
|
727
|
+
else columns.add(`${table}.${column}`);
|
|
728
|
+
}
|
|
729
|
+
return { columns, tables };
|
|
730
|
+
}
|
|
731
|
+
function strongest(a, b) {
|
|
732
|
+
if (!a) return b;
|
|
733
|
+
if (!b) return a;
|
|
734
|
+
return b.confidence === "strong" && a.confidence === "weak" ? b : a;
|
|
735
|
+
}
|
|
736
|
+
function checkEraseAgainstColumn(erase, column, path, where, issues) {
|
|
737
|
+
const fixed = erase === void 0 || erase === "null" ? null : erase === "redact" ? REDACTED : typeof erase.value === "function" ? void 0 : erase.value;
|
|
738
|
+
if (fixed === void 0) return;
|
|
739
|
+
if (fixed === null) {
|
|
740
|
+
if (column.notNull) {
|
|
741
|
+
issues.push({
|
|
742
|
+
level: "error",
|
|
743
|
+
code: "erase-not-null",
|
|
744
|
+
path,
|
|
745
|
+
message: `${where} is NOT NULL, so erasing it to NULL will fail. Set erase: "redact" for text, or erase: { value: \u2026 } with something the column accepts.`
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
const kind = typeof fixed;
|
|
751
|
+
if (column.type && column.type !== "other" && column.type !== kind) {
|
|
752
|
+
issues.push({
|
|
753
|
+
level: "error",
|
|
754
|
+
code: "erase-type-mismatch",
|
|
755
|
+
path,
|
|
756
|
+
message: `${where} holds ${column.type} values, but erase writes a ${kind} (${JSON.stringify(fixed)}). Use erase: { value: \u2026 } with a ${column.type}.`
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
function checkSchema(config, schema) {
|
|
761
|
+
const issues = [];
|
|
762
|
+
const byName = /* @__PURE__ */ new Map();
|
|
763
|
+
for (const table of schema.tables) if (!byName.has(table.name)) byName.set(table.name, table);
|
|
764
|
+
const mapped = /* @__PURE__ */ new Map();
|
|
765
|
+
for (const [key, def] of Object.entries(config.data ?? {})) {
|
|
766
|
+
const table = byName.get(def.table);
|
|
767
|
+
if (!table) {
|
|
768
|
+
issues.push({
|
|
769
|
+
level: "error",
|
|
770
|
+
code: "unknown-table",
|
|
771
|
+
path: `data.${key}.table`,
|
|
772
|
+
message: `Table "${def.table}" isn't in the schema. Fix the name, or remove it from the data map if the table is gone.`
|
|
773
|
+
});
|
|
774
|
+
continue;
|
|
775
|
+
}
|
|
776
|
+
const list = mapped.get(table.name) ?? [];
|
|
777
|
+
const subject = table.columns.find((c) => matches(c, def.subject));
|
|
778
|
+
if (subject) list.push(subject);
|
|
779
|
+
else {
|
|
780
|
+
issues.push({
|
|
781
|
+
level: "error",
|
|
782
|
+
code: "unknown-column",
|
|
783
|
+
path: `data.${key}.subject`,
|
|
784
|
+
message: `Column "${def.subject}" isn't in table "${table.name}". The subject column must exist to find a person's rows.`
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
for (const [field, fieldDef] of Object.entries(def.fields ?? {})) {
|
|
788
|
+
const column = table.columns.find((c) => matches(c, field));
|
|
789
|
+
if (column) {
|
|
790
|
+
list.push(column);
|
|
791
|
+
checkEraseAgainstColumn(fieldDef.erase, column, `data.${key}.fields.${field}.erase`, `${table.name}.${column.name}`, issues);
|
|
792
|
+
} else {
|
|
793
|
+
issues.push({
|
|
794
|
+
level: "error",
|
|
795
|
+
code: "unknown-column",
|
|
796
|
+
path: `data.${key}.fields.${field}`,
|
|
797
|
+
message: `Column "${field}" isn't in table "${table.name}". Fix the name, or remove it if the column was dropped.`
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
mapped.set(table.name, list);
|
|
802
|
+
}
|
|
803
|
+
const ignore = parseNotPersonal(config.notPersonal);
|
|
804
|
+
for (const entry of config.notPersonal ?? []) {
|
|
805
|
+
const dot = entry.lastIndexOf(".");
|
|
806
|
+
const tableName = entry.slice(0, Math.max(dot, 0));
|
|
807
|
+
const columnName = entry.slice(dot + 1);
|
|
808
|
+
const table = byName.get(tableName);
|
|
809
|
+
const exists = table && (columnName === "*" || table.columns.some((c) => matches(c, columnName)));
|
|
810
|
+
if (!exists) {
|
|
811
|
+
issues.push({
|
|
812
|
+
level: "warning",
|
|
813
|
+
code: "stale-not-personal",
|
|
814
|
+
path: "notPersonal",
|
|
815
|
+
message: `"${entry}" doesn't match any table or column. Use "table.column" or "table.*", or remove it.`
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
for (const table of byName.values()) {
|
|
820
|
+
if (table.name.startsWith(OWN_TABLE_PREFIX) || ignore.tables.has(table.name)) continue;
|
|
821
|
+
const known = mapped.get(table.name) ?? [];
|
|
822
|
+
for (const column of table.columns) {
|
|
823
|
+
if (known.includes(column)) continue;
|
|
824
|
+
if (ignore.columns.has(`${table.name}.${column.name}`)) continue;
|
|
825
|
+
if (column.key && ignore.columns.has(`${table.name}.${column.key}`)) continue;
|
|
826
|
+
const guess = strongest(guessPersonal(column.name), column.key ? guessPersonal(column.key) : void 0);
|
|
827
|
+
if (!guess) continue;
|
|
828
|
+
issues.push({
|
|
829
|
+
level: guess.confidence === "strong" ? "error" : "warning",
|
|
830
|
+
code: "unmapped-personal-column",
|
|
831
|
+
path: `schema.${table.name}.${column.name}`,
|
|
832
|
+
message: `Looks like personal data (${guess.category}, matched "${guess.matched}") but isn't in the data map. Add it under \`data\` with the purposes it's held for, or add "${table.name}.${column.name}" to \`notPersonal\`.`
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
return issues;
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
// src/safeguards.ts
|
|
840
|
+
function safeguardsReport(config) {
|
|
841
|
+
const security = config.security ?? {};
|
|
842
|
+
const report = [];
|
|
843
|
+
const protectedFields = [];
|
|
844
|
+
const unprotected = [];
|
|
845
|
+
for (const [key, table] of Object.entries(config.data ?? {})) {
|
|
846
|
+
for (const [field, def] of Object.entries(table.fields ?? {})) {
|
|
847
|
+
if (def.protect) protectedFields.push(`${key}.${field}: ${def.protect}`);
|
|
848
|
+
else if (isHighRisk(field, def.category)) unprotected.push(`${key}.${field}`);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
report.push({
|
|
852
|
+
clause: "(a)",
|
|
853
|
+
title: "Encryption, obfuscation, masking or virtual tokens",
|
|
854
|
+
status: unprotected.length === 0 ? protectedFields.length > 0 ? "declared" : "missing" : protectedFields.length > 0 ? "partly" : "missing",
|
|
855
|
+
details: [
|
|
856
|
+
...protectedFields,
|
|
857
|
+
...unprotected.map((f) => `${f}: high-risk and unprotected; encrypt it (dpdp-kit/protect) and set protect`),
|
|
858
|
+
...protectedFields.length === 0 && unprotected.length === 0 ? ["No field declares protect. Mark how sensitive fields are stored."] : []
|
|
859
|
+
]
|
|
860
|
+
});
|
|
861
|
+
const described = (clause, title, value, key, hint) => ({
|
|
862
|
+
clause,
|
|
863
|
+
title,
|
|
864
|
+
status: value ? "declared" : "missing",
|
|
865
|
+
details: [value ?? `Add security.${key}: ${hint}`]
|
|
866
|
+
});
|
|
867
|
+
report.push(described("(b)", "Access control to computer resources", security.accessControl, "accessControl", "how access is controlled, e.g. SSO and least-privilege roles"));
|
|
868
|
+
report.push({
|
|
869
|
+
clause: "(c)",
|
|
870
|
+
title: "Logs, monitoring and review of access",
|
|
871
|
+
status: "in-code",
|
|
872
|
+
details: ["Record access with dpdp.access.log() and run dpdp.access.review() daily to find unauthorised access."]
|
|
873
|
+
});
|
|
874
|
+
report.push(described("(d)", "Continued processing after a compromise (e.g. backups)", security.backups, "backups", "your backups and how often restores are tested"));
|
|
875
|
+
const logKeep = security.accessLogRetention ?? DEFAULT_ACCESS_LOG_RETENTION;
|
|
876
|
+
const logsOk = isDuration(logKeep) && durationMs(logKeep) >= durationMs("1y");
|
|
877
|
+
const dataOk = config.minimumRetention !== void 0 && isDuration(config.minimumRetention) && durationMs(config.minimumRetention) >= durationMs("1y");
|
|
878
|
+
report.push({
|
|
879
|
+
clause: "(e)",
|
|
880
|
+
title: "Keep logs and personal data for one year",
|
|
881
|
+
status: logsOk && dataOk ? "declared" : logsOk || dataOk ? "partly" : "missing",
|
|
882
|
+
details: [
|
|
883
|
+
logsOk ? `Access log kept ${logKeep}; prune with dpdp.access.prune()` : `security.accessLogRetention must be at least "1y"`,
|
|
884
|
+
dataOk ? `Personal data kept at least ${config.minimumRetention} (minimumRetention)` : `Set minimumRetention: "1y"`
|
|
885
|
+
]
|
|
886
|
+
});
|
|
887
|
+
const processors = Object.entries(config.processors ?? {});
|
|
888
|
+
const withContract = processors.filter(([, p]) => p.contract);
|
|
889
|
+
report.push({
|
|
890
|
+
clause: "(f)",
|
|
891
|
+
title: "Security safeguards in contracts with Data Processors",
|
|
892
|
+
status: processors.length === 0 || withContract.length === processors.length ? "declared" : withContract.length > 0 ? "partly" : "missing",
|
|
893
|
+
details: processors.length === 0 ? ["No processors declared."] : processors.map(([name, p]) => p.contract ? `${name}: ${p.contract}` : `${name}: add contract, where the security provision is`)
|
|
894
|
+
});
|
|
895
|
+
report.push(described("(g)", "Technical and organisational measures", security.measures, "measures", "e.g. security training and periodic access reviews"));
|
|
896
|
+
return report;
|
|
897
|
+
}
|
|
898
|
+
var MARK = { declared: "\u2714", "in-code": "\u25C6", partly: "\u25D0", missing: "\u2716" };
|
|
899
|
+
function formatSafeguards(report) {
|
|
900
|
+
const lines = ["Rule 6(1) of the DPDP Rules: reasonable security safeguards", ""];
|
|
901
|
+
for (const s of report) {
|
|
902
|
+
lines.push(`${MARK[s.status]} ${s.clause} ${s.title} [${s.status}]`);
|
|
903
|
+
for (const d of s.details) lines.push(` ${d}`);
|
|
904
|
+
}
|
|
905
|
+
const missing = report.filter((s) => s.status === "missing" || s.status === "partly").length;
|
|
906
|
+
lines.push("", missing === 0 ? "Every safeguard is recorded or provided." : `${missing} safeguard(s) missing or partly recorded.`);
|
|
907
|
+
lines.push("This reads your config, not your infrastructure: keep the descriptions true.");
|
|
908
|
+
return lines.join("\n");
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
export {
|
|
912
|
+
isDuration,
|
|
913
|
+
durationMs,
|
|
914
|
+
maxDuration,
|
|
915
|
+
REQUEST_TYPES,
|
|
916
|
+
isDpdpStore,
|
|
917
|
+
memoryStore,
|
|
918
|
+
grantedPurposes,
|
|
919
|
+
isAccessLogStore,
|
|
920
|
+
accessId,
|
|
921
|
+
DEFAULT_ACCESS_LOG_RETENTION,
|
|
922
|
+
resolveFields,
|
|
923
|
+
fieldsOutsidePurpose,
|
|
924
|
+
reviewAccess,
|
|
925
|
+
tokenize,
|
|
926
|
+
guessPersonal,
|
|
927
|
+
audit,
|
|
928
|
+
isHighRisk,
|
|
929
|
+
hasErrors,
|
|
930
|
+
REDACTED,
|
|
931
|
+
defineConfig,
|
|
932
|
+
purposeNames,
|
|
933
|
+
erasedValue,
|
|
934
|
+
checkSchema,
|
|
935
|
+
safeguardsReport,
|
|
936
|
+
formatSafeguards
|
|
937
|
+
};
|