dpdp-kit 0.3.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 +53 -0
- package/README.md +115 -14
- package/dist/access.d.ts +5 -0
- package/dist/audit.d.ts +1 -1
- package/dist/breach.d.ts +170 -0
- package/dist/chunk-2ELTZSW3.js +94 -0
- package/dist/{chunk-YOEAZX65.js → chunk-HXVP446N.js} +118 -4
- package/dist/chunk-KLMR7S7I.js +438 -0
- package/dist/cli.js +24 -8
- package/dist/ddl.d.ts +10 -0
- package/dist/dpdp.d.ts +66 -2
- package/dist/drizzle.d.ts +213 -11
- package/dist/drizzle.js +138 -77
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +235 -1
- package/dist/mongodb.d.ts +87 -0
- package/dist/mongodb.js +344 -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/run-audit.d.ts +8 -1
- package/dist/schema-check.d.ts +20 -1
- package/dist/store.d.ts +4 -1
- package/package.json +25 -3
|
@@ -40,15 +40,73 @@ function memoryStore() {
|
|
|
40
40
|
const requests = /* @__PURE__ */ new Map();
|
|
41
41
|
const tasks = /* @__PURE__ */ new Map();
|
|
42
42
|
const accessLog = [];
|
|
43
|
+
const breaches = /* @__PURE__ */ new Map();
|
|
44
|
+
const notices = /* @__PURE__ */ new Map();
|
|
45
|
+
const noticeKey = (breachId, subjectId) => `${breachId}\0${subjectId}`;
|
|
46
|
+
const noticesOf = (breachId) => [...notices.values()].filter((n) => n.breachId === breachId);
|
|
43
47
|
const byDue = (a, b) => a.dueAt < b.dueAt ? -1 : a.dueAt > b.dueAt ? 1 : 0;
|
|
44
48
|
return {
|
|
45
49
|
all,
|
|
46
50
|
requests,
|
|
47
51
|
tasks,
|
|
48
52
|
accessLog,
|
|
53
|
+
breaches,
|
|
54
|
+
notices,
|
|
55
|
+
async insertBreach(breach) {
|
|
56
|
+
breaches.set(breach.id, structuredClone(breach));
|
|
57
|
+
},
|
|
58
|
+
async getBreach(id) {
|
|
59
|
+
const b = breaches.get(id);
|
|
60
|
+
return b && structuredClone(b);
|
|
61
|
+
},
|
|
62
|
+
async updateBreach(breach) {
|
|
63
|
+
const stored = breaches.get(breach.id);
|
|
64
|
+
if (!stored || stored.history.length !== breach.history.length - 1) return false;
|
|
65
|
+
breaches.set(breach.id, structuredClone(breach));
|
|
66
|
+
return true;
|
|
67
|
+
},
|
|
68
|
+
async listBreaches(filter) {
|
|
69
|
+
return [...breaches.values()].filter((b) => !filter.status || b.status === filter.status).sort((a, b) => a.awareAt < b.awareAt ? 1 : a.awareAt > b.awareAt ? -1 : 0).map((b) => structuredClone(b));
|
|
70
|
+
},
|
|
71
|
+
async addNotices(list) {
|
|
72
|
+
let added = 0;
|
|
73
|
+
for (const n of list) {
|
|
74
|
+
const key = noticeKey(n.breachId, n.subjectId);
|
|
75
|
+
if (notices.has(key)) continue;
|
|
76
|
+
notices.set(key, structuredClone(n));
|
|
77
|
+
added++;
|
|
78
|
+
}
|
|
79
|
+
return added;
|
|
80
|
+
},
|
|
81
|
+
async markNotices(breachId, subjectIds, sentAt, channel) {
|
|
82
|
+
let changed = 0;
|
|
83
|
+
for (const subjectId of subjectIds) {
|
|
84
|
+
const n = notices.get(noticeKey(breachId, subjectId));
|
|
85
|
+
if (!n || n.status === "sent") continue;
|
|
86
|
+
notices.set(noticeKey(breachId, subjectId), { ...n, status: "sent", sentAt, ...channel !== void 0 ? { channel } : {} });
|
|
87
|
+
changed++;
|
|
88
|
+
}
|
|
89
|
+
return changed;
|
|
90
|
+
},
|
|
91
|
+
async listNotices(breachId, filter) {
|
|
92
|
+
const list = noticesOf(breachId).filter((n) => !filter.status || n.status === filter.status).sort((a, b) => a.addedAt < b.addedAt ? -1 : a.addedAt > b.addedAt ? 1 : 0).map((n) => structuredClone(n));
|
|
93
|
+
return filter.limit === void 0 ? list : list.slice(0, filter.limit);
|
|
94
|
+
},
|
|
95
|
+
async countNotices(breachId) {
|
|
96
|
+
const list = noticesOf(breachId);
|
|
97
|
+
const sentTimes = list.flatMap((n) => n.sentAt ? [n.sentAt] : []).sort();
|
|
98
|
+
return {
|
|
99
|
+
total: list.length,
|
|
100
|
+
sent: sentTimes.length,
|
|
101
|
+
...sentTimes.length ? { firstSentAt: sentTimes[0], lastSentAt: sentTimes[sentTimes.length - 1] } : {}
|
|
102
|
+
};
|
|
103
|
+
},
|
|
49
104
|
async insertAccess(entries) {
|
|
50
105
|
for (const e of entries) accessLog.push(structuredClone(e));
|
|
51
106
|
},
|
|
107
|
+
async accessSubjects(filter) {
|
|
108
|
+
return [...new Set((await this.listAccess(filter)).map((e) => e.subjectId))];
|
|
109
|
+
},
|
|
52
110
|
async listAccess(filter) {
|
|
53
111
|
const list = accessLog.filter(
|
|
54
112
|
(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)
|
|
@@ -464,6 +522,14 @@ function audit(config) {
|
|
|
464
522
|
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
523
|
});
|
|
466
524
|
}
|
|
525
|
+
if (!config.fiduciary?.dpo) {
|
|
526
|
+
issues.push({
|
|
527
|
+
level: "warning",
|
|
528
|
+
code: "contact-missing",
|
|
529
|
+
path: "fiduciary.dpo",
|
|
530
|
+
message: "Add your Data Protection Officer, or a person who can answer questions about personal data, with their business contact. Section 8(9) requires publishing it, and Rule 7(1)(e) requires it in every breach notice."
|
|
531
|
+
});
|
|
532
|
+
}
|
|
467
533
|
if (known.size === 0) {
|
|
468
534
|
issues.push({ level: "error", code: "no-purposes", path: "purposes", message: "Declare at least one purpose." });
|
|
469
535
|
}
|
|
@@ -761,6 +827,7 @@ function checkSchema(config, schema) {
|
|
|
761
827
|
const issues = [];
|
|
762
828
|
const byName = /* @__PURE__ */ new Map();
|
|
763
829
|
for (const table of schema.tables) if (!byName.has(table.name)) byName.set(table.name, table);
|
|
830
|
+
for (const table of schema.tables) if (table.key && !byName.has(table.key)) byName.set(table.key, table);
|
|
764
831
|
const mapped = /* @__PURE__ */ new Map();
|
|
765
832
|
for (const [key, def] of Object.entries(config.data ?? {})) {
|
|
766
833
|
const table = byName.get(def.table);
|
|
@@ -816,13 +883,14 @@ function checkSchema(config, schema) {
|
|
|
816
883
|
});
|
|
817
884
|
}
|
|
818
885
|
}
|
|
819
|
-
for (const table of byName.values()) {
|
|
820
|
-
|
|
886
|
+
for (const table of new Set(byName.values())) {
|
|
887
|
+
const tableNames = table.key ? [table.name, table.key] : [table.name];
|
|
888
|
+
if (table.name.startsWith(OWN_TABLE_PREFIX) || tableNames.some((t) => ignore.tables.has(t))) continue;
|
|
821
889
|
const known = mapped.get(table.name) ?? [];
|
|
822
890
|
for (const column of table.columns) {
|
|
823
891
|
if (known.includes(column)) continue;
|
|
824
|
-
|
|
825
|
-
if (
|
|
892
|
+
const columnNames = column.key ? [column.name, column.key] : [column.name];
|
|
893
|
+
if (tableNames.some((t) => columnNames.some((c) => ignore.columns.has(`${t}.${c}`)))) continue;
|
|
826
894
|
const guess = strongest(guessPersonal(column.name), column.key ? guessPersonal(column.key) : void 0);
|
|
827
895
|
if (!guess) continue;
|
|
828
896
|
issues.push({
|
|
@@ -833,6 +901,52 @@ function checkSchema(config, schema) {
|
|
|
833
901
|
});
|
|
834
902
|
}
|
|
835
903
|
}
|
|
904
|
+
issues.push(...checkHeldReferences(config, byName));
|
|
905
|
+
return issues;
|
|
906
|
+
}
|
|
907
|
+
function checkHeldReferences(config, byName) {
|
|
908
|
+
const issues = [];
|
|
909
|
+
const entries = Object.entries(config.data ?? {});
|
|
910
|
+
const deleted = new Set(entries.filter(([, def]) => !def.legalHold).flatMap(([, def]) => byName.get(def.table) ? [byName.get(def.table).name] : []));
|
|
911
|
+
for (const [key, def] of entries) {
|
|
912
|
+
const hold = def.legalHold;
|
|
913
|
+
const held = byName.get(def.table);
|
|
914
|
+
if (!hold || !held) continue;
|
|
915
|
+
const subject = held.columns.find((c) => matches(c, def.subject))?.name;
|
|
916
|
+
for (const ref of held.references ?? []) {
|
|
917
|
+
const target = byName.get(ref.table)?.name ?? ref.table;
|
|
918
|
+
if (!deleted.has(target) || target === held.name) continue;
|
|
919
|
+
const where = `${held.name}.${ref.columns.join(", ")}`;
|
|
920
|
+
const path = `schema.${held.name}.${ref.columns.join(",")}`;
|
|
921
|
+
const keep = `the legal hold (${hold.reason}, ${hold.keep})`;
|
|
922
|
+
const plain = `Drop the foreign key and keep ${ref.columns.join(", ")} as a plain, indexed column, so the ${held.name} rows outlive the account.`;
|
|
923
|
+
const onDelete = ref.onDelete ?? "no action";
|
|
924
|
+
if (onDelete === "cascade") {
|
|
925
|
+
issues.push({
|
|
926
|
+
level: "error",
|
|
927
|
+
code: "held-rows-cascade",
|
|
928
|
+
path,
|
|
929
|
+
message: `${where} references ${target} with ON DELETE CASCADE, so deleting an account also deletes the ${held.name} rows that ${keep} says to keep. ${plain}`
|
|
930
|
+
});
|
|
931
|
+
} else if (onDelete === "set null" || onDelete === "set default") {
|
|
932
|
+
if (subject && ref.columns.includes(subject)) {
|
|
933
|
+
issues.push({
|
|
934
|
+
level: "error",
|
|
935
|
+
code: "held-link-lost",
|
|
936
|
+
path,
|
|
937
|
+
message: `${where} references ${target} with ON DELETE ${onDelete.toUpperCase()}, so deleting an account clears whose ${held.name} rows they are, and they can't be found to ${hold.then} when ${keep} ends. ${plain}`
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
} else {
|
|
941
|
+
issues.push({
|
|
942
|
+
level: "warning",
|
|
943
|
+
code: "held-rows-block-delete",
|
|
944
|
+
path,
|
|
945
|
+
message: `${where} references ${target} (ON DELETE ${onDelete.toUpperCase()}), so deleting an account fails while ${keep} keeps the ${held.name} rows. The erasure task will fail and retry. ${plain}`
|
|
946
|
+
});
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
836
950
|
return issues;
|
|
837
951
|
}
|
|
838
952
|
|
|
@@ -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
|
@@ -4,12 +4,12 @@ import {
|
|
|
4
4
|
checkSchema,
|
|
5
5
|
formatSafeguards,
|
|
6
6
|
safeguardsReport
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-HXVP446N.js";
|
|
8
8
|
import "./chunk-67Q5GFD6.js";
|
|
9
9
|
|
|
10
10
|
// src/run-audit.ts
|
|
11
|
-
import { existsSync } from "fs";
|
|
12
|
-
import { relative, resolve } from "path";
|
|
11
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
|
|
12
|
+
import { join, relative, resolve } from "path";
|
|
13
13
|
import { pathToFileURL } from "url";
|
|
14
14
|
var CONFIG_FILES = ["dpdp.config.ts", "dpdp.config.mts", "dpdp.config.js", "dpdp.config.mjs"];
|
|
15
15
|
function findConfig(cwd, explicit) {
|
|
@@ -53,6 +53,9 @@ function formatGithubAnnotations(issues, files, root) {
|
|
|
53
53
|
}
|
|
54
54
|
async function loadDrizzleSchema(file) {
|
|
55
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;
|
|
56
59
|
let drizzleSchema;
|
|
57
60
|
try {
|
|
58
61
|
({ drizzleSchema } = await import("./drizzle.js"));
|
|
@@ -61,10 +64,21 @@ async function loadDrizzleSchema(file) {
|
|
|
61
64
|
}
|
|
62
65
|
const schema = drizzleSchema(mod);
|
|
63
66
|
if (schema.tables.length === 0) {
|
|
64
|
-
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.`);
|
|
65
68
|
}
|
|
66
69
|
return schema;
|
|
67
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
|
+
}
|
|
68
82
|
var flagValue = (argv, flag) => {
|
|
69
83
|
const i = argv.indexOf(flag);
|
|
70
84
|
return i >= 0 ? argv[i + 1] : void 0;
|
|
@@ -93,7 +107,7 @@ async function runAudit(argv, cwd, log) {
|
|
|
93
107
|
}
|
|
94
108
|
const config = await loadConfig(file);
|
|
95
109
|
const issues = audit(config);
|
|
96
|
-
if (schemaFile) issues.push(...checkSchema(config, await
|
|
110
|
+
if (schemaFile) issues.push(...checkSchema(config, await loadSchema(schemaFile)));
|
|
97
111
|
if (format === "github" && issues.length > 0) {
|
|
98
112
|
log(formatGithubAnnotations(issues, { config: file, schema: schemaFile }, process.env.GITHUB_WORKSPACE ?? cwd));
|
|
99
113
|
}
|
|
@@ -118,12 +132,14 @@ Usage:
|
|
|
118
132
|
dpdp-kit safeguards [--config <file>] [--strict]
|
|
119
133
|
|
|
120
134
|
Checks your data map and purposes. With --schema, also compares the data map with your
|
|
121
|
-
Drizzle schema: personal-looking columns missing from the map,
|
|
122
|
-
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.
|
|
123
137
|
|
|
124
138
|
Options:
|
|
125
139
|
--config <file> Config file (default: dpdp.config.ts / .mts / .js / .mjs)
|
|
126
|
-
--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
|
|
127
143
|
--strict Fail on warnings too
|
|
128
144
|
--format github Also print GitHub Actions annotations, so problems show on the pull request
|
|
129
145
|
|