@openbrf/plugin-sdk 0.0.0-stage → 1.2.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 (134) hide show
  1. package/LICENSE +661 -0
  2. package/LICENSE-EXCEPTION.md +28 -0
  3. package/README.md +94 -2
  4. package/dist/abort-signal.d.ts +23 -0
  5. package/dist/abort-signal.d.ts.map +1 -0
  6. package/dist/abort-signal.js +2 -0
  7. package/dist/abort-signal.js.map +1 -0
  8. package/dist/action-refusal.d.ts +101 -0
  9. package/dist/action-refusal.d.ts.map +1 -0
  10. package/dist/action-refusal.js +96 -0
  11. package/dist/action-refusal.js.map +1 -0
  12. package/dist/action-schema.d.ts +42 -0
  13. package/dist/action-schema.d.ts.map +1 -0
  14. package/dist/action-schema.js +67 -0
  15. package/dist/action-schema.js.map +1 -0
  16. package/dist/actions.d.ts +287 -0
  17. package/dist/actions.d.ts.map +1 -0
  18. package/dist/actions.js +68 -0
  19. package/dist/actions.js.map +1 -0
  20. package/dist/api-version.d.ts +23 -0
  21. package/dist/api-version.d.ts.map +1 -0
  22. package/dist/api-version.js +27 -0
  23. package/dist/api-version.js.map +1 -0
  24. package/dist/blocks.d.ts +310 -0
  25. package/dist/blocks.d.ts.map +1 -0
  26. package/dist/blocks.js +185 -0
  27. package/dist/blocks.js.map +1 -0
  28. package/dist/catalog.d.ts +707 -0
  29. package/dist/catalog.d.ts.map +1 -0
  30. package/dist/catalog.js +567 -0
  31. package/dist/catalog.js.map +1 -0
  32. package/dist/define-plugin.d.ts +14 -0
  33. package/dist/define-plugin.d.ts.map +1 -0
  34. package/dist/define-plugin.js +15 -0
  35. package/dist/define-plugin.js.map +1 -0
  36. package/dist/entitlements.d.ts +117 -0
  37. package/dist/entitlements.d.ts.map +1 -0
  38. package/dist/entitlements.js +62 -0
  39. package/dist/entitlements.js.map +1 -0
  40. package/dist/extensions.d.ts +183 -0
  41. package/dist/extensions.d.ts.map +1 -0
  42. package/dist/extensions.js +67 -0
  43. package/dist/extensions.js.map +1 -0
  44. package/dist/finances.d.ts +294 -0
  45. package/dist/finances.d.ts.map +1 -0
  46. package/dist/finances.js +56 -0
  47. package/dist/finances.js.map +1 -0
  48. package/dist/findings.d.ts +156 -0
  49. package/dist/findings.d.ts.map +1 -0
  50. package/dist/findings.js +146 -0
  51. package/dist/findings.js.map +1 -0
  52. package/dist/host.d.ts +827 -0
  53. package/dist/host.d.ts.map +1 -0
  54. package/dist/host.js +80 -0
  55. package/dist/host.js.map +1 -0
  56. package/dist/imports.d.ts +104 -0
  57. package/dist/imports.d.ts.map +1 -0
  58. package/dist/imports.js +70 -0
  59. package/dist/imports.js.map +1 -0
  60. package/dist/index.d.ts +34 -0
  61. package/dist/index.d.ts.map +1 -0
  62. package/dist/index.js +31 -0
  63. package/dist/index.js.map +1 -0
  64. package/dist/integrity.d.ts +38 -0
  65. package/dist/integrity.d.ts.map +1 -0
  66. package/dist/integrity.js +105 -0
  67. package/dist/integrity.js.map +1 -0
  68. package/dist/localized-text.d.ts +18 -0
  69. package/dist/localized-text.d.ts.map +1 -0
  70. package/dist/localized-text.js +17 -0
  71. package/dist/localized-text.js.map +1 -0
  72. package/dist/manifest.d.ts +1122 -0
  73. package/dist/manifest.d.ts.map +1 -0
  74. package/dist/manifest.js +1118 -0
  75. package/dist/manifest.js.map +1 -0
  76. package/dist/module.d.ts +35 -0
  77. package/dist/module.d.ts.map +1 -0
  78. package/dist/module.js +2 -0
  79. package/dist/module.js.map +1 -0
  80. package/dist/package-check.d.ts +66 -0
  81. package/dist/package-check.d.ts.map +1 -0
  82. package/dist/package-check.js +755 -0
  83. package/dist/package-check.js.map +1 -0
  84. package/dist/permissions.d.ts +224 -0
  85. package/dist/permissions.d.ts.map +1 -0
  86. package/dist/permissions.js +277 -0
  87. package/dist/permissions.js.map +1 -0
  88. package/dist/recipients.d.ts +139 -0
  89. package/dist/recipients.d.ts.map +1 -0
  90. package/dist/recipients.js +223 -0
  91. package/dist/recipients.js.map +1 -0
  92. package/dist/records.d.ts +130 -0
  93. package/dist/records.d.ts.map +1 -0
  94. package/dist/records.js +85 -0
  95. package/dist/records.js.map +1 -0
  96. package/dist/require-capability.d.ts +37 -0
  97. package/dist/require-capability.d.ts.map +1 -0
  98. package/dist/require-capability.js +88 -0
  99. package/dist/require-capability.js.map +1 -0
  100. package/dist/settings-schema.d.ts +130 -0
  101. package/dist/settings-schema.d.ts.map +1 -0
  102. package/dist/settings-schema.js +272 -0
  103. package/dist/settings-schema.js.map +1 -0
  104. package/dist/standard-schema.d.ts +43 -0
  105. package/dist/standard-schema.d.ts.map +1 -0
  106. package/dist/standard-schema.js +2 -0
  107. package/dist/standard-schema.js.map +1 -0
  108. package/package.json +55 -3
  109. package/src/abort-signal.ts +22 -0
  110. package/src/action-refusal.ts +178 -0
  111. package/src/action-schema.ts +89 -0
  112. package/src/actions.ts +344 -0
  113. package/src/api-version.ts +28 -0
  114. package/src/blocks.ts +289 -0
  115. package/src/catalog.ts +679 -0
  116. package/src/define-plugin.ts +18 -0
  117. package/src/entitlements.ts +150 -0
  118. package/src/extensions.ts +212 -0
  119. package/src/finances.ts +317 -0
  120. package/src/findings.ts +163 -0
  121. package/src/host.ts +894 -0
  122. package/src/imports.ts +135 -0
  123. package/src/index.ts +316 -0
  124. package/src/integrity.ts +125 -0
  125. package/src/localized-text.ts +19 -0
  126. package/src/manifest.ts +1412 -0
  127. package/src/module.ts +38 -0
  128. package/src/package-check.ts +894 -0
  129. package/src/permissions.ts +295 -0
  130. package/src/recipients.ts +294 -0
  131. package/src/records.ts +172 -0
  132. package/src/require-capability.ts +106 -0
  133. package/src/settings-schema.ts +334 -0
  134. package/src/standard-schema.ts +39 -0
@@ -0,0 +1,1118 @@
1
+ import { z } from "zod";
2
+ import { ACTION_EFFECTS, ACTION_NAME_PATTERN, ACTION_PERSONAL_DATA, ACTION_SURFACES, } from "./actions.js";
3
+ import { PLUGIN_API_VERSION } from "./api-version.js";
4
+ import { pluginBlocksSchema } from "./blocks.js";
5
+ import { EXTENSION_POINT_ID_PATTERN, MAX_EXTENSION_POINTS, } from "./extensions.js";
6
+ import { PLUGIN_MANIFEST_RULES } from "./findings.js";
7
+ import { PLUGIN_PERMISSIONS, PLUGIN_PERSONAL_DATA_CATEGORIES, PROPOSE_IMPORT_ONLY_PERSONAL_DATA, PROPOSE_IMPORT_PERSONAL_DATA, readsRegister, REGISTER_READING_PERMISSIONS, } from "./permissions.js";
8
+ import { pluginRecipientsSchema, recipientCategoryIssues, } from "./recipients.js";
9
+ import { pluginSettingsSchema } from "./settings-schema.js";
10
+ /**
11
+ * The plugin manifest.
12
+ *
13
+ * It lives in the `openbrf` field of the plugin's own package.json rather
14
+ * than in a separate file: npm already installs package.json, already
15
+ * validates its name and version, and already refuses to install a package
16
+ * without one. A second manifest file could go missing, disagree with the
17
+ * package it sits in, or be left behind by a partial extraction, and the
18
+ * loader would have to decide which of the two to believe.
19
+ */
20
+ /**
21
+ * A plugin id.
22
+ *
23
+ * Used as a URL segment (`/api/plugin/<id>/...`), an i18n namespace
24
+ * (`plugin-<id>`), a database key and a directory name, so it is restricted to
25
+ * what is safe in all four. In particular it may not contain a dot or a
26
+ * slash: a plugin id is resolved against paths, and `..` must not be
27
+ * expressible.
28
+ */
29
+ export const pluginIdSchema = z
30
+ .string()
31
+ .min(2)
32
+ .max(48)
33
+ .regex(/^[a-z][a-z0-9]*(-[a-z0-9]+)*$/, "must be lowercase letters, digits and single hyphens, starting with a letter");
34
+ /**
35
+ * A path inside the plugin package.
36
+ *
37
+ * Relative and normalized by the schema's own rules rather than by the
38
+ * loader: an entry point is read from a manifest that arrived over the
39
+ * network, so "../../../etc/passwd" has to be rejected where the shape is
40
+ * defined and not at each of the places that later join it onto a directory.
41
+ */
42
+ const packageRelativePathSchema = z
43
+ .string()
44
+ .min(1)
45
+ .max(200)
46
+ .refine((value) => !value.startsWith("/"), "must be relative")
47
+ .refine((value) => !value.split("/").includes(".."), "must not step outside the package")
48
+ .refine((value) => !value.includes("\0"), "must not contain a null byte");
49
+ export const pluginEntrySchema = z
50
+ .object({
51
+ /**
52
+ * Prebuilt CJS bundle whose only externals are host packages and Node's
53
+ * built-in modules (ADR 0003).
54
+ * Optional: a plugin may contribute a view and no backend behaviour.
55
+ */
56
+ server: packageRelativePathSchema.optional(),
57
+ /**
58
+ * Module Federation remote entry, served to the browser and loaded at
59
+ * runtime. Optional: a plugin may contribute a background job and no view.
60
+ */
61
+ client: packageRelativePathSchema.optional(),
62
+ })
63
+ .refine((entry) => entry.server !== undefined || entry.client !== undefined, "a plugin must declare at least one entry point");
64
+ /**
65
+ * A route under the plugin's own mount at `/api/plugin/<id>/`.
66
+ *
67
+ * Written without a leading slash and joined onto that mount by the host, for
68
+ * the reason the package-relative schema exists: the value arrives in a
69
+ * manifest fetched over the network, and it decides a URL an unauthenticated
70
+ * caller will be pointed at, so what it may contain is settled here rather
71
+ * than at each place that joins it.
72
+ */
73
+ const pluginRoutePathSchema = z
74
+ .string()
75
+ .min(1)
76
+ .max(100)
77
+ .regex(/^[a-z0-9][a-z0-9-]*(\/[a-z0-9][a-z0-9-]*)*$/, "must be lowercase path segments separated by single slashes, with no leading or trailing slash");
78
+ /**
79
+ * One action the plugin proposes.
80
+ *
81
+ * Declared in the manifest rather than at registration, because the board reads
82
+ * it on the install consent screen before anything has been downloaded, and
83
+ * nothing that can refuse a plugin may run after the code that executes it. The
84
+ * plugin's bundle supplies the schemas and the handler; what it may ask for is
85
+ * settled here.
86
+ *
87
+ * `capability` is a free string because this package cannot import the core
88
+ * capability union - it is published to plugin authors and the union is the
89
+ * instance's. It is resolved against the real list at the boot gate, which also
90
+ * refuses the ones no action of any kind may hold.
91
+ */
92
+ export const pluginActionSchema = z.object({
93
+ id: z.string().regex(/^[a-z][a-z0-9_]{2,31}$/),
94
+ capability: z.string().min(1).max(64),
95
+ effect: z.enum(ACTION_EFFECTS),
96
+ personalData: z.array(z.enum(ACTION_PERSONAL_DATA)).max(14).default([]),
97
+ surfaces: z.array(z.enum(ACTION_SURFACES)).max(3).default(["ui"]),
98
+ });
99
+ /**
100
+ * The declared actions, as every boundary that reads them must read them.
101
+ *
102
+ * One schema rather than the same array written twice, because the invariant
103
+ * below has to hold at both: the catalog entry the consent screen renders from,
104
+ * and the manifest the loader enforces against it.
105
+ *
106
+ * An id appears once. Two declarations sharing one id are not a redundancy that
107
+ * sorts itself out - each of the three places that reads them resolves the
108
+ * collision differently. The binder takes the LAST, through a Map; consent
109
+ * compares canonical strings as a SET, so reordering the pair leaves the
110
+ * comparison equal and asks the board nothing; and arming stores the bare id,
111
+ * which names both. A republished version that only swapped the order of a
112
+ * duplicated pair would therefore keep its arming and go live at the OTHER
113
+ * declaration's capability, personal data and surfaces, with nobody having
114
+ * decided that. An action is one operation with one capability; two of them
115
+ * wearing one id is not a declaration this can read.
116
+ *
117
+ * Undefaulted, so each use site says for itself whether an absent list means
118
+ * "none declared" or "you did not answer". A manifest and a catalog entry mean
119
+ * the first and add `.default([])`; the install request means the second, since
120
+ * a board's echo that silently became an empty list would read as consent to no
121
+ * action at all - and a plugin that declares one would then be refused with a
122
+ * mismatch nothing the board could do would satisfy.
123
+ */
124
+ export const pluginActionsSchema = declaredActionsSchema(pluginActionSchema);
125
+ /**
126
+ * The declared actions over a given reading of one action.
127
+ *
128
+ * The manifest reads an action as `pluginActionSchema` does; the catalog index
129
+ * reads the same fields strictly, because the index refuses a field its
130
+ * version does not define. Both keep the rule above, which is why the list is
131
+ * built here rather than written again.
132
+ */
133
+ export function declaredActionsSchema(action) {
134
+ return z
135
+ .array(action)
136
+ .max(16)
137
+ .superRefine((actions, ctx) => {
138
+ const seen = new Set();
139
+ for (const [index, declared] of actions.entries()) {
140
+ if (seen.has(declared.id)) {
141
+ ctx.addIssue({
142
+ code: "custom",
143
+ path: [index, "id"],
144
+ message: `two actions are declared with the id "${declared.id}"`,
145
+ });
146
+ }
147
+ seen.add(declared.id);
148
+ }
149
+ });
150
+ }
151
+ /**
152
+ * The public name a declared action is offered under.
153
+ *
154
+ * The plugin's id with dashes folded to underscores, then the action's id. The
155
+ * folding has a consequence worth knowing: a plugin `a-b` declaring `c` and a
156
+ * plugin `a` declaring `b_c` compose the same name, so the registry refuses the
157
+ * second one to arrive and docs/plugin-contract.md says so.
158
+ */
159
+ export function composedActionName(pluginId, actionId) {
160
+ return `${pluginId.replaceAll("-", "_")}_${actionId}`;
161
+ }
162
+ /**
163
+ * The sections of the application's navigation a plugin's view may stand in.
164
+ *
165
+ * The application's own four, in the order its band shows them: the
166
+ * association and its life, the building and the apartments in it, the board's
167
+ * own desk, and the settings. A plugin names one of them rather than a section
168
+ * of its own, so a view is found where a destination of its kind already is.
169
+ */
170
+ export const PLUGIN_VIEW_SECTIONS = [
171
+ "association",
172
+ "building",
173
+ "board",
174
+ "settings",
175
+ ];
176
+ export const pluginViewSchema = z.object({
177
+ /** Named export of the remote module, per Module Federation. */
178
+ module: z.string().min(1).max(100).default("./View"),
179
+ titleKey: z.string().min(1).max(200),
180
+ /**
181
+ * The section of the navigation the view is offered in, under its title.
182
+ *
183
+ * Optional, and a view without one is reachable by its address alone, as
184
+ * every view was before the field existed. Naming a section asks for an
185
+ * entry rather than granting one: the host offers it only to an account that
186
+ * holds what the plugin's routes require, and in the board's section only to
187
+ * the board, because that section holds nothing a resident is offered.
188
+ */
189
+ section: z.enum(PLUGIN_VIEW_SECTIONS).optional(),
190
+ });
191
+ /** The id of an extension point, unique within the plugin that declares it. */
192
+ export const extensionPointIdSchema = z
193
+ .string()
194
+ .min(2)
195
+ .max(32)
196
+ .regex(EXTENSION_POINT_ID_PATTERN, "must be lowercase letters, digits and single hyphens, starting with a letter");
197
+ /**
198
+ * One extension point the plugin offers other plugins (ADR 0030).
199
+ *
200
+ * Declared here rather than at registration for the reason an action is: the
201
+ * board reads what the point accepts on the consent screen before anything is
202
+ * downloaded. The bundle supplies the schemas; what may cross is settled here.
203
+ */
204
+ export const pluginExtensionPointSchema = z.object({
205
+ id: extensionPointIdSchema,
206
+ /**
207
+ * The point's major version, raised when a change to its schemas would break
208
+ * an addon written for the last one.
209
+ */
210
+ version: z.int().min(1),
211
+ /**
212
+ * The categories of personal data a contribution may hand the plugin
213
+ * through this point. Among the plugin's own `personalData`, because what an
214
+ * addon hands it is handled by it as much as what it reads itself.
215
+ */
216
+ personalData: z
217
+ .array(z.enum(PLUGIN_PERSONAL_DATA_CATEGORIES))
218
+ .max(16)
219
+ .default([]),
220
+ });
221
+ /** One extension point of another plugin that this plugin extends. */
222
+ export const pluginExtensionSchema = z.object({
223
+ /** The id of the host plugin. */
224
+ plugin: pluginIdSchema,
225
+ /** The id of the extension point. */
226
+ point: extensionPointIdSchema,
227
+ /** The point's major version the addon was written against. */
228
+ version: z.int().min(1),
229
+ /**
230
+ * The categories the contribution hands the host plugin. Among the addon's
231
+ * own `personalData`, and among what the point accepts, which the host
232
+ * checks against the host plugin's manifest when both are loaded.
233
+ */
234
+ personalData: z
235
+ .array(z.enum(PLUGIN_PERSONAL_DATA_CATEGORIES))
236
+ .max(16)
237
+ .default([]),
238
+ });
239
+ /**
240
+ * The declared extension points over a given reading of one point.
241
+ *
242
+ * The manifest reads a point as `pluginExtensionPointSchema` does; the catalog
243
+ * index reads it strictly and with the curator's title and description. Both
244
+ * keep the rule that an id appears once, which is why the list is built here.
245
+ */
246
+ export function declaredExtensionPointsSchema(point) {
247
+ return z
248
+ .array(point)
249
+ .max(MAX_EXTENSION_POINTS)
250
+ .superRefine((points, ctx) => {
251
+ const seen = new Set();
252
+ for (const [index, declared] of points.entries()) {
253
+ if (seen.has(declared.id)) {
254
+ ctx.addIssue({
255
+ code: "custom",
256
+ path: [index, "id"],
257
+ message: `two extension points are declared with the id "${declared.id}"`,
258
+ });
259
+ }
260
+ seen.add(declared.id);
261
+ }
262
+ });
263
+ }
264
+ /**
265
+ * The declared extensions over a given reading of one, each pair of plugin and
266
+ * point once: an addon contributes once to each point it extends.
267
+ */
268
+ export function declaredExtensionsSchema(extension) {
269
+ return z
270
+ .array(extension)
271
+ .max(MAX_EXTENSION_POINTS)
272
+ .superRefine((extensions, ctx) => {
273
+ const seen = new Set();
274
+ for (const [index, declared] of extensions.entries()) {
275
+ const key = `${declared.plugin}/${declared.point}`;
276
+ if (seen.has(key)) {
277
+ ctx.addIssue({
278
+ code: "custom",
279
+ path: [index, "point"],
280
+ message: `"${declared.point}" of "${declared.plugin}" is extended twice`,
281
+ });
282
+ }
283
+ seen.add(key);
284
+ }
285
+ });
286
+ }
287
+ /**
288
+ * The rules between a plugin's extension declarations and the rest of its
289
+ * declaration (ADR 0030), as issues with the path each one is at.
290
+ *
291
+ * One function for the manifest and the catalog entry, so an entry the loader
292
+ * would refuse is never offered for consent.
293
+ */
294
+ export function extensionRuleIssues(declaration) {
295
+ const issues = [];
296
+ const own = new Set(declaration.personalData);
297
+ /*
298
+ * One level. An addon that could be extended in turn would make "what can
299
+ * reach this screen" a question the board could not answer from the two
300
+ * consent screens in front of it.
301
+ */
302
+ if (declaration.extends.length > 0 &&
303
+ declaration.extensionPoints.length > 0) {
304
+ issues.push({
305
+ path: ["extensionPoints"],
306
+ message: "a plugin that extends another declares no extension points of its own",
307
+ });
308
+ }
309
+ for (const [index, point] of declaration.extensionPoints.entries()) {
310
+ const foreign = point.personalData.filter((category) => !own.has(category));
311
+ if (foreign.length > 0) {
312
+ issues.push({
313
+ path: ["extensionPoints", index, "personalData"],
314
+ message: `${foreign.join(", ")} must be among the plugin's own personalData: what an addon hands a plugin is handled by it`,
315
+ });
316
+ }
317
+ /*
318
+ * What a website block renders is published to the street, and
319
+ * `site:blocks` already keeps the address book away from such a plugin
320
+ * (ADR 0006). A point that accepted names would be the same path through a
321
+ * second plugin.
322
+ */
323
+ if (declaration.permissions.includes("site:blocks") &&
324
+ point.personalData.length > 0) {
325
+ issues.push({
326
+ path: ["extensionPoints", index, "personalData"],
327
+ message: 'a plugin that asks for "site:blocks" declares extension points that accept no personal data',
328
+ });
329
+ }
330
+ }
331
+ for (const [index, extension] of declaration.extends.entries()) {
332
+ if (extension.plugin === declaration.id) {
333
+ issues.push({
334
+ path: ["extends", index, "plugin"],
335
+ message: "a plugin does not extend itself",
336
+ });
337
+ }
338
+ const foreign = extension.personalData.filter((category) => !own.has(category));
339
+ if (foreign.length > 0) {
340
+ issues.push({
341
+ path: ["extends", index, "personalData"],
342
+ message: `${foreign.join(", ")} must be among the plugin's own personalData`,
343
+ });
344
+ }
345
+ }
346
+ return issues;
347
+ }
348
+ /**
349
+ * The permissions in a list that reach the members' details, which a plugin
350
+ * that puts blocks on the website may not hold, and may not be handed data
351
+ * from (ADR 0006): those that read the register, following
352
+ * `PERMISSION_REGISTER_ACCESS`, and `addressBook:proposeImport`. That one reads
353
+ * nothing in the register, so it is not classified as a read, but the plugin
354
+ * holds the member list it proposes, identity numbers and protected persons
355
+ * included (ADR 0043).
356
+ */
357
+ function memberDataPermissions(permissions) {
358
+ return permissions.filter((permission) => REGISTER_READING_PERMISSIONS.includes(permission) || permission === "addressBook:proposeImport");
359
+ }
360
+ /**
361
+ * The permissions that keep an addon and the plugin it extends apart, or an
362
+ * empty list when there are none (ADR 0030): those of whichever of the two
363
+ * does not ask for `site:blocks` that read the register or propose an import,
364
+ * when the other does.
365
+ *
366
+ * A plugin that asks for `site:blocks` reads nothing in the register and holds
367
+ * no member list, so what it publishes cannot name a resident. An extension is
368
+ * a path between two plugins in both directions: the host hands the addon the
369
+ * point's input, and the addon hands the host its output. A website host's
370
+ * points accept no personal data, but an addon that reads the register, or
371
+ * holds the member list it proposes, could still return a name in a field the
372
+ * point types as text, and only the addon's own `personalData` would say it
373
+ * does not. A website addon extending such a host could be handed names as the
374
+ * point's input, keep them, and render them in a block. So the rule reads what
375
+ * each side can reach, from the two manifests, rather than what either
376
+ * declares it hands over. Like the rule on a single plugin, it holds an honest
377
+ * pair to the website's argument rather than a hostile one.
378
+ */
379
+ export function extensionPermissionsRefused(addon, host) {
380
+ return [
381
+ ...(host.permissions.includes("site:blocks")
382
+ ? memberDataPermissions(addon.permissions)
383
+ : []),
384
+ ...(addon.permissions.includes("site:blocks")
385
+ ? memberDataPermissions(host.permissions)
386
+ : []),
387
+ ];
388
+ }
389
+ /**
390
+ * The personal data a plugin handles that keeps an addon asking for
391
+ * `site:blocks` off it, or an empty list when there is none (ADR 0030): the
392
+ * host plugin's own `personalData`, when the addon asks for `site:blocks`.
393
+ *
394
+ * The point's input is the one thing that crosses an extension and that no
395
+ * manifest declares. A host plugin that reads nothing in the register can
396
+ * still be handed names by another of its addons, and then hand them on as a
397
+ * point's input. A host plugin that handles no personal data has none to hand
398
+ * on, so a website addon extends only such a plugin.
399
+ */
400
+ export function extensionHostPersonalDataRefused(addon, host) {
401
+ return addon.permissions.includes("site:blocks")
402
+ ? [...host.personalData]
403
+ : [];
404
+ }
405
+ /**
406
+ * Why the host plugin's points cannot serve an addon's extension, or null when
407
+ * they can (ADR 0030): the host declares no such point, declares it at another
408
+ * major version, or the point does not accept a personal-data category the
409
+ * addon hands over.
410
+ *
411
+ * Both the loader and the catalog read this, so an index that lists an addon
412
+ * beside the plugin it extends is refused for the three refusals the loader
413
+ * would give those two declarations. What the installed packages declare is
414
+ * the loader's to read.
415
+ */
416
+ export function extensionPointRefusal(extension, hostPoints) {
417
+ const point = hostPoints.find((declared) => declared.id === extension.point);
418
+ if (point === undefined) {
419
+ return { reason: "extension-point-unknown" };
420
+ }
421
+ if (point.version !== extension.version) {
422
+ return {
423
+ reason: "extension-version-unsupported",
424
+ supported: point.version,
425
+ };
426
+ }
427
+ const accepted = new Set(point.personalData);
428
+ const categories = extension.personalData.filter((category) => !accepted.has(category));
429
+ return categories.length > 0
430
+ ? { reason: "extension-personal-data-refused", categories }
431
+ : null;
432
+ }
433
+ /**
434
+ * The plugins joined to one another by extensions, each group in the order
435
+ * of `members`. An extension joins an addon to its host plugin whichever way
436
+ * data crosses it, and an addon that extends two plugins joins both groups.
437
+ */
438
+ function extensionGroups(members) {
439
+ const root = new Map(members.map((member) => [member.id, member.id]));
440
+ const find = (id) => {
441
+ let current = id;
442
+ let parent = root.get(current);
443
+ while (parent !== undefined && parent !== current) {
444
+ current = parent;
445
+ parent = root.get(current);
446
+ }
447
+ return current;
448
+ };
449
+ for (const member of members) {
450
+ for (const extension of member.extends) {
451
+ if (root.has(extension.plugin)) {
452
+ root.set(find(member.id), find(extension.plugin));
453
+ }
454
+ }
455
+ }
456
+ const groups = new Map();
457
+ for (const member of members) {
458
+ const group = groups.get(find(member.id)) ?? [];
459
+ group.push(member);
460
+ groups.set(find(member.id), group);
461
+ }
462
+ return [...groups.values()];
463
+ }
464
+ /**
465
+ * Which addons give way, in turn, while a group still holds both a plugin
466
+ * that asks for `site:blocks` and one that reads the register or proposes an
467
+ * import: the website addons first, then the addons that read the register or
468
+ * propose an import, then the addons that join two of the group's plugins. The
469
+ * host plugins never do, because every other addon of theirs would go with
470
+ * them; once the addons of the first two kinds are gone, the two sides are
471
+ * host plugins, and what still joins them is an addon that extends both.
472
+ */
473
+ const GROUP_YIELDS = [
474
+ (member) => member.permissions.includes("site:blocks"),
475
+ (member) => memberDataPermissions(member.permissions).length > 0,
476
+ (member, standing) => new Set(member.extends
477
+ .map((extension) => extension.plugin)
478
+ .filter((plugin) => standing.has(plugin))).size > 1,
479
+ ];
480
+ /**
481
+ * The addons refused because the plugins joined to them by extensions hold
482
+ * both a plugin that asks for `site:blocks` and one that reads the register or
483
+ * proposes an import, by id (ADR 0030).
484
+ *
485
+ * The rule between an addon and its host plugin reads two manifests. Data
486
+ * also crosses between two addons of one host plugin, and further: an addon
487
+ * that reads the register, or holds the member list it proposes, hands the
488
+ * host plugin a name while declaring nothing, the host plugin hands it to a
489
+ * website addon as a point's input, and the website addon renders it. Neither manifest of any one pair says so.
490
+ * So the plugins joined by extensions, through one host plugin or several,
491
+ * are read as one group, and a group never holds both kinds.
492
+ *
493
+ * The answer depends only on the set of members, never on their order, so it
494
+ * is the same whichever order the loader finds the packages in. Addons give
495
+ * way in the order of `GROUP_YIELDS`, which keeps the plugin that reads the
496
+ * register or proposes an import serving and refuses the website addon beside
497
+ * it.
498
+ */
499
+ export function extensionGroupRefusals(members) {
500
+ const refused = new Map();
501
+ for (const yields of GROUP_YIELDS) {
502
+ const standing = members.filter((member) => !refused.has(member.id));
503
+ const ids = new Set(standing.map((member) => member.id));
504
+ for (const group of extensionGroups(standing)) {
505
+ const website = group
506
+ .filter((member) => member.permissions.includes("site:blocks"))
507
+ .map((member) => member.id);
508
+ const register = group
509
+ .filter((member) => memberDataPermissions(member.permissions).length > 0)
510
+ .map((member) => member.id);
511
+ if (website.length === 0 || register.length === 0) {
512
+ continue;
513
+ }
514
+ for (const member of group) {
515
+ if (member.extends.length > 0 && yields(member, ids)) {
516
+ refused.set(member.id, { website, register });
517
+ }
518
+ }
519
+ }
520
+ }
521
+ return refused;
522
+ }
523
+ /**
524
+ * One device route: a path under the plugin's own mount that answers to a
525
+ * device token and to nothing else (ADR 0031).
526
+ *
527
+ * Declared in the manifest because the board reads it on the install consent
528
+ * screen, and because the guard has to know which paths take no session cookie
529
+ * before any of the plugin's code has run.
530
+ */
531
+ export const pluginDeviceRouteSchema = z.object({
532
+ /** The handle a token is issued for. */
533
+ id: z
534
+ .string()
535
+ .min(2)
536
+ .max(32)
537
+ .regex(/^[a-z0-9]+(-[a-z0-9]+)*$/, "must be lowercase letters, digits and single hyphens"),
538
+ /** The path, which covers itself and everything below it. */
539
+ path: pluginRoutePathSchema,
540
+ });
541
+ /** At most this many device routes per plugin. */
542
+ export const MAX_DEVICE_ROUTES = 4;
543
+ /**
544
+ * Whether one route path is the other or lies below it, segment by segment.
545
+ *
546
+ * `screen` and `screen/pictures` overlap; `screen` and `screens` do not.
547
+ */
548
+ export function routePathsOverlap(left, right) {
549
+ return (left === right ||
550
+ left.startsWith(`${right}/`) ||
551
+ right.startsWith(`${left}/`));
552
+ }
553
+ /**
554
+ * The declared device routes, as every boundary that reads them must read
555
+ * them: the manifest and the catalog entry the consent screen renders from.
556
+ *
557
+ * An id names one route, and no two routes overlap. A token is issued for one
558
+ * route by its id, and a request is matched to one route by its path, so a
559
+ * duplicated id or a path inside another route's would leave the guard two
560
+ * answers to choose from.
561
+ */
562
+ export function declaredDeviceRoutesSchema(route) {
563
+ return z
564
+ .array(route)
565
+ .max(MAX_DEVICE_ROUTES)
566
+ .superRefine((routes, ctx) => {
567
+ for (const [index, declared] of routes.entries()) {
568
+ for (const earlier of routes.slice(0, index)) {
569
+ if (earlier.id === declared.id) {
570
+ ctx.addIssue({
571
+ code: "custom",
572
+ path: [index, "id"],
573
+ message: `two device routes are declared with the id "${declared.id}"`,
574
+ });
575
+ }
576
+ if (routePathsOverlap(earlier.path, declared.path)) {
577
+ ctx.addIssue({
578
+ code: "custom",
579
+ path: [index, "path"],
580
+ message: `the device route "${declared.path}" overlaps "${earlier.path}"`,
581
+ });
582
+ }
583
+ }
584
+ }
585
+ });
586
+ }
587
+ export const pluginDeviceRoutesSchema = declaredDeviceRoutesSchema(pluginDeviceRouteSchema);
588
+ /**
589
+ * The rules a declaration of device routes has to keep beside the rest of the
590
+ * manifest, for the manifest and the catalog entry alike.
591
+ *
592
+ * A device route may not overlap the OAuth protected resource, which takes a
593
+ * different token on its own branch of the guard, and a plugin with a device
594
+ * route reads nothing from the register: a device is a screen on a wall that
595
+ * anyone passing reads (ADR 0031).
596
+ */
597
+ export function deviceRouteRuleIssues(declaration) {
598
+ const issues = [];
599
+ if (declaration.deviceRoutes.length === 0) {
600
+ return issues;
601
+ }
602
+ const resource = declaration.oauthProtectedResource;
603
+ for (const [index, route] of declaration.deviceRoutes.entries()) {
604
+ if (resource !== undefined && routePathsOverlap(route.path, resource)) {
605
+ issues.push({
606
+ path: ["deviceRoutes", index, "path"],
607
+ message: `the device route "${route.path}" overlaps the oauthProtectedResource "${resource}"`,
608
+ rule: "device-route-overlaps-oauth-resource",
609
+ });
610
+ }
611
+ }
612
+ // The finances too: a fee notice and a charge carry the names of the
613
+ // people they are for, as they do for `site:blocks`.
614
+ if (readsRegister(declaration.permissions)) {
615
+ issues.push({
616
+ path: ["permissions"],
617
+ message: "a plugin that declares deviceRoutes may not also read the address book or the finances: what a device shows, anyone passing it reads",
618
+ rule: "device-route-reads-register",
619
+ });
620
+ }
621
+ // And the member list a plugin that proposes an import holds, for the same
622
+ // reason.
623
+ if (declaration.permissions.includes("addressBook:proposeImport")) {
624
+ issues.push({
625
+ path: ["permissions"],
626
+ message: "a plugin that declares deviceRoutes may not also propose imports: it holds the member list, and what a device shows, anyone passing it reads",
627
+ rule: "device-route-proposes-import",
628
+ });
629
+ }
630
+ return issues;
631
+ }
632
+ /**
633
+ * What a plugin that proposes an import declares about the data it carries,
634
+ * for the manifest and the catalog entry alike (ADR 0043).
635
+ *
636
+ * The file is the association's member list, and the plugin carries it
637
+ * whole: every name, contact detail and personal identity number, and rows
638
+ * about persons with protected personal data, which the file does not mark.
639
+ * So the board is told so on the consent screen. And no other plugin can reach
640
+ * an identity number or a protected person, so for any other plugin either
641
+ * category would describe something that cannot happen.
642
+ */
643
+ export function proposeImportPersonalDataIssues(declaration) {
644
+ const proposes = declaration.permissions.includes("addressBook:proposeImport");
645
+ if (proposes) {
646
+ const missing = PROPOSE_IMPORT_PERSONAL_DATA.filter((category) => !declaration.personalData.includes(category));
647
+ return missing.length === 0
648
+ ? []
649
+ : [
650
+ {
651
+ message: `a plugin that asks for "addressBook:proposeImport" carries the member list and must declare ${missing.map((category) => `"${category}"`).join(", ")}`,
652
+ },
653
+ ];
654
+ }
655
+ return PROPOSE_IMPORT_ONLY_PERSONAL_DATA.filter((category) => declaration.personalData.includes(category)).map((category) => ({
656
+ message: `only a plugin that asks for "addressBook:proposeImport" may declare "${category}": no other plugin can reach it`,
657
+ }));
658
+ }
659
+ /**
660
+ * A calendar date written `YYYY-MM-DD`, and a real one: `2027-02-30` is
661
+ * refused rather than rolled over into March.
662
+ */
663
+ export function isCalendarDay(text) {
664
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
665
+ if (match === null) {
666
+ return false;
667
+ }
668
+ const [year, month, day] = [match[1], match[2], match[3]].map(Number);
669
+ const instant = new Date(Date.UTC(year, month - 1, day));
670
+ return (instant.getUTCFullYear() === year &&
671
+ instant.getUTCMonth() === month - 1 &&
672
+ instant.getUTCDate() === day);
673
+ }
674
+ const calendarDaySchema = z
675
+ .string()
676
+ .refine(isCalendarDay, "must be a calendar date, YYYY-MM-DD");
677
+ /**
678
+ * Which of an issuer's keys signed a licence (ADR 0042).
679
+ *
680
+ * It selects the keys to try and asserts nothing: two plugins may declare one
681
+ * key id with different keys, and a key is known by its fingerprint.
682
+ */
683
+ export const licenceKeyIdSchema = z
684
+ .string()
685
+ .regex(/^[a-z0-9-]{1,64}$/, "must be 1 to 64 lowercase letters, digits and hyphens");
686
+ /**
687
+ * An Ed25519 public key: the base64 of its 32 raw bytes, padded, and in the
688
+ * one spelling that decodes to them. The last character before the padding
689
+ * carries two bits no key has, which is what the class of letters it may be
690
+ * from says.
691
+ */
692
+ export const licencePublicKeySchema = z
693
+ .string()
694
+ .regex(/^[A-Za-z0-9+/]{42}[AEIMQUYcgkosw048]=$/, "must be the base64 of a 32-byte Ed25519 public key");
695
+ /**
696
+ * A service a plugin may present the instance's licence to: an `https` origin
697
+ * as the WHATWG URL standard serialises one, so it compares exactly.
698
+ *
699
+ * A lower-case domain in punycode, a port only when it is not 443, and no
700
+ * userinfo, path, query or fragment. A host whose last label is a number is
701
+ * an IPv4 address to that standard, which serialises it otherwise, so it is
702
+ * refused rather than compared in a form no browser would print. Plain `http`
703
+ * is refused too: a presentation sent in the clear could be replayed before
704
+ * the service saw its nonce.
705
+ */
706
+ export const licenceAudienceSchema = z
707
+ .string()
708
+ .max(256)
709
+ .refine(isCanonicalHttpsOrigin, {
710
+ error: "must be an https origin in its canonical form, such as https://themes.example.org",
711
+ });
712
+ function isCanonicalHttpsOrigin(value) {
713
+ const match = /^https:\/\/([a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*)(?::([1-9][0-9]{0,4}))?$/.exec(value);
714
+ if (match === null) {
715
+ return false;
716
+ }
717
+ const host = match[1] ?? "";
718
+ const lastLabel = host.slice(host.lastIndexOf(".") + 1);
719
+ if (/^(?:[0-9]+|0x[0-9a-f]*)$/.test(lastLabel) || host.length > 253) {
720
+ return false;
721
+ }
722
+ const port = match[2];
723
+ return port === undefined || (Number(port) <= 65535 && port !== "443");
724
+ }
725
+ /**
726
+ * The licence a paid plugin reads (ADR 0042), and whom it trusts to issue it.
727
+ *
728
+ * The keys sit here rather than in the core, so the core holds no vendor's
729
+ * key and anyone who sells a plugin can issue licences for it in the same
730
+ * format. The host verifies; a plugin reads the result through
731
+ * `host.entitlements`.
732
+ *
733
+ * Strict, because what it grants is consented to: a misspelled field read as
734
+ * absent would be a declaration the board never saw.
735
+ */
736
+ export const pluginLicenceSchema = z.strictObject({
737
+ /**
738
+ * The one entitlement the plugin reads, named as a plugin id is. A licence
739
+ * may carry several; the host reports this one only.
740
+ */
741
+ entitlement: pluginIdSchema,
742
+ /**
743
+ * Who issues the licence. The licence screen and the audit log name the
744
+ * issuer from here, never from text in a licence file.
745
+ */
746
+ issuer: z.strictObject({
747
+ name: z.string().min(1).max(100),
748
+ url: z
749
+ .string()
750
+ .max(2000)
751
+ .regex(/^https:\/\/[^\s/?#]+(?:\/\S*)?$/, "must be an https address"),
752
+ }),
753
+ /**
754
+ * The issuer's public keys by key id. At most two at once: a new key is
755
+ * declared in a release before any licence is signed with it, and the old
756
+ * one leaves in a later release.
757
+ */
758
+ keys: z.record(licenceKeyIdSchema, licencePublicKeySchema).refine((keys) => {
759
+ const count = Object.keys(keys).length;
760
+ return count >= 1 && count <= 2;
761
+ }, "must declare one or two keys"),
762
+ /**
763
+ * The services of the issuer's the plugin may present the licence to.
764
+ * None for a plugin that uses no service of its issuer's.
765
+ */
766
+ audiences: z
767
+ .array(licenceAudienceSchema)
768
+ .max(8)
769
+ .refine((audiences) => new Set(audiences).size === audiences.length, "must not name an audience twice")
770
+ .default([]),
771
+ });
772
+ export const pluginManifestSchema = z
773
+ .object({
774
+ /**
775
+ * The contract version this plugin was built against. The loader refuses
776
+ * anything it does not implement rather than loading it and hoping.
777
+ */
778
+ apiVersion: z.int().min(1),
779
+ id: pluginIdSchema,
780
+ entry: pluginEntrySchema,
781
+ permissions: z.array(z.enum(PLUGIN_PERMISSIONS)).max(16).default([]),
782
+ personalData: z
783
+ .array(z.enum(PLUGIN_PERSONAL_DATA_CATEGORIES))
784
+ .max(16)
785
+ .default([]),
786
+ settingsSchema: pluginSettingsSchema.optional(),
787
+ /**
788
+ * The plugin's own screen in the application, when it has one, at
789
+ * `/plugin/<id>`. The title is an i18n key in the plugin's own namespace.
790
+ */
791
+ view: pluginViewSchema.optional(),
792
+ /** What this plugin proposes the platform be able to do. */
793
+ actions: pluginActionsSchema.default([]),
794
+ /**
795
+ * The route, under this plugin's own mount, that serves MCP.
796
+ *
797
+ * Declaring it makes that route the instance's OAuth protected resource:
798
+ * its full URL becomes the audience every issued token is bound to, the
799
+ * discovery documents point at it, and the route stops accepting the
800
+ * browser's session cookie and accepts only a Bearer token issued for it.
801
+ *
802
+ * At most one installed plugin may declare it, because the audience is a
803
+ * single URL and moving it would strand every token already issued. A
804
+ * second one is refused at install.
805
+ */
806
+ oauthProtectedResource: pluginRoutePathSchema.optional(),
807
+ /**
808
+ * The routes, under this plugin's own mount, that a device reaches with a
809
+ * device token the board issued (ADR 0031).
810
+ *
811
+ * Such a route answers `GET` and `HEAD` with `Authorization: Bearer
812
+ * <device token>` and nothing else: no session cookie, no person and no
813
+ * capability floor. What stands in for the floor is that a plugin declaring
814
+ * one may not read the register.
815
+ */
816
+ deviceRoutes: pluginDeviceRoutesSchema.default([]),
817
+ /**
818
+ * That this plugin is an AI package: a language model chooses the actions
819
+ * it dispatches, for the person whose request its route is serving.
820
+ *
821
+ * Declaring it moves the plugin's dispatch onto the `ai` audit channel and
822
+ * the `ai` surface. Its writes are recorded as made through the AI package
823
+ * rather than as any plugin's; it reaches only the actions offered on `ai`;
824
+ * and a plugin action is reached only once an administrator has armed it,
825
+ * as on `mcp`. A connected app's token on its route keeps `mcp`.
826
+ *
827
+ * It narrows what the plugin can reach and never widens it, and it is the
828
+ * only say a plugin has in its channel. The host derives the channel from
829
+ * this declaration and from how the request arrived; nothing a plugin
830
+ * passes to `host.actions` names one. The consent screen shows it.
831
+ */
832
+ ai: z.boolean().default(false),
833
+ /**
834
+ * The blocks this plugin adds to the association's website.
835
+ *
836
+ * Declared here rather than at registration for the reason actions are:
837
+ * the board consents to what a plugin may put on the website before any of
838
+ * its code has run. A plugin declaring one asks for `site:blocks`.
839
+ */
840
+ blocks: pluginBlocksSchema.default([]),
841
+ /**
842
+ * The extension points this plugin offers other plugins (ADR 0030). At
843
+ * most eight.
844
+ */
845
+ extensionPoints: declaredExtensionPointsSchema(pluginExtensionPointSchema).default([]),
846
+ /**
847
+ * The extension points of other plugins this plugin extends, which makes
848
+ * it an addon (ADR 0030). At most eight.
849
+ */
850
+ extends: declaredExtensionsSchema(pluginExtensionSchema).default([]),
851
+ /**
852
+ * Where the plugin connects to outside the instance, and which personal
853
+ * data each place receives (ADR 0037).
854
+ *
855
+ * Shown on the install consent screen, where it fills in the question
856
+ * about where the plugin sends personal data, and consented to as a
857
+ * snapshot: a republished version that adds a recipient, moves one or
858
+ * sends one more is refused at load until the board consents again.
859
+ */
860
+ recipients: pluginRecipientsSchema.default([]),
861
+ /**
862
+ * The licence this plugin reads, for a paid plugin (ADR 0042).
863
+ *
864
+ * Declaring it gives the plugin `host.entitlements`. Nothing else in the
865
+ * host reads it: the plugin loads and serves either way, and what it
866
+ * withholds without an active entitlement is its own decision.
867
+ */
868
+ licence: pluginLicenceSchema.optional(),
869
+ /**
870
+ * The day this release was published, written by the release workflow and
871
+ * compared with a licence's `updatesUntil`. Required with `licence`.
872
+ */
873
+ released: calendarDaySchema.optional(),
874
+ })
875
+ .superRefine((manifest, ctx) => {
876
+ for (const issue of extensionRuleIssues(manifest)) {
877
+ ctx.addIssue({
878
+ code: "custom",
879
+ path: issue.path,
880
+ message: issue.message,
881
+ });
882
+ }
883
+ /*
884
+ * A licence sold with a year of updates covers the releases published in
885
+ * that year, and a release that does not say when it was published cannot
886
+ * be judged. Refused here rather than read as covered, which would make
887
+ * every licence for updates cover every release.
888
+ */
889
+ if (manifest.licence !== undefined && manifest.released === undefined) {
890
+ ctx.addIssue({
891
+ code: "custom",
892
+ path: ["released"],
893
+ message: "a plugin that declares a licence must say when it was released",
894
+ });
895
+ }
896
+ // What a recipient receives is part of what the plugin handles.
897
+ for (const issue of recipientCategoryIssues(manifest.recipients, manifest.personalData)) {
898
+ ctx.addIssue({ code: "custom", ...issue });
899
+ }
900
+ /*
901
+ * A block is published on the website, to readers with no account, so it
902
+ * is something the board consents to by name rather than something a
903
+ * plugin acquires by declaring it. The permission is what the install
904
+ * consent screen shows; the declarations are what the host registers.
905
+ */
906
+ if (manifest.blocks.length > 0 &&
907
+ !manifest.permissions.includes("site:blocks")) {
908
+ ctx.addIssue({
909
+ code: "custom",
910
+ path: ["blocks"],
911
+ message: 'a plugin that declares blocks must ask for the "site:blocks" permission',
912
+ });
913
+ }
914
+ /*
915
+ * And a plugin that puts blocks on the website does not read the
916
+ * register. The website's argument that it cannot publish a resident's
917
+ * details rests on there being no path from it to the register (ADR 0006),
918
+ * and a block that could ask the address book what to print would be one.
919
+ * So would one that could ask the finances: a fee notice and a charge carry
920
+ * the names of the people they are for. Permissions are not a sandbox, so
921
+ * this holds an honest plugin to that argument rather than a hostile one;
922
+ * curation does the rest.
923
+ */
924
+ if (manifest.permissions.includes("site:blocks") &&
925
+ readsRegister(manifest.permissions)) {
926
+ ctx.addIssue({
927
+ code: "custom",
928
+ path: ["permissions"],
929
+ message: 'a plugin that asks for "site:blocks" may not also read the address book or the finances: what a block renders is published to anybody',
930
+ params: { rule: "blocks-read-register" },
931
+ });
932
+ }
933
+ /*
934
+ * Nor one that proposes an import. It reads nothing from the register, but
935
+ * it holds the member list itself, identity numbers and protected persons
936
+ * included, and a block is published to anybody (ADR 0043).
937
+ */
938
+ if (manifest.permissions.includes("site:blocks") &&
939
+ manifest.permissions.includes("addressBook:proposeImport")) {
940
+ ctx.addIssue({
941
+ code: "custom",
942
+ path: ["permissions"],
943
+ message: 'a plugin that asks for "site:blocks" may not also propose imports: it holds the member list, and what a block renders is published to anybody',
944
+ params: { rule: "blocks-propose-import" },
945
+ });
946
+ }
947
+ for (const issue of proposeImportPersonalDataIssues(manifest)) {
948
+ ctx.addIssue({ code: "custom", path: ["personalData"], ...issue });
949
+ }
950
+ /*
951
+ * A news item and an event are text the board wrote, and published text
952
+ * may name anybody. `personalData` names everything a plugin handles, not
953
+ * only what it reads from the register, so a plugin reading that text says
954
+ * so, and the record of processing carries it (ADR 0032).
955
+ */
956
+ for (const permission of ["news:read", "calendar:read"]) {
957
+ if (manifest.permissions.includes(permission) &&
958
+ !manifest.personalData.includes("freeText")) {
959
+ ctx.addIssue({
960
+ code: "custom",
961
+ path: ["personalData"],
962
+ message: `a plugin that asks for "${permission}" must declare "freeText": published text may name anybody`,
963
+ });
964
+ }
965
+ }
966
+ /*
967
+ * The same for a picture. The host opens only one whose upload declared
968
+ * that it shows no identifiable person, but that is somebody's declaration
969
+ * and not a fact about the picture, and a photograph is a category of its
970
+ * own in the record of processing whoever is in it (ADR 0032).
971
+ */
972
+ if (manifest.permissions.includes("media:read") &&
973
+ !manifest.personalData.includes("photograph")) {
974
+ ctx.addIssue({
975
+ code: "custom",
976
+ path: ["personalData"],
977
+ message: 'a plugin that asks for "media:read" must declare "photograph": a stored picture is a photograph whoever is in it',
978
+ });
979
+ }
980
+ for (const { rule, ...issue } of deviceRouteRuleIssues(manifest)) {
981
+ ctx.addIssue({ code: "custom", ...issue, params: { rule } });
982
+ }
983
+ /*
984
+ * The composed name has to fit the public pattern, and only the pair can
985
+ * know whether it does: a plugin id runs to 48 characters and an action id to
986
+ * 32, which composes to 81 against a limit of 64. Checked pairwise rather
987
+ * than by shortening the action id to a worst case, because that would charge
988
+ * every plugin for the longest possible plugin id and forbid
989
+ * `monthly_occupancy_report` to a plugin called `brf`, where the composed
990
+ * name is 28 characters.
991
+ *
992
+ * Refused here rather than at registration so that it is manifest-invalid
993
+ * before the install consent screen, instead of a finding after the plugin's
994
+ * code has already run.
995
+ */
996
+ for (const [index, action] of manifest.actions.entries()) {
997
+ const composed = composedActionName(manifest.id, action.id);
998
+ if (!ACTION_NAME_PATTERN.test(composed)) {
999
+ ctx.addIssue({
1000
+ code: "custom",
1001
+ path: ["actions", index, "id"],
1002
+ message: `the public name "${composed}" is ${String(composed.length)} characters; a plugin id and an action id compose to at most 64`,
1003
+ });
1004
+ }
1005
+ }
1006
+ });
1007
+ /**
1008
+ * Dependency fields npm installs at install time.
1009
+ *
1010
+ * The installer omits peer, dev and optional dependencies and runs no scripts,
1011
+ * but it still hands npm the package: anything listed here would send that npm
1012
+ * to a registry, which an instance never contacts. optionalDependencies is
1013
+ * listed although it is omitted, because a package relying on one being
1014
+ * present works in development and quietly not on an instance.
1015
+ */
1016
+ export const RUNTIME_DEPENDENCY_FIELDS = [
1017
+ "dependencies",
1018
+ "optionalDependencies",
1019
+ "bundleDependencies",
1020
+ "bundledDependencies",
1021
+ ];
1022
+ /** Package names in a dependency field, whether a map or npm's bundle list. */
1023
+ export function dependencyNames(value) {
1024
+ if (Array.isArray(value)) {
1025
+ return value.filter((name) => typeof name === "string");
1026
+ }
1027
+ return typeof value === "object" && value !== null ? Object.keys(value) : [];
1028
+ }
1029
+ /** Whether a dependency field is an empty map or an empty list. */
1030
+ export function isEmptyDependencyField(value) {
1031
+ if (Array.isArray(value)) {
1032
+ return value.length === 0;
1033
+ }
1034
+ return (typeof value === "object" &&
1035
+ value !== null &&
1036
+ Object.keys(value).length === 0);
1037
+ }
1038
+ /**
1039
+ * A dependency field that names nothing: absent, an empty map or an empty
1040
+ * list. Anything else is refused, a string included, since npm reads even
1041
+ * that as something to install. See RUNTIME_DEPENDENCY_FIELDS for why.
1042
+ *
1043
+ * Part of the schema rather than a check beside it, so the loader refuses such
1044
+ * a package at boot as well as the catalog refusing to list it.
1045
+ */
1046
+ const noRuntimeDependencies = z
1047
+ .unknown()
1048
+ .superRefine((value, ctx) => {
1049
+ const names = dependencyNames(value);
1050
+ if (names.length > 0) {
1051
+ ctx.addIssue({
1052
+ code: "custom",
1053
+ message: `names ${names.join(", ")}; a plugin declares no runtime dependencies, because the installer resolves nothing from a registry`,
1054
+ });
1055
+ }
1056
+ else if (!isEmptyDependencyField(value)) {
1057
+ ctx.addIssue({
1058
+ code: "custom",
1059
+ message: "is neither an empty map nor an empty list; a plugin declares no runtime dependencies, because the installer resolves nothing from a registry",
1060
+ });
1061
+ }
1062
+ })
1063
+ .optional();
1064
+ /**
1065
+ * The subset of package.json the loader reads. Everything else in the file is
1066
+ * npm's business, apart from the dependency fields, which must name nothing.
1067
+ */
1068
+ export const pluginPackageSchema = z.object({
1069
+ name: z.string().min(1),
1070
+ version: z.string().min(1),
1071
+ openbrf: pluginManifestSchema,
1072
+ dependencies: noRuntimeDependencies,
1073
+ optionalDependencies: noRuntimeDependencies,
1074
+ bundleDependencies: noRuntimeDependencies,
1075
+ bundledDependencies: noRuntimeDependencies,
1076
+ });
1077
+ /**
1078
+ * Parses a plugin's package.json.
1079
+ *
1080
+ * Returns a result rather than throwing, because the only caller that matters
1081
+ * is the boot-time scan, and a malformed plugin directory must be skipped and
1082
+ * reported rather than being allowed to take the register offline (ADR 0003).
1083
+ * A thrown error would make "skip and report" the caller's discipline instead
1084
+ * of the function's contract.
1085
+ */
1086
+ export function parsePluginPackage(input) {
1087
+ const result = pluginPackageSchema.safeParse(input);
1088
+ if (result.success) {
1089
+ return { ok: true, value: result.data };
1090
+ }
1091
+ const diagnostics = result.error.issues.map((issue) => ({
1092
+ rule: manifestRuleOf(issue),
1093
+ issue: `${issue.path.join(".") || "(root)"}: ${issue.message}`,
1094
+ }));
1095
+ return {
1096
+ ok: false,
1097
+ issues: diagnostics.map((diagnostic) => diagnostic.issue),
1098
+ diagnostics,
1099
+ };
1100
+ }
1101
+ function manifestRuleOf(issue) {
1102
+ if (issue.code !== "custom" || typeof issue.params !== "object") {
1103
+ return null;
1104
+ }
1105
+ const rule = issue.params?.rule;
1106
+ return PLUGIN_MANIFEST_RULES.find((known) => known === rule) ?? null;
1107
+ }
1108
+ /** Convenience for a plugin's own CI: throws with every problem at once. */
1109
+ export function assertPluginPackage(input) {
1110
+ const result = parsePluginPackage(input);
1111
+ if (!result.ok) {
1112
+ throw new Error(`Invalid Open BRF plugin manifest:\n ${result.issues.join("\n ")}`);
1113
+ }
1114
+ return result.value;
1115
+ }
1116
+ /** The api version a plugin authored against this SDK should declare. */
1117
+ export const CURRENT_PLUGIN_API_VERSION = PLUGIN_API_VERSION;
1118
+ //# sourceMappingURL=manifest.js.map