@openbrf/plugin-sdk 1.0.0 → 1.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.
Files changed (104) hide show
  1. package/README.md +95 -0
  2. package/dist/abort-signal.d.ts +23 -0
  3. package/dist/abort-signal.d.ts.map +1 -0
  4. package/dist/abort-signal.js +2 -0
  5. package/dist/abort-signal.js.map +1 -0
  6. package/dist/action-refusal.d.ts +101 -0
  7. package/dist/action-refusal.d.ts.map +1 -0
  8. package/dist/action-refusal.js +96 -0
  9. package/dist/action-refusal.js.map +1 -0
  10. package/dist/action-schema.d.ts.map +1 -1
  11. package/dist/action-schema.js +4 -1
  12. package/dist/action-schema.js.map +1 -1
  13. package/dist/actions.d.ts +94 -6
  14. package/dist/actions.d.ts.map +1 -1
  15. package/dist/actions.js.map +1 -1
  16. package/dist/blocks.d.ts +310 -0
  17. package/dist/blocks.d.ts.map +1 -0
  18. package/dist/blocks.js +185 -0
  19. package/dist/blocks.js.map +1 -0
  20. package/dist/catalog.d.ts +1092 -41
  21. package/dist/catalog.d.ts.map +1 -1
  22. package/dist/catalog.js +472 -15
  23. package/dist/catalog.js.map +1 -1
  24. package/dist/entitlements.d.ts +117 -0
  25. package/dist/entitlements.d.ts.map +1 -0
  26. package/dist/entitlements.js +62 -0
  27. package/dist/entitlements.js.map +1 -0
  28. package/dist/extensions.d.ts +183 -0
  29. package/dist/extensions.d.ts.map +1 -0
  30. package/dist/extensions.js +67 -0
  31. package/dist/extensions.js.map +1 -0
  32. package/dist/finances.d.ts +294 -0
  33. package/dist/finances.d.ts.map +1 -0
  34. package/dist/finances.js +56 -0
  35. package/dist/finances.js.map +1 -0
  36. package/dist/findings.d.ts +92 -1
  37. package/dist/findings.d.ts.map +1 -1
  38. package/dist/findings.js +91 -0
  39. package/dist/findings.js.map +1 -1
  40. package/dist/host.d.ts +729 -4
  41. package/dist/host.d.ts.map +1 -1
  42. package/dist/host.js +54 -0
  43. package/dist/host.js.map +1 -1
  44. package/dist/imports.d.ts +104 -0
  45. package/dist/imports.d.ts.map +1 -0
  46. package/dist/imports.js +70 -0
  47. package/dist/imports.js.map +1 -0
  48. package/dist/index.d.ts +17 -7
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +15 -6
  51. package/dist/index.js.map +1 -1
  52. package/dist/localized-text.d.ts +18 -0
  53. package/dist/localized-text.d.ts.map +1 -0
  54. package/dist/localized-text.js +17 -0
  55. package/dist/localized-text.js.map +1 -0
  56. package/dist/manifest.d.ts +859 -0
  57. package/dist/manifest.d.ts.map +1 -1
  58. package/dist/manifest.js +957 -12
  59. package/dist/manifest.js.map +1 -1
  60. package/dist/package-check.d.ts.map +1 -1
  61. package/dist/package-check.js +9 -1
  62. package/dist/package-check.js.map +1 -1
  63. package/dist/permissions.d.ts +170 -11
  64. package/dist/permissions.d.ts.map +1 -1
  65. package/dist/permissions.js +210 -2
  66. package/dist/permissions.js.map +1 -1
  67. package/dist/recipients.d.ts +139 -0
  68. package/dist/recipients.d.ts.map +1 -0
  69. package/dist/recipients.js +223 -0
  70. package/dist/recipients.js.map +1 -0
  71. package/dist/records.d.ts +130 -0
  72. package/dist/records.d.ts.map +1 -0
  73. package/dist/records.js +85 -0
  74. package/dist/records.js.map +1 -0
  75. package/dist/require-capability.d.ts +37 -0
  76. package/dist/require-capability.d.ts.map +1 -0
  77. package/dist/require-capability.js +88 -0
  78. package/dist/require-capability.js.map +1 -0
  79. package/dist/settings-schema.d.ts +35 -0
  80. package/dist/settings-schema.d.ts.map +1 -1
  81. package/dist/settings-schema.js +55 -2
  82. package/dist/settings-schema.js.map +1 -1
  83. package/package.json +4 -4
  84. package/src/abort-signal.ts +22 -0
  85. package/src/action-refusal.ts +178 -0
  86. package/src/action-schema.ts +4 -1
  87. package/src/actions.ts +103 -6
  88. package/src/blocks.ts +289 -0
  89. package/src/catalog.ts +630 -72
  90. package/src/entitlements.ts +150 -0
  91. package/src/extensions.ts +212 -0
  92. package/src/finances.ts +317 -0
  93. package/src/findings.ts +94 -0
  94. package/src/host.ts +768 -4
  95. package/src/imports.ts +135 -0
  96. package/src/index.ts +209 -0
  97. package/src/localized-text.ts +19 -0
  98. package/src/manifest.ts +1231 -12
  99. package/src/package-check.ts +15 -0
  100. package/src/permissions.ts +223 -3
  101. package/src/recipients.ts +294 -0
  102. package/src/records.ts +172 -0
  103. package/src/require-capability.ts +106 -0
  104. package/src/settings-schema.ts +64 -2
@@ -1,6 +1,8 @@
1
1
  import {
2
2
  dependencyNames,
3
3
  isEmptyDependencyField,
4
+ missingProcessingProblem,
5
+ undeclaredPersonalDataProblem,
4
6
  parsePluginPackage,
5
7
  RUNTIME_DEPENDENCY_FIELDS,
6
8
  } from "./manifest.ts";
@@ -183,6 +185,19 @@ export function pluginPackageProblems(
183
185
  problems.push(...dependencyProblems(packageJson));
184
186
 
185
187
  if (parsed.ok) {
188
+ const processing = missingProcessingProblem(parsed.value.openbrf);
189
+ if (processing !== null) {
190
+ problems.push(
191
+ `The manifest in package.json is invalid at openbrf.processing: ${processing}.`,
192
+ );
193
+ }
194
+ const personalData = undeclaredPersonalDataProblem(parsed.value.openbrf);
195
+ if (personalData !== null) {
196
+ problems.push(
197
+ `The manifest in package.json is invalid at openbrf.personalData: ${personalData}.`,
198
+ );
199
+ }
200
+
186
201
  const entries = parsed.value.openbrf.entry;
187
202
  const files = new Set(contents.files.map(normalizedPath));
188
203
  for (const [kind, declared] of [
@@ -1,3 +1,5 @@
1
+ import type { ActionPersonalData } from "./actions.ts";
2
+
1
3
  /**
2
4
  * What a plugin may ask the host for.
3
5
  *
@@ -11,6 +13,12 @@
11
13
  *
12
14
  * No permission grants write access to the register in v0. A plugin that
13
15
  * needs to change resident data is a core feature request, not a plugin.
16
+ *
17
+ * A new value here is a new member of the catalog's permission list, and an
18
+ * instance that does not know it refuses the whole catalog index that carries
19
+ * it. The catalog therefore lists an entry asking for one of the content
20
+ * permissions below only in an index that older instances do not read: the
21
+ * version 2 index (ADR 0032).
14
22
  */
15
23
  export const PLUGIN_PERMISSIONS = [
16
24
  /**
@@ -44,6 +52,106 @@ export const PLUGIN_PERMISSIONS = [
44
52
  "sms:send",
45
53
  /** Register background workers and enqueue or schedule jobs. */
46
54
  "jobs:schedule",
55
+ /**
56
+ * The fee notification runs and their notices, and the charges to members,
57
+ * as the board's own documents state them, and the events that say one was
58
+ * issued, recorded, corrected or removed.
59
+ *
60
+ * Masked as the files the board hands over are, and further: the names on a
61
+ * notice whose household holds a person with protected personal data are
62
+ * withheld, as on the fee notice file, and a charge on such a person withholds
63
+ * who and where entirely, where the debiting list withholds only where. A
64
+ * plugin is further from the board than the economic manager is, and the
65
+ * address book's rule that a protected person never reaches a plugin holds
66
+ * here too. A personal identity number is never returned, and there is no
67
+ * payment state to return: core records none.
68
+ */
69
+ "finances:read",
70
+ /**
71
+ * Record that a fee notice or a charge was delivered: by which route, when,
72
+ * and under which reference the receiving system knows it. A receipt and
73
+ * never a paid flag. Separate from finances:read because it writes, and the
74
+ * board consents to a plugin that only reads the books knowing that it cannot
75
+ * add anything to them.
76
+ */
77
+ "finances:recordDelivery",
78
+ /**
79
+ * Add blocks to the association's website.
80
+ *
81
+ * What such a block renders is published to anybody who reads the page,
82
+ * account or not, which is why it is a permission the board consents to
83
+ * rather than something a manifest may simply declare. The host prints what
84
+ * the block renders through an allowlist of elements, so it is never a
85
+ * script, a stylesheet or a resource from another host - but the words in it
86
+ * are the plugin's to choose.
87
+ */
88
+ "site:blocks",
89
+ /**
90
+ * The association's published news items: the title, the body's text, when
91
+ * each was published and whether it is for the members or for anyone.
92
+ *
93
+ * Not a register permission. It reads what the board has already
94
+ * published, and never who wrote an item, a comment on it or who read it
95
+ * (ADR 0032). The text is the board's and may name somebody, which is why a
96
+ * plugin holding it declares `freeText`.
97
+ */
98
+ "news:read",
99
+ /**
100
+ * The published events and their occurrences, and the notice of every
101
+ * general meeting that has been called and not yet held.
102
+ *
103
+ * Never a sign-up, a roll-call, an attendance or a proxy: how many places
104
+ * are left at an occurrence is the most it says about who is coming. Like
105
+ * news:read, it reads the board's text and declares `freeText`.
106
+ */
107
+ "calendar:read",
108
+ /**
109
+ * The bookable resources the house offers, and when each is taken. Never by
110
+ * whom: no booker, no apartment, no booking's identifier, and two bookings
111
+ * that touch are one interval (ADR 0032).
112
+ */
113
+ "bookings:readAvailability",
114
+ /**
115
+ * A stored picture, by its id: one held for anyone or for the members, whose
116
+ * upload declared that it shows no identifiable person (ADR 0032).
117
+ *
118
+ * Never a document, never a household's or the board's file, and never a
119
+ * picture of people: a photograph of a person needs a publication consent,
120
+ * and the consent the core records is for the website, not for wherever a
121
+ * plugin puts the picture. A picture is still a photograph, which is why a
122
+ * plugin holding it declares `photograph`.
123
+ */
124
+ "media:read",
125
+ /**
126
+ * The association's addresses: the street and number of each entrance, and
127
+ * nothing about who lives at them. Not addressBook:read, which reads the
128
+ * apartments and the people in them.
129
+ */
130
+ "addresses:read",
131
+ /**
132
+ * Install and update themes on the instance (ADR 0046).
133
+ *
134
+ * A theme is data and passes the same install lint however it arrives, so
135
+ * this grants no way past the theme gate: what a board consents to is a
136
+ * plugin putting themes on the instance's theme screen, for an
137
+ * administrator to preview and activate there. Each install is made for a
138
+ * person in the web interface who may install themes, never by the plugin on
139
+ * its own.
140
+ */
141
+ "themes:install",
142
+ /**
143
+ * Hand a member list to the core for the board to review (ADR 0043).
144
+ *
145
+ * The one way a plugin brings data towards the register, and it writes
146
+ * nothing: the core holds the file as an import proposal on its own import
147
+ * screen, and a board member previews, decides and applies it there, or
148
+ * rejects it. It grants no read of the register and raises no route's
149
+ * capability floor, so the plugin learns nothing about what the file would
150
+ * do. It does hold the file, though, which can carry every personal identity
151
+ * number in the cooperative and rows about persons with protected personal
152
+ * data, so a manifest declaring it declares those categories too.
153
+ */
154
+ "addressBook:proposeImport",
47
155
  ] as const;
48
156
 
49
157
  export type PluginPermission = (typeof PLUGIN_PERMISSIONS)[number];
@@ -52,13 +160,79 @@ export function isPluginPermission(value: string): value is PluginPermission {
52
160
  return (PLUGIN_PERMISSIONS as readonly string[]).includes(value);
53
161
  }
54
162
 
163
+ /**
164
+ * Whether a permission lets a plugin read the register, or the finances that
165
+ * carry the names of the people in it.
166
+ *
167
+ * Every permission is classified, and the record is typed against
168
+ * `PluginPermission`, so a permission added to the list above fails to compile
169
+ * until somebody has said which it is. That is the point of a classification
170
+ * over a list of the refused: the rules that keep a plugin with a device route
171
+ * or a website block away from the register (ADR 0006, ADR 0031) hold a new
172
+ * permission that reads it out unless somebody remembers to add it to each.
173
+ * `finances:recordDelivery` writes a receipt and returns no one's details, so
174
+ * it is not a read. The content permissions (`news:read`, `calendar:read`,
175
+ * `bookings:readAvailability`, `media:read`, `addresses:read`) read what the
176
+ * website publishes, never the register, so a plugin with a device route may
177
+ * hold them; the host withholds text that names a resident from such a plugin.
178
+ */
179
+ export const PERMISSION_REGISTER_ACCESS = {
180
+ "addressBook:read": "reads",
181
+ "addressBook:readContact": "reads",
182
+ "mail:send": "none",
183
+ "sms:send": "none",
184
+ "jobs:schedule": "none",
185
+ "finances:read": "reads",
186
+ "finances:recordDelivery": "none",
187
+ "site:blocks": "none",
188
+ "themes:install": "none",
189
+ "news:read": "none",
190
+ "calendar:read": "none",
191
+ "bookings:readAvailability": "none",
192
+ "media:read": "none",
193
+ "addresses:read": "none",
194
+ // Reads nothing from the register, but holds the member list it proposes.
195
+ // The rules that keep a device route or a website block away from it name
196
+ // this permission alongside `readsRegister` (ADR 0043).
197
+ "addressBook:proposeImport": "none",
198
+ } as const satisfies Record<PluginPermission, "reads" | "none">;
199
+
200
+ /** The permissions that read the register, in the order of the list above. */
201
+ export const REGISTER_READING_PERMISSIONS: readonly PluginPermission[] =
202
+ PLUGIN_PERMISSIONS.filter(
203
+ (permission) => PERMISSION_REGISTER_ACCESS[permission] === "reads",
204
+ );
205
+
206
+ /** Whether any of the permissions a manifest asks for reads the register. */
207
+ export function readsRegister(permissions: readonly string[]): boolean {
208
+ return permissions.some((permission) =>
209
+ (REGISTER_READING_PERMISSIONS as readonly string[]).includes(permission),
210
+ );
211
+ }
212
+
55
213
  /**
56
214
  * Categories of personal data a plugin declares it will process.
57
215
  *
58
216
  * Shown on the consent screen before an install, and stored with the
59
- * installation as the snapshot the board agreed to. It is a declaration, not
217
+ * installation as the snapshot the board agreed to. Installing the plugin
218
+ * writes them into the association's record of processing activities (GDPR
219
+ * art. 30) as the categories that processing touches. It is a declaration, not
60
220
  * an enforcement point: what the plugin can actually reach follows from its
61
- * permissions above.
221
+ * permissions above and from the actions it is allowed to call.
222
+ *
223
+ * Not only the register. A plugin that dispatches core actions handles
224
+ * whatever those actions return: an assistant passing issue reports and
225
+ * minutes to a model provider handles free text, and one reading charges
226
+ * handles financial data. So the list is ACTION_PERSONAL_DATA, in its order.
227
+ *
228
+ * Two of them only one kind of plugin may declare. A personal identity number
229
+ * is never returned to a plugin, and a person with protected personal data
230
+ * (skyddade personuppgifter) never appears in one's reads at all, so for any
231
+ * other plugin a declaration naming either would describe something that
232
+ * cannot happen. A plugin that proposes an import carries the member list the
233
+ * other way, and that list can hold both (ADR 0043): the manifest refuses
234
+ * either category without `addressBook:proposeImport`, and requires both with
235
+ * it.
62
236
  */
63
237
  export const PLUGIN_PERSONAL_DATA_CATEGORIES = [
64
238
  /** Given and family name. */
@@ -69,7 +243,53 @@ export const PLUGIN_PERSONAL_DATA_CATEGORIES = [
69
243
  "residency",
70
244
  "email",
71
245
  "phone",
72
- ] as const;
246
+ /** A postal address of the person's own, not the apartment's. */
247
+ "postalAddress",
248
+ /**
249
+ * Personal identity number (personnummer). Only with
250
+ * `addressBook:proposeImport`.
251
+ */
252
+ "personalIdentityNumber",
253
+ /** Sign-in credentials and sessions. */
254
+ "account",
255
+ /** Charges, debiting basis and what a person owes or has paid. */
256
+ "financial",
257
+ /**
258
+ * Health, an art. 9 special category. Declared by a plugin that handles text
259
+ * people wrote themselves, where it may be volunteered.
260
+ */
261
+ "health",
262
+ /** A photograph, whether or not anybody is recognisable in it. */
263
+ "photograph",
264
+ /** Text a person wrote themselves, which may name anybody. */
265
+ "freeText",
266
+ /** The audit log's record of who did what to whose data. */
267
+ "auditTrail",
268
+ /**
269
+ * Rows about a person with protected personal data (skyddade
270
+ * personuppgifter). Only with `addressBook:proposeImport`.
271
+ */
272
+ "protected",
273
+ ] as const satisfies readonly ActionPersonalData[];
274
+
275
+ /**
276
+ * The personal data categories a plugin that proposes an import must declare,
277
+ * and the two no other plugin may.
278
+ */
279
+ export const PROPOSE_IMPORT_PERSONAL_DATA = [
280
+ "name",
281
+ "apartment",
282
+ "residency",
283
+ "email",
284
+ "phone",
285
+ "personalIdentityNumber",
286
+ "protected",
287
+ ] as const satisfies readonly PluginPersonalDataCategory[];
288
+
289
+ export const PROPOSE_IMPORT_ONLY_PERSONAL_DATA = [
290
+ "personalIdentityNumber",
291
+ "protected",
292
+ ] as const satisfies readonly PluginPersonalDataCategory[];
73
293
 
74
294
  export type PluginPersonalDataCategory =
75
295
  (typeof PLUGIN_PERSONAL_DATA_CATEGORIES)[number];
@@ -0,0 +1,294 @@
1
+ import { z } from "zod";
2
+
3
+ import { localizedTextSchema } from "./localized-text.ts";
4
+ import { PLUGIN_PERSONAL_DATA_CATEGORIES } from "./permissions.ts";
5
+
6
+ /**
7
+ * Where a plugin connects to outside the instance (ADR 0037).
8
+ *
9
+ * Declared in the manifest, and repeated in the catalog entry, for the reason
10
+ * actions are: the board reads it on the install consent screen before anything
11
+ * has been downloaded, and it is what the screen's question about where the
12
+ * plugin sends personal data is answered from.
13
+ *
14
+ * A declaration rather than a fence. A backend plugin runs at full process
15
+ * privilege (ADR 0003), so nothing here stops one opening a connection it did
16
+ * not declare; the catalog's reading of the server bundle is what holds a
17
+ * listed plugin to it.
18
+ */
19
+
20
+ /** The longest origin a recipient may name. */
21
+ const RECIPIENT_ORIGIN_MAX_LENGTH = 200;
22
+
23
+ /** How many recipients a plugin may declare. */
24
+ const MAX_RECIPIENTS = 8;
25
+
26
+ /** What the joined hosts separate them with. */
27
+ const HOST_SEPARATOR = ", ";
28
+
29
+ /**
30
+ * An `https` origin: the scheme, the host and a port only where it is not 443.
31
+ *
32
+ * Written as the WHATWG URL parser writes an origin, so one place has one
33
+ * spelling: `https://API.example.com:443/` and `https://api.example.com` are the
34
+ * same recipient, and only the second is accepted. No path, because the board
35
+ * consents to a party and not to one of its endpoints; no credentials, because
36
+ * a manifest is published; and nothing but `https`, because what crosses the
37
+ * connection is the association's data. The host is a name: not an IP literal
38
+ * or `localhost`, which name no party the board can recognise, and not a name
39
+ * with a trailing dot, which is the same host under a second spelling.
40
+ */
41
+ export const recipientOriginSchema = z
42
+ .string()
43
+ .min(1)
44
+ .max(RECIPIENT_ORIGIN_MAX_LENGTH)
45
+ .refine(isCanonicalHttpsOrigin, {
46
+ error:
47
+ 'must be an https origin as a browser writes it, such as "https://api.example.com": lowercase, with no path, no trailing slash, no port 443, and a host name that is not an IP address, localhost or written with a trailing dot',
48
+ });
49
+
50
+ /**
51
+ * The parts of a WHATWG URL read here.
52
+ *
53
+ * Reached through `globalThis` rather than the DOM's or Node's types: this
54
+ * package is compiled with neither, because it is bundled into the browser as
55
+ * well as required by the server, and both have the same `URL`.
56
+ */
57
+ interface ParsedUrl {
58
+ protocol: string;
59
+ username: string;
60
+ password: string;
61
+ host: string;
62
+ hostname: string;
63
+ origin: string;
64
+ }
65
+
66
+ const WhatwgUrl = (
67
+ globalThis as unknown as { URL: new (input: string) => ParsedUrl }
68
+ ).URL;
69
+
70
+ function isCanonicalHttpsOrigin(value: string): boolean {
71
+ let parsed: ParsedUrl;
72
+ try {
73
+ parsed = new WhatwgUrl(value);
74
+ } catch {
75
+ return false;
76
+ }
77
+ return (
78
+ parsed.protocol === "https:" &&
79
+ parsed.username === "" &&
80
+ parsed.password === "" &&
81
+ parsed.origin === value &&
82
+ isNamedHost(parsed.hostname)
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Whether a host is a registered-style name: not a trailing-dot spelling, an
88
+ * IPv6 literal, a dotted IPv4 address or `localhost`. The parser has already
89
+ * turned any other IPv4 spelling (`0x7f.1`, `2130706433`) into the dotted one.
90
+ */
91
+ function isNamedHost(hostname: string): boolean {
92
+ return !(
93
+ hostname.endsWith(".") ||
94
+ hostname.startsWith("[") ||
95
+ /^\d+\.\d+\.\d+\.\d+$/.test(hostname) ||
96
+ hostname === "localhost" ||
97
+ hostname.endsWith(".localhost")
98
+ );
99
+ }
100
+
101
+ /** The host a recipient's origin names, which is what a board recognises. */
102
+ export function recipientHost(origin: string): string {
103
+ return new WhatwgUrl(origin).host;
104
+ }
105
+
106
+ /**
107
+ * The hosts of the declared recipients that receive personal data, joined.
108
+ *
109
+ * The answer a declaration gives to who receives the data (GDPR art.
110
+ * 30(1)(d)): what the consent screen fills its recipient field with and what
111
+ * the record of processing activities lists. A recipient receiving none is a
112
+ * place the plugin connects to, not a recipient in the sense of art. 4(9), so
113
+ * it is left out; an empty string when none receives any.
114
+ */
115
+ export function receivingHosts(
116
+ recipients: readonly Pick<PluginRecipient, "origin" | "personalData">[],
117
+ ): string {
118
+ return recipients
119
+ .filter((recipient) => recipient.personalData.length > 0)
120
+ .map((recipient) => recipientHost(recipient.origin))
121
+ .join(HOST_SEPARATOR);
122
+ }
123
+
124
+ /**
125
+ * The longest `receivingHosts` can be: every recipient the declaration may
126
+ * hold, each at the longest origin less its `https://`. A field the answer is
127
+ * written to has to take at least this, or a valid declaration fills in an
128
+ * answer the field then refuses.
129
+ */
130
+ export const RECEIVING_HOSTS_MAX_LENGTH =
131
+ MAX_RECIPIENTS * (RECIPIENT_ORIGIN_MAX_LENGTH - "https://".length) +
132
+ (MAX_RECIPIENTS - 1) * HOST_SEPARATOR.length;
133
+
134
+ export const pluginRecipientSchema = z.object({
135
+ /** Unique in the manifest. Lowercase letters, digits and single hyphens. */
136
+ id: z
137
+ .string()
138
+ .min(2)
139
+ .max(32)
140
+ .regex(
141
+ /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/,
142
+ "must be lowercase letters, digits and single hyphens, starting with a letter",
143
+ ),
144
+ origin: recipientOriginSchema,
145
+ /** What the plugin connects there for, in Swedish and English. */
146
+ purpose: localizedTextSchema,
147
+ /**
148
+ * The personal-data categories this recipient receives, from the plugin's
149
+ * own `personalData`. Empty for a connection that carries none, such as
150
+ * fetching a list of models or the day's exchange rates: it is declared all
151
+ * the same, because the board is shown every place the plugin connects to.
152
+ */
153
+ personalData: z.array(z.enum(PLUGIN_PERSONAL_DATA_CATEGORIES)).max(16),
154
+ });
155
+
156
+ export type PluginRecipient = z.infer<typeof pluginRecipientSchema>;
157
+
158
+ /**
159
+ * The declared recipients over a given reading of one recipient.
160
+ *
161
+ * The manifest reads a recipient as `pluginRecipientSchema` does and the
162
+ * catalog index reads the same fields strictly, as it reads everything. Both
163
+ * keep the rules below, which is why the list is built here rather than
164
+ * written twice.
165
+ *
166
+ * An id appears once and an origin appears once. Two declarations of one id
167
+ * would leave the consent snapshot and the board's screen naming different
168
+ * things by it; two of one origin would let a plugin state a narrow purpose
169
+ * and a wide one for the same party, and a board reading the first would have
170
+ * consented to the second.
171
+ *
172
+ * Undefaulted, on the terms of `declaredActionsSchema`: a manifest and a
173
+ * catalog entry add `.default([])`, and the install request does not, so an
174
+ * echo that left the field out is not read as consent to none.
175
+ */
176
+ export function declaredRecipientsSchema<
177
+ T extends z.ZodType<{ id: string; origin: string }>,
178
+ >(recipient: T) {
179
+ return z
180
+ .array(recipient)
181
+ .max(MAX_RECIPIENTS)
182
+ .superRefine((recipients, ctx) => {
183
+ const ids = new Set<string>();
184
+ const origins = new Set<string>();
185
+ for (const [index, declared] of recipients.entries()) {
186
+ if (ids.has(declared.id)) {
187
+ ctx.addIssue({
188
+ code: "custom",
189
+ path: [index, "id"],
190
+ message: `two recipients are declared with the id "${declared.id}"`,
191
+ });
192
+ }
193
+ if (origins.has(declared.origin)) {
194
+ ctx.addIssue({
195
+ code: "custom",
196
+ path: [index, "origin"],
197
+ message: `two recipients are declared with the origin "${declared.origin}"`,
198
+ });
199
+ }
200
+ ids.add(declared.id);
201
+ origins.add(declared.origin);
202
+ }
203
+ });
204
+ }
205
+
206
+ export const pluginRecipientsSchema = declaredRecipientsSchema(
207
+ pluginRecipientSchema,
208
+ );
209
+
210
+ /**
211
+ * The canonical string a recipient is consented to and compared by.
212
+ *
213
+ * The id, the origin and the categories: what the plugin may reach and what
214
+ * goes there. The purpose is the plugin's words for it and is shown, not
215
+ * compared, so a republished version that rewords one is not refused for it.
216
+ */
217
+ export function canonicalRecipient(
218
+ recipient: Pick<PluginRecipient, "id" | "origin" | "personalData">,
219
+ ): string {
220
+ return [
221
+ recipient.id,
222
+ recipient.origin,
223
+ [...recipient.personalData].sort().join("|"),
224
+ ].join(" ");
225
+ }
226
+
227
+ /**
228
+ * The declared recipients a consent given to `consented`, as
229
+ * `canonicalRecipient` wrote it, does not cover.
230
+ *
231
+ * A recipient is covered by a consented one with the same id and origin that
232
+ * received every category it now receives. Fewer categories are covered: the
233
+ * plugin's data reaching that party narrowed, and the board agreed to more.
234
+ * One more, a new id or an id moved to another origin is not.
235
+ */
236
+ export function recipientsBeyondConsent<
237
+ T extends Pick<PluginRecipient, "id" | "origin" | "personalData">,
238
+ >(declared: readonly T[], consented: readonly string[]): T[] {
239
+ const agreed = new Map<string, ReadonlySet<string>>();
240
+ for (const canonical of consented) {
241
+ // Neither an id nor a canonical origin has a space in it.
242
+ const [id, origin, categories = ""] = canonical.split(" ");
243
+ agreed.set(
244
+ `${id} ${origin}`,
245
+ new Set(categories === "" ? [] : categories.split("|")),
246
+ );
247
+ }
248
+ return declared.filter((recipient) => {
249
+ const categories = agreed.get(`${recipient.id} ${recipient.origin}`);
250
+ return (
251
+ categories === undefined ||
252
+ recipient.personalData.some((category) => !categories.has(category))
253
+ );
254
+ });
255
+ }
256
+
257
+ /** Whether two declarations name the same recipients, as multisets. */
258
+ export function sameRecipientDeclaration(
259
+ left: readonly Pick<PluginRecipient, "id" | "origin" | "personalData">[],
260
+ right: readonly Pick<PluginRecipient, "id" | "origin" | "personalData">[],
261
+ ): boolean {
262
+ const a = left.map(canonicalRecipient).sort();
263
+ const b = right.map(canonicalRecipient).sort();
264
+ return a.length === b.length && a.every((value, index) => value === b[index]);
265
+ }
266
+
267
+ /**
268
+ * The recipients' categories that are not among the ones the plugin declares
269
+ * it handles, as issues at the path of each.
270
+ *
271
+ * What a recipient receives is part of what the plugin handles. One naming a
272
+ * category the plugin does not declare would put it on the consent screen, and
273
+ * into the record of who receives what, without the record of processing
274
+ * activities hearing of it. The manifest and the catalog entry repeat the same
275
+ * two fields, so both refuse it with this rule.
276
+ */
277
+ export function recipientCategoryIssues(
278
+ recipients: readonly Pick<PluginRecipient, "personalData">[],
279
+ handled: readonly string[],
280
+ ): { path: (string | number)[]; message: string }[] {
281
+ const declared = new Set<string>(handled);
282
+ return recipients.flatMap((recipient, index) =>
283
+ recipient.personalData.flatMap((category, at) =>
284
+ declared.has(category)
285
+ ? []
286
+ : [
287
+ {
288
+ path: ["recipients", index, "personalData", at],
289
+ message: `"${category}" is not among the plugin's own personalData; a recipient receives only what the plugin declares it handles`,
290
+ },
291
+ ],
292
+ ),
293
+ );
294
+ }