dpdp-kit 0.1.1 → 0.2.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/README.md +138 -11
- package/dist/audit.d.ts +2 -2
- package/dist/chunk-KVTUMHXO.js +491 -0
- package/dist/cli.js +61 -9
- package/dist/config.d.ts +15 -0
- package/dist/data-access.d.ts +19 -0
- package/dist/dpdp.d.ts +60 -3
- package/dist/drizzle.d.ts +505 -4
- package/dist/drizzle.js +268 -3
- package/dist/erasure.d.ts +2 -1
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.js +375 -24
- package/dist/personal.d.ts +17 -0
- package/dist/records.d.ts +124 -0
- package/dist/retention.d.ts +47 -0
- package/dist/rights.d.ts +49 -0
- package/dist/run-audit.d.ts +12 -0
- package/dist/schema-check.d.ts +22 -0
- package/dist/store.d.ts +9 -2
- package/package.json +1 -1
- package/dist/chunk-HRKHRKV4.js +0 -185
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
// src/duration.ts
|
|
2
|
+
var PATTERN = /^(\d+)(h|d|y)$/;
|
|
3
|
+
var HOUR_MS = 60 * 60 * 1e3;
|
|
4
|
+
var UNIT_MS = { h: HOUR_MS, d: 24 * HOUR_MS, y: 365 * 24 * HOUR_MS };
|
|
5
|
+
function isDuration(value) {
|
|
6
|
+
return typeof value === "string" && PATTERN.test(value);
|
|
7
|
+
}
|
|
8
|
+
function durationMs(value) {
|
|
9
|
+
const match = PATTERN.exec(value);
|
|
10
|
+
if (!match) throw new TypeError(`Not a duration: ${JSON.stringify(value)} (use e.g. "48h", "30d", "1y")`);
|
|
11
|
+
const [, amount, unit] = match;
|
|
12
|
+
return Number(amount) * UNIT_MS[unit];
|
|
13
|
+
}
|
|
14
|
+
function maxDuration(a, b) {
|
|
15
|
+
return durationMs(b) > durationMs(a) ? b : a;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/records.ts
|
|
19
|
+
var REQUEST_TYPES = ["access", "correction", "erasure", "grievance", "nomination"];
|
|
20
|
+
function isRunnable(task, { now, staleBefore, maxAttempts }) {
|
|
21
|
+
if (task.dueAt > now) return false;
|
|
22
|
+
if (task.status === "pending") return true;
|
|
23
|
+
if (task.status === "failed") return task.attempts < maxAttempts;
|
|
24
|
+
if (task.status === "running") return (task.claimedAt ?? "") < staleBefore;
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/audit.ts
|
|
29
|
+
var ONE_YEAR_MS = durationMs("1y");
|
|
30
|
+
function checkRetention(retain, path, issues) {
|
|
31
|
+
if (retain === "while-active" || isDuration(retain)) return;
|
|
32
|
+
if (retain && typeof retain === "object" && "afterWithdrawal" in retain) {
|
|
33
|
+
if (!isDuration(retain.afterWithdrawal)) {
|
|
34
|
+
issues.push({
|
|
35
|
+
level: "error",
|
|
36
|
+
code: "invalid-duration",
|
|
37
|
+
path: `${path}.afterWithdrawal`,
|
|
38
|
+
message: `${JSON.stringify(retain.afterWithdrawal)} is not a duration. Use e.g. "0d", "30d", "1y".`
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
issues.push({
|
|
44
|
+
level: "error",
|
|
45
|
+
code: "invalid-retention",
|
|
46
|
+
path,
|
|
47
|
+
message: `Retention must be "while-active", a duration like "1y", or { afterWithdrawal: "30d" }.`
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function checkPurposeList(purposes, known, used, path, issues) {
|
|
51
|
+
if (!purposes || purposes.length === 0) {
|
|
52
|
+
issues.push({
|
|
53
|
+
level: "error",
|
|
54
|
+
code: "field-without-purpose",
|
|
55
|
+
path: `${path}.purposes`,
|
|
56
|
+
message: "List at least one purpose. Personal data with no purpose must not be collected."
|
|
57
|
+
});
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
for (const p of purposes) {
|
|
61
|
+
used.add(p);
|
|
62
|
+
if (!known.has(p)) {
|
|
63
|
+
issues.push({
|
|
64
|
+
level: "error",
|
|
65
|
+
code: "unknown-purpose",
|
|
66
|
+
path: `${path}.purposes`,
|
|
67
|
+
message: `Unknown purpose ${JSON.stringify(p)}. Declare it under \`purposes\` or fix the spelling.`
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function audit(config) {
|
|
73
|
+
const issues = [];
|
|
74
|
+
const purposes = config.purposes ?? {};
|
|
75
|
+
const known = new Set(Object.keys(purposes));
|
|
76
|
+
const used = /* @__PURE__ */ new Set();
|
|
77
|
+
if (!config.fiduciary?.name) {
|
|
78
|
+
issues.push({
|
|
79
|
+
level: "error",
|
|
80
|
+
code: "fiduciary-missing",
|
|
81
|
+
path: "fiduciary.name",
|
|
82
|
+
message: "Name the Data Fiduciary (your organisation). It appears on every notice."
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
if (!config.fiduciary?.grievanceUrl) {
|
|
86
|
+
issues.push({
|
|
87
|
+
level: "warning",
|
|
88
|
+
code: "grievance-missing",
|
|
89
|
+
path: "fiduciary.grievanceUrl",
|
|
90
|
+
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."
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (known.size === 0) {
|
|
94
|
+
issues.push({ level: "error", code: "no-purposes", path: "purposes", message: "Declare at least one purpose." });
|
|
95
|
+
}
|
|
96
|
+
for (const [name, def] of Object.entries(purposes)) {
|
|
97
|
+
const path = `purposes.${name}`;
|
|
98
|
+
if (def.basis !== "consent" && def.basis !== "legitimate-use") {
|
|
99
|
+
issues.push({
|
|
100
|
+
level: "error",
|
|
101
|
+
code: "invalid-basis",
|
|
102
|
+
path: `${path}.basis`,
|
|
103
|
+
message: `Basis must be "consent" or "legitimate-use".`
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
checkRetention(def.retain, `${path}.retain`, issues);
|
|
107
|
+
if (def.basis === "legitimate-use" && typeof def.retain === "object") {
|
|
108
|
+
issues.push({
|
|
109
|
+
level: "error",
|
|
110
|
+
code: "invalid-retention",
|
|
111
|
+
path: `${path}.retain`,
|
|
112
|
+
message: 'afterWithdrawal only applies to consent purposes (Section 6(4)). Use "while-active" or a duration for a legitimate use.'
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (const [key, table] of Object.entries(config.data ?? {})) {
|
|
117
|
+
const path = `data.${key}`;
|
|
118
|
+
if (!table.subject) {
|
|
119
|
+
issues.push({
|
|
120
|
+
level: "error",
|
|
121
|
+
code: "missing-subject-column",
|
|
122
|
+
path: `${path}.subject`,
|
|
123
|
+
message: "Name the column that holds the person's id, so their rows can be found."
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
const fields = Object.entries(table.fields ?? {});
|
|
127
|
+
if (fields.length === 0) {
|
|
128
|
+
issues.push({
|
|
129
|
+
level: "warning",
|
|
130
|
+
code: "table-without-fields",
|
|
131
|
+
path: `${path}.fields`,
|
|
132
|
+
message: "No personal fields listed. Remove the table from the map, or list its fields."
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
for (const [field, def] of fields) {
|
|
136
|
+
checkPurposeList(def.purposes, known, used, `${path}.fields.${field}`, issues);
|
|
137
|
+
}
|
|
138
|
+
if (table.legalHold && !isDuration(table.legalHold.keep)) {
|
|
139
|
+
issues.push({
|
|
140
|
+
level: "error",
|
|
141
|
+
code: "invalid-duration",
|
|
142
|
+
path: `${path}.legalHold.keep`,
|
|
143
|
+
message: `${JSON.stringify(table.legalHold.keep)} is not a duration. Use e.g. "8y".`
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (const [name, processor] of Object.entries(config.processors ?? {})) {
|
|
148
|
+
checkPurposeList(processor.purposes, known, used, `processors.${name}`, issues);
|
|
149
|
+
}
|
|
150
|
+
for (const name of known) {
|
|
151
|
+
if (!used.has(name)) {
|
|
152
|
+
issues.push({
|
|
153
|
+
level: "warning",
|
|
154
|
+
code: "unused-purpose",
|
|
155
|
+
path: `purposes.${name}`,
|
|
156
|
+
message: "No field or processor uses this purpose. Remove it, or map the data it covers."
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
for (const entry of config.notPersonal ?? []) {
|
|
161
|
+
if (!/^[^.\s]+(\.[^.\s]+)*\.([^.\s]+|\*)$/.test(entry)) {
|
|
162
|
+
issues.push({
|
|
163
|
+
level: "error",
|
|
164
|
+
code: "invalid-not-personal",
|
|
165
|
+
path: "notPersonal",
|
|
166
|
+
message: `${JSON.stringify(entry)} should be "table.column" or "table.*".`
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
const within = config.rights?.responseWithin;
|
|
171
|
+
if (within !== void 0) {
|
|
172
|
+
const entries = typeof within === "string" ? [["", within]] : Object.entries(within);
|
|
173
|
+
for (const [type, value] of entries) {
|
|
174
|
+
const path = type ? `rights.responseWithin.${type}` : "rights.responseWithin";
|
|
175
|
+
if (type && !REQUEST_TYPES.includes(type)) {
|
|
176
|
+
issues.push({
|
|
177
|
+
level: "error",
|
|
178
|
+
code: "invalid-request-type",
|
|
179
|
+
path,
|
|
180
|
+
message: `Unknown request type. Use one of: ${REQUEST_TYPES.join(", ")}.`
|
|
181
|
+
});
|
|
182
|
+
} else if (!isDuration(value)) {
|
|
183
|
+
issues.push({ level: "error", code: "invalid-duration", path, message: `${JSON.stringify(value)} is not a duration. Use e.g. "30d".` });
|
|
184
|
+
} else if ((type === "" || type === "grievance") && durationMs(value) > durationMs("90d")) {
|
|
185
|
+
issues.push({
|
|
186
|
+
level: "error",
|
|
187
|
+
code: "grievance-response-too-long",
|
|
188
|
+
path,
|
|
189
|
+
message: 'Rule 14(3) of the DPDP Rules requires answering grievances within ninety days. Use at most "90d".'
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const minimum = config.minimumRetention;
|
|
195
|
+
if (minimum === void 0) {
|
|
196
|
+
issues.push({
|
|
197
|
+
level: "warning",
|
|
198
|
+
code: "minimum-retention-missing",
|
|
199
|
+
path: "minimumRetention",
|
|
200
|
+
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").'
|
|
201
|
+
});
|
|
202
|
+
} else if (!isDuration(minimum)) {
|
|
203
|
+
issues.push({
|
|
204
|
+
level: "error",
|
|
205
|
+
code: "invalid-duration",
|
|
206
|
+
path: "minimumRetention",
|
|
207
|
+
message: `${JSON.stringify(minimum)} is not a duration. Use e.g. "1y".`
|
|
208
|
+
});
|
|
209
|
+
} else if (durationMs(minimum) < ONE_YEAR_MS) {
|
|
210
|
+
issues.push({
|
|
211
|
+
level: "error",
|
|
212
|
+
code: "minimum-retention-too-short",
|
|
213
|
+
path: "minimumRetention",
|
|
214
|
+
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".'
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
return issues;
|
|
218
|
+
}
|
|
219
|
+
function hasErrors(issues) {
|
|
220
|
+
return issues.some((i) => i.level === "error");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// src/personal.ts
|
|
224
|
+
var rule = (spec, category, confidence = "strong") => ({
|
|
225
|
+
tokens: spec.split(" "),
|
|
226
|
+
category,
|
|
227
|
+
confidence
|
|
228
|
+
});
|
|
229
|
+
var RULES = [
|
|
230
|
+
// device and online identifiers
|
|
231
|
+
rule("ip address", "device"),
|
|
232
|
+
rule("ip", "device"),
|
|
233
|
+
rule("imei", "device"),
|
|
234
|
+
rule("device id", "device"),
|
|
235
|
+
rule("user agent", "device"),
|
|
236
|
+
rule("fcm token", "device"),
|
|
237
|
+
rule("push token", "device"),
|
|
238
|
+
rule("advertising id", "device"),
|
|
239
|
+
rule("gaid", "device"),
|
|
240
|
+
rule("idfa", "device"),
|
|
241
|
+
// Indian and other government identifiers
|
|
242
|
+
rule("aadhaar", "identity"),
|
|
243
|
+
rule("aadhar", "identity"),
|
|
244
|
+
rule("uidai", "identity"),
|
|
245
|
+
rule("pan", "identity"),
|
|
246
|
+
rule("passport", "identity"),
|
|
247
|
+
rule("voter id", "identity"),
|
|
248
|
+
rule("epic", "identity"),
|
|
249
|
+
rule("driving licence", "identity"),
|
|
250
|
+
rule("driving license", "identity"),
|
|
251
|
+
rule("abha", "health"),
|
|
252
|
+
// dates of birth and names
|
|
253
|
+
rule("date of birth", "identity"),
|
|
254
|
+
rule("birth date", "identity"),
|
|
255
|
+
rule("birthdate", "identity"),
|
|
256
|
+
rule("birthday", "identity"),
|
|
257
|
+
rule("dob", "identity"),
|
|
258
|
+
rule("first name", "identity"),
|
|
259
|
+
rule("last name", "identity"),
|
|
260
|
+
rule("middle name", "identity"),
|
|
261
|
+
rule("full name", "identity"),
|
|
262
|
+
rule("surname", "identity"),
|
|
263
|
+
rule("father name", "identity"),
|
|
264
|
+
rule("mother name", "identity"),
|
|
265
|
+
rule("guardian name", "identity"),
|
|
266
|
+
rule("parent name", "identity"),
|
|
267
|
+
// contact
|
|
268
|
+
rule("phone", "contact"),
|
|
269
|
+
rule("mobile", "contact"),
|
|
270
|
+
rule("msisdn", "contact"),
|
|
271
|
+
rule("whatsapp", "contact"),
|
|
272
|
+
rule("email", "contact"),
|
|
273
|
+
rule("address", "contact"),
|
|
274
|
+
rule("pincode", "contact"),
|
|
275
|
+
rule("pin code", "contact"),
|
|
276
|
+
rule("postcode", "contact"),
|
|
277
|
+
rule("postal code", "contact"),
|
|
278
|
+
rule("zipcode", "contact"),
|
|
279
|
+
rule("zip code", "contact"),
|
|
280
|
+
// financial
|
|
281
|
+
rule("account number", "financial"),
|
|
282
|
+
rule("bank account", "financial"),
|
|
283
|
+
rule("ifsc", "financial"),
|
|
284
|
+
rule("upi", "financial"),
|
|
285
|
+
rule("vpa", "financial"),
|
|
286
|
+
rule("card number", "financial"),
|
|
287
|
+
rule("iban", "financial"),
|
|
288
|
+
rule("salary", "financial"),
|
|
289
|
+
rule("income", "financial"),
|
|
290
|
+
// health and biometric
|
|
291
|
+
rule("blood group", "health"),
|
|
292
|
+
rule("diagnosis", "health"),
|
|
293
|
+
rule("medical", "health"),
|
|
294
|
+
rule("allergy", "health"),
|
|
295
|
+
rule("allergies", "health"),
|
|
296
|
+
rule("prescription", "health"),
|
|
297
|
+
rule("fingerprint", "biometric"),
|
|
298
|
+
rule("biometric", "biometric"),
|
|
299
|
+
rule("face embedding", "biometric"),
|
|
300
|
+
// location
|
|
301
|
+
rule("latitude", "location"),
|
|
302
|
+
rule("longitude", "location"),
|
|
303
|
+
rule("lat", "location"),
|
|
304
|
+
rule("lng", "location"),
|
|
305
|
+
rule("gps", "location"),
|
|
306
|
+
// weak: personal in a people table, not in a products table
|
|
307
|
+
rule("name", "identity", "weak"),
|
|
308
|
+
rule("gender", "identity", "weak"),
|
|
309
|
+
rule("age", "identity", "weak"),
|
|
310
|
+
rule("photo", "identity", "weak"),
|
|
311
|
+
rule("avatar", "identity", "weak"),
|
|
312
|
+
rule("selfie", "biometric", "weak"),
|
|
313
|
+
rule("signature", "identity", "weak"),
|
|
314
|
+
rule("city", "location", "weak"),
|
|
315
|
+
rule("street", "contact", "weak"),
|
|
316
|
+
rule("location", "location", "weak")
|
|
317
|
+
];
|
|
318
|
+
var DESCRIPTIVE_ENDINGS = /* @__PURE__ */ new Set([
|
|
319
|
+
"verified",
|
|
320
|
+
"verification",
|
|
321
|
+
"confirmed",
|
|
322
|
+
"enabled",
|
|
323
|
+
"disabled",
|
|
324
|
+
"count",
|
|
325
|
+
"type",
|
|
326
|
+
"status",
|
|
327
|
+
"kind",
|
|
328
|
+
"flag",
|
|
329
|
+
"visible",
|
|
330
|
+
"public",
|
|
331
|
+
"at",
|
|
332
|
+
"sent",
|
|
333
|
+
"in",
|
|
334
|
+
"out",
|
|
335
|
+
"allowed",
|
|
336
|
+
"required"
|
|
337
|
+
]);
|
|
338
|
+
var ORGANISATION_WORDS = /* @__PURE__ */ new Set([
|
|
339
|
+
"company",
|
|
340
|
+
"business",
|
|
341
|
+
"org",
|
|
342
|
+
"organisation",
|
|
343
|
+
"organization",
|
|
344
|
+
"merchant",
|
|
345
|
+
"vendor",
|
|
346
|
+
"supplier",
|
|
347
|
+
"firm",
|
|
348
|
+
"branch",
|
|
349
|
+
"office",
|
|
350
|
+
"school",
|
|
351
|
+
"institute",
|
|
352
|
+
"college"
|
|
353
|
+
]);
|
|
354
|
+
function tokenize(name) {
|
|
355
|
+
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);
|
|
356
|
+
}
|
|
357
|
+
function containsRun(tokens, run) {
|
|
358
|
+
outer: for (let i = 0; i + run.length <= tokens.length; i++) {
|
|
359
|
+
for (let j = 0; j < run.length; j++) if (tokens[i + j] !== run[j]) continue outer;
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
function guessPersonal(columnName) {
|
|
365
|
+
const tokens = tokenize(columnName);
|
|
366
|
+
if (tokens.length === 0) return void 0;
|
|
367
|
+
const last = tokens[tokens.length - 1];
|
|
368
|
+
if (tokens.length > 1 && DESCRIPTIVE_ENDINGS.has(last)) return void 0;
|
|
369
|
+
const organisational = tokens.some((t) => ORGANISATION_WORDS.has(t));
|
|
370
|
+
for (const r of RULES) {
|
|
371
|
+
if (containsRun(tokens, r.tokens)) {
|
|
372
|
+
return {
|
|
373
|
+
category: r.category,
|
|
374
|
+
confidence: organisational ? "weak" : r.confidence,
|
|
375
|
+
matched: r.tokens.join(" ")
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return void 0;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// src/schema-check.ts
|
|
383
|
+
var OWN_TABLE_PREFIX = "dpdp_";
|
|
384
|
+
var matches = (column, name) => column.name === name || column.key === name;
|
|
385
|
+
function parseNotPersonal(entries) {
|
|
386
|
+
const columns = /* @__PURE__ */ new Set();
|
|
387
|
+
const tables = /* @__PURE__ */ new Set();
|
|
388
|
+
for (const entry of entries ?? []) {
|
|
389
|
+
const dot = entry.lastIndexOf(".");
|
|
390
|
+
if (dot <= 0) continue;
|
|
391
|
+
const table = entry.slice(0, dot);
|
|
392
|
+
const column = entry.slice(dot + 1);
|
|
393
|
+
if (column === "*") tables.add(table);
|
|
394
|
+
else columns.add(`${table}.${column}`);
|
|
395
|
+
}
|
|
396
|
+
return { columns, tables };
|
|
397
|
+
}
|
|
398
|
+
function strongest(a, b) {
|
|
399
|
+
if (!a) return b;
|
|
400
|
+
if (!b) return a;
|
|
401
|
+
return b.confidence === "strong" && a.confidence === "weak" ? b : a;
|
|
402
|
+
}
|
|
403
|
+
function checkSchema(config, schema) {
|
|
404
|
+
const issues = [];
|
|
405
|
+
const byName = /* @__PURE__ */ new Map();
|
|
406
|
+
for (const table of schema.tables) if (!byName.has(table.name)) byName.set(table.name, table);
|
|
407
|
+
const mapped = /* @__PURE__ */ new Map();
|
|
408
|
+
for (const [key, def] of Object.entries(config.data ?? {})) {
|
|
409
|
+
const table = byName.get(def.table);
|
|
410
|
+
if (!table) {
|
|
411
|
+
issues.push({
|
|
412
|
+
level: "error",
|
|
413
|
+
code: "unknown-table",
|
|
414
|
+
path: `data.${key}.table`,
|
|
415
|
+
message: `Table "${def.table}" isn't in the schema. Fix the name, or remove it from the data map if the table is gone.`
|
|
416
|
+
});
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
const list = mapped.get(table.name) ?? [];
|
|
420
|
+
const subject = table.columns.find((c) => matches(c, def.subject));
|
|
421
|
+
if (subject) list.push(subject);
|
|
422
|
+
else {
|
|
423
|
+
issues.push({
|
|
424
|
+
level: "error",
|
|
425
|
+
code: "unknown-column",
|
|
426
|
+
path: `data.${key}.subject`,
|
|
427
|
+
message: `Column "${def.subject}" isn't in table "${table.name}". The subject column must exist to find a person's rows.`
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
for (const field of Object.keys(def.fields ?? {})) {
|
|
431
|
+
const column = table.columns.find((c) => matches(c, field));
|
|
432
|
+
if (column) list.push(column);
|
|
433
|
+
else {
|
|
434
|
+
issues.push({
|
|
435
|
+
level: "error",
|
|
436
|
+
code: "unknown-column",
|
|
437
|
+
path: `data.${key}.fields.${field}`,
|
|
438
|
+
message: `Column "${field}" isn't in table "${table.name}". Fix the name, or remove it if the column was dropped.`
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
mapped.set(table.name, list);
|
|
443
|
+
}
|
|
444
|
+
const ignore = parseNotPersonal(config.notPersonal);
|
|
445
|
+
for (const entry of config.notPersonal ?? []) {
|
|
446
|
+
const dot = entry.lastIndexOf(".");
|
|
447
|
+
const tableName = entry.slice(0, Math.max(dot, 0));
|
|
448
|
+
const columnName = entry.slice(dot + 1);
|
|
449
|
+
const table = byName.get(tableName);
|
|
450
|
+
const exists = table && (columnName === "*" || table.columns.some((c) => matches(c, columnName)));
|
|
451
|
+
if (!exists) {
|
|
452
|
+
issues.push({
|
|
453
|
+
level: "warning",
|
|
454
|
+
code: "stale-not-personal",
|
|
455
|
+
path: "notPersonal",
|
|
456
|
+
message: `"${entry}" doesn't match any table or column. Use "table.column" or "table.*", or remove it.`
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
for (const table of byName.values()) {
|
|
461
|
+
if (table.name.startsWith(OWN_TABLE_PREFIX) || ignore.tables.has(table.name)) continue;
|
|
462
|
+
const known = mapped.get(table.name) ?? [];
|
|
463
|
+
for (const column of table.columns) {
|
|
464
|
+
if (known.includes(column)) continue;
|
|
465
|
+
if (ignore.columns.has(`${table.name}.${column.name}`)) continue;
|
|
466
|
+
if (column.key && ignore.columns.has(`${table.name}.${column.key}`)) continue;
|
|
467
|
+
const guess = strongest(guessPersonal(column.name), column.key ? guessPersonal(column.key) : void 0);
|
|
468
|
+
if (!guess) continue;
|
|
469
|
+
issues.push({
|
|
470
|
+
level: guess.confidence === "strong" ? "error" : "warning",
|
|
471
|
+
code: "unmapped-personal-column",
|
|
472
|
+
path: `schema.${table.name}.${column.name}`,
|
|
473
|
+
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\`.`
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
return issues;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
export {
|
|
481
|
+
isDuration,
|
|
482
|
+
durationMs,
|
|
483
|
+
maxDuration,
|
|
484
|
+
REQUEST_TYPES,
|
|
485
|
+
isRunnable,
|
|
486
|
+
audit,
|
|
487
|
+
hasErrors,
|
|
488
|
+
tokenize,
|
|
489
|
+
guessPersonal,
|
|
490
|
+
checkSchema
|
|
491
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
|
-
audit
|
|
4
|
-
|
|
3
|
+
audit,
|
|
4
|
+
checkSchema
|
|
5
|
+
} from "./chunk-KVTUMHXO.js";
|
|
5
6
|
|
|
6
7
|
// src/run-audit.ts
|
|
7
8
|
import { existsSync } from "fs";
|
|
8
|
-
import { resolve } from "path";
|
|
9
|
+
import { relative, resolve } from "path";
|
|
9
10
|
import { pathToFileURL } from "url";
|
|
10
11
|
var CONFIG_FILES = ["dpdp.config.ts", "dpdp.config.mts", "dpdp.config.js", "dpdp.config.mjs"];
|
|
11
12
|
function findConfig(cwd, explicit) {
|
|
12
13
|
if (explicit) return resolve(cwd, explicit);
|
|
13
14
|
return CONFIG_FILES.map((f) => resolve(cwd, f)).find((f) => existsSync(f));
|
|
14
15
|
}
|
|
15
|
-
async function
|
|
16
|
+
async function loadModule(file) {
|
|
16
17
|
const { createJiti } = await import("jiti");
|
|
17
18
|
const jiti = createJiti(pathToFileURL(file).href);
|
|
18
|
-
|
|
19
|
+
return await jiti.import(file);
|
|
20
|
+
}
|
|
21
|
+
async function loadConfig(file) {
|
|
22
|
+
const mod = await loadModule(file);
|
|
19
23
|
const config = [mod.default, mod.config, mod.default?.config].find(
|
|
20
24
|
(c) => typeof c === "object" && c !== null && "purposes" in c
|
|
21
25
|
);
|
|
@@ -35,16 +39,58 @@ function formatIssues(issues) {
|
|
|
35
39
|
lines.push("", `${errors.length} error(s), ${warnings.length} warning(s)`);
|
|
36
40
|
return lines.join("\n");
|
|
37
41
|
}
|
|
42
|
+
var escapeData = (text) => text.replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
|
|
43
|
+
var escapeProperty = (text) => escapeData(text).replace(/:/g, "%3A").replace(/,/g, "%2C");
|
|
44
|
+
function formatGithubAnnotations(issues, files, root) {
|
|
45
|
+
return issues.map((issue) => {
|
|
46
|
+
const file = issue.path.startsWith("schema.") && files.schema ? files.schema : files.config;
|
|
47
|
+
const props = [`file=${escapeProperty(relative(root, file))}`, `title=${escapeProperty(`dpdp-kit: ${issue.path}`)}`];
|
|
48
|
+
return `::${issue.level} ${props.join(",")}::${escapeData(issue.message)}`;
|
|
49
|
+
}).join("\n");
|
|
50
|
+
}
|
|
51
|
+
async function loadDrizzleSchema(file) {
|
|
52
|
+
const mod = await loadModule(file);
|
|
53
|
+
let drizzleSchema;
|
|
54
|
+
try {
|
|
55
|
+
({ drizzleSchema } = await import("./drizzle.js"));
|
|
56
|
+
} catch {
|
|
57
|
+
throw new Error("--schema reads a Drizzle schema, so drizzle-orm must be installed in this project.");
|
|
58
|
+
}
|
|
59
|
+
const schema = drizzleSchema(mod);
|
|
60
|
+
if (schema.tables.length === 0) {
|
|
61
|
+
throw new Error(`${file} exports no Drizzle tables. Point --schema at the file that defines them.`);
|
|
62
|
+
}
|
|
63
|
+
return schema;
|
|
64
|
+
}
|
|
65
|
+
var flagValue = (argv, flag) => {
|
|
66
|
+
const i = argv.indexOf(flag);
|
|
67
|
+
return i >= 0 ? argv[i + 1] : void 0;
|
|
68
|
+
};
|
|
38
69
|
async function runAudit(argv, cwd, log) {
|
|
39
|
-
const
|
|
40
|
-
const
|
|
70
|
+
const explicit = flagValue(argv, "--config");
|
|
71
|
+
const schemaFlag = flagValue(argv, "--schema");
|
|
41
72
|
const strict = argv.includes("--strict");
|
|
73
|
+
const format = flagValue(argv, "--format") ?? "text";
|
|
74
|
+
if (format !== "text" && format !== "github") {
|
|
75
|
+
log(`Unknown --format ${JSON.stringify(format)}. Use "text" or "github".`);
|
|
76
|
+
return 1;
|
|
77
|
+
}
|
|
42
78
|
const file = findConfig(cwd, explicit);
|
|
43
79
|
if (!file || !existsSync(file)) {
|
|
44
80
|
log(explicit ? `Config file not found: ${explicit}` : `No config found. Create one of: ${CONFIG_FILES.join(", ")}`);
|
|
45
81
|
return 1;
|
|
46
82
|
}
|
|
47
|
-
const
|
|
83
|
+
const schemaFile = schemaFlag ? resolve(cwd, schemaFlag) : void 0;
|
|
84
|
+
if (schemaFile && !existsSync(schemaFile)) {
|
|
85
|
+
log(`Schema file not found: ${schemaFlag}`);
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
const config = await loadConfig(file);
|
|
89
|
+
const issues = audit(config);
|
|
90
|
+
if (schemaFile) issues.push(...checkSchema(config, await loadDrizzleSchema(schemaFile)));
|
|
91
|
+
if (format === "github" && issues.length > 0) {
|
|
92
|
+
log(formatGithubAnnotations(issues, { config: file, schema: schemaFile }, process.env.GITHUB_WORKSPACE ?? cwd));
|
|
93
|
+
}
|
|
48
94
|
log(formatIssues(issues));
|
|
49
95
|
const failing = issues.filter((i) => i.level === "error" || strict && i.level === "warning");
|
|
50
96
|
return failing.length > 0 ? 1 : 0;
|
|
@@ -54,11 +100,17 @@ async function runAudit(argv, cwd, log) {
|
|
|
54
100
|
var HELP = `dpdp-kit \u2014 DPDP Act tooling for Node.js
|
|
55
101
|
|
|
56
102
|
Usage:
|
|
57
|
-
dpdp-kit audit [--config <file>] [--strict]
|
|
103
|
+
dpdp-kit audit [--config <file>] [--schema <file>] [--strict] [--format text|github]
|
|
104
|
+
|
|
105
|
+
Checks your data map and purposes. With --schema, also compares the data map with your
|
|
106
|
+
Drizzle schema: personal-looking columns missing from the map, and map entries that no
|
|
107
|
+
longer exist.
|
|
58
108
|
|
|
59
109
|
Options:
|
|
60
110
|
--config <file> Config file (default: dpdp.config.ts / .mts / .js / .mjs)
|
|
111
|
+
--schema <file> Drizzle schema file (Postgres, MySQL or SQLite), e.g. src/db/schema.ts
|
|
61
112
|
--strict Fail on warnings too
|
|
113
|
+
--format github Also print GitHub Actions annotations, so problems show on the pull request
|
|
62
114
|
`;
|
|
63
115
|
var [command, ...rest] = process.argv.slice(2);
|
|
64
116
|
if (command === "audit") {
|
package/dist/config.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Duration } from "./duration.js";
|
|
2
|
+
import type { RequestType } from "./records.js";
|
|
2
3
|
/**
|
|
3
4
|
* Why you may process data for a purpose.
|
|
4
5
|
* - `"consent"`: the person agreed (Section 6(1)). They can withdraw at any time, as easily as
|
|
@@ -63,6 +64,14 @@ export interface ProcessorDef<P extends string = string> {
|
|
|
63
64
|
purposes: readonly P[];
|
|
64
65
|
description?: string;
|
|
65
66
|
}
|
|
67
|
+
export interface RightsConfig {
|
|
68
|
+
/**
|
|
69
|
+
* How long you give yourself to answer a rights request, from when it's raised. One duration for
|
|
70
|
+
* every type, or one per type. Default `"90d"`. Rule 14(3) of the DPDP Rules caps grievance
|
|
71
|
+
* responses at ninety days; the Rules set no period for the other types, so pick one you can keep.
|
|
72
|
+
*/
|
|
73
|
+
responseWithin?: Duration | Partial<Record<RequestType, Duration>>;
|
|
74
|
+
}
|
|
66
75
|
export interface FiduciaryDef {
|
|
67
76
|
name: string;
|
|
68
77
|
/**
|
|
@@ -92,6 +101,12 @@ export interface DpdpConfig<P extends string = string> {
|
|
|
92
101
|
* security investigations.
|
|
93
102
|
*/
|
|
94
103
|
minimumRetention?: Duration;
|
|
104
|
+
/**
|
|
105
|
+
* Columns whose names look personal but aren't, for the schema drift check: `"products.name"`,
|
|
106
|
+
* or `"products.*"` for a whole table. Use database table and column names.
|
|
107
|
+
*/
|
|
108
|
+
notPersonal?: readonly string[];
|
|
109
|
+
rights?: RightsConfig;
|
|
95
110
|
}
|
|
96
111
|
/** Purpose names of a config, e.g. `"account" | "marketing_sms"`. */
|
|
97
112
|
export type PurposeOf<C> = C extends DpdpConfig<infer P> ? P : never;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How dpdp-kit reaches the personal data in your own tables: to show a person what you hold
|
|
3
|
+
* (Section 11) and to carry out erasure (Section 8(7)). Names are as written in the data map
|
|
4
|
+
* (`config.data[...].table`, `subject` and field names); an implementation maps them to columns.
|
|
5
|
+
*/
|
|
6
|
+
export interface DataAccess {
|
|
7
|
+
/** The person's rows, with only the listed columns. */
|
|
8
|
+
read(table: string, subjectColumn: string, subjectId: string, columns: readonly string[]): Promise<Record<string, unknown>[]>;
|
|
9
|
+
/** Set the listed columns to null in the person's rows. Returns rows changed. */
|
|
10
|
+
eraseFields(table: string, subjectColumn: string, subjectId: string, columns: readonly string[]): Promise<number>;
|
|
11
|
+
/** Delete the person's rows. Returns rows deleted. */
|
|
12
|
+
deleteRows(table: string, subjectColumn: string, subjectId: string): Promise<number>;
|
|
13
|
+
}
|
|
14
|
+
type Row = Record<string, unknown>;
|
|
15
|
+
/** Data access over plain arrays, for tests and examples. */
|
|
16
|
+
export declare function memoryData(tables: Record<string, Row[]>): DataAccess & {
|
|
17
|
+
readonly tables: Record<string, Row[]>;
|
|
18
|
+
};
|
|
19
|
+
export {};
|