open-privacy-protocol 0.1.0-alpha.1 → 0.2.0-alpha.1
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 +28 -1
- package/dist/client.d.ts +73 -4
- package/dist/client.js +21 -6
- package/dist/core.d.ts +143 -11
- package/dist/core.js +223 -18
- package/dist/data.d.ts +456 -3
- package/dist/data.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/package.json +4 -3
package/dist/core.js
CHANGED
|
@@ -3,12 +3,31 @@
|
|
|
3
3
|
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
4
4
|
import YAML from "yaml";
|
|
5
5
|
import { BASELINE_CATALOG, PRESETS_DOCUMENT, SCHEMAS } from "./data.js";
|
|
6
|
-
export const SPEC_VERSION = "0.
|
|
6
|
+
export const SPEC_VERSION = "0.2.0.dev0";
|
|
7
7
|
export const SENSITIVITY = ["public", "internal", "confidential", "restricted"];
|
|
8
8
|
export const HANDLING = ["allow", "generalize", "pseudonymize", "redact", "block"];
|
|
9
9
|
export const DESTINATIONS = ["internal", "partner", "external_ai", "public"];
|
|
10
10
|
/** Strictness order of actions (spec section 5, step 4). */
|
|
11
11
|
export const ACTION_ORDER = ["allow", "generalize", "pseudonymize", "redact", "review", "block"];
|
|
12
|
+
/** How a destination processes content (spec section 2.6). */
|
|
13
|
+
export const MECHANISMS = ["plaintext", "tee", "fhe", "mpc", "local"];
|
|
14
|
+
/** How far a capability claim is backed, lowest first (spec section 2.7). */
|
|
15
|
+
export const ASSURANCE = ["asserted", "approved", "verified"];
|
|
16
|
+
/** Who can read the content while it is processed, most protective first. */
|
|
17
|
+
export const PLAINTEXT_ACCESS = ["none", "enclave", "provider"];
|
|
18
|
+
export const RETENTION = ["none", "transient", "retained"];
|
|
19
|
+
/** What the sender must do when a decision carries it (spec section 2.8). */
|
|
20
|
+
export const OBLIGATIONS = ["enforce_mode", "no_plaintext_fallback", "check_output", "record_decision", "human_approval"];
|
|
21
|
+
const VERIFIABLE_EVIDENCE = ["attestation", "client_build"];
|
|
22
|
+
/** The mode assumed when a request names only a destination category: an ordinary API that reads the content. */
|
|
23
|
+
export const PLAINTEXT_MODE = Object.freeze({
|
|
24
|
+
id: "plaintext",
|
|
25
|
+
mechanism: "plaintext",
|
|
26
|
+
plaintext_access: "provider",
|
|
27
|
+
key_custody: "provider",
|
|
28
|
+
retention: "retained",
|
|
29
|
+
assurance: "asserted",
|
|
30
|
+
});
|
|
12
31
|
export const DEFAULT_RULES = {
|
|
13
32
|
public: { internal: "allow", partner: "allow", external_ai: "allow", public: "allow" },
|
|
14
33
|
internal: { internal: "allow", partner: "allow", external_ai: "allow", public: "review" },
|
|
@@ -41,13 +60,40 @@ export function read(src) {
|
|
|
41
60
|
return doc;
|
|
42
61
|
}
|
|
43
62
|
export function kindOf(doc) {
|
|
63
|
+
if ("destinations" in doc)
|
|
64
|
+
return "destinations";
|
|
44
65
|
if ("presets" in doc)
|
|
45
66
|
return "presets";
|
|
46
67
|
if ("classes" in doc)
|
|
47
68
|
return "catalog";
|
|
48
69
|
if ("catalog" in doc)
|
|
49
70
|
return "model";
|
|
50
|
-
throw new OppError("not an OPP document: no 'classes' (catalog)
|
|
71
|
+
throw new OppError("not an OPP document: no 'classes' (catalog), 'catalog' (model), 'presets' or 'destinations' at the root");
|
|
72
|
+
}
|
|
73
|
+
function minor(version) {
|
|
74
|
+
const m = /^(\d+)\.(\d+)/.exec(String(version));
|
|
75
|
+
return m ? [Number(m[1]), Number(m[2])] : null;
|
|
76
|
+
}
|
|
77
|
+
const isMapping = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
78
|
+
/** True when a privacy model uses a field that 0.1 implementations do not know. */
|
|
79
|
+
function uses02(doc) {
|
|
80
|
+
const fresh = ["id", "purpose", "requires", "obligations", "on_unmet"];
|
|
81
|
+
const overrides = (doc.rules || {}).overrides || [];
|
|
82
|
+
return "purposes" in doc || (Array.isArray(overrides) && overrides.some((o) => isMapping(o) && fresh.some((f) => f in o)));
|
|
83
|
+
}
|
|
84
|
+
/** Spec section 1.1: reject documents written for a later version, and 0.2 features declared as 0.1. */
|
|
85
|
+
export function versionErrors(doc, kind) {
|
|
86
|
+
const v = minor(doc.opp ?? "");
|
|
87
|
+
const mine = minor(SPEC_VERSION);
|
|
88
|
+
if (!v)
|
|
89
|
+
return ["opp: not a version (expected MAJOR.MINOR...)"];
|
|
90
|
+
if (v[0] > mine[0] || (v[0] === mine[0] && v[1] > mine[1]))
|
|
91
|
+
return [`opp: written for OPP ${doc.opp}; this implementation supports up to ${mine[0]}.${mine[1]}`];
|
|
92
|
+
const k = kind ?? kindOf(doc);
|
|
93
|
+
const before02 = v[0] === 0 && v[1] < 2;
|
|
94
|
+
if (before02 && (k === "destinations" || (k === "model" && uses02(doc))))
|
|
95
|
+
return [`opp: this document uses OPP 0.2 features but declares ${doc.opp}`];
|
|
96
|
+
return [];
|
|
51
97
|
}
|
|
52
98
|
export function schemaErrors(doc, kind) {
|
|
53
99
|
const k = kind ?? kindOf(doc);
|
|
@@ -56,11 +102,10 @@ export function schemaErrors(doc, kind) {
|
|
|
56
102
|
v = ajv.compile(schema(k));
|
|
57
103
|
validators.set(k, v);
|
|
58
104
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.sort();
|
|
105
|
+
const errs = v(doc)
|
|
106
|
+
? []
|
|
107
|
+
: (v.errors ?? []).map((e) => `${e.instancePath.replace(/^\//, "") || "(root)"}: ${e.message ?? "invalid"}`).sort();
|
|
108
|
+
return typeof doc.opp === "string" ? [...errs, ...versionErrors(doc, k)] : errs;
|
|
64
109
|
}
|
|
65
110
|
const idx = (order, v) => order.indexOf(v);
|
|
66
111
|
export class Catalog {
|
|
@@ -120,6 +165,7 @@ export class Model {
|
|
|
120
165
|
patterns = new Map();
|
|
121
166
|
rules;
|
|
122
167
|
overrides;
|
|
168
|
+
purposes = new Map();
|
|
123
169
|
constructor(src, catalog) {
|
|
124
170
|
const doc = read(src);
|
|
125
171
|
const schemaProblems = schemaErrors(doc, "model");
|
|
@@ -190,9 +236,26 @@ export class Model {
|
|
|
190
236
|
errs.push(`sources/${s.id}/${d.name}: unknown topic class ${c.class}`);
|
|
191
237
|
}
|
|
192
238
|
}
|
|
239
|
+
const rules = doc.rules ?? {};
|
|
240
|
+
for (const p of doc.purposes ?? [])
|
|
241
|
+
this.purposes.set(p.id, p);
|
|
242
|
+
if (this.purposes.size !== (doc.purposes ?? []).length)
|
|
243
|
+
errs.push("purposes: duplicate id");
|
|
244
|
+
const ids = new Set();
|
|
245
|
+
(rules.overrides ?? []).forEach((o, n) => {
|
|
246
|
+
const where = `rules/overrides/${o.id ?? n}`;
|
|
247
|
+
if ("id" in o) {
|
|
248
|
+
if (ids.has(o.id))
|
|
249
|
+
errs.push(`${where}: duplicate id`);
|
|
250
|
+
ids.add(o.id);
|
|
251
|
+
}
|
|
252
|
+
if ("purpose" in o && !this.purposes.has(o.purpose))
|
|
253
|
+
errs.push(`${where}: purpose '${o.purpose}' is not declared in purposes`);
|
|
254
|
+
if ("on_unmet" in o && !("requires" in o))
|
|
255
|
+
errs.push(`${where}: on_unmet needs requires`);
|
|
256
|
+
});
|
|
193
257
|
if (errs.length)
|
|
194
258
|
throw new OppError(errs);
|
|
195
|
-
const rules = doc.rules ?? {};
|
|
196
259
|
this.rules = Object.fromEntries(SENSITIVITY.map((s) => [s, { ...DEFAULT_RULES[s], ...(rules.default?.[s] ?? {}) }]));
|
|
197
260
|
this.overrides = rules.overrides ?? [];
|
|
198
261
|
}
|
|
@@ -217,20 +280,58 @@ export class Model {
|
|
|
217
280
|
label(id) {
|
|
218
281
|
return (this.concepts.get(id) ?? this.catalog.get(id)).label;
|
|
219
282
|
}
|
|
220
|
-
/**
|
|
221
|
-
|
|
283
|
+
/**
|
|
284
|
+
* The decision for content of class or concept `id` going to `destination` (spec section 5).
|
|
285
|
+
*
|
|
286
|
+
* `destination` is a category. `opts.mode` is how the destination will process the content: a mode of a
|
|
287
|
+
* destination registry entry, or nothing for an ordinary plaintext API. `opts.purpose` is the purpose the
|
|
288
|
+
* caller is authorised for, if any.
|
|
289
|
+
*/
|
|
290
|
+
decide(id, destination, opts = {}) {
|
|
222
291
|
if (!DESTINATIONS.includes(destination))
|
|
223
292
|
throw new OppError(`unknown destination ${destination}`);
|
|
293
|
+
const purpose = opts.purpose ?? null;
|
|
294
|
+
if (purpose !== null && !this.purposes.has(purpose))
|
|
295
|
+
throw new OppError(`unknown purpose ${purpose}`);
|
|
296
|
+
const mode = opts.mode && Object.keys(opts.mode).length ? opts.mode : PLAINTEXT_MODE;
|
|
224
297
|
const [s0, h0] = this.profile(id);
|
|
225
|
-
const s = opts.sensitivity
|
|
226
|
-
const h = opts.handling
|
|
298
|
+
const s = opts.sensitivity || s0;
|
|
299
|
+
const h = opts.handling || h0;
|
|
227
300
|
const targets = [id, ...(opts.targetNames ?? [])];
|
|
228
|
-
|
|
301
|
+
const result = (action, rule, o, unmet = false) => ({
|
|
302
|
+
action: (action === "handling" ? h : action),
|
|
303
|
+
rule,
|
|
304
|
+
unmet,
|
|
305
|
+
obligations: unmet || !o ? [] : [...(o.obligations ?? [])],
|
|
306
|
+
reason: unmet ? "" : (o?.reason ?? ""),
|
|
307
|
+
});
|
|
308
|
+
const name = (n, o) => o.id ?? `overrides[${n}]`;
|
|
309
|
+
const conditional = (o) => "requires" in o || "purpose" in o;
|
|
310
|
+
const matches = [];
|
|
311
|
+
this.overrides.forEach((o, n) => {
|
|
229
312
|
if (o.destination === destination && targets.includes(o.target))
|
|
230
|
-
|
|
313
|
+
matches.push([n, o]);
|
|
314
|
+
});
|
|
315
|
+
const hardBlock = matches.find(([, o]) => o.action === "block" && !conditional(o));
|
|
316
|
+
for (const [n, o] of matches) {
|
|
317
|
+
const rule = name(n, o);
|
|
318
|
+
if (conditional(o) && hardBlock)
|
|
319
|
+
continue; // a conditional override never lifts a plain block
|
|
320
|
+
if ("purpose" in o && o.purpose !== purpose)
|
|
321
|
+
continue; // a grant for another purpose does not apply
|
|
322
|
+
if ("requires" in o && !satisfies(mode, o.requires, opts.today)) {
|
|
323
|
+
const onUnmet = o.on_unmet ?? "block";
|
|
324
|
+
if (onUnmet === "fall_through")
|
|
325
|
+
continue;
|
|
326
|
+
return result(onUnmet, rule, o, true);
|
|
327
|
+
}
|
|
328
|
+
return result(o.action, rule, o);
|
|
231
329
|
}
|
|
232
|
-
|
|
233
|
-
|
|
330
|
+
return result(this.rules[s][destination], `default.${s}.${destination}`);
|
|
331
|
+
}
|
|
332
|
+
/** Action for content of class or concept `id` going to `destination` (spec section 5). */
|
|
333
|
+
action(id, destination, opts = {}) {
|
|
334
|
+
return this.decide(id, destination, opts).action;
|
|
234
335
|
}
|
|
235
336
|
/** [sensitivity, handling] of a field: its overrides, else the strictest of its classes. */
|
|
236
337
|
fieldProfile(field) {
|
|
@@ -244,6 +345,108 @@ export class Model {
|
|
|
244
345
|
return [(field.sensitivity ?? sens), (field.handling ?? hand)];
|
|
245
346
|
}
|
|
246
347
|
}
|
|
348
|
+
/** The assurance a mode can claim today: its evidence must be present, unexpired and of the right kind. */
|
|
349
|
+
export function effectiveAssurance(mode, today) {
|
|
350
|
+
const day = today || new Date().toISOString().slice(0, 10);
|
|
351
|
+
const level = mode.assurance ?? "asserted";
|
|
352
|
+
const live = (mode.evidence ?? []).filter((e) => (e.expires ?? "9999-12-31") >= day);
|
|
353
|
+
if (level === "asserted" || !live.length)
|
|
354
|
+
return "asserted";
|
|
355
|
+
if (level === "verified" && !live.some((e) => VERIFIABLE_EVIDENCE.includes(e.kind)))
|
|
356
|
+
return "approved";
|
|
357
|
+
return level;
|
|
358
|
+
}
|
|
359
|
+
function meets(mode, req, today) {
|
|
360
|
+
if ("mechanism" in req && mode.mechanism !== req.mechanism)
|
|
361
|
+
return false;
|
|
362
|
+
if ("key_custody" in req && mode.key_custody !== req.key_custody)
|
|
363
|
+
return false;
|
|
364
|
+
const ordered = [["plaintext_access", PLAINTEXT_ACCESS], ["retention", RETENTION]];
|
|
365
|
+
for (const [key, order] of ordered)
|
|
366
|
+
if (key in req && idx(order, mode[key] ?? order[order.length - 1]) > idx(order, req[key]))
|
|
367
|
+
return false;
|
|
368
|
+
if ("regions" in req && !(mode.region !== undefined && req.regions.includes(mode.region)))
|
|
369
|
+
return false;
|
|
370
|
+
const need = req.assurance ?? "approved"; // a bare provider assertion never satisfies a rule
|
|
371
|
+
return idx(ASSURANCE, effectiveAssurance(mode, today)) >= idx(ASSURANCE, need);
|
|
372
|
+
}
|
|
373
|
+
/** True when `mode` meets `requires` (one Requirement, or `any_of` a list of them). */
|
|
374
|
+
export function satisfies(mode, requires, today) {
|
|
375
|
+
const any = "any_of" in requires ? requires.any_of : [requires];
|
|
376
|
+
return any.some((r) => meets(mode, r, today));
|
|
377
|
+
}
|
|
378
|
+
/** `*` matches any run of characters; everything else is literal (spec section 5.1). */
|
|
379
|
+
function hostMatches(host, pattern) {
|
|
380
|
+
const re = pattern.split("*").map((p) => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*");
|
|
381
|
+
return new RegExp(`^${re}$`, "s").test(host);
|
|
382
|
+
}
|
|
383
|
+
/** A destination registry: the actual services behind the destination categories (spec section 5.1). */
|
|
384
|
+
export class Destinations {
|
|
385
|
+
doc;
|
|
386
|
+
name;
|
|
387
|
+
items;
|
|
388
|
+
constructor(src) {
|
|
389
|
+
const doc = read(src);
|
|
390
|
+
const errs = schemaErrors(doc, "destinations");
|
|
391
|
+
const seen = new Set();
|
|
392
|
+
for (const d of (errs.length ? [] : doc.destinations)) {
|
|
393
|
+
if (seen.has(d.id) || DESTINATIONS.includes(d.id))
|
|
394
|
+
errs.push(`destinations/${d.id}: id is a duplicate or a category name`);
|
|
395
|
+
seen.add(d.id);
|
|
396
|
+
if (new Set(d.modes.map((m) => m.id)).size !== d.modes.length)
|
|
397
|
+
errs.push(`destinations/${d.id}: duplicate mode id`);
|
|
398
|
+
for (const m of d.modes) {
|
|
399
|
+
const kinds = (m.evidence ?? []).map((e) => e.kind);
|
|
400
|
+
if (m.assurance !== "asserted" && !kinds.length)
|
|
401
|
+
errs.push(`destinations/${d.id}/${m.id}: assurance '${m.assurance}' needs evidence`);
|
|
402
|
+
if (m.assurance === "verified" && !kinds.some((k) => VERIFIABLE_EVIDENCE.includes(k)))
|
|
403
|
+
errs.push(`destinations/${d.id}/${m.id}: 'verified' needs attestation or client_build evidence`);
|
|
404
|
+
if (m.mechanism === "plaintext" && m.plaintext_access !== "provider")
|
|
405
|
+
errs.push(`destinations/${d.id}/${m.id}: a plaintext mode gives the provider plaintext access`);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (errs.length)
|
|
409
|
+
throw new OppError(errs);
|
|
410
|
+
this.doc = doc;
|
|
411
|
+
this.name = doc.name;
|
|
412
|
+
this.items = new Map(doc.destinations.map((d) => [d.id, d]));
|
|
413
|
+
}
|
|
414
|
+
has(id) {
|
|
415
|
+
return this.items.has(id);
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* The category, registry entry (null for a bare category) and mode for a destination id or a bare category.
|
|
419
|
+
*
|
|
420
|
+
* A bare category is an ordinary plaintext service. A registry entry with several modes needs `mode`.
|
|
421
|
+
*/
|
|
422
|
+
resolve(destination, mode) {
|
|
423
|
+
const wanted = mode ?? null;
|
|
424
|
+
if (DESTINATIONS.includes(destination)) {
|
|
425
|
+
if (wanted !== null && wanted !== "plaintext")
|
|
426
|
+
throw new OppError(`mode '${wanted}' needs a destination id, not the category '${destination}'`);
|
|
427
|
+
return { category: destination, entry: null, mode: PLAINTEXT_MODE };
|
|
428
|
+
}
|
|
429
|
+
const entry = this.items.get(destination);
|
|
430
|
+
if (!entry)
|
|
431
|
+
throw new OppError(`unknown destination ${destination}`);
|
|
432
|
+
const modes = new Map(entry.modes.map((m) => [m.id, m]));
|
|
433
|
+
if (wanted === null && modes.size > 1) {
|
|
434
|
+
const names = [...modes.keys()].sort().map((m) => `'${m}'`).join(", ");
|
|
435
|
+
throw new OppError(`destination ${destination} has several modes; name one of [${names}]`);
|
|
436
|
+
}
|
|
437
|
+
if (wanted !== null && !modes.has(wanted))
|
|
438
|
+
throw new OppError(`destination ${destination} has no mode '${wanted}'`);
|
|
439
|
+
return { category: entry.category, entry, mode: wanted ? modes.get(wanted) : entry.modes[0] };
|
|
440
|
+
}
|
|
441
|
+
/** The registry entry whose `endpoints` match a host name, or null. `*` matches any run of characters. */
|
|
442
|
+
forEndpoint(host) {
|
|
443
|
+
const h = host.toLowerCase();
|
|
444
|
+
for (const d of this.items.values())
|
|
445
|
+
if ((d.endpoints ?? []).some((p) => hostMatches(h, p.toLowerCase())))
|
|
446
|
+
return d;
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
247
450
|
/** The strictest of several actions ('allow' when there are none). */
|
|
248
451
|
export function strictestAction(actions) {
|
|
249
452
|
return (actions.length ? actions.reduce((a, b) => (idx(ACTION_ORDER, b) > idx(ACTION_ORDER, a) ? b : a)) : "allow");
|
|
@@ -279,14 +482,16 @@ export function presetForNaics(code) {
|
|
|
279
482
|
[best, length] = [id, pre.length];
|
|
280
483
|
return presets().get(best);
|
|
281
484
|
}
|
|
282
|
-
/** Problems in a catalog, model or
|
|
485
|
+
/** Problems in a catalog, model, presets or destination registry document; empty when it conforms. */
|
|
283
486
|
export function validate(src, catalog) {
|
|
284
487
|
const doc = read(src);
|
|
285
488
|
try {
|
|
286
489
|
const kind = kindOf(doc);
|
|
287
490
|
if (kind === "presets")
|
|
288
491
|
return presetErrors(doc, catalog);
|
|
289
|
-
if (kind === "
|
|
492
|
+
if (kind === "destinations")
|
|
493
|
+
new Destinations(doc);
|
|
494
|
+
else if (kind === "catalog")
|
|
290
495
|
new Catalog(doc);
|
|
291
496
|
else
|
|
292
497
|
new Model(doc, catalog);
|