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