dpdp-kit 0.2.0 → 0.4.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 +90 -0
- package/README.md +240 -17
- package/dist/access.d.ts +111 -0
- package/dist/audit.d.ts +3 -1
- package/dist/breach.d.ts +170 -0
- package/dist/chunk-2ELTZSW3.js +94 -0
- package/dist/chunk-5TVA2TKG.js +37 -0
- package/dist/chunk-HXVP446N.js +1051 -0
- package/dist/chunk-KLMR7S7I.js +438 -0
- package/dist/cli.js +53 -17
- package/dist/config.d.ts +69 -0
- package/dist/data-access.d.ts +6 -2
- package/dist/ddl.d.ts +10 -0
- package/dist/dpdp.d.ts +210 -0
- package/dist/drizzle.d.ts +403 -11
- package/dist/drizzle.js +209 -56
- 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 +7 -4
- package/dist/index.js +420 -100
- package/dist/mongodb.d.ts +87 -0
- package/dist/mongodb.js +344 -0
- package/dist/nestjs.d.ts +65 -0
- package/dist/nestjs.js +41 -0
- package/dist/postgres.d.ts +45 -0
- package/dist/postgres.js +27 -0
- package/dist/prisma.d.ts +43 -0
- package/dist/prisma.js +281 -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 +13 -1
- 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 +25 -8
- package/dist/store.d.ts +11 -1
- package/package.json +58 -5
- package/dist/chunk-FGZAH2NF.js +0 -16
- package/dist/chunk-KVTUMHXO.js +0 -491
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SeqConflictError
|
|
3
|
+
} from "./chunk-67Q5GFD6.js";
|
|
4
|
+
|
|
5
|
+
// src/postgres.ts
|
|
6
|
+
var ident = (name) => `"${name.replace(/"/g, '""')}"`;
|
|
7
|
+
var iso = (v) => v === null || v === void 0 ? void 0 : new Date(v).toISOString();
|
|
8
|
+
var num = (v) => Number(v ?? 0);
|
|
9
|
+
var json = (v) => typeof v === "string" ? JSON.parse(v) : v;
|
|
10
|
+
var optJson = (v) => v === null || v === void 0 ? void 0 : json(v);
|
|
11
|
+
var optText = (v) => v === null || v === void 0 ? void 0 : String(v);
|
|
12
|
+
var jsonParam = (v) => v === void 0 || v === null ? null : JSON.stringify(v);
|
|
13
|
+
var defined = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== void 0));
|
|
14
|
+
function isUniqueViolation(err) {
|
|
15
|
+
for (let e = err, depth = 0; e && depth < 5; depth++) {
|
|
16
|
+
const x = e;
|
|
17
|
+
if (x.code === "23505" || x.meta?.code === "23505") return true;
|
|
18
|
+
if (typeof x.message === "string" && /\b23505\b|unique constraint|duplicate key/i.test(x.message)) return true;
|
|
19
|
+
e = x.cause;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
var eventFromRow = (r) => defined({
|
|
24
|
+
subjectId: String(r.subject_id),
|
|
25
|
+
seq: num(r.seq),
|
|
26
|
+
action: r.action,
|
|
27
|
+
purposes: json(r.purposes),
|
|
28
|
+
noticeVersion: optText(r.notice_version),
|
|
29
|
+
language: optText(r.language),
|
|
30
|
+
channel: optText(r.channel),
|
|
31
|
+
evidence: optJson(r.evidence),
|
|
32
|
+
at: iso(r.at),
|
|
33
|
+
prevHash: optText(r.prev_hash) ?? null,
|
|
34
|
+
hash: String(r.hash)
|
|
35
|
+
});
|
|
36
|
+
var requestFromRow = (r) => defined({
|
|
37
|
+
id: String(r.id),
|
|
38
|
+
subjectId: String(r.subject_id),
|
|
39
|
+
type: r.type,
|
|
40
|
+
status: r.status,
|
|
41
|
+
openedAt: iso(r.opened_at),
|
|
42
|
+
dueAt: iso(r.due_at),
|
|
43
|
+
closedAt: iso(r.closed_at),
|
|
44
|
+
details: optJson(r.details),
|
|
45
|
+
history: json(r.history)
|
|
46
|
+
});
|
|
47
|
+
var taskFromRow = (r) => defined({
|
|
48
|
+
id: String(r.id),
|
|
49
|
+
subjectId: String(r.subject_id),
|
|
50
|
+
reason: r.reason,
|
|
51
|
+
kind: r.kind,
|
|
52
|
+
table: optText(r.table_key),
|
|
53
|
+
fields: optJson(r.fields),
|
|
54
|
+
processor: optText(r.processor),
|
|
55
|
+
purposes: optJson(r.purposes),
|
|
56
|
+
createdAt: iso(r.created_at),
|
|
57
|
+
dueAt: iso(r.due_at),
|
|
58
|
+
status: r.status,
|
|
59
|
+
attempts: num(r.attempts),
|
|
60
|
+
claimedAt: iso(r.claimed_at),
|
|
61
|
+
doneAt: iso(r.done_at),
|
|
62
|
+
affected: r.affected === null || r.affected === void 0 ? void 0 : num(r.affected),
|
|
63
|
+
lastError: optText(r.last_error),
|
|
64
|
+
note: optText(r.note)
|
|
65
|
+
});
|
|
66
|
+
var accessFromRow = (r) => defined({
|
|
67
|
+
id: String(r.id),
|
|
68
|
+
at: iso(r.at),
|
|
69
|
+
actor: String(r.actor),
|
|
70
|
+
subjectId: String(r.subject_id),
|
|
71
|
+
action: r.action,
|
|
72
|
+
purpose: String(r.purpose),
|
|
73
|
+
fields: optJson(r.fields),
|
|
74
|
+
outOfPurpose: optJson(r.out_of_purpose),
|
|
75
|
+
reason: optText(r.reason),
|
|
76
|
+
evidence: optJson(r.evidence)
|
|
77
|
+
});
|
|
78
|
+
var noticeFromRow = (r) => defined({
|
|
79
|
+
breachId: String(r.breach_id),
|
|
80
|
+
subjectId: String(r.subject_id),
|
|
81
|
+
status: r.status,
|
|
82
|
+
addedAt: iso(r.added_at),
|
|
83
|
+
sentAt: iso(r.sent_at),
|
|
84
|
+
channel: optText(r.channel)
|
|
85
|
+
});
|
|
86
|
+
var p = (n, type) => `$${n}::${type}`;
|
|
87
|
+
var insertSql = (table, cols) => `INSERT INTO ${table} (${cols.map(([c]) => c).join(", ")}) VALUES (${cols.map(([, t], i) => p(i + 1, t)).join(", ")})`;
|
|
88
|
+
var updateSql = (table, cols) => `UPDATE ${table} SET ${cols.slice(1).map(([c, t], i) => `${c} = ${p(i + 2, t)}`).join(", ")} WHERE ${cols[0][0]} = $1::text`;
|
|
89
|
+
var EVENT_COLUMNS = [
|
|
90
|
+
["subject_id", "text"],
|
|
91
|
+
["seq", "int"],
|
|
92
|
+
["action", "text"],
|
|
93
|
+
["purposes", "jsonb"],
|
|
94
|
+
["notice_version", "text"],
|
|
95
|
+
["language", "text"],
|
|
96
|
+
["channel", "text"],
|
|
97
|
+
["evidence", "jsonb"],
|
|
98
|
+
["at", "timestamptz"],
|
|
99
|
+
["prev_hash", "text"],
|
|
100
|
+
["hash", "text"]
|
|
101
|
+
];
|
|
102
|
+
var eventValues = (e) => [
|
|
103
|
+
e.subjectId,
|
|
104
|
+
e.seq,
|
|
105
|
+
e.action,
|
|
106
|
+
JSON.stringify(e.purposes),
|
|
107
|
+
e.noticeVersion ?? null,
|
|
108
|
+
e.language ?? null,
|
|
109
|
+
e.channel ?? null,
|
|
110
|
+
jsonParam(e.evidence),
|
|
111
|
+
e.at,
|
|
112
|
+
e.prevHash,
|
|
113
|
+
e.hash
|
|
114
|
+
];
|
|
115
|
+
var REQUEST_COLUMNS = [
|
|
116
|
+
["id", "text"],
|
|
117
|
+
["subject_id", "text"],
|
|
118
|
+
["type", "text"],
|
|
119
|
+
["status", "text"],
|
|
120
|
+
["opened_at", "timestamptz"],
|
|
121
|
+
["due_at", "timestamptz"],
|
|
122
|
+
["closed_at", "timestamptz"],
|
|
123
|
+
["details", "jsonb"],
|
|
124
|
+
["history", "jsonb"]
|
|
125
|
+
];
|
|
126
|
+
var requestValues = (r) => [
|
|
127
|
+
r.id,
|
|
128
|
+
r.subjectId,
|
|
129
|
+
r.type,
|
|
130
|
+
r.status,
|
|
131
|
+
r.openedAt,
|
|
132
|
+
r.dueAt,
|
|
133
|
+
r.closedAt ?? null,
|
|
134
|
+
jsonParam(r.details),
|
|
135
|
+
JSON.stringify(r.history)
|
|
136
|
+
];
|
|
137
|
+
var TASK_COLUMNS = [
|
|
138
|
+
["id", "text"],
|
|
139
|
+
["subject_id", "text"],
|
|
140
|
+
["reason", "text"],
|
|
141
|
+
["kind", "text"],
|
|
142
|
+
["table_key", "text"],
|
|
143
|
+
["fields", "jsonb"],
|
|
144
|
+
["processor", "text"],
|
|
145
|
+
["purposes", "jsonb"],
|
|
146
|
+
["created_at", "timestamptz"],
|
|
147
|
+
["due_at", "timestamptz"],
|
|
148
|
+
["status", "text"],
|
|
149
|
+
["attempts", "int"],
|
|
150
|
+
["claimed_at", "timestamptz"],
|
|
151
|
+
["done_at", "timestamptz"],
|
|
152
|
+
["affected", "int"],
|
|
153
|
+
["last_error", "text"],
|
|
154
|
+
["note", "text"]
|
|
155
|
+
];
|
|
156
|
+
var taskValues = (t) => [
|
|
157
|
+
t.id,
|
|
158
|
+
t.subjectId,
|
|
159
|
+
t.reason,
|
|
160
|
+
t.kind,
|
|
161
|
+
t.table ?? null,
|
|
162
|
+
jsonParam(t.fields),
|
|
163
|
+
t.processor ?? null,
|
|
164
|
+
jsonParam(t.purposes),
|
|
165
|
+
t.createdAt,
|
|
166
|
+
t.dueAt,
|
|
167
|
+
t.status,
|
|
168
|
+
t.attempts,
|
|
169
|
+
t.claimedAt ?? null,
|
|
170
|
+
t.doneAt ?? null,
|
|
171
|
+
t.affected ?? null,
|
|
172
|
+
t.lastError ?? null,
|
|
173
|
+
t.note ?? null
|
|
174
|
+
];
|
|
175
|
+
var ACCESS_COLUMNS = [
|
|
176
|
+
["id", "text"],
|
|
177
|
+
["at", "timestamptz"],
|
|
178
|
+
["actor", "text"],
|
|
179
|
+
["subject_id", "text"],
|
|
180
|
+
["action", "text"],
|
|
181
|
+
["purpose", "text"],
|
|
182
|
+
["fields", "jsonb"],
|
|
183
|
+
["out_of_purpose", "jsonb"],
|
|
184
|
+
["reason", "text"],
|
|
185
|
+
["evidence", "jsonb"]
|
|
186
|
+
];
|
|
187
|
+
var accessValues = (e) => [
|
|
188
|
+
e.id,
|
|
189
|
+
e.at,
|
|
190
|
+
e.actor,
|
|
191
|
+
e.subjectId,
|
|
192
|
+
e.action,
|
|
193
|
+
e.purpose,
|
|
194
|
+
jsonParam(e.fields),
|
|
195
|
+
jsonParam(e.outOfPurpose),
|
|
196
|
+
e.reason ?? null,
|
|
197
|
+
jsonParam(e.evidence)
|
|
198
|
+
];
|
|
199
|
+
var BREACH_COLUMNS = [["id", "text"], ["status", "text"], ["aware_at", "timestamptz"], ["report_due_at", "timestamptz"], ["data", "jsonb"]];
|
|
200
|
+
var breachValues = (b) => [b.id, b.status, b.awareAt, b.reportDueAt, JSON.stringify(b)];
|
|
201
|
+
var NOTICE_COLUMNS = [
|
|
202
|
+
["breach_id", "text"],
|
|
203
|
+
["subject_id", "text"],
|
|
204
|
+
["status", "text"],
|
|
205
|
+
["added_at", "timestamptz"],
|
|
206
|
+
["sent_at", "timestamptz"],
|
|
207
|
+
["channel", "text"]
|
|
208
|
+
];
|
|
209
|
+
var utc = (expr) => `to_char(${expr} AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"')`;
|
|
210
|
+
var selectList = (cols) => cols.map(([c, t]) => t === "timestamptz" ? `${utc(c)} AS ${c}` : c).join(", ");
|
|
211
|
+
var EVENT_SELECT = selectList(EVENT_COLUMNS);
|
|
212
|
+
var REQUEST_SELECT = selectList(REQUEST_COLUMNS);
|
|
213
|
+
var TASK_SELECT = selectList(TASK_COLUMNS);
|
|
214
|
+
var ACCESS_SELECT = selectList(ACCESS_COLUMNS);
|
|
215
|
+
var NOTICE_SELECT = selectList(NOTICE_COLUMNS);
|
|
216
|
+
var RUNNABLE = `due_at <= $1::timestamptz AND (status = 'pending' OR (status = 'failed' AND attempts < $3::int) OR (status = 'running' AND claimed_at < $2::timestamptz))`;
|
|
217
|
+
var BATCH = 1e3;
|
|
218
|
+
var insertManySql = (table, cols, rows) => `INSERT INTO ${table} (${cols.map(([c]) => c).join(", ")}) VALUES ${Array.from(
|
|
219
|
+
{ length: rows },
|
|
220
|
+
(_, r) => `(${cols.map(([, t], i) => p(r * cols.length + i + 1, t)).join(", ")})`
|
|
221
|
+
).join(", ")}`;
|
|
222
|
+
async function insertMany(query, table, cols, items, values, suffix = "") {
|
|
223
|
+
const results = [];
|
|
224
|
+
for (let i = 0; i < items.length; i += BATCH) {
|
|
225
|
+
const batch = items.slice(i, i + BATCH);
|
|
226
|
+
results.push(...await query(`${insertManySql(table, cols, batch.length)}${suffix}`, batch.flatMap(values)));
|
|
227
|
+
}
|
|
228
|
+
return results;
|
|
229
|
+
}
|
|
230
|
+
var ACCESS_WHERE = `($1::text IS NULL OR subject_id = $1::text) AND ($2::text IS NULL OR actor = $2::text) AND ($3::text IS NULL OR purpose = $3::text)
|
|
231
|
+
AND ($4::timestamptz IS NULL OR at >= $4::timestamptz) AND ($5::timestamptz IS NULL OR at < $5::timestamptz)`;
|
|
232
|
+
var accessParams = (f) => [f.subjectId ?? null, f.actor ?? null, f.purpose ?? null, f.from ?? null, f.to ?? null];
|
|
233
|
+
function postgresStore(query) {
|
|
234
|
+
return {
|
|
235
|
+
async lastEvent(subjectId) {
|
|
236
|
+
const [row] = await query(`SELECT ${EVENT_SELECT} FROM dpdp_consent_events WHERE subject_id = $1::text ORDER BY seq DESC LIMIT 1`, [subjectId]);
|
|
237
|
+
return row ? eventFromRow(row) : void 0;
|
|
238
|
+
},
|
|
239
|
+
async insert(e) {
|
|
240
|
+
try {
|
|
241
|
+
await query(insertSql("dpdp_consent_events", EVENT_COLUMNS), eventValues(e));
|
|
242
|
+
} catch (err) {
|
|
243
|
+
if (isUniqueViolation(err)) throw new SeqConflictError(e.subjectId, e.seq);
|
|
244
|
+
throw err;
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
async events(subjectId) {
|
|
248
|
+
return (await query(`SELECT ${EVENT_SELECT} FROM dpdp_consent_events WHERE subject_id = $1::text ORDER BY seq ASC`, [subjectId])).map(eventFromRow);
|
|
249
|
+
},
|
|
250
|
+
async insertRequest(r) {
|
|
251
|
+
await query(insertSql("dpdp_requests", REQUEST_COLUMNS), requestValues(r));
|
|
252
|
+
},
|
|
253
|
+
async getRequest(id) {
|
|
254
|
+
const [row] = await query(`SELECT ${REQUEST_SELECT} FROM dpdp_requests WHERE id = $1::text`, [id]);
|
|
255
|
+
return row ? requestFromRow(row) : void 0;
|
|
256
|
+
},
|
|
257
|
+
async updateRequest(r) {
|
|
258
|
+
await query(updateSql("dpdp_requests", REQUEST_COLUMNS), requestValues(r));
|
|
259
|
+
},
|
|
260
|
+
async listRequests(filter) {
|
|
261
|
+
const rows = await query(
|
|
262
|
+
`SELECT ${REQUEST_SELECT} FROM dpdp_requests WHERE ($1::text IS NULL OR subject_id = $1::text) AND ($2::text IS NULL OR status = $2::text) ORDER BY opened_at ASC`,
|
|
263
|
+
[filter.subjectId ?? null, filter.status ?? null]
|
|
264
|
+
);
|
|
265
|
+
return rows.map(requestFromRow);
|
|
266
|
+
},
|
|
267
|
+
async insertTasks(tasks) {
|
|
268
|
+
await insertMany(query, "dpdp_erasure_tasks", TASK_COLUMNS, tasks, taskValues);
|
|
269
|
+
},
|
|
270
|
+
async dueTasks({ limit, now, staleBefore, maxAttempts }) {
|
|
271
|
+
const rows = await query(`SELECT ${TASK_SELECT} FROM dpdp_erasure_tasks WHERE ${RUNNABLE} ORDER BY due_at ASC LIMIT $4::int`, [now, staleBefore, maxAttempts, limit]);
|
|
272
|
+
return rows.map(taskFromRow);
|
|
273
|
+
},
|
|
274
|
+
async claimTask({ id, now, staleBefore, maxAttempts }) {
|
|
275
|
+
const rows = await query(
|
|
276
|
+
`UPDATE dpdp_erasure_tasks SET status = 'running', claimed_at = $1::timestamptz WHERE id = $4::text AND ${RUNNABLE} RETURNING id`,
|
|
277
|
+
[now, staleBefore, maxAttempts, id]
|
|
278
|
+
);
|
|
279
|
+
return rows.length === 1;
|
|
280
|
+
},
|
|
281
|
+
async updateTask(t) {
|
|
282
|
+
await query(updateSql("dpdp_erasure_tasks", TASK_COLUMNS), taskValues(t));
|
|
283
|
+
},
|
|
284
|
+
async getTask(id) {
|
|
285
|
+
const [row] = await query(`SELECT ${TASK_SELECT} FROM dpdp_erasure_tasks WHERE id = $1::text`, [id]);
|
|
286
|
+
return row ? taskFromRow(row) : void 0;
|
|
287
|
+
},
|
|
288
|
+
async listTasks(filter) {
|
|
289
|
+
const rows = await query(
|
|
290
|
+
`SELECT ${TASK_SELECT} FROM dpdp_erasure_tasks WHERE ($1::text IS NULL OR subject_id = $1::text) AND ($2::text IS NULL OR status = $2::text) ORDER BY due_at ASC`,
|
|
291
|
+
[filter.subjectId ?? null, filter.status ?? null]
|
|
292
|
+
);
|
|
293
|
+
return rows.map(taskFromRow);
|
|
294
|
+
},
|
|
295
|
+
async insertAccess(entries) {
|
|
296
|
+
await insertMany(query, "dpdp_access_log", ACCESS_COLUMNS, entries, accessValues);
|
|
297
|
+
},
|
|
298
|
+
async listAccess(f) {
|
|
299
|
+
const rows = await query(`SELECT ${ACCESS_SELECT} FROM dpdp_access_log WHERE ${ACCESS_WHERE} ORDER BY at ASC, id ASC LIMIT $6::int`, [
|
|
300
|
+
...accessParams(f),
|
|
301
|
+
f.limit ?? null
|
|
302
|
+
]);
|
|
303
|
+
return rows.map(accessFromRow);
|
|
304
|
+
},
|
|
305
|
+
async accessSubjects(f) {
|
|
306
|
+
const rows = await query(`SELECT DISTINCT subject_id FROM dpdp_access_log WHERE ${ACCESS_WHERE}`, accessParams(f));
|
|
307
|
+
return rows.map((r) => String(r.subject_id));
|
|
308
|
+
},
|
|
309
|
+
async pruneAccess(before) {
|
|
310
|
+
let total = 0;
|
|
311
|
+
for (; ; ) {
|
|
312
|
+
const rows = await query(
|
|
313
|
+
`DELETE FROM dpdp_access_log WHERE id IN (SELECT id FROM dpdp_access_log WHERE at < $1::timestamptz LIMIT 5000) RETURNING 1 AS one`,
|
|
314
|
+
[before]
|
|
315
|
+
);
|
|
316
|
+
total += rows.length;
|
|
317
|
+
if (rows.length < 5e3) return total;
|
|
318
|
+
}
|
|
319
|
+
},
|
|
320
|
+
async insertBreach(b) {
|
|
321
|
+
await query(insertSql("dpdp_breaches", BREACH_COLUMNS), breachValues(b));
|
|
322
|
+
},
|
|
323
|
+
async getBreach(id) {
|
|
324
|
+
const [row] = await query(`SELECT data FROM dpdp_breaches WHERE id = $1::text`, [id]);
|
|
325
|
+
return row ? json(row.data) : void 0;
|
|
326
|
+
},
|
|
327
|
+
async updateBreach(b) {
|
|
328
|
+
const unchanged = `jsonb_array_length(data->'history') = $${BREACH_COLUMNS.length + 1}::int`;
|
|
329
|
+
const rows = await query(`${updateSql("dpdp_breaches", BREACH_COLUMNS)} AND ${unchanged} RETURNING 1 AS one`, [...breachValues(b), b.history.length - 1]);
|
|
330
|
+
return rows.length === 1;
|
|
331
|
+
},
|
|
332
|
+
async listBreaches(filter) {
|
|
333
|
+
const rows = await query(`SELECT data FROM dpdp_breaches WHERE ($1::text IS NULL OR status = $1::text) ORDER BY aware_at DESC`, [filter.status ?? null]);
|
|
334
|
+
return rows.map((r) => json(r.data));
|
|
335
|
+
},
|
|
336
|
+
async addNotices(list) {
|
|
337
|
+
const values = (n) => [n.breachId, n.subjectId, n.status, n.addedAt, n.sentAt ?? null, n.channel ?? null];
|
|
338
|
+
const rows = await insertMany(query, "dpdp_breach_notices", NOTICE_COLUMNS, list, values, " ON CONFLICT (breach_id, subject_id) DO NOTHING RETURNING 1 AS one");
|
|
339
|
+
return rows.length;
|
|
340
|
+
},
|
|
341
|
+
async markNotices(breachId, subjectIds, sentAt, channel) {
|
|
342
|
+
let changed = 0;
|
|
343
|
+
for (let i = 0; i < subjectIds.length; i += BATCH) {
|
|
344
|
+
const rows = await query(
|
|
345
|
+
`UPDATE dpdp_breach_notices SET status = 'sent', sent_at = $2::timestamptz, channel = $3::text
|
|
346
|
+
WHERE breach_id = $1::text AND status = 'pending' AND subject_id = ANY($4::text[]) RETURNING 1 AS one`,
|
|
347
|
+
[breachId, sentAt, channel ?? null, subjectIds.slice(i, i + BATCH)]
|
|
348
|
+
);
|
|
349
|
+
changed += rows.length;
|
|
350
|
+
}
|
|
351
|
+
return changed;
|
|
352
|
+
},
|
|
353
|
+
async listNotices(breachId, filter) {
|
|
354
|
+
const rows = await query(
|
|
355
|
+
`SELECT ${NOTICE_SELECT} FROM dpdp_breach_notices WHERE breach_id = $1::text AND ($2::text IS NULL OR status = $2::text)
|
|
356
|
+
ORDER BY added_at ASC, subject_id ASC LIMIT $3::int`,
|
|
357
|
+
[breachId, filter.status ?? null, filter.limit ?? null]
|
|
358
|
+
);
|
|
359
|
+
return rows.map(noticeFromRow);
|
|
360
|
+
},
|
|
361
|
+
async countNotices(breachId) {
|
|
362
|
+
const [r] = await query(
|
|
363
|
+
`SELECT count(*) AS total, count(*) FILTER (WHERE status = 'sent') AS sent, ${utc("min(sent_at)")} AS first, ${utc("max(sent_at)")} AS last
|
|
364
|
+
FROM dpdp_breach_notices WHERE breach_id = $1::text`,
|
|
365
|
+
[breachId]
|
|
366
|
+
);
|
|
367
|
+
return defined({ total: num(r?.total), sent: num(r?.sent), firstSentAt: iso(r?.first), lastSentAt: iso(r?.last) });
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
var SERIAL = { serial: "integer", serial4: "integer", bigserial: "bigint", serial8: "bigint", smallserial: "smallint", serial2: "smallint" };
|
|
372
|
+
function castType(sqlType) {
|
|
373
|
+
const base = sqlType.trim().replace(/\s*\([^)]*\)/g, "");
|
|
374
|
+
const array = base.endsWith("[]") ? "[]" : "";
|
|
375
|
+
const name = array ? base.slice(0, -2) : base;
|
|
376
|
+
const lower = name.toLowerCase();
|
|
377
|
+
if (Object.hasOwn(SERIAL, lower)) return SERIAL[lower] + array;
|
|
378
|
+
if (lower === "char" || lower === "character") return `bpchar${array}`;
|
|
379
|
+
return name + array;
|
|
380
|
+
}
|
|
381
|
+
function readable(column, sqlType) {
|
|
382
|
+
const type = sqlType ? castType(sqlType).toLowerCase() : "";
|
|
383
|
+
if (type === "timestamptz" || type === "timestamp with time zone") return utc(column);
|
|
384
|
+
if (type === "timestamp" || type === "timestamp without time zone") return `to_char(${column}, 'YYYY-MM-DD"T"HH24:MI:SS.MS')`;
|
|
385
|
+
return column;
|
|
386
|
+
}
|
|
387
|
+
var plainRow = (row) => Object.fromEntries(
|
|
388
|
+
Object.entries(row).map(([k, v]) => [k, typeof v === "bigint" ? Number.isSafeInteger(Number(v)) ? Number(v) : v.toString() : v])
|
|
389
|
+
);
|
|
390
|
+
function postgresData(query, options = {}) {
|
|
391
|
+
const tables = /* @__PURE__ */ new Map();
|
|
392
|
+
for (const table of options.schema?.tables ?? []) {
|
|
393
|
+
const columns = new Map(table.columns.map((c) => [c.key ?? c.name, c]));
|
|
394
|
+
for (const c of table.columns) if (!columns.has(c.name)) columns.set(c.name, c);
|
|
395
|
+
const entry = { name: table.name, columns };
|
|
396
|
+
if (!tables.has(table.name)) tables.set(table.name, entry);
|
|
397
|
+
if (table.key && !tables.has(table.key)) tables.set(table.key, entry);
|
|
398
|
+
}
|
|
399
|
+
const from = (table) => ident(tables.get(table)?.name ?? table);
|
|
400
|
+
const column = (table, name) => tables.get(table)?.columns.get(name);
|
|
401
|
+
const col = (table, name) => ident(column(table, name)?.name ?? name);
|
|
402
|
+
const param = (n, table, name) => {
|
|
403
|
+
const sqlType = column(table, name)?.sqlType;
|
|
404
|
+
return sqlType ? `$${n}::${castType(sqlType)}` : `$${n}`;
|
|
405
|
+
};
|
|
406
|
+
const where = (table, subjectColumn) => `${col(table, subjectColumn)} = ${param(1, table, subjectColumn)}`;
|
|
407
|
+
return {
|
|
408
|
+
async read(table, subjectColumn, subjectId, columns) {
|
|
409
|
+
if (columns.length === 0) return [];
|
|
410
|
+
const select = columns.map((c) => `${readable(col(table, c), column(table, c)?.sqlType)} AS ${ident(c)}`).join(", ");
|
|
411
|
+
const rows = await query(`SELECT ${select} FROM ${from(table)} WHERE ${where(table, subjectColumn)}`, [subjectId]);
|
|
412
|
+
return rows.map(plainRow);
|
|
413
|
+
},
|
|
414
|
+
async eraseFields(table, subjectColumn, subjectId, columns, values = {}) {
|
|
415
|
+
if (columns.length === 0) return 0;
|
|
416
|
+
const params = [subjectId];
|
|
417
|
+
const set = columns.map((c) => {
|
|
418
|
+
if (!Object.hasOwn(values, c) || values[c] === null) return `${col(table, c)} = NULL`;
|
|
419
|
+
params.push(values[c]);
|
|
420
|
+
return `${col(table, c)} = ${param(params.length, table, c)}`;
|
|
421
|
+
}).join(", ");
|
|
422
|
+
const rows = await query(`UPDATE ${from(table)} SET ${set} WHERE ${where(table, subjectColumn)} RETURNING 1 AS one`, params);
|
|
423
|
+
return rows.length;
|
|
424
|
+
},
|
|
425
|
+
async deleteRows(table, subjectColumn, subjectId) {
|
|
426
|
+
const rows = await query(`DELETE FROM ${from(table)} WHERE ${where(table, subjectColumn)} RETURNING 1 AS one`, [subjectId]);
|
|
427
|
+
return rows.length;
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
export {
|
|
433
|
+
ident,
|
|
434
|
+
isUniqueViolation,
|
|
435
|
+
postgresStore,
|
|
436
|
+
castType,
|
|
437
|
+
postgresData
|
|
438
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
audit,
|
|
4
|
-
checkSchema
|
|
5
|
-
|
|
4
|
+
checkSchema,
|
|
5
|
+
formatSafeguards,
|
|
6
|
+
safeguardsReport
|
|
7
|
+
} from "./chunk-HXVP446N.js";
|
|
8
|
+
import "./chunk-67Q5GFD6.js";
|
|
6
9
|
|
|
7
10
|
// src/run-audit.ts
|
|
8
|
-
import { existsSync } from "fs";
|
|
9
|
-
import { relative, resolve } from "path";
|
|
11
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
|
|
12
|
+
import { join, relative, resolve } from "path";
|
|
10
13
|
import { pathToFileURL } from "url";
|
|
11
14
|
var CONFIG_FILES = ["dpdp.config.ts", "dpdp.config.mts", "dpdp.config.js", "dpdp.config.mjs"];
|
|
12
15
|
function findConfig(cwd, explicit) {
|
|
@@ -50,6 +53,9 @@ function formatGithubAnnotations(issues, files, root) {
|
|
|
50
53
|
}
|
|
51
54
|
async function loadDrizzleSchema(file) {
|
|
52
55
|
const mod = await loadModule(file);
|
|
56
|
+
const { mongooseSchema } = await import("./mongodb.js");
|
|
57
|
+
const mongoose = mongooseSchema(mod);
|
|
58
|
+
if (mongoose.tables.length > 0) return mongoose;
|
|
53
59
|
let drizzleSchema;
|
|
54
60
|
try {
|
|
55
61
|
({ drizzleSchema } = await import("./drizzle.js"));
|
|
@@ -58,16 +64,33 @@ async function loadDrizzleSchema(file) {
|
|
|
58
64
|
}
|
|
59
65
|
const schema = drizzleSchema(mod);
|
|
60
66
|
if (schema.tables.length === 0) {
|
|
61
|
-
throw new Error(`${file} exports no Drizzle tables. Point --schema at the file that defines them.`);
|
|
67
|
+
throw new Error(`${file} exports no Drizzle tables or Mongoose models. Point --schema at the file that defines them.`);
|
|
62
68
|
}
|
|
63
69
|
return schema;
|
|
64
70
|
}
|
|
71
|
+
async function loadPrismaSchema(path) {
|
|
72
|
+
const files = statSync(path).isDirectory() ? readdirSync(path, { recursive: true, encoding: "utf8" }).filter((f) => f.endsWith(".prisma")).sort().map((f) => join(path, f)) : [path];
|
|
73
|
+
const { prismaSchema } = await import("./prisma.js");
|
|
74
|
+
const schema = prismaSchema(files.map((f) => readFileSync(f, "utf8")).join("\n"));
|
|
75
|
+
if (schema.tables.length === 0) throw new Error(`${path} has no Prisma models. Point --schema at schema.prisma.`);
|
|
76
|
+
return schema;
|
|
77
|
+
}
|
|
78
|
+
function loadSchema(path) {
|
|
79
|
+
const prisma = path.endsWith(".prisma") || statSync(path).isDirectory() && readdirSync(path, { recursive: true, encoding: "utf8" }).some((f) => f.endsWith(".prisma"));
|
|
80
|
+
return prisma ? loadPrismaSchema(path) : loadDrizzleSchema(path);
|
|
81
|
+
}
|
|
65
82
|
var flagValue = (argv, flag) => {
|
|
66
83
|
const i = argv.indexOf(flag);
|
|
67
84
|
return i >= 0 ? argv[i + 1] : void 0;
|
|
68
85
|
};
|
|
69
|
-
|
|
86
|
+
function configFile(argv, cwd, log) {
|
|
70
87
|
const explicit = flagValue(argv, "--config");
|
|
88
|
+
const file = findConfig(cwd, explicit);
|
|
89
|
+
if (file && existsSync(file)) return file;
|
|
90
|
+
log(explicit ? `Config file not found: ${explicit}` : `No config found. Create one of: ${CONFIG_FILES.join(", ")}`);
|
|
91
|
+
return void 0;
|
|
92
|
+
}
|
|
93
|
+
async function runAudit(argv, cwd, log) {
|
|
71
94
|
const schemaFlag = flagValue(argv, "--schema");
|
|
72
95
|
const strict = argv.includes("--strict");
|
|
73
96
|
const format = flagValue(argv, "--format") ?? "text";
|
|
@@ -75,11 +98,8 @@ async function runAudit(argv, cwd, log) {
|
|
|
75
98
|
log(`Unknown --format ${JSON.stringify(format)}. Use "text" or "github".`);
|
|
76
99
|
return 1;
|
|
77
100
|
}
|
|
78
|
-
const file =
|
|
79
|
-
if (!file
|
|
80
|
-
log(explicit ? `Config file not found: ${explicit}` : `No config found. Create one of: ${CONFIG_FILES.join(", ")}`);
|
|
81
|
-
return 1;
|
|
82
|
-
}
|
|
101
|
+
const file = configFile(argv, cwd, log);
|
|
102
|
+
if (!file) return 1;
|
|
83
103
|
const schemaFile = schemaFlag ? resolve(cwd, schemaFlag) : void 0;
|
|
84
104
|
if (schemaFile && !existsSync(schemaFile)) {
|
|
85
105
|
log(`Schema file not found: ${schemaFlag}`);
|
|
@@ -87,7 +107,7 @@ async function runAudit(argv, cwd, log) {
|
|
|
87
107
|
}
|
|
88
108
|
const config = await loadConfig(file);
|
|
89
109
|
const issues = audit(config);
|
|
90
|
-
if (schemaFile) issues.push(...checkSchema(config, await
|
|
110
|
+
if (schemaFile) issues.push(...checkSchema(config, await loadSchema(schemaFile)));
|
|
91
111
|
if (format === "github" && issues.length > 0) {
|
|
92
112
|
log(formatGithubAnnotations(issues, { config: file, schema: schemaFile }, process.env.GITHUB_WORKSPACE ?? cwd));
|
|
93
113
|
}
|
|
@@ -95,26 +115,42 @@ async function runAudit(argv, cwd, log) {
|
|
|
95
115
|
const failing = issues.filter((i) => i.level === "error" || strict && i.level === "warning");
|
|
96
116
|
return failing.length > 0 ? 1 : 0;
|
|
97
117
|
}
|
|
118
|
+
async function runSafeguards(argv, cwd, log) {
|
|
119
|
+
const file = configFile(argv, cwd, log);
|
|
120
|
+
if (!file) return 1;
|
|
121
|
+
const report = safeguardsReport(await loadConfig(file));
|
|
122
|
+
log(formatSafeguards(report));
|
|
123
|
+
const gaps = report.some((s) => s.status === "missing" || s.status === "partly");
|
|
124
|
+
return argv.includes("--strict") && gaps ? 1 : 0;
|
|
125
|
+
}
|
|
98
126
|
|
|
99
127
|
// src/cli.ts
|
|
100
128
|
var HELP = `dpdp-kit \u2014 DPDP Act tooling for Node.js
|
|
101
129
|
|
|
102
130
|
Usage:
|
|
103
131
|
dpdp-kit audit [--config <file>] [--schema <file>] [--strict] [--format text|github]
|
|
132
|
+
dpdp-kit safeguards [--config <file>] [--strict]
|
|
104
133
|
|
|
105
134
|
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,
|
|
107
|
-
longer exist.
|
|
135
|
+
Drizzle, Prisma or Mongoose schema: personal-looking columns missing from the map, map entries that no
|
|
136
|
+
longer exist, and erase settings a column can't take.
|
|
108
137
|
|
|
109
138
|
Options:
|
|
110
139
|
--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
|
|
140
|
+
--schema <file> Drizzle schema file (Postgres, MySQL or SQLite), e.g. src/db/schema.ts,
|
|
141
|
+
or Prisma schema, e.g. prisma/schema.prisma (or a folder of .prisma files)
|
|
142
|
+
or a file that exports Mongoose models, e.g. src/models.ts
|
|
112
143
|
--strict Fail on warnings too
|
|
113
144
|
--format github Also print GitHub Actions annotations, so problems show on the pull request
|
|
145
|
+
|
|
146
|
+
safeguards reports each security safeguard of Rule 6(1), (a) to (g): what your config records
|
|
147
|
+
and what's missing. With --strict, exits 1 when one is missing.
|
|
114
148
|
`;
|
|
115
149
|
var [command, ...rest] = process.argv.slice(2);
|
|
116
|
-
|
|
117
|
-
|
|
150
|
+
var commands = { audit: runAudit, safeguards: runSafeguards };
|
|
151
|
+
var run = command !== void 0 && Object.hasOwn(commands, command) ? commands[command] : void 0;
|
|
152
|
+
if (run) {
|
|
153
|
+
run(rest, process.cwd(), (line) => console.log(line)).then(
|
|
118
154
|
(code) => {
|
|
119
155
|
process.exitCode = code;
|
|
120
156
|
},
|
package/dist/config.d.ts
CHANGED
|
@@ -34,12 +34,50 @@ export interface PurposeDef {
|
|
|
34
34
|
}
|
|
35
35
|
/** Kind of personal data. Free text is allowed; these are the common ones. */
|
|
36
36
|
export type Category = "contact" | "identity" | "financial" | "health" | "location" | "device" | "biometric" | "behavioural" | (string & {});
|
|
37
|
+
/**
|
|
38
|
+
* How a field is protected where it's stored. Rule 6(1)(a) of the DPDP Rules lists "encryption,
|
|
39
|
+
* obfuscation, masking or the use of virtual tokens mapped to that personal data" as safeguards.
|
|
40
|
+
* - `"encrypted"`: stored encrypted (e.g. with `createEncryptor` from `dpdp-kit/protect`).
|
|
41
|
+
* - `"tokenised"`: replaced by a token that maps to the value kept elsewhere (a vault).
|
|
42
|
+
* - `"masked"`: only a masked form is stored (e.g. the last four digits of an Aadhaar number).
|
|
43
|
+
* - `"hashed"`: only a keyed hash is stored, for matching (e.g. `createTokenizer`).
|
|
44
|
+
*/
|
|
45
|
+
export type Protection = "encrypted" | "tokenised" | "masked" | "hashed";
|
|
46
|
+
/** A value a field can be set to when it's erased. */
|
|
47
|
+
export type ErasedValue = string | number | boolean | null;
|
|
48
|
+
/** Text written by `erase: "redact"`. */
|
|
49
|
+
export declare const REDACTED = "[erased]";
|
|
50
|
+
/**
|
|
51
|
+
* What a field becomes when it's erased. Default `"null"`.
|
|
52
|
+
* - `"null"`: set to `NULL`. The column must be nullable.
|
|
53
|
+
* - `"redact"`: set to the text `"[erased]"`, for `NOT NULL` text columns.
|
|
54
|
+
* - `{ value }`: a fixed value, or a function of the person and field, for columns with other
|
|
55
|
+
* constraints, e.g. a `NOT NULL UNIQUE` email:
|
|
56
|
+
* `{ value: ({ subjectId }) => \`erased-${subjectId}@invalid\` }`.
|
|
57
|
+
*
|
|
58
|
+
* The replacement must not identify the person: use their internal id, never a hash of their data.
|
|
59
|
+
*/
|
|
60
|
+
export type EraseWith = "null" | "redact" | {
|
|
61
|
+
value: ErasedValue | ((field: {
|
|
62
|
+
subjectId: string;
|
|
63
|
+
table: string;
|
|
64
|
+
field: string;
|
|
65
|
+
}) => ErasedValue);
|
|
66
|
+
};
|
|
37
67
|
export interface PersonalField<P extends string = string> {
|
|
38
68
|
category: Category;
|
|
39
69
|
/** Every purpose this field is held for. When all of them end, the field must go. */
|
|
40
70
|
purposes: readonly P[];
|
|
41
71
|
/** This field can tell you the person is under 18 (e.g. date of birth). Used from v0.3. */
|
|
42
72
|
childSignal?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* How the field is protected at rest (Rule 6(1)(a)). The audit warns when a high-risk
|
|
75
|
+
* identifier (Aadhaar, PAN, passport, bank account, card number, health or biometric data)
|
|
76
|
+
* has none.
|
|
77
|
+
*/
|
|
78
|
+
protect?: Protection;
|
|
79
|
+
/** What the field becomes when it's erased. Default `"null"`. See `EraseWith`. */
|
|
80
|
+
erase?: EraseWith;
|
|
43
81
|
}
|
|
44
82
|
/**
|
|
45
83
|
* Another law says you must keep these rows (e.g. tax or KYC records). Section 8(7) requires
|
|
@@ -63,6 +101,29 @@ export interface TableDef<P extends string = string> {
|
|
|
63
101
|
export interface ProcessorDef<P extends string = string> {
|
|
64
102
|
purposes: readonly P[];
|
|
65
103
|
description?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Where your contract with this processor requires reasonable security safeguards, e.g.
|
|
106
|
+
* `"DPA signed 2026-03-01, clause 7"`. Rule 6(1)(f) requires such a provision. The audit warns
|
|
107
|
+
* when it's missing.
|
|
108
|
+
*/
|
|
109
|
+
contract?: string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The security safeguards of Rule 6(1) that live outside your code, written down as evidence.
|
|
113
|
+
* Describe what you actually do, in a sentence; the audit warns about any you leave out.
|
|
114
|
+
*/
|
|
115
|
+
export interface SecurityConfig {
|
|
116
|
+
/** Rule 6(1)(b): how access to your computer resources is controlled, e.g. `"SSO, least-privilege IAM roles"`. */
|
|
117
|
+
accessControl?: string;
|
|
118
|
+
/** Rule 6(1)(d): how processing continues after a compromise, e.g. `"Daily encrypted backups, restore tested monthly"`. */
|
|
119
|
+
backups?: string;
|
|
120
|
+
/** Rule 6(1)(g): technical and organisational measures, e.g. `"Annual security training, quarterly access reviews"`. */
|
|
121
|
+
measures?: string;
|
|
122
|
+
/**
|
|
123
|
+
* Rule 6(1)(c) and (e): how long the access log (`dpdp.access`) is kept. Default and minimum
|
|
124
|
+
* `"1y"`: the Rule requires a year of logs to detect and investigate unauthorised access.
|
|
125
|
+
*/
|
|
126
|
+
accessLogRetention?: Duration;
|
|
66
127
|
}
|
|
67
128
|
export interface RightsConfig {
|
|
68
129
|
/**
|
|
@@ -107,6 +168,8 @@ export interface DpdpConfig<P extends string = string> {
|
|
|
107
168
|
*/
|
|
108
169
|
notPersonal?: readonly string[];
|
|
109
170
|
rights?: RightsConfig;
|
|
171
|
+
/** Security safeguards under Rule 6(1). See `SecurityConfig`. */
|
|
172
|
+
security?: SecurityConfig;
|
|
110
173
|
}
|
|
111
174
|
/** Purpose names of a config, e.g. `"account" | "marketing_sms"`. */
|
|
112
175
|
export type PurposeOf<C> = C extends DpdpConfig<infer P> ? P : never;
|
|
@@ -116,3 +179,9 @@ export type PurposeOf<C> = C extends DpdpConfig<infer P> ? P : never;
|
|
|
116
179
|
*/
|
|
117
180
|
export declare function defineConfig<const P extends string>(config: DpdpConfig<P>): DpdpConfig<P>;
|
|
118
181
|
export declare function purposeNames<P extends string>(config: DpdpConfig<P>): P[];
|
|
182
|
+
/** The value a field is set to when it's erased, from its `erase` setting. */
|
|
183
|
+
export declare function erasedValue(erase: EraseWith | undefined, field: {
|
|
184
|
+
subjectId: string;
|
|
185
|
+
table: string;
|
|
186
|
+
field: string;
|
|
187
|
+
}): ErasedValue;
|