open-privacy-protocol 0.1.0-alpha.2 → 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 CHANGED
@@ -26,7 +26,8 @@
26
26
 
27
27
  The JavaScript and TypeScript SDK for the [Open Privacy Protocol](https://openprivacyprotocol.com/) (OPP), the open standard for
28
28
  describing what is private and confidential in any data. Use it in agents, LLM gateways and web services to validate
29
- privacy models, decide what may be shared with each destination (PII redaction, pseudonymization, blocking), and call
29
+ privacy models, resolve how sensitive information may be used and shared, whatever protects it (redaction,
30
+ pseudonymization, blocking, or routing to confidential or encrypted compute), and call
30
31
  an [OPP Engine](https://github.com/kannancet/open-privacy-protocol-engine) query server.
31
32
 
32
33
  ```bash
@@ -53,6 +54,29 @@ validate(readFileSync("acme.opp.yaml", "utf8")); // [] when the document confor
53
54
  npx open-privacy-protocol validate acme.opp.yaml
54
55
  ```
55
56
 
57
+ ### Protection-aware rules (OPP 0.2)
58
+
59
+ Since OPP 0.2 a rule can depend on how a destination processes the content and on the purpose of the request.
60
+ A destination registry lists the services behind each category and the modes they offer.
61
+
62
+ ```ts
63
+ import { Destinations, Model } from "open-privacy-protocol";
64
+ import { readFileSync } from "node:fs";
65
+
66
+ const model = new Model(readFileSync("acme.opp.yaml", "utf8"));
67
+ const registry = new Destinations(readFileSync("acme.destinations.yaml", "utf8"));
68
+
69
+ // A registry id and one of its modes, or a bare category for an ordinary plaintext API.
70
+ const { category, mode } = registry.resolve("vendor-x", "enclave");
71
+ const decision = model.decide("special.health", category, { mode, purpose: "medical_analysis" });
72
+
73
+ decision.action; // "allow" when the mode meets the rule's requirement, otherwise "block"
74
+ decision.rule; // the override id, or "default.restricted.external_ai"
75
+ decision.obligations; // e.g. ["enforce_mode", "no_plaintext_fallback"]: fulfil each one before sending
76
+ ```
77
+
78
+ Pass only a purpose the caller is authorised for. `model.action(id, destination)` still returns the action alone.
79
+
56
80
  ### Ask OPP Engine before an agent shares
57
81
 
58
82
  ```ts
@@ -71,9 +95,12 @@ await sendToLlm(result.safe_content ?? original); // pseudonymized when needed
71
95
  |---|---|
72
96
  | `Model`, `Catalog` | Load a privacy model or catalog, validate it, resolve every class and concept |
73
97
  | `model.action(id, destination)` | The action for content of a class going to `internal`, `partner`, `external_ai` or `public` |
98
+ | `model.decide(id, destination, { mode, purpose })` | The action with the rule that produced it, whether its requirement was unmet, and the obligations |
99
+ | `Destinations` | Load a destination registry; `resolve(destination, mode)` and `forEndpoint(host)` |
74
100
  | `validate`, `schemaErrors` | Conformance checks, with every problem listed |
75
101
  | `baseline()`, `presets()`, `presetForNaics()` | The baseline catalog (179 classes) and 15 industry presets, bundled |
76
102
  | `EngineClient` | `check`, `explain`, `classes`, `model`, `health` against OPP Engine, using `fetch` |
103
+ | `mustEnforce(result)` | The obligations of a check result, to fulfil before sending |
77
104
  | `opp` CLI | `opp validate`, `opp classes` |
78
105
 
79
106
  Behaviour matches the [Python package](https://github.com/kannancet/opp-python); both bundle the schemas, catalog
package/dist/client.d.ts CHANGED
@@ -1,12 +1,63 @@
1
- import { OppError, type Destination } from "./core.js";
1
+ import { OppError, type Assurance, type Destination, type Mechanism, type Obligation } from "./core.js";
2
2
  export interface Finding {
3
3
  class: string;
4
+ label?: string;
4
5
  sensitivity: string;
5
6
  action: string;
6
- reason: string;
7
+ /** The rule that produced the action: an override id, `overrides[N]` or `default.<sensitivity>.<destination>`. */
8
+ rule?: string;
9
+ /** True when the rule's requirement was not met by the mode used. */
10
+ unmet?: boolean;
11
+ source?: string;
12
+ reason?: string;
7
13
  start?: number | null;
8
14
  end?: number | null;
15
+ row?: number | null;
9
16
  field?: string | null;
17
+ dataset?: string | null;
18
+ }
19
+ export type CheckStage = "input" | "output" | "tool_call";
20
+ export interface Detector {
21
+ id: string;
22
+ version?: string;
23
+ digest?: string;
24
+ covers?: string[];
25
+ languages?: string[];
26
+ human_reviewed?: boolean;
27
+ }
28
+ /** The destination a decision was made for: the category, and the registry id and mode when one was used. */
29
+ export interface DecisionDestination {
30
+ category: Destination;
31
+ id?: string;
32
+ mode?: string;
33
+ mechanism: Mechanism;
34
+ assurance: Assurance;
35
+ }
36
+ /** The Decision of a check (spec section 5.3). It holds no data value. */
37
+ export interface Decision {
38
+ id: string;
39
+ opp: string;
40
+ at: string;
41
+ expires: string;
42
+ stage?: CheckStage;
43
+ destination: DecisionDestination;
44
+ purpose?: {
45
+ id: string;
46
+ status: "authorised" | "asserted";
47
+ };
48
+ model: {
49
+ name: string;
50
+ digest: string;
51
+ };
52
+ registry?: {
53
+ name: string;
54
+ digest: string;
55
+ };
56
+ content_digest: string;
57
+ rules: string[];
58
+ obligations: Obligation[];
59
+ evidence?: string[];
60
+ detectors?: Detector[];
10
61
  }
11
62
  export interface CheckResult {
12
63
  verdict: string;
@@ -15,7 +66,20 @@ export interface CheckResult {
15
66
  findings: Finding[];
16
67
  safe_content: unknown;
17
68
  model: string;
69
+ /** Present when the engine implements OPP 0.2. */
70
+ decision?: Decision;
71
+ }
72
+ export interface CheckOptions {
73
+ /** The dataset the content comes from. */
74
+ dataset?: string;
75
+ /** The mode of a registry destination; required when the destination has several. */
76
+ mode?: string;
77
+ /** The purpose of the request. The engine decides whether the caller is authorised for it. */
78
+ purpose?: string;
79
+ stage?: CheckStage;
18
80
  }
81
+ /** The obligations the sender must fulfil before acting on a check result; empty when there are none. */
82
+ export declare function mustEnforce(result: Pick<CheckResult, "decision">): Obligation[];
19
83
  export declare class EngineError extends OppError {
20
84
  readonly status?: number | undefined;
21
85
  constructor(message: string, status?: number | undefined);
@@ -32,8 +96,13 @@ export declare class EngineClient {
32
96
  private readonly fetchImpl;
33
97
  constructor(baseUrl?: string, opts?: EngineClientOptions);
34
98
  private request;
35
- /** Verdict, findings and safe content for text, a record or a list of records. */
36
- check(content: unknown, destination?: Destination, dataset?: string): Promise<CheckResult>;
99
+ /**
100
+ * Verdict, findings, safe content and the decision for text, a record or a list of records.
101
+ *
102
+ * `destination` is a category or the id of an entry in the engine's destination registry; the engine
103
+ * validates it. The third argument is the options, or the dataset name as a string.
104
+ */
105
+ check(content: unknown, destination?: Destination | (string & {}), options?: CheckOptions | string): Promise<CheckResult>;
37
106
  /** What the privacy model records about a field, value, class or concept. */
38
107
  explain(query: string): Promise<unknown>;
39
108
  /** Classes and concepts the organization tracks. */
package/dist/client.js CHANGED
@@ -1,6 +1,10 @@
1
1
  // A client for an OPP Engine query server (`opp-engine serve --http`). Uses the global fetch
2
2
  // (Node.js 18+, Deno, Bun, browsers). Run the engine next to the agent: the content you send is the data.
3
- import { DESTINATIONS, OppError } from "./core.js";
3
+ import { OppError } from "./core.js";
4
+ /** The obligations the sender must fulfil before acting on a check result; empty when there are none. */
5
+ export function mustEnforce(result) {
6
+ return [...(result.decision?.obligations ?? [])];
7
+ }
4
8
  export class EngineError extends OppError {
5
9
  status;
6
10
  constructor(message, status) {
@@ -46,11 +50,22 @@ export class EngineClient {
46
50
  throw new EngineError(`${method} ${path}: HTTP ${res.status}: ${(await res.text()).slice(0, 300)}`, res.status);
47
51
  return (await res.json());
48
52
  }
49
- /** Verdict, findings and safe content for text, a record or a list of records. */
50
- check(content, destination = "external_ai", dataset) {
51
- if (!DESTINATIONS.includes(destination))
52
- throw new OppError(`unknown destination ${destination}`);
53
- return this.request("POST", "/v1/check", { content, destination, dataset: dataset ?? null });
53
+ /**
54
+ * Verdict, findings, safe content and the decision for text, a record or a list of records.
55
+ *
56
+ * `destination` is a category or the id of an entry in the engine's destination registry; the engine
57
+ * validates it. The third argument is the options, or the dataset name as a string.
58
+ */
59
+ check(content, destination = "external_ai", options) {
60
+ const { dataset, mode, purpose, stage } = typeof options === "string" ? { dataset: options } : (options ?? {});
61
+ const body = { content, destination, dataset: dataset ?? null, obligations_supported: true };
62
+ if (mode !== undefined)
63
+ body.mode = mode;
64
+ if (purpose !== undefined)
65
+ body.purpose = purpose;
66
+ if (stage !== undefined)
67
+ body.stage = stage;
68
+ return this.request("POST", "/v1/check", body);
54
69
  }
55
70
  /** What the privacy model records about a field, value, class or concept. */
56
71
  explain(query) {
package/dist/core.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare const SPEC_VERSION = "0.1.0.dev0";
1
+ export declare const SPEC_VERSION = "0.2.0.dev0";
2
2
  export declare const SENSITIVITY: readonly ["public", "internal", "confidential", "restricted"];
3
3
  export declare const HANDLING: readonly ["allow", "generalize", "pseudonymize", "redact", "block"];
4
4
  export declare const DESTINATIONS: readonly ["internal", "partner", "external_ai", "public"];
@@ -8,7 +8,112 @@ export type Sensitivity = (typeof SENSITIVITY)[number];
8
8
  export type Handling = (typeof HANDLING)[number];
9
9
  export type Destination = (typeof DESTINATIONS)[number];
10
10
  export type Action = (typeof ACTION_ORDER)[number];
11
- export type DocumentKind = "catalog" | "model" | "presets";
11
+ /** How a destination processes content (spec section 2.6). */
12
+ export declare const MECHANISMS: readonly ["plaintext", "tee", "fhe", "mpc", "local"];
13
+ /** How far a capability claim is backed, lowest first (spec section 2.7). */
14
+ export declare const ASSURANCE: readonly ["asserted", "approved", "verified"];
15
+ /** Who can read the content while it is processed, most protective first. */
16
+ export declare const PLAINTEXT_ACCESS: readonly ["none", "enclave", "provider"];
17
+ export declare const RETENTION: readonly ["none", "transient", "retained"];
18
+ /** What the sender must do when a decision carries it (spec section 2.8). */
19
+ export declare const OBLIGATIONS: readonly ["enforce_mode", "no_plaintext_fallback", "check_output", "record_decision", "human_approval"];
20
+ export type Mechanism = (typeof MECHANISMS)[number];
21
+ export type Assurance = (typeof ASSURANCE)[number];
22
+ export type PlaintextAccess = (typeof PLAINTEXT_ACCESS)[number];
23
+ export type Retention = (typeof RETENTION)[number];
24
+ export type Obligation = (typeof OBLIGATIONS)[number];
25
+ export type KeyCustody = "client" | "provider" | "shared";
26
+ export type OnUnmet = "block" | "review" | "fall_through";
27
+ export type DocumentKind = "catalog" | "model" | "presets" | "destinations";
28
+ /** Every bundled schema: the document kinds, and the Decision an engine returns. */
29
+ export type SchemaKind = DocumentKind | "decision";
30
+ /** Evidence behind a mode's assurance. Dates are ISO `YYYY-MM-DD` strings. */
31
+ export interface Evidence {
32
+ kind: "admin_approval" | "contract" | "audit_report" | "attestation" | "client_build";
33
+ ref: string;
34
+ issued?: string;
35
+ expires?: string;
36
+ verifier?: string;
37
+ }
38
+ /** One way a destination processes content (spec section 5.1). */
39
+ export interface Mode {
40
+ id: string;
41
+ mechanism: Mechanism;
42
+ plaintext_access: PlaintextAccess;
43
+ key_custody: KeyCustody;
44
+ retention: Retention;
45
+ assurance: Assurance;
46
+ region?: string;
47
+ operations?: string[];
48
+ evidence?: Evidence[];
49
+ }
50
+ /** What an override requires of the mode used (spec section 5). */
51
+ export interface Requirement {
52
+ mechanism?: Mechanism;
53
+ key_custody?: KeyCustody;
54
+ plaintext_access?: PlaintextAccess;
55
+ retention?: Retention;
56
+ regions?: string[];
57
+ assurance?: "approved" | "verified";
58
+ }
59
+ /** One Requirement, or `any_of` a list of them. */
60
+ export type Requires = Requirement | {
61
+ any_of: Requirement[];
62
+ };
63
+ /** An entry of `rules.overrides`. */
64
+ export interface Override {
65
+ target: string;
66
+ destination: Destination;
67
+ action: Action | "handling";
68
+ reason?: string;
69
+ id?: string;
70
+ purpose?: string;
71
+ requires?: Requires;
72
+ obligations?: Obligation[];
73
+ on_unmet?: OnUnmet;
74
+ }
75
+ export interface Purpose {
76
+ id: string;
77
+ label: string;
78
+ description?: string;
79
+ }
80
+ /** An entry of a destination registry. */
81
+ export interface DestinationEntry {
82
+ id: string;
83
+ label?: string;
84
+ category: Destination;
85
+ endpoints?: string[];
86
+ modes: Mode[];
87
+ }
88
+ /** What `Destinations.resolve` returns: the category, the registry entry (null for a bare category) and the mode. */
89
+ export interface ResolvedDestination {
90
+ category: Destination;
91
+ entry: DestinationEntry | null;
92
+ mode: Mode;
93
+ }
94
+ /** What `Model.decide` returns. */
95
+ export interface DecideResult {
96
+ action: Action;
97
+ /** The override `id`, `overrides[N]`, or `default.<sensitivity>.<destination>`. */
98
+ rule: string;
99
+ /** True when the rule's `requires` was not met by the mode. */
100
+ unmet: boolean;
101
+ obligations: Obligation[];
102
+ reason: string;
103
+ }
104
+ export interface DecideOptions {
105
+ /** How the destination will process the content: a mode of a registry entry. An ordinary plaintext API when omitted. */
106
+ mode?: Mode | null;
107
+ /** The purpose the caller is authorised for. */
108
+ purpose?: string | null;
109
+ sensitivity?: Sensitivity;
110
+ handling?: Handling;
111
+ targetNames?: readonly string[];
112
+ /** Evaluation date for evidence expiry, `YYYY-MM-DD`. The current UTC date when omitted. */
113
+ today?: string;
114
+ }
115
+ /** The mode assumed when a request names only a destination category: an ordinary API that reads the content. */
116
+ export declare const PLAINTEXT_MODE: Readonly<Mode>;
12
117
  export type OppDocument = Record<string, any>;
13
118
  type RuleCell = Action | "handling";
14
119
  export declare const DEFAULT_RULES: Record<Sensitivity, Record<Destination, RuleCell>>;
@@ -18,11 +123,13 @@ export declare class OppError extends Error {
18
123
  constructor(problems: string | string[]);
19
124
  }
20
125
  /** The JSON Schema for a document kind. */
21
- export declare function schema(kind: DocumentKind): object;
126
+ export declare function schema(kind: SchemaKind): object;
22
127
  /** A document from a YAML or JSON string, or an object. */
23
128
  export declare function read(src: string | OppDocument): OppDocument;
24
129
  export declare function kindOf(doc: OppDocument): DocumentKind;
25
- export declare function schemaErrors(doc: OppDocument, kind?: DocumentKind): string[];
130
+ /** Spec section 1.1: reject documents written for a later version, and 0.2 features declared as 0.1. */
131
+ export declare function versionErrors(doc: OppDocument, kind?: SchemaKind): string[];
132
+ export declare function schemaErrors(doc: OppDocument, kind?: SchemaKind): string[];
26
133
  export declare class Catalog {
27
134
  readonly doc: OppDocument;
28
135
  readonly name: string;
@@ -43,7 +150,8 @@ export declare class Model {
43
150
  readonly identifiers: Map<string, OppDocument>;
44
151
  readonly patterns: Map<string, RegExp>;
45
152
  readonly rules: Record<Sensitivity, Record<Destination, RuleCell>>;
46
- readonly overrides: OppDocument[];
153
+ readonly overrides: Override[];
154
+ readonly purposes: Map<string, Purpose>;
47
155
  constructor(src: string | OppDocument, catalog?: Catalog);
48
156
  known(id: string): boolean;
49
157
  /** The catalog class behind a class or concept id. */
@@ -52,21 +160,45 @@ export declare class Model {
52
160
  /** [sensitivity, handling] of a class or concept, after the model's classification settings. */
53
161
  profile(id: string): [Sensitivity, Handling];
54
162
  label(id: string): string;
163
+ /**
164
+ * The decision for content of class or concept `id` going to `destination` (spec section 5).
165
+ *
166
+ * `destination` is a category. `opts.mode` is how the destination will process the content: a mode of a
167
+ * destination registry entry, or nothing for an ordinary plaintext API. `opts.purpose` is the purpose the
168
+ * caller is authorised for, if any.
169
+ */
170
+ decide(id: string, destination: Destination, opts?: DecideOptions): DecideResult;
55
171
  /** Action for content of class or concept `id` going to `destination` (spec section 5). */
56
- action(id: string, destination: Destination, opts?: {
57
- sensitivity?: Sensitivity;
58
- handling?: Handling;
59
- targetNames?: string[];
60
- }): Action;
172
+ action(id: string, destination: Destination, opts?: DecideOptions): Action;
61
173
  /** [sensitivity, handling] of a field: its overrides, else the strictest of its classes. */
62
174
  fieldProfile(field: OppDocument): [Sensitivity, Handling];
63
175
  }
176
+ /** The assurance a mode can claim today: its evidence must be present, unexpired and of the right kind. */
177
+ export declare function effectiveAssurance(mode: Partial<Mode>, today?: string): Assurance;
178
+ /** True when `mode` meets `requires` (one Requirement, or `any_of` a list of them). */
179
+ export declare function satisfies(mode: Partial<Mode>, requires: Requires, today?: string): boolean;
180
+ /** A destination registry: the actual services behind the destination categories (spec section 5.1). */
181
+ export declare class Destinations {
182
+ readonly doc: OppDocument;
183
+ readonly name: string;
184
+ readonly items: Map<string, DestinationEntry>;
185
+ constructor(src: string | OppDocument);
186
+ has(id: string): boolean;
187
+ /**
188
+ * The category, registry entry (null for a bare category) and mode for a destination id or a bare category.
189
+ *
190
+ * A bare category is an ordinary plaintext service. A registry entry with several modes needs `mode`.
191
+ */
192
+ resolve(destination: string, mode?: string | null): ResolvedDestination;
193
+ /** The registry entry whose `endpoints` match a host name, or null. `*` matches any run of characters. */
194
+ forEndpoint(host: string): DestinationEntry | null;
195
+ }
64
196
  /** The strictest of several actions ('allow' when there are none). */
65
197
  export declare function strictestAction(actions: readonly string[]): Action;
66
198
  /** The industry presets that ship with the standard, by id. */
67
199
  export declare function presets(): Map<string, OppDocument>;
68
200
  /** The preset whose longest NAICS prefix matches a 2- to 6-digit code, or 'generic'. */
69
201
  export declare function presetForNaics(code: string | number): OppDocument;
70
- /** Problems in a catalog, model or presets document; empty when it conforms. */
202
+ /** Problems in a catalog, model, presets or destination registry document; empty when it conforms. */
71
203
  export declare function validate(src: string | OppDocument, catalog?: Catalog): string[];
72
204
  export {};