@fedify/interaction-controls 2.5.0-dev.2440 → 2.5.0-dev.2448

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/dist/mod.cjs CHANGED
@@ -1,7 +1,354 @@
1
1
 
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ //#region \0rolldown/runtime.js
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
21
+ value: mod,
22
+ enumerable: true
23
+ }) : target, mod));
24
+ //#endregion
3
25
  let _fedify_vocab = require("@fedify/vocab");
26
+ let _fedify_fedify = require("@fedify/fedify");
4
27
  let _fedify_vocab_runtime = require("@fedify/vocab-runtime");
28
+ let _fedify_vocab_runtime_jsonld = require("@fedify/vocab-runtime/jsonld");
29
+ _fedify_vocab_runtime_jsonld = __toESM(_fedify_vocab_runtime_jsonld, 1);
30
+ //#region src/loaders.ts
31
+ /**
32
+ * Wraps the loaders for a single dereferencing operation so that errors they
33
+ * throw can be told apart from other errors afterwards. Parsed objects keep
34
+ * the loaders they were given, so the wrappers stop recording and pass calls
35
+ * through once the operation is released.
36
+ */
37
+ function trackLoaders(documentLoader, contextLoader) {
38
+ const records = /* @__PURE__ */ new Map();
39
+ let released = false;
40
+ const wrap = (loader) => {
41
+ if (loader == null) return void 0;
42
+ return async (url, options) => {
43
+ if (released) return await loader(url, options);
44
+ let remoteDocument;
45
+ try {
46
+ remoteDocument = await loader(url, options);
47
+ } catch (error) {
48
+ if (!released) records.set(error, url);
49
+ throw error;
50
+ }
51
+ if (remoteDocument == null) {
52
+ const message = "The document loader returned no document.";
53
+ let error;
54
+ try {
55
+ error = new _fedify_vocab_runtime.FetchError((0, _fedify_vocab_runtime.parseIri)(url), message);
56
+ } catch {
57
+ error = /* @__PURE__ */ new Error(`${url}: ${message}`);
58
+ }
59
+ if (!released) records.set(error, url);
60
+ throw error;
61
+ }
62
+ return remoteDocument;
63
+ };
64
+ };
65
+ const wrappedDocumentLoader = wrap(documentLoader);
66
+ return {
67
+ documentLoader: wrappedDocumentLoader,
68
+ contextLoader: contextLoader === documentLoader ? wrappedDocumentLoader : wrap(contextLoader),
69
+ records,
70
+ release: () => {
71
+ released = true;
72
+ records.clear();
73
+ }
74
+ };
75
+ }
76
+ const MAX_CAUSE_DEPTH = 8;
77
+ function collectCauses(error, causes = [], depth = 0) {
78
+ if (depth > MAX_CAUSE_DEPTH || causes.includes(error)) return causes;
79
+ causes.push(error);
80
+ if (typeof error !== "object" || error == null) return causes;
81
+ if (error instanceof AggregateError) for (const e of error.errors) collectCauses(e, causes, depth + 1);
82
+ if ("cause" in error) collectCauses(error.cause, causes, depth + 1);
83
+ const details = error.details;
84
+ if (typeof details === "object" && details != null && "cause" in details) collectCauses(details.cause, causes, depth + 1);
85
+ return causes;
86
+ }
87
+ /**
88
+ * Classifies the error thrown by a dereferencing operation. It is a fetch
89
+ * failure only if a loader error is among its causes; errors that loaders
90
+ * threw during attempts the operation recovered from are ignored.
91
+ */
92
+ function classifyFailure(error, tracker) {
93
+ const loaderErrors = collectCauses(error).filter((e) => tracker.records.has(e));
94
+ const transientIndex = loaderErrors.findIndex(_fedify_vocab_runtime.isTransientFetchError);
95
+ const selected = loaderErrors[transientIndex < 0 ? 0 : transientIndex];
96
+ const url = loaderErrors.length < 1 ? null : getFailedUrl(selected, tracker.records.get(selected));
97
+ if (url == null) return {
98
+ category: "unverifiable",
99
+ type: "invalidJsonLd",
100
+ cause: error,
101
+ transient: false
102
+ };
103
+ return {
104
+ category: "unverifiable",
105
+ type: "notDereferenceable",
106
+ url,
107
+ cause: loaderErrors.length === 1 ? selected : error,
108
+ transient: transientIndex >= 0
109
+ };
110
+ }
111
+ function getFailedUrl(error, requestedUrl) {
112
+ const url = error?.url;
113
+ if (url instanceof URL) return url;
114
+ for (const candidate of [url, requestedUrl]) {
115
+ if (typeof candidate !== "string") continue;
116
+ try {
117
+ return (0, _fedify_vocab_runtime.parseIri)(candidate);
118
+ } catch {
119
+ continue;
120
+ }
121
+ }
122
+ return null;
123
+ }
124
+ function notDereferenceable(url) {
125
+ return {
126
+ category: "unverifiable",
127
+ type: "notDereferenceable",
128
+ url,
129
+ transient: false
130
+ };
131
+ }
132
+ //#endregion
133
+ //#region src/evidence.ts
134
+ const COMPATIBLE_ID_PATH_PATTERN = /^\/\.well-known\/apgateway\//i;
135
+ /**
136
+ * Tells whether an ID is an [FEP-ef61] portable ID or a compatible
137
+ * identifier, which no web origin speaks for.
138
+ *
139
+ * [FEP-ef61]: https://w3id.org/fep/ef61
140
+ */
141
+ function isPortableClaim(id) {
142
+ return id.protocol === "ap:" || id.protocol === "ap+ef61:" || (id.protocol === "http:" || id.protocol === "https:") && COMPATIBLE_ID_PATH_PATTERN.test(id.pathname);
143
+ }
144
+ /**
145
+ * Normalizes an ID for comparison: [FEP-ef61] portable IDs and compatible
146
+ * identifiers to their canonical portable IDs, other IDs to their `href`.
147
+ *
148
+ * [FEP-ef61]: https://w3id.org/fep/ef61
149
+ * @returns The normalized ID, or `null` if it is a malformed portable ID or
150
+ * compatible identifier, which never matches anything.
151
+ */
152
+ function canonicalizeId(id) {
153
+ try {
154
+ if (id.protocol === "ap:" || id.protocol === "ap+ef61:") return (0, _fedify_vocab_runtime.canonicalizePortableUri)((0, _fedify_vocab_runtime.formatIri)(id));
155
+ if (isPortableClaim(id)) {
156
+ const portable = (0, _fedify_vocab_runtime.fromCompatibleEf61Id)(id);
157
+ return portable == null ? null : (0, _fedify_vocab_runtime.canonicalizePortableUri)((0, _fedify_vocab_runtime.formatIri)(portable));
158
+ }
159
+ return id.href;
160
+ } catch {
161
+ return null;
162
+ }
163
+ }
164
+ /**
165
+ * Compares two IDs, canonicalizing [FEP-ef61] portable IDs.
166
+ *
167
+ * [FEP-ef61]: https://w3id.org/fep/ef61
168
+ */
169
+ function sameId(left, right) {
170
+ if (left == null || right == null) return false;
171
+ const canonical = canonicalizeId(left);
172
+ return canonical != null && canonical === canonicalizeId(right);
173
+ }
174
+ /** Tells whether a URL carries a username or a password. */
175
+ function hasCredentials(url) {
176
+ return url.username !== "" || url.password !== "";
177
+ }
178
+ /**
179
+ * Gets the single root node of an expanded document.
180
+ * @returns The root node, or `null` if there is not exactly one.
181
+ */
182
+ function getRootNode(expanded) {
183
+ if (expanded.length !== 1) return null;
184
+ const node = expanded[0];
185
+ if (node == null || typeof node !== "object" || Array.isArray(node)) return null;
186
+ return node;
187
+ }
188
+ /**
189
+ * Gets the values of a property of an expanded node, unwrapping a single
190
+ * `@list` container the way generated vocabulary decoders do.
191
+ */
192
+ function getPropertyValues(node, iri) {
193
+ const values = node[iri];
194
+ if (values == null) return [];
195
+ if (!Array.isArray(values)) return [values];
196
+ if (values.length === 1 && values[0] != null && typeof values[0] === "object" && "@list" in values[0]) {
197
+ const list = values[0]["@list"];
198
+ return Array.isArray(list) ? list : [list];
199
+ }
200
+ return values;
201
+ }
202
+ /**
203
+ * Resolves the `@id` of an expanded value, the way generated vocabulary
204
+ * decoders resolve references.
205
+ * @param value The expanded value, a node object or a node reference.
206
+ * @param base The base URL to resolve a relative IRI against.
207
+ * @returns The IRI, or `null` if the value is not a node with a non-blank
208
+ * `@id` that can be resolved.
209
+ */
210
+ function resolveNodeId(value, base) {
211
+ if (value == null || typeof value !== "object" || Array.isArray(value)) return null;
212
+ const id = value["@id"];
213
+ if (typeof id !== "string" || id.startsWith("_:")) return null;
214
+ try {
215
+ return base == null ? (0, _fedify_vocab_runtime.parseIri)(id) : (0, _fedify_vocab_runtime.parseIri)(id, base);
216
+ } catch {
217
+ return null;
218
+ }
219
+ }
220
+ /** Tells whether an expanded node has the given type. */
221
+ function hasType(node, typeIri) {
222
+ const types = node["@type"];
223
+ return Array.isArray(types) ? types.includes(typeIri) : types === typeIri;
224
+ }
225
+ //#endregion
226
+ //#region src/collection.ts
227
+ const ATTRIBUTED_TO = "https://www.w3.org/ns/activitystreams#attributedTo";
228
+ /**
229
+ * Fetches an interacting object, such as a featured collection, from its ID
230
+ * and determines its owner from that document only.
231
+ *
232
+ * What the request embeds is never trusted. An ordinary web document must
233
+ * be served from the origin of its ID, and an [FEP-ef61] portable object
234
+ * must satisfy the portable object policy, as `lookupObject()` checks. The
235
+ * owner is the only `attributedTo` value of the document's expanded form, so
236
+ * that values typed parsing drops, such as anonymous attributions, are
237
+ * counted, and it has to agree with the parsed object. WebFinger is not
238
+ * used.
239
+ *
240
+ * [FEP-ef61]: https://w3id.org/fep/ef61
241
+ * @param id The ID of the object.
242
+ * @param options Options.
243
+ * @returns The object and its owner, or why they could not be resolved.
244
+ */
245
+ async function resolveAuthoritativeObject(id, options) {
246
+ if (hasCredentials(id)) return {
247
+ ok: false,
248
+ failure: {
249
+ category: "invalid",
250
+ type: "credentialsInUrl",
251
+ url: id
252
+ }
253
+ };
254
+ const tracker = trackLoaders(options.documentLoader, options.contextLoader);
255
+ const failures = [];
256
+ const rejections = [];
257
+ let accepted;
258
+ const reject = (reason) => {
259
+ rejections.push(reason);
260
+ return false;
261
+ };
262
+ try {
263
+ if (await (0, _fedify_vocab.lookupObject)(id, {
264
+ documentLoader: tracker.documentLoader,
265
+ contextLoader: tracker.contextLoader,
266
+ verifyPortableObject: options.verifyPortableObject,
267
+ crossOrigin: "ignore",
268
+ [_fedify_vocab.lookupObserver]: {
269
+ webFinger: false,
270
+ failed: (failure) => failures.push(failure),
271
+ accept: async (candidate) => {
272
+ let documentUrl;
273
+ try {
274
+ documentUrl = (0, _fedify_vocab_runtime.parseIri)(candidate.documentUrl);
275
+ } catch {
276
+ return reject("id");
277
+ }
278
+ if (hasCredentials(documentUrl)) return reject("credentials");
279
+ const root = getRootNode(await _fedify_vocab_runtime_jsonld.default.expand(candidate.document, { documentLoader: candidate.contextLoader }));
280
+ if (root == null) return reject("id");
281
+ const rootId = resolveNodeId(root, candidate.baseUrl);
282
+ if (rootId == null || !sameId(rootId, id) || !sameId(candidate.object.id, id)) return reject("id");
283
+ if (!hasType(root, options.typeId.href) || !options.isObject(candidate.object)) return reject("type");
284
+ const owners = getPropertyValues(root, ATTRIBUTED_TO);
285
+ if (owners.length !== 1) return reject("attribution");
286
+ const owner = resolveNodeId(owners[0], candidate.baseUrl);
287
+ if (owner == null || !sameId(owner, candidate.object.attributionId)) return reject("attribution");
288
+ accepted = {
289
+ object: candidate.object,
290
+ owner
291
+ };
292
+ return true;
293
+ }
294
+ }
295
+ }) != null && accepted != null) return {
296
+ ok: true,
297
+ ...accepted
298
+ };
299
+ return {
300
+ ok: false,
301
+ failure: classifyNull(id, failures, rejections)
302
+ };
303
+ } catch (error) {
304
+ if (failures.find((failure) => failure.operation === "parse" && failure.error === error) != null && !collectCauses(error).some((cause) => tracker.records.has(cause))) return {
305
+ ok: false,
306
+ failure: {
307
+ category: "invalid",
308
+ type: "invalidInteractingObject",
309
+ reason: "decoding",
310
+ cause: error
311
+ }
312
+ };
313
+ return {
314
+ ok: false,
315
+ failure: classifyFailure(error, tracker)
316
+ };
317
+ } finally {
318
+ tracker.release();
319
+ }
320
+ function classifyNull(id, failures, rejections) {
321
+ const loaderErrors = [...failures.flatMap((failure) => failure.error === void 0 ? [] : collectCauses(failure.error).filter((cause) => failure.operation === "fetch" || tracker.records.has(cause))), ...tracker.records.keys()];
322
+ const transient = loaderErrors.find(_fedify_vocab_runtime.isTransientFetchError);
323
+ if (transient !== void 0) return {
324
+ category: "unverifiable",
325
+ type: "notDereferenceable",
326
+ url: id,
327
+ cause: transient,
328
+ transient: true
329
+ };
330
+ const decoding = failures.find((failure) => failure.operation === "parse" && !collectCauses(failure.error).some((cause) => tracker.records.has(cause)));
331
+ if (decoding != null) return {
332
+ category: "invalid",
333
+ type: "invalidInteractingObject",
334
+ reason: "decoding",
335
+ cause: decoding.error
336
+ };
337
+ if (rejections.length > 0) return {
338
+ category: "invalid",
339
+ type: "invalidInteractingObject",
340
+ reason: rejections[rejections.length - 1]
341
+ };
342
+ return {
343
+ category: "unverifiable",
344
+ type: "notDereferenceable",
345
+ url: id,
346
+ ...loaderErrors.length > 0 ? { cause: loaderErrors[0] } : {},
347
+ transient: false
348
+ };
349
+ }
350
+ }
351
+ //#endregion
5
352
  //#region src/control.ts
6
353
  function createInteractionControl(config) {
7
354
  return {
@@ -119,6 +466,41 @@ async function verifyRequest(context, options, config) {
119
466
  actual: request.id
120
467
  }
121
468
  };
469
+ const authentication = options.authentication == null || !options.authentication.verified && options.authentication.expandedActivity.length < 1 ? void 0 : options.authentication;
470
+ if (config.authoritativeInteractingObject) {
471
+ const evidence = authentication != null ? {
472
+ expanded: authentication.expandedActivity,
473
+ baseUrl: authentication.baseUrl
474
+ } : requestResult.evidence;
475
+ if (authentication == null && expectedRequestId != null) {
476
+ if (evidence == null) return {
477
+ verified: false,
478
+ request,
479
+ requestId: request.id,
480
+ failure: {
481
+ category: "unauthorized",
482
+ type: "unauthenticatedRequester"
483
+ }
484
+ };
485
+ }
486
+ const bindingFailure = bindRequest(request, evidence, config.requestClass.typeId);
487
+ if (bindingFailure != null) return {
488
+ verified: false,
489
+ request,
490
+ requestId: request.id,
491
+ failure: bindingFailure
492
+ };
493
+ if (authentication != null && request.actorId != null && !(0, _fedify_fedify.isActorAuthenticated)(authentication, request.actorId)) return {
494
+ verified: false,
495
+ request,
496
+ requestId: request.id,
497
+ failure: {
498
+ category: "unauthorized",
499
+ type: "unauthenticatedRequester",
500
+ requester: request.actorId
501
+ }
502
+ };
503
+ }
122
504
  const targetResult = options.resolvedInteractionTarget != null && request.objectId != null ? {
123
505
  ok: true,
124
506
  value: options.resolvedInteractionTarget
@@ -150,7 +532,25 @@ async function verifyRequest(context, options, config) {
150
532
  actualTypes: [getTypeId(interactionTarget)]
151
533
  }
152
534
  };
153
- const interactingResult = options.resolvedInteractingObject != null && request.instrumentId != null ? {
535
+ let owner;
536
+ const interactingResult = config.authoritativeInteractingObject && request.instrumentId != null ? await (async () => {
537
+ const resolution = await resolveAuthoritativeObject(request.instrumentId, {
538
+ documentLoader,
539
+ contextLoader,
540
+ verifyPortableObject: context.verifyPortableObject,
541
+ typeId: config.interactingObjectTypes[0],
542
+ isObject: (object) => config.isInteractingObject?.(object) ?? true
543
+ });
544
+ if (!resolution.ok) return {
545
+ ok: false,
546
+ failure: resolution.failure
547
+ };
548
+ owner = resolution.owner;
549
+ return {
550
+ ok: true,
551
+ value: resolution.object
552
+ };
553
+ })() : options.resolvedInteractingObject != null && request.instrumentId != null ? {
154
554
  ok: true,
155
555
  value: options.resolvedInteractingObject
156
556
  } : await dereferenceField(request, request.instrumentId, config.getInteractingObject, documentLoader, contextLoader);
@@ -181,7 +581,20 @@ async function verifyRequest(context, options, config) {
181
581
  actualTypes: [getTypeId(interactingObject)]
182
582
  }
183
583
  };
184
- const requester = request.actorId ?? config.getRequester?.(request, interactingObject, interactionTarget);
584
+ if (config.authoritativeInteractingObject && request.actorId == null && (owner == null || authentication == null || !(0, _fedify_fedify.isActorAuthenticated)(authentication, owner))) return {
585
+ verified: false,
586
+ request,
587
+ requestId: request.id,
588
+ failure: authentication == null ? {
589
+ category: "invalid",
590
+ type: "missingActor"
591
+ } : {
592
+ category: "unauthorized",
593
+ type: "unauthenticatedRequester",
594
+ ...owner == null ? {} : { requester: owner }
595
+ }
596
+ };
597
+ const requester = request.actorId ?? (config.authoritativeInteractingObject ? owner : config.getRequester?.(request, interactingObject, interactionTarget));
185
598
  if (requester == null) return {
186
599
  verified: false,
187
600
  request,
@@ -456,107 +869,6 @@ async function verifyAuthorization(context, options, config) {
456
869
  };
457
870
  }
458
871
  /**
459
- * Wraps the loaders for a single dereferencing operation so that errors they
460
- * throw can be told apart from other errors afterwards. Parsed objects keep
461
- * the loaders they were given, so the wrappers stop recording and pass calls
462
- * through once the operation is released.
463
- */
464
- function trackLoaders(documentLoader, contextLoader) {
465
- const records = /* @__PURE__ */ new Map();
466
- let released = false;
467
- const wrap = (loader) => {
468
- if (loader == null) return void 0;
469
- return async (url, options) => {
470
- if (released) return await loader(url, options);
471
- let remoteDocument;
472
- try {
473
- remoteDocument = await loader(url, options);
474
- } catch (error) {
475
- if (!released) records.set(error, url);
476
- throw error;
477
- }
478
- if (remoteDocument == null) {
479
- const message = "The document loader returned no document.";
480
- let error;
481
- try {
482
- error = new _fedify_vocab_runtime.FetchError((0, _fedify_vocab_runtime.parseIri)(url), message);
483
- } catch {
484
- error = /* @__PURE__ */ new Error(`${url}: ${message}`);
485
- }
486
- if (!released) records.set(error, url);
487
- throw error;
488
- }
489
- return remoteDocument;
490
- };
491
- };
492
- const wrappedDocumentLoader = wrap(documentLoader);
493
- return {
494
- documentLoader: wrappedDocumentLoader,
495
- contextLoader: contextLoader === documentLoader ? wrappedDocumentLoader : wrap(contextLoader),
496
- records,
497
- release: () => {
498
- released = true;
499
- records.clear();
500
- }
501
- };
502
- }
503
- const MAX_CAUSE_DEPTH = 8;
504
- function collectCauses(error, causes = [], depth = 0) {
505
- if (depth > MAX_CAUSE_DEPTH || causes.includes(error)) return causes;
506
- causes.push(error);
507
- if (typeof error !== "object" || error == null) return causes;
508
- if (error instanceof AggregateError) for (const e of error.errors) collectCauses(e, causes, depth + 1);
509
- if ("cause" in error) collectCauses(error.cause, causes, depth + 1);
510
- const details = error.details;
511
- if (typeof details === "object" && details != null && "cause" in details) collectCauses(details.cause, causes, depth + 1);
512
- return causes;
513
- }
514
- /**
515
- * Classifies the error thrown by a dereferencing operation. It is a fetch
516
- * failure only if a loader error is among its causes; errors that loaders
517
- * threw during attempts the operation recovered from are ignored.
518
- */
519
- function classifyFailure(error, tracker) {
520
- const loaderErrors = collectCauses(error).filter((e) => tracker.records.has(e));
521
- const transientIndex = loaderErrors.findIndex(_fedify_vocab_runtime.isTransientFetchError);
522
- const selected = loaderErrors[transientIndex < 0 ? 0 : transientIndex];
523
- const url = loaderErrors.length < 1 ? null : getFailedUrl(selected, tracker.records.get(selected));
524
- if (url == null) return {
525
- category: "unverifiable",
526
- type: "invalidJsonLd",
527
- cause: error,
528
- transient: false
529
- };
530
- return {
531
- category: "unverifiable",
532
- type: "notDereferenceable",
533
- url,
534
- cause: loaderErrors.length === 1 ? selected : error,
535
- transient: transientIndex >= 0
536
- };
537
- }
538
- function getFailedUrl(error, requestedUrl) {
539
- const url = error?.url;
540
- if (url instanceof URL) return url;
541
- for (const candidate of [url, requestedUrl]) {
542
- if (typeof candidate !== "string") continue;
543
- try {
544
- return (0, _fedify_vocab_runtime.parseIri)(candidate);
545
- } catch {
546
- continue;
547
- }
548
- }
549
- return null;
550
- }
551
- function notDereferenceable(url) {
552
- return {
553
- category: "unverifiable",
554
- type: "notDereferenceable",
555
- url,
556
- transient: false
557
- };
558
- }
559
- /**
560
872
  * Dereferences a field of the request, such as its `object` or `instrument`.
561
873
  * Returns `null` if the request has no such field at all.
562
874
  */
@@ -588,7 +900,8 @@ async function dereferenceField(request, reference, dereference, documentLoader,
588
900
  async function materialize(value, constructor, documentLoader, contextLoader) {
589
901
  if (!(value instanceof URL)) return {
590
902
  ok: true,
591
- object: value
903
+ object: value,
904
+ evidence: null
592
905
  };
593
906
  if (documentLoader == null) return {
594
907
  ok: false,
@@ -597,13 +910,33 @@ async function materialize(value, constructor, documentLoader, contextLoader) {
597
910
  const tracker = trackLoaders(documentLoader, contextLoader);
598
911
  try {
599
912
  const remoteDocument = await tracker.documentLoader(value.href);
913
+ const memoizedContextLoader = memoizeLoader(tracker.contextLoader ?? (0, _fedify_vocab_runtime.getDocumentLoader)());
914
+ const object = await constructor.fromJsonLd(remoteDocument.document, {
915
+ documentLoader: tracker.documentLoader,
916
+ contextLoader: memoizedContextLoader,
917
+ baseUrl: value
918
+ });
919
+ let evidence = null;
920
+ let documentUrl;
921
+ try {
922
+ documentUrl = (0, _fedify_vocab_runtime.parseIri)(remoteDocument.documentUrl);
923
+ } catch {
924
+ documentUrl = null;
925
+ }
926
+ if (documentUrl != null && !isPortableClaim(value) && !isPortableClaim(documentUrl) && !hasCredentials(documentUrl) && documentUrl.origin === value.origin) {
927
+ const expanded = await _fedify_vocab_runtime_jsonld.default.expand(remoteDocument.document, { documentLoader: memoizedContextLoader });
928
+ const root = getRootNode(expanded);
929
+ const rootId = root == null ? null : resolveNodeId(root, value);
930
+ const actors = root == null ? [] : getPropertyValues(root, AS_ACTOR).map((actor) => resolveNodeId(actor, value));
931
+ if (rootId != null && !isPortableClaim(rootId) && rootId.href === value.href && actors.every((actor) => actor != null && !isPortableClaim(actor) && actor.origin === value.origin)) evidence = {
932
+ expanded,
933
+ baseUrl: value
934
+ };
935
+ }
600
936
  return {
601
937
  ok: true,
602
- object: await constructor.fromJsonLd(remoteDocument.document, {
603
- documentLoader: tracker.documentLoader,
604
- contextLoader: tracker.contextLoader,
605
- baseUrl: value
606
- })
938
+ object,
939
+ evidence
607
940
  };
608
941
  } catch (error) {
609
942
  return {
@@ -614,6 +947,92 @@ async function materialize(value, constructor, documentLoader, contextLoader) {
614
947
  tracker.release();
615
948
  }
616
949
  }
950
+ function memoizeLoader(loader) {
951
+ const cache = /* @__PURE__ */ new Map();
952
+ return async (url, options) => {
953
+ let promise = cache.get(url);
954
+ if (promise == null) {
955
+ promise = loader(url, options);
956
+ cache.set(url, promise);
957
+ }
958
+ return structuredClone(await promise);
959
+ };
960
+ }
961
+ const AS_ACTOR = "https://www.w3.org/ns/activitystreams#actor";
962
+ const AS_OBJECT = "https://www.w3.org/ns/activitystreams#object";
963
+ const AS_INSTRUMENT = "https://www.w3.org/ns/activitystreams#instrument";
964
+ /**
965
+ * Checks that a request has exactly the type, ID, actors, `object`, and
966
+ * `instrument` that its authenticated interpretation has, counting every
967
+ * value, including those typed parsing drops.
968
+ * @returns The failure, or `null` if the request is bound to the evidence.
969
+ */
970
+ function bindRequest(request, evidence, typeId) {
971
+ if (evidence == null) {
972
+ if (request.actorIds.length < 1) return {
973
+ category: "invalid",
974
+ type: "missingActor"
975
+ };
976
+ if (request.objectIds.length !== 1 || !sameId(request.objectIds[0], request.objectId)) return {
977
+ category: "invalid",
978
+ type: "ambiguousRequest",
979
+ property: "object"
980
+ };
981
+ if (request.instrumentIds.length !== 1 || !sameId(request.instrumentIds[0], request.instrumentId)) return {
982
+ category: "invalid",
983
+ type: "ambiguousRequest",
984
+ property: "instrument"
985
+ };
986
+ return null;
987
+ }
988
+ const root = getRootNode(evidence.expanded);
989
+ if (root == null) return {
990
+ category: "invalid",
991
+ type: "ambiguousRequest",
992
+ property: "id"
993
+ };
994
+ if (!hasType(root, typeId.href)) return {
995
+ category: "invalid",
996
+ type: "ambiguousRequest",
997
+ property: "type"
998
+ };
999
+ const rootId = resolveNodeId(root, evidence.baseUrl);
1000
+ if (rootId == null || !sameId(rootId, request.id)) return {
1001
+ category: "invalid",
1002
+ type: "ambiguousRequest",
1003
+ property: "id"
1004
+ };
1005
+ const base = evidence.baseUrl ?? rootId;
1006
+ const actors = getPropertyValues(root, AS_ACTOR);
1007
+ if (actors.length !== request.actorIds.length || !actors.every((actor, i) => sameId(resolveNodeId(actor, base), request.actorIds[i]))) return {
1008
+ category: "invalid",
1009
+ type: "ambiguousRequest",
1010
+ property: "actor"
1011
+ };
1012
+ for (const [iri, property, missing, id] of [[
1013
+ AS_OBJECT,
1014
+ "object",
1015
+ "missingObject",
1016
+ request.objectId
1017
+ ], [
1018
+ AS_INSTRUMENT,
1019
+ "instrument",
1020
+ "missingInstrument",
1021
+ request.instrumentId
1022
+ ]]) {
1023
+ const values = getPropertyValues(root, iri);
1024
+ if (values.length < 1) return {
1025
+ category: "invalid",
1026
+ type: missing
1027
+ };
1028
+ if (values.length !== 1 || !sameId(resolveNodeId(values[0], base), id)) return {
1029
+ category: "invalid",
1030
+ type: "ambiguousRequest",
1031
+ property
1032
+ };
1033
+ }
1034
+ return null;
1035
+ }
617
1036
  async function evaluatePolicy(context, options, config) {
618
1037
  if (idsEqual(config.getSelfActor(options.subject), options.requester)) return {
619
1038
  result: "automatic",
@@ -901,9 +1320,9 @@ const featureInteraction = createInteractionControl({
901
1320
  _fedify_vocab.Person.typeId,
902
1321
  _fedify_vocab.Service.typeId
903
1322
  ],
904
- getRequester: (_request, collection) => collection.attributionId,
1323
+ authoritativeInteractingObject: true,
905
1324
  validateRequest: (_request, collection, _target, requester) => {
906
- if (!idsEqual(collection.attributionId, requester)) return {
1325
+ if (!sameId(collection.attributionId, requester)) return {
907
1326
  type: "requesterMismatch",
908
1327
  expected: requester,
909
1328
  actual: collection.attributionId ?? void 0