@fedify/fedify 2.4.0-dev.2054 → 2.4.0-dev.2063
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/{builder-C86Z3BKs.mjs → builder-BzS4brdy.mjs} +8 -2
- package/dist/compat/mod.d.cts +1 -1
- package/dist/compat/mod.d.ts +1 -1
- package/dist/compat/transformers.test.mjs +1 -1
- package/dist/{compound-proof-BcZqTCwy.mjs → compound-proof-BrSXP5so.mjs} +1 -1
- package/dist/{context-BJPHhBmD.d.ts → context-D_gg4M0n.d.ts} +102 -1
- package/dist/{context-Bh_mJfbD.d.cts → context-b36TpPoG.d.cts} +102 -1
- package/dist/{deno-Bz6KTcon.mjs → deno-CVZgG4sc.mjs} +1 -1
- package/dist/{docloader-DS54zLD1.mjs → docloader-DBb8z7_l.mjs} +2 -2
- package/dist/federation/builder.test.mjs +1 -1
- package/dist/federation/compound-fanout.test.mjs +3 -3
- package/dist/federation/handler-compound-proof.test.mjs +3 -3
- package/dist/federation/handler.test.mjs +5 -5
- package/dist/federation/hashlink-media.test.d.mts +2 -0
- package/dist/federation/hashlink-media.test.mjs +422 -0
- package/dist/federation/idempotency.test.mjs +2 -2
- package/dist/federation/metrics.test.mjs +1 -1
- package/dist/federation/middleware.test.mjs +7 -7
- package/dist/federation/mod.cjs +1 -1
- package/dist/federation/mod.d.cts +3 -3
- package/dist/federation/mod.d.ts +3 -3
- package/dist/federation/mod.js +1 -1
- package/dist/federation/outgoing-proof.test.mjs +5 -5
- package/dist/federation/portable.test.mjs +3 -3
- package/dist/federation/router.test.mjs +2 -152
- package/dist/federation/send.test.mjs +3 -3
- package/dist/federation/tasks/enqueue.test.mjs +1 -1
- package/dist/federation/tasks/tasks.test.mjs +2 -2
- package/dist/federation/temporal.test.mjs +1 -1
- package/dist/federation/webfinger.test.mjs +1 -1
- package/dist/{federation-su1OEWsM.cjs → federation-BrsIzNK5.cjs} +139 -21
- package/dist/{http-C3WGK1dL.js → http-99KVDtMr.js} +1 -1
- package/dist/{http-LmUsMjK_.cjs → http-CVjwUCCu.cjs} +1 -1
- package/dist/{http-Ty805y3A.mjs → http-znjq5j6a.mjs} +3 -3
- package/dist/{key-BMc-2eW0.mjs → key-BnMA6vpl.mjs} +2 -2
- package/dist/{kv-cache-BhPPtHg7.mjs → kv-cache--REpvHgG.mjs} +1 -1
- package/dist/{kv-cache-CuaZnk6r.cjs → kv-cache-D2O0JspA.cjs} +1 -1
- package/dist/{kv-cache-BcHCmrC8.js → kv-cache-aijDx_jN.js} +1 -1
- package/dist/{ld-OpXWt-80.mjs → ld-BBVP8ock.mjs} +3 -3
- package/dist/{metrics-CTAe8lYz.mjs → metrics-B5spFq82.mjs} +1 -1
- package/dist/{middleware-C-JLcqy7.mjs → middleware-ClrGS1xy.mjs} +1 -1
- package/dist/{middleware-BtwW3G41.mjs → middleware-Cw8EWwAT.mjs} +145 -33
- package/dist/{middleware-_CMav1o7.js → middleware-CzYM4cJI.js} +140 -22
- package/dist/{mod-DU8aVzQJ.d.cts → mod-Bfenkg_c.d.cts} +1 -1
- package/dist/{mod-BofpDIcU.d.ts → mod-BhMvsgdF.d.ts} +1 -1
- package/dist/{mod-CNRAxXEb.d.ts → mod-Dhm7SqyE.d.ts} +150 -2
- package/dist/{mod-CVQFWefn.d.cts → mod-pLbeNwcC.d.cts} +150 -2
- package/dist/mod.cjs +6 -5
- package/dist/mod.d.cts +5 -5
- package/dist/mod.d.ts +5 -5
- package/dist/mod.js +6 -6
- package/dist/nodeinfo/handler.test.mjs +1 -1
- package/dist/{owner-Bcm4pLXw.cjs → owner-CHBQ0sSj.cjs} +34 -2
- package/dist/{owner-B-TLr-1I.js → owner-Cu_KMvQN.js} +29 -3
- package/dist/{owner-RxB_eQpU.mjs → owner-Hc0hvbbX.mjs} +2 -2
- package/dist/{proof-CVf2XbEa.mjs → proof-B69bJaSn.mjs} +32 -6
- package/dist/router-u5O5KCQr.mjs +156 -0
- package/dist/{send-DF9KzhJH.mjs → send-BB5rBhvN.mjs} +3 -3
- package/dist/sig/compound-proof-discovery.test.mjs +1 -1
- package/dist/sig/compound-proof-producer.test.mjs +1 -1
- package/dist/sig/compound-proof-verification.test.mjs +2 -2
- package/dist/sig/compound-proof.test.mjs +2 -2
- package/dist/sig/http.test.mjs +2 -2
- package/dist/sig/key.test.mjs +1 -1
- package/dist/sig/ld.test.mjs +2 -2
- package/dist/sig/mod.cjs +4 -2
- package/dist/sig/mod.d.cts +2 -2
- package/dist/sig/mod.d.ts +2 -2
- package/dist/sig/mod.js +4 -3
- package/dist/sig/owner.test.mjs +1 -1
- package/dist/sig/portable-collection.test.d.mts +2 -0
- package/dist/sig/portable-collection.test.mjs +971 -0
- package/dist/sig/proof.test.mjs +3 -3
- package/dist/sig-B8WDBV-h.js +401 -0
- package/dist/sig-dut4BzEW.cjs +405 -0
- package/dist/{temporal-B6U3fEUW.mjs → temporal-CcRkOx3C.mjs} +1 -1
- package/dist/testing/kv.test.mjs +1 -1
- package/dist/testing/mod.d.mts +101 -0
- package/dist/utils/docloader.test.mjs +2 -2
- package/dist/utils/kv-cache.test.mjs +1 -1
- package/dist/utils/mod.cjs +1 -1
- package/dist/utils/mod.js +1 -1
- package/package.json +7 -7
|
@@ -0,0 +1,971 @@
|
|
|
1
|
+
import "temporal-polyfill";
|
|
2
|
+
import "urlpattern-polyfill";
|
|
3
|
+
globalThis.addEventListener = () => {};
|
|
4
|
+
import { n as version, t as name } from "../deno-CVZgG4sc.mjs";
|
|
5
|
+
import { n as ed25519PrivateKey, r as ed25519PublicKey } from "../keys-DIJNHW-Y.mjs";
|
|
6
|
+
import { c as verifyPortableObjectProofWithRoot, o as verifyPortableObjectProof } from "../proof-B69bJaSn.mjs";
|
|
7
|
+
import { getLogger } from "@logtape/logtape";
|
|
8
|
+
import { Collection, CollectionPage, Note, Object as Object$1, OrderedCollection, Person, isActor, traverseCollection } from "@fedify/vocab";
|
|
9
|
+
import { trace } from "@opentelemetry/api";
|
|
10
|
+
import { test } from "@fedify/fixture";
|
|
11
|
+
import { FetchError, canonicalizePortableUri, encodeMultibase, exportDidKey, formatIri, fromCompatibleEf61Id, getDocumentLoader, getFe34Origin, parseIri, preloadedContexts } from "@fedify/vocab-runtime";
|
|
12
|
+
import { deepStrictEqual, ok, strictEqual } from "node:assert/strict";
|
|
13
|
+
import serialize from "json-canon";
|
|
14
|
+
import { dereferencePortableIri, getPortableGatewayCandidates } from "@fedify/vocab-runtime/internal/portable-dereference";
|
|
15
|
+
//#region src/sig/portable-collection.ts
|
|
16
|
+
const logger = getLogger([
|
|
17
|
+
"fedify",
|
|
18
|
+
"sig",
|
|
19
|
+
"proof"
|
|
20
|
+
]);
|
|
21
|
+
const AS_NAMESPACE = "https://www.w3.org/ns/activitystreams#";
|
|
22
|
+
const AS_ATTRIBUTED_TO = `${AS_NAMESPACE}attributedTo`;
|
|
23
|
+
const AS_PART_OF = `${AS_NAMESPACE}partOf`;
|
|
24
|
+
/**
|
|
25
|
+
* The properties through which a collection refers to its pages, and a page
|
|
26
|
+
* to other pages of the same collection.
|
|
27
|
+
*/
|
|
28
|
+
const PAGING_PROPERTIES = new Set([
|
|
29
|
+
"first",
|
|
30
|
+
"last",
|
|
31
|
+
"current",
|
|
32
|
+
"next",
|
|
33
|
+
"prev"
|
|
34
|
+
].map((p) => AS_NAMESPACE + p));
|
|
35
|
+
/**
|
|
36
|
+
* The properties through which an actor refers to its own collections.
|
|
37
|
+
*/
|
|
38
|
+
const ACTOR_COLLECTION_PROPERTIES = /* @__PURE__ */ new Set([
|
|
39
|
+
"http://www.w3.org/ns/ldp#inbox",
|
|
40
|
+
`${AS_NAMESPACE}outbox`,
|
|
41
|
+
`${AS_NAMESPACE}followers`,
|
|
42
|
+
`${AS_NAMESPACE}following`,
|
|
43
|
+
`${AS_NAMESPACE}liked`
|
|
44
|
+
]);
|
|
45
|
+
const contextStates = /* @__PURE__ */ new WeakMap();
|
|
46
|
+
function createContext(state) {
|
|
47
|
+
const token = Object.freeze(Object.create(null));
|
|
48
|
+
contextStates.set(token, state);
|
|
49
|
+
return token;
|
|
50
|
+
}
|
|
51
|
+
function getContextState(context) {
|
|
52
|
+
if (context == null || typeof context !== "object") return void 0;
|
|
53
|
+
return contextStates.get(context);
|
|
54
|
+
}
|
|
55
|
+
function canonicalize(id) {
|
|
56
|
+
try {
|
|
57
|
+
return canonicalizePortableUri(typeof id === "string" ? id : formatIri(id));
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (error instanceof TypeError) return null;
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function toUrl(canonicalId) {
|
|
64
|
+
return parseIri(canonicalId);
|
|
65
|
+
}
|
|
66
|
+
function canonicalizeAll(ids) {
|
|
67
|
+
const result = [];
|
|
68
|
+
for (const id of ids) {
|
|
69
|
+
const canonical = canonicalize(id);
|
|
70
|
+
if (canonical == null) return null;
|
|
71
|
+
result.push(canonical);
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
function getExpandedIds(node, property) {
|
|
76
|
+
const values = node[property];
|
|
77
|
+
if (values == null) return [];
|
|
78
|
+
if (!Array.isArray(values)) return null;
|
|
79
|
+
const ids = [];
|
|
80
|
+
for (const value of values) {
|
|
81
|
+
if (value == null || typeof value !== "object" || Array.isArray(value) || typeof value["@id"] !== "string") return null;
|
|
82
|
+
ids.push(value["@id"]);
|
|
83
|
+
}
|
|
84
|
+
return ids;
|
|
85
|
+
}
|
|
86
|
+
function parseAll(ids) {
|
|
87
|
+
const result = [];
|
|
88
|
+
for (const id of ids) try {
|
|
89
|
+
result.push(parseIri(id));
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof TypeError) return null;
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
function describeRoot(root) {
|
|
97
|
+
const rawId = root["@id"];
|
|
98
|
+
if (typeof rawId !== "string") return null;
|
|
99
|
+
const id = canonicalize(rawId);
|
|
100
|
+
if (id == null) return null;
|
|
101
|
+
const partOf = getExpandedIds(root, AS_PART_OF);
|
|
102
|
+
const attributedTo = getExpandedIds(root, AS_ATTRIBUTED_TO);
|
|
103
|
+
return {
|
|
104
|
+
id,
|
|
105
|
+
url: toUrl(id),
|
|
106
|
+
origin: getFe34Origin(id),
|
|
107
|
+
partOf: partOf == null ? null : canonicalizeAll(partOf),
|
|
108
|
+
attributedTo: attributedTo == null ? null : canonicalizeAll(attributedTo),
|
|
109
|
+
attributedToUrls: attributedTo == null ? null : parseAll(attributedTo)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function describeObject(object) {
|
|
113
|
+
if (!(object instanceof Object$1) || object.id == null) return null;
|
|
114
|
+
const id = canonicalize(object.id);
|
|
115
|
+
if (id == null) return null;
|
|
116
|
+
const partOfId = object instanceof CollectionPage ? object.partOfId : null;
|
|
117
|
+
return {
|
|
118
|
+
id,
|
|
119
|
+
url: toUrl(id),
|
|
120
|
+
origin: getFe34Origin(id),
|
|
121
|
+
partOf: canonicalizeAll(partOfId == null ? [] : [partOfId]),
|
|
122
|
+
attributedTo: canonicalizeAll(object.attributionIds),
|
|
123
|
+
attributedToUrls: object.attributionIds
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function fail(reason) {
|
|
127
|
+
return {
|
|
128
|
+
verified: false,
|
|
129
|
+
reason
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Walks the referrer chain of a collection or page to find the root
|
|
134
|
+
* collection and its owner candidate, checking every page on the way.
|
|
135
|
+
*/
|
|
136
|
+
function resolveChain(node, referrer) {
|
|
137
|
+
const pages = [];
|
|
138
|
+
let top = node;
|
|
139
|
+
let link = referrer;
|
|
140
|
+
let state;
|
|
141
|
+
while (link != null && PAGING_PROPERTIES.has(link.property)) {
|
|
142
|
+
pages.push(top);
|
|
143
|
+
state = getContextState(link.collectionContext);
|
|
144
|
+
if (state != null) break;
|
|
145
|
+
const parent = describeObject(link.object);
|
|
146
|
+
if (parent == null || link.truncated) return fail({ type: "unknownCollectionOwner" });
|
|
147
|
+
top = parent;
|
|
148
|
+
link = link.referrer;
|
|
149
|
+
}
|
|
150
|
+
let rootId;
|
|
151
|
+
let rootOrigin;
|
|
152
|
+
let candidate;
|
|
153
|
+
if (state != null) {
|
|
154
|
+
rootId = state.rootId;
|
|
155
|
+
rootOrigin = state.rootOrigin;
|
|
156
|
+
const ownerCandidate = state.owner == null ? state.ownerCandidate : { id: canonicalize(state.owner.id) };
|
|
157
|
+
if (ownerCandidate == null) return fail({ type: "unknownCollectionOwner" });
|
|
158
|
+
candidate = ownerCandidate;
|
|
159
|
+
} else {
|
|
160
|
+
rootId = top.id;
|
|
161
|
+
rootOrigin = top.origin;
|
|
162
|
+
const attributedTo = top.attributedTo;
|
|
163
|
+
if (attributedTo == null) return fail({ type: "unknownCollectionOwner" });
|
|
164
|
+
if (link != null && ACTOR_COLLECTION_PROPERTIES.has(link.property)) {
|
|
165
|
+
const id = link.id == null ? null : canonicalize(link.id);
|
|
166
|
+
if (id == null) return fail({ type: "unknownCollectionOwner" });
|
|
167
|
+
candidate = {
|
|
168
|
+
id,
|
|
169
|
+
object: link.object,
|
|
170
|
+
acceptance: link.acceptance
|
|
171
|
+
};
|
|
172
|
+
} else if (attributedTo.length === 1) candidate = {
|
|
173
|
+
id: attributedTo[0],
|
|
174
|
+
url: top.attributedToUrls?.[0]
|
|
175
|
+
};
|
|
176
|
+
else if (attributedTo.length > 1) return fail({
|
|
177
|
+
type: "conflictingCollectionOwners",
|
|
178
|
+
candidates: attributedTo.map(toUrl)
|
|
179
|
+
});
|
|
180
|
+
else return fail({ type: "unknownCollectionOwner" });
|
|
181
|
+
const conflict = attributedTo.find((id) => id !== candidate.id);
|
|
182
|
+
if (conflict != null) return fail({
|
|
183
|
+
type: "conflictingCollectionOwners",
|
|
184
|
+
candidates: [toUrl(candidate.id), toUrl(conflict)]
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
if (getFe34Origin(candidate.id) !== rootOrigin) return fail({ type: "unknownCollectionOwner" });
|
|
188
|
+
for (const page of pages) {
|
|
189
|
+
if (page.origin !== rootOrigin) return fail({
|
|
190
|
+
type: "collectionPageMismatch",
|
|
191
|
+
pageId: page.url,
|
|
192
|
+
collectionId: toUrl(rootId),
|
|
193
|
+
partOf: null
|
|
194
|
+
});
|
|
195
|
+
if (page.partOf == null) return fail({ type: "unknownCollectionOwner" });
|
|
196
|
+
const partOf = page.partOf.find((id) => id !== rootId);
|
|
197
|
+
if (partOf != null) return fail({
|
|
198
|
+
type: "collectionPageMismatch",
|
|
199
|
+
pageId: page.url,
|
|
200
|
+
collectionId: toUrl(rootId),
|
|
201
|
+
partOf: toUrl(partOf)
|
|
202
|
+
});
|
|
203
|
+
if (page.attributedTo == null) return fail({ type: "unknownCollectionOwner" });
|
|
204
|
+
const conflict = page.attributedTo.find((id) => id !== candidate.id);
|
|
205
|
+
if (conflict != null) return fail({
|
|
206
|
+
type: "conflictingCollectionOwners",
|
|
207
|
+
candidates: [toUrl(candidate.id), toUrl(conflict)]
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
rootId,
|
|
212
|
+
rootOrigin,
|
|
213
|
+
owner: state?.owner ?? null,
|
|
214
|
+
candidate
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
function getActorCollectionIds(actor) {
|
|
218
|
+
const ids = [];
|
|
219
|
+
for (const id of [
|
|
220
|
+
actor.inboxId,
|
|
221
|
+
actor.outboxId,
|
|
222
|
+
actor.followersId,
|
|
223
|
+
actor.followingId,
|
|
224
|
+
actor.likedId
|
|
225
|
+
]) {
|
|
226
|
+
if (id == null) continue;
|
|
227
|
+
const canonical = canonicalize(id);
|
|
228
|
+
if (canonical != null) ids.push(canonical);
|
|
229
|
+
}
|
|
230
|
+
return ids;
|
|
231
|
+
}
|
|
232
|
+
async function fetchOwner(candidate, options) {
|
|
233
|
+
const documentLoader = options.documentLoader ?? getDocumentLoader();
|
|
234
|
+
const contextLoader = options.contextLoader ?? getDocumentLoader();
|
|
235
|
+
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
|
|
236
|
+
const tracer = tracerProvider.getTracer(name, version);
|
|
237
|
+
const gateways = options.gateways ?? (getPortableGatewayCandidates(candidate).length > 0 ? void 0 : options.gatewayHints);
|
|
238
|
+
const span = tracer.startSpan("activitypub.lookup_object");
|
|
239
|
+
try {
|
|
240
|
+
const object = await dereferencePortableIri(candidate, {
|
|
241
|
+
documentLoader,
|
|
242
|
+
contextLoader,
|
|
243
|
+
tracerProvider,
|
|
244
|
+
gateways,
|
|
245
|
+
verifyPortableObject: (document, verifierOptions) => verifyPortableObjectProof(document, {
|
|
246
|
+
...options,
|
|
247
|
+
...verifierOptions
|
|
248
|
+
}),
|
|
249
|
+
suppressError: true,
|
|
250
|
+
span,
|
|
251
|
+
parse: (document, parseOptions) => Object$1.fromJsonLd(document, {
|
|
252
|
+
documentLoader,
|
|
253
|
+
contextLoader: parseOptions.contextLoader,
|
|
254
|
+
tracerProvider,
|
|
255
|
+
baseUrl: parseOptions.baseUrl
|
|
256
|
+
})
|
|
257
|
+
});
|
|
258
|
+
return isActor(object) ? object : null;
|
|
259
|
+
} catch (error) {
|
|
260
|
+
logger.debug("Failed to fetch the owner {owner} of a portable collection: {error}", {
|
|
261
|
+
owner: formatIri(candidate),
|
|
262
|
+
error
|
|
263
|
+
});
|
|
264
|
+
return null;
|
|
265
|
+
} finally {
|
|
266
|
+
span.end();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
async function resolveOwner(chain, options) {
|
|
270
|
+
if (chain.owner != null) return chain.owner;
|
|
271
|
+
const { candidate } = chain;
|
|
272
|
+
const candidateUrl = toUrl(candidate.id);
|
|
273
|
+
let actor = null;
|
|
274
|
+
if (candidate.acceptance === "verified" && isActor(candidate.object) && candidate.object.id != null && canonicalize(candidate.object.id) === candidate.id) actor = candidate.object;
|
|
275
|
+
else actor = await fetchOwner(candidate.url ?? (candidate.object instanceof Object$1 && candidate.object.id != null ? candidate.object.id : candidateUrl), options);
|
|
276
|
+
if (actor == null || actor.id == null) return fail({
|
|
277
|
+
type: "collectionOwnerUnavailable",
|
|
278
|
+
owner: candidateUrl
|
|
279
|
+
});
|
|
280
|
+
const gateways = actor.gateways;
|
|
281
|
+
if (gateways.length < 1) return fail({
|
|
282
|
+
type: "collectionOwnerUnavailable",
|
|
283
|
+
owner: candidateUrl
|
|
284
|
+
});
|
|
285
|
+
if (!getActorCollectionIds(actor).includes(chain.rootId)) return fail({
|
|
286
|
+
type: "collectionNotListedByOwner",
|
|
287
|
+
owner: candidateUrl,
|
|
288
|
+
collectionId: toUrl(chain.rootId)
|
|
289
|
+
});
|
|
290
|
+
return {
|
|
291
|
+
id: candidateUrl,
|
|
292
|
+
gateways
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
function getSourceGateway(node, documentUrl) {
|
|
296
|
+
if (documentUrl == null || documentUrl.protocol !== "http:" && documentUrl.protocol !== "https:") return null;
|
|
297
|
+
let portableId;
|
|
298
|
+
try {
|
|
299
|
+
portableId = fromCompatibleEf61Id(documentUrl);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (error instanceof TypeError) return null;
|
|
302
|
+
throw error;
|
|
303
|
+
}
|
|
304
|
+
if (portableId == null || canonicalize(portableId) !== node.id) return null;
|
|
305
|
+
return new URL(documentUrl.origin);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Applies the [FEP-ef61] trust policy to a portable object fetched through
|
|
309
|
+
* a gateway.
|
|
310
|
+
*
|
|
311
|
+
* Portable actors, activities, objects, and collections with proofs are
|
|
312
|
+
* verified by {@link verifyPortableObjectProof}. A portable collection or
|
|
313
|
+
* collection page without a proof is accepted only if all of the following
|
|
314
|
+
* hold:
|
|
315
|
+
*
|
|
316
|
+
* - It was retrieved from a gateway: its final URL (`documentUrl`) is
|
|
317
|
+
* a compatible identifier for the same portable collection.
|
|
318
|
+
* - Its owner is determined unambiguously: the actor whose `inbox`,
|
|
319
|
+
* `outbox`, `followers`, `following`, or `liked` property referred to
|
|
320
|
+
* the collection, or the single actor in its `attributedTo`. A page
|
|
321
|
+
* inherits the owner of the collection it was reached from through
|
|
322
|
+
* `first`, `last`, `current`, `next`, or `prev`, and must not name
|
|
323
|
+
* another collection in `partOf`.
|
|
324
|
+
* - The owner's actor document is verified by its proof and lists the
|
|
325
|
+
* collection as its `inbox`, `outbox`, `followers`, `following`, or
|
|
326
|
+
* `liked`.
|
|
327
|
+
* - The gateway that served the collection is in the owner's `gateways`.
|
|
328
|
+
*
|
|
329
|
+
* Where the document came from and which object referred to it are passed
|
|
330
|
+
* by property accessors when this function is used as their
|
|
331
|
+
* `verifyPortableObject` option. Objects embedded in an unsecured
|
|
332
|
+
* collection accepted this way are not trusted: accessors dereference and
|
|
333
|
+
* verify them one by one.
|
|
334
|
+
*
|
|
335
|
+
* Note that acceptance through the gateway policy does not verify anything
|
|
336
|
+
* cryptographically; it only means that the owner trusts the gateway.
|
|
337
|
+
*
|
|
338
|
+
* [FEP-ef61]: https://w3id.org/fep/ef61
|
|
339
|
+
*
|
|
340
|
+
* @param jsonLd The JSON-LD document to verify.
|
|
341
|
+
* @param options Additional options. See also
|
|
342
|
+
* {@link VerifyPortableObjectOptions}.
|
|
343
|
+
* @returns The detailed result.
|
|
344
|
+
* @throws {TypeError} If the input is not a single JSON-LD object or has a
|
|
345
|
+
* malformed portable ID.
|
|
346
|
+
* @since 2.4.0
|
|
347
|
+
*/
|
|
348
|
+
async function verifyPortableObject(jsonLd, options = {}) {
|
|
349
|
+
const { result, root, objectType } = await verifyPortableObjectProofWithRoot(jsonLd, options);
|
|
350
|
+
if (result.verified) {
|
|
351
|
+
const node = objectType === "collection" && root != null ? describeRoot(root) : null;
|
|
352
|
+
const chain = node == null ? null : resolveChain(node, options.referrer);
|
|
353
|
+
if (chain == null || !("rootId" in chain)) return {
|
|
354
|
+
verified: true,
|
|
355
|
+
method: "proof",
|
|
356
|
+
keys: result.keys
|
|
357
|
+
};
|
|
358
|
+
return {
|
|
359
|
+
verified: true,
|
|
360
|
+
method: "proof",
|
|
361
|
+
keys: result.keys,
|
|
362
|
+
collectionContext: createContext({
|
|
363
|
+
rootId: chain.rootId,
|
|
364
|
+
rootOrigin: chain.rootOrigin,
|
|
365
|
+
owner: chain.owner,
|
|
366
|
+
ownerCandidate: chain.candidate
|
|
367
|
+
})
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
if (result.reason.type !== "unsecuredCollection") return {
|
|
371
|
+
verified: false,
|
|
372
|
+
reason: result.reason
|
|
373
|
+
};
|
|
374
|
+
const node = root == null ? null : describeRoot(root);
|
|
375
|
+
if (node == null) return fail({ type: "unknownCollectionOwner" });
|
|
376
|
+
const gateway = getSourceGateway(node, options.documentUrl);
|
|
377
|
+
if (gateway == null) return fail({
|
|
378
|
+
type: "unknownCollectionSource",
|
|
379
|
+
documentUrl: options.documentUrl ?? null
|
|
380
|
+
});
|
|
381
|
+
const chain = resolveChain(node, options.referrer);
|
|
382
|
+
if (!("rootId" in chain)) return chain;
|
|
383
|
+
const owner = await resolveOwner(chain, options);
|
|
384
|
+
if (!("gateways" in owner)) return owner;
|
|
385
|
+
if (!owner.gateways.some((g) => g.origin === gateway.origin)) return fail({
|
|
386
|
+
type: "untrustedGateway",
|
|
387
|
+
owner: owner.id,
|
|
388
|
+
gateway,
|
|
389
|
+
gateways: owner.gateways
|
|
390
|
+
});
|
|
391
|
+
return {
|
|
392
|
+
verified: true,
|
|
393
|
+
method: "gateway",
|
|
394
|
+
unsecured: true,
|
|
395
|
+
owner: owner.id,
|
|
396
|
+
gateway,
|
|
397
|
+
collectionContext: createContext({
|
|
398
|
+
rootId: chain.rootId,
|
|
399
|
+
rootOrigin: chain.rootOrigin,
|
|
400
|
+
owner,
|
|
401
|
+
ownerCandidate: null
|
|
402
|
+
})
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/sig/portable-collection.test.ts
|
|
407
|
+
const AS = "https://www.w3.org/ns/activitystreams#";
|
|
408
|
+
const context$1 = [
|
|
409
|
+
"https://www.w3.org/ns/activitystreams",
|
|
410
|
+
"https://w3id.org/security/data-integrity/v1",
|
|
411
|
+
"https://w3id.org/fep/ef61"
|
|
412
|
+
];
|
|
413
|
+
const did = await exportDidKey(ed25519PublicKey.publicKey);
|
|
414
|
+
const keyId = `${did}#${did.slice(8)}`;
|
|
415
|
+
const otherKeyPair = await crypto.subtle.generateKey("Ed25519", true, ["sign", "verify"]);
|
|
416
|
+
const otherDid = await exportDidKey(otherKeyPair.publicKey);
|
|
417
|
+
const actorId = `ap://${did}/actor`;
|
|
418
|
+
const outboxId = `ap://${did}/actor/outbox`;
|
|
419
|
+
const gw1 = "https://gw1.example";
|
|
420
|
+
const gw2 = "https://gw2.example";
|
|
421
|
+
const evil = "https://evil.example";
|
|
422
|
+
function gatewayUrl(gateway, id) {
|
|
423
|
+
return `${gateway}/.well-known/apgateway/${id.replace(/^ap:\/\//, "")}`;
|
|
424
|
+
}
|
|
425
|
+
async function sign(document, privateKey = ed25519PrivateKey, verificationMethod = keyId) {
|
|
426
|
+
const proofConfig = {
|
|
427
|
+
"@context": document["@context"],
|
|
428
|
+
type: "DataIntegrityProof",
|
|
429
|
+
cryptosuite: "eddsa-jcs-2022",
|
|
430
|
+
verificationMethod,
|
|
431
|
+
proofPurpose: "assertionMethod",
|
|
432
|
+
created: "2023-02-24T23:36:38Z"
|
|
433
|
+
};
|
|
434
|
+
const encoder = new TextEncoder();
|
|
435
|
+
const proofDigest = await crypto.subtle.digest("SHA-256", encoder.encode(serialize(proofConfig)));
|
|
436
|
+
const messageDigest = await crypto.subtle.digest("SHA-256", encoder.encode(serialize(document)));
|
|
437
|
+
const digest = /* @__PURE__ */ new Uint8Array(64);
|
|
438
|
+
digest.set(new Uint8Array(proofDigest), 0);
|
|
439
|
+
digest.set(new Uint8Array(messageDigest), 32);
|
|
440
|
+
const signature = await crypto.subtle.sign("Ed25519", privateKey, digest);
|
|
441
|
+
return {
|
|
442
|
+
...document,
|
|
443
|
+
proof: {
|
|
444
|
+
...proofConfig,
|
|
445
|
+
proofValue: new TextDecoder().decode(encodeMultibase("base58btc", new Uint8Array(signature)))
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function actor(overrides = {}) {
|
|
450
|
+
return {
|
|
451
|
+
"@context": context$1,
|
|
452
|
+
id: actorId,
|
|
453
|
+
type: "Person",
|
|
454
|
+
inbox: `ap://${did}/actor/inbox`,
|
|
455
|
+
outbox: outboxId,
|
|
456
|
+
followers: `ap://${did}/actor/followers`,
|
|
457
|
+
gateways: [gw1, gw2],
|
|
458
|
+
...overrides
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
function outbox(overrides = {}) {
|
|
462
|
+
return {
|
|
463
|
+
"@context": context$1,
|
|
464
|
+
id: outboxId,
|
|
465
|
+
type: "OrderedCollection",
|
|
466
|
+
totalItems: 0,
|
|
467
|
+
...overrides
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
const contextLoader = (url) => {
|
|
471
|
+
const document = preloadedContexts[url];
|
|
472
|
+
if (document == null) return Promise.reject(/* @__PURE__ */ new Error(`No context: ${url}`));
|
|
473
|
+
return Promise.resolve({
|
|
474
|
+
contextUrl: null,
|
|
475
|
+
documentUrl: url,
|
|
476
|
+
document
|
|
477
|
+
});
|
|
478
|
+
};
|
|
479
|
+
function createLoader(responses) {
|
|
480
|
+
const fetched = [];
|
|
481
|
+
const loader = (url) => {
|
|
482
|
+
fetched.push(url);
|
|
483
|
+
let response = responses[url];
|
|
484
|
+
let documentUrl = url;
|
|
485
|
+
if (response != null && "redirect" in response) {
|
|
486
|
+
documentUrl = response.redirect;
|
|
487
|
+
response = responses[documentUrl];
|
|
488
|
+
}
|
|
489
|
+
if (response == null) return Promise.reject(URL.canParse(url) ? new FetchError(url, "HTTP 404", new Response(null, { status: 404 })) : /* @__PURE__ */ new Error(`Not found: ${url}`));
|
|
490
|
+
return Promise.resolve({
|
|
491
|
+
contextUrl: null,
|
|
492
|
+
documentUrl,
|
|
493
|
+
document: structuredClone(response)
|
|
494
|
+
});
|
|
495
|
+
};
|
|
496
|
+
return Object.assign(loader, { fetched });
|
|
497
|
+
}
|
|
498
|
+
function linkFrom(object, property, overrides = {}) {
|
|
499
|
+
return {
|
|
500
|
+
object,
|
|
501
|
+
id: object.id ?? null,
|
|
502
|
+
property: AS + property,
|
|
503
|
+
...overrides
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
async function verify(document, options) {
|
|
507
|
+
return await verifyPortableObject(document, {
|
|
508
|
+
contextLoader,
|
|
509
|
+
...options
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
function assertFailure(result, type) {
|
|
513
|
+
ok(!result.verified, `expected ${type}, got ${JSON.stringify(result)}`);
|
|
514
|
+
strictEqual(result.reason.type, type);
|
|
515
|
+
}
|
|
516
|
+
const signedActor = await sign(actor());
|
|
517
|
+
const actorObject = await Person.fromJsonLd(signedActor, { contextLoader });
|
|
518
|
+
test("verifyPortableObject() trusts unsecured collections from listed gateways", async () => {
|
|
519
|
+
const documentLoader = createLoader({ [gatewayUrl(gw1, actorId)]: signedActor });
|
|
520
|
+
const result = await verify(outbox(), {
|
|
521
|
+
documentLoader,
|
|
522
|
+
documentUrl: new URL(gatewayUrl(gw2, outboxId)),
|
|
523
|
+
gatewayHints: [new URL(gw1)],
|
|
524
|
+
referrer: linkFrom(actorObject, "outbox")
|
|
525
|
+
});
|
|
526
|
+
ok(result.verified);
|
|
527
|
+
strictEqual(result.method, "gateway");
|
|
528
|
+
if (result.method !== "gateway") return;
|
|
529
|
+
deepStrictEqual(result.gateway, new URL(gw2));
|
|
530
|
+
strictEqual(result.owner.href, actorObject.id?.href);
|
|
531
|
+
deepStrictEqual(documentLoader.fetched, [gatewayUrl(gw1, actorId)]);
|
|
532
|
+
});
|
|
533
|
+
test("verifyPortableObject() rejects unsecured collections from unlisted gateways", async () => {
|
|
534
|
+
const result = await verify(outbox(), {
|
|
535
|
+
documentLoader: createLoader({ [gatewayUrl(gw1, actorId)]: signedActor }),
|
|
536
|
+
documentUrl: new URL(gatewayUrl(evil, outboxId)),
|
|
537
|
+
gatewayHints: [new URL(evil), new URL(gw1)],
|
|
538
|
+
referrer: linkFrom(actorObject, "outbox")
|
|
539
|
+
});
|
|
540
|
+
assertFailure(result, "untrustedGateway");
|
|
541
|
+
if (result.verified || result.reason.type !== "untrustedGateway") return;
|
|
542
|
+
deepStrictEqual(result.reason.gateway, new URL(evil));
|
|
543
|
+
deepStrictEqual(result.reason.gateways, [new URL(gw1), new URL(gw2)]);
|
|
544
|
+
});
|
|
545
|
+
test("verifyPortableObject() uses the actual fetch source", async () => {
|
|
546
|
+
const documentLoader = createLoader({ [gatewayUrl(gw1, actorId)]: signedActor });
|
|
547
|
+
const referrer = linkFrom(actorObject, "outbox");
|
|
548
|
+
const gateways = [new URL(gw1)];
|
|
549
|
+
assertFailure(await verify(outbox(), {
|
|
550
|
+
documentLoader,
|
|
551
|
+
gateways,
|
|
552
|
+
referrer
|
|
553
|
+
}), "unknownCollectionSource");
|
|
554
|
+
assertFailure(await verify(outbox(), {
|
|
555
|
+
documentLoader,
|
|
556
|
+
gateways,
|
|
557
|
+
referrer,
|
|
558
|
+
documentUrl: new URL("https://gw1.example/cdn/outbox.json")
|
|
559
|
+
}), "unknownCollectionSource");
|
|
560
|
+
assertFailure(await verify(outbox(), {
|
|
561
|
+
documentLoader,
|
|
562
|
+
gateways,
|
|
563
|
+
referrer,
|
|
564
|
+
documentUrl: new URL(gatewayUrl(gw1, `ap://${did}/actor/inbox`))
|
|
565
|
+
}), "unknownCollectionSource");
|
|
566
|
+
assertFailure(await verify(outbox(), {
|
|
567
|
+
documentLoader,
|
|
568
|
+
gateways,
|
|
569
|
+
referrer,
|
|
570
|
+
documentUrl: new URL(`https://user@gw1.example/.well-known/apgateway/${did}/actor/outbox`)
|
|
571
|
+
}), "unknownCollectionSource");
|
|
572
|
+
assertFailure(await verify(outbox({ id: gatewayUrl(evil, outboxId) }), {
|
|
573
|
+
documentLoader,
|
|
574
|
+
gateways,
|
|
575
|
+
referrer,
|
|
576
|
+
documentUrl: new URL(gatewayUrl(gw1, outboxId))
|
|
577
|
+
}), "notPortableObject");
|
|
578
|
+
});
|
|
579
|
+
test("verifyPortableObject() requires an unambiguous owner", async () => {
|
|
580
|
+
const base = {
|
|
581
|
+
documentLoader: createLoader({ [gatewayUrl(gw1, actorId)]: signedActor }),
|
|
582
|
+
documentUrl: new URL(gatewayUrl(gw1, outboxId)),
|
|
583
|
+
gateways: [new URL(gw1)]
|
|
584
|
+
};
|
|
585
|
+
assertFailure(await verify(outbox(), base), "unknownCollectionOwner");
|
|
586
|
+
const note = await Note.fromJsonLd({
|
|
587
|
+
"@context": context$1,
|
|
588
|
+
id: `ap://${did}/notes/1`,
|
|
589
|
+
type: "Note"
|
|
590
|
+
}, { contextLoader });
|
|
591
|
+
assertFailure(await verify(outbox(), {
|
|
592
|
+
...base,
|
|
593
|
+
referrer: linkFrom(note, "replies")
|
|
594
|
+
}), "unknownCollectionOwner");
|
|
595
|
+
const attributed = await verify(outbox({ attributedTo: actorId }), base);
|
|
596
|
+
ok(attributed.verified);
|
|
597
|
+
strictEqual(attributed.method, "gateway");
|
|
598
|
+
assertFailure(await verify(outbox({ attributedTo: [actorId, `ap://${did}/actor2`] }), base), "conflictingCollectionOwners");
|
|
599
|
+
assertFailure(await verify(outbox({ attributedTo: `ap://${did}/actor2` }), {
|
|
600
|
+
...base,
|
|
601
|
+
referrer: linkFrom(actorObject, "outbox")
|
|
602
|
+
}), "conflictingCollectionOwners");
|
|
603
|
+
assertFailure(await verify(outbox({ attributedTo: "https://example.com/actor" }), base), "unknownCollectionOwner");
|
|
604
|
+
});
|
|
605
|
+
test("verifyPortableObject() requires the owner to share the collection's DID", async () => {
|
|
606
|
+
const foreignActorId = `ap://${otherDid}/actor`;
|
|
607
|
+
const foreignActor = await sign(actor({
|
|
608
|
+
id: foreignActorId,
|
|
609
|
+
gateways: [evil]
|
|
610
|
+
}), otherKeyPair.privateKey, `${otherDid}#${otherDid.slice(8)}`);
|
|
611
|
+
const foreignActorObject = await Person.fromJsonLd(foreignActor, { contextLoader });
|
|
612
|
+
const documentLoader = createLoader({ [gatewayUrl(evil, foreignActorId)]: foreignActor });
|
|
613
|
+
const base = {
|
|
614
|
+
documentLoader,
|
|
615
|
+
documentUrl: new URL(gatewayUrl(evil, outboxId)),
|
|
616
|
+
gateways: [new URL(evil)]
|
|
617
|
+
};
|
|
618
|
+
assertFailure(await verify(outbox(), {
|
|
619
|
+
...base,
|
|
620
|
+
referrer: linkFrom(foreignActorObject, "outbox")
|
|
621
|
+
}), "unknownCollectionOwner");
|
|
622
|
+
assertFailure(await verify(outbox({ attributedTo: foreignActorId }), base), "unknownCollectionOwner");
|
|
623
|
+
deepStrictEqual(documentLoader.fetched, []);
|
|
624
|
+
});
|
|
625
|
+
test("verifyPortableObject() confirms the owner with its actor document", async () => {
|
|
626
|
+
const documentUrl = new URL(gatewayUrl(gw1, outboxId));
|
|
627
|
+
const gateways = [new URL(gw1)];
|
|
628
|
+
const referrer = linkFrom(actorObject, "outbox");
|
|
629
|
+
assertFailure(await verify(outbox(), {
|
|
630
|
+
documentLoader: createLoader({}),
|
|
631
|
+
documentUrl,
|
|
632
|
+
gateways,
|
|
633
|
+
referrer
|
|
634
|
+
}), "collectionOwnerUnavailable");
|
|
635
|
+
assertFailure(await verify(outbox(), {
|
|
636
|
+
documentLoader: createLoader({ [gatewayUrl(gw1, actorId)]: actor() }),
|
|
637
|
+
documentUrl,
|
|
638
|
+
gateways,
|
|
639
|
+
referrer
|
|
640
|
+
}), "collectionOwnerUnavailable");
|
|
641
|
+
const { gateways: _, ...noGateways } = actor();
|
|
642
|
+
assertFailure(await verify(outbox(), {
|
|
643
|
+
documentLoader: createLoader({ [gatewayUrl(gw1, actorId)]: await sign(noGateways) }),
|
|
644
|
+
documentUrl,
|
|
645
|
+
gateways,
|
|
646
|
+
referrer
|
|
647
|
+
}), "collectionOwnerUnavailable");
|
|
648
|
+
const otherCollectionId = `ap://${did}/collections/1`;
|
|
649
|
+
assertFailure(await verify(outbox({
|
|
650
|
+
id: otherCollectionId,
|
|
651
|
+
attributedTo: actorId
|
|
652
|
+
}), {
|
|
653
|
+
documentLoader: createLoader({ [gatewayUrl(gw1, actorId)]: signedActor }),
|
|
654
|
+
documentUrl: new URL(gatewayUrl(gw1, otherCollectionId)),
|
|
655
|
+
gateways
|
|
656
|
+
}), "collectionNotListedByOwner");
|
|
657
|
+
const emptyLoader = createLoader({ [gatewayUrl(gw1, actorId)]: signedActor });
|
|
658
|
+
assertFailure(await verify(outbox(), {
|
|
659
|
+
documentLoader: emptyLoader,
|
|
660
|
+
documentUrl,
|
|
661
|
+
gateways: [],
|
|
662
|
+
gatewayHints: [new URL(gw1)],
|
|
663
|
+
referrer
|
|
664
|
+
}), "collectionOwnerUnavailable");
|
|
665
|
+
deepStrictEqual(emptyLoader.fetched, [`ap+ef61://${did}/actor`]);
|
|
666
|
+
});
|
|
667
|
+
test("verifyPortableObject() reuses a verified referring actor", async () => {
|
|
668
|
+
const documentLoader = createLoader({});
|
|
669
|
+
const result = await verify(outbox(), {
|
|
670
|
+
documentLoader,
|
|
671
|
+
documentUrl: new URL(gatewayUrl(gw1, outboxId)),
|
|
672
|
+
referrer: linkFrom(actorObject, "outbox", { acceptance: "verified" })
|
|
673
|
+
});
|
|
674
|
+
ok(result.verified);
|
|
675
|
+
strictEqual(result.method, "gateway");
|
|
676
|
+
deepStrictEqual(documentLoader.fetched, []);
|
|
677
|
+
});
|
|
678
|
+
test("verifyPortableObject() does not downgrade signed collections", async () => {
|
|
679
|
+
const documentLoader = createLoader({});
|
|
680
|
+
const signedOutbox = await sign(outbox());
|
|
681
|
+
const result = await verify(signedOutbox, {
|
|
682
|
+
documentLoader,
|
|
683
|
+
referrer: linkFrom(actorObject, "outbox")
|
|
684
|
+
});
|
|
685
|
+
ok(result.verified);
|
|
686
|
+
strictEqual(result.method, "proof");
|
|
687
|
+
assertFailure(await verify({
|
|
688
|
+
...signedOutbox,
|
|
689
|
+
totalItems: 1
|
|
690
|
+
}, {
|
|
691
|
+
documentLoader: createLoader({ [gatewayUrl(gw1, actorId)]: signedActor }),
|
|
692
|
+
documentUrl: new URL(gatewayUrl(gw1, outboxId)),
|
|
693
|
+
gateways: [new URL(gw1)],
|
|
694
|
+
referrer: linkFrom(actorObject, "outbox")
|
|
695
|
+
}), "invalidProof");
|
|
696
|
+
assertFailure(await verify(outbox({ id: "https://example.com/outbox" }), { documentLoader }), "notPortableObject");
|
|
697
|
+
});
|
|
698
|
+
test("verifyPortableObject() checks pages against their collection", async () => {
|
|
699
|
+
const documentLoader = createLoader({ [gatewayUrl(gw1, actorId)]: signedActor });
|
|
700
|
+
const collectionResult = await verify(outbox(), {
|
|
701
|
+
documentLoader,
|
|
702
|
+
documentUrl: new URL(gatewayUrl(gw1, outboxId)),
|
|
703
|
+
gateways: [new URL(gw1)],
|
|
704
|
+
referrer: linkFrom(actorObject, "outbox")
|
|
705
|
+
});
|
|
706
|
+
ok(collectionResult.verified);
|
|
707
|
+
const collection = await OrderedCollection.fromJsonLd(outbox(), { contextLoader });
|
|
708
|
+
const collectionContext = collectionResult.collectionContext;
|
|
709
|
+
const pageId = `ap://${did}/actor/outbox?page=1`;
|
|
710
|
+
const page = (overrides = {}) => ({
|
|
711
|
+
"@context": context$1,
|
|
712
|
+
id: pageId,
|
|
713
|
+
type: "OrderedCollectionPage",
|
|
714
|
+
partOf: outboxId,
|
|
715
|
+
orderedItems: [],
|
|
716
|
+
...overrides
|
|
717
|
+
});
|
|
718
|
+
const pageReferrer = linkFrom(collection, "first", { collectionContext });
|
|
719
|
+
const fetchedBefore = documentLoader.fetched.length;
|
|
720
|
+
const accepted = await verify(page(), {
|
|
721
|
+
documentLoader,
|
|
722
|
+
documentUrl: new URL(gatewayUrl(gw2, pageId)),
|
|
723
|
+
referrer: pageReferrer
|
|
724
|
+
});
|
|
725
|
+
ok(accepted.verified);
|
|
726
|
+
strictEqual(accepted.method, "gateway");
|
|
727
|
+
strictEqual(documentLoader.fetched.length, fetchedBefore);
|
|
728
|
+
assertFailure(await verify(page(), {
|
|
729
|
+
documentLoader,
|
|
730
|
+
documentUrl: new URL(gatewayUrl(evil, pageId)),
|
|
731
|
+
referrer: pageReferrer
|
|
732
|
+
}), "untrustedGateway");
|
|
733
|
+
assertFailure(await verify(page({ partOf: `ap://${did}/actor/inbox` }), {
|
|
734
|
+
documentLoader,
|
|
735
|
+
documentUrl: new URL(gatewayUrl(gw1, pageId)),
|
|
736
|
+
referrer: pageReferrer
|
|
737
|
+
}), "collectionPageMismatch");
|
|
738
|
+
const foreignPageId = `ap://${otherDid}/outbox?page=1`;
|
|
739
|
+
assertFailure(await verify(page({
|
|
740
|
+
id: foreignPageId,
|
|
741
|
+
partOf: void 0
|
|
742
|
+
}), {
|
|
743
|
+
documentLoader,
|
|
744
|
+
documentUrl: new URL(gatewayUrl(gw1, foreignPageId)),
|
|
745
|
+
referrer: pageReferrer
|
|
746
|
+
}), "collectionPageMismatch");
|
|
747
|
+
assertFailure(await verify(page({ attributedTo: `ap://${did}/actor2` }), {
|
|
748
|
+
documentLoader,
|
|
749
|
+
documentUrl: new URL(gatewayUrl(gw1, pageId)),
|
|
750
|
+
referrer: pageReferrer
|
|
751
|
+
}), "conflictingCollectionOwners");
|
|
752
|
+
assertFailure(await verify(page({ attributedTo: actorId }), {
|
|
753
|
+
documentLoader,
|
|
754
|
+
documentUrl: new URL(gatewayUrl(gw1, pageId)),
|
|
755
|
+
referrer: linkFrom(collection, "first")
|
|
756
|
+
}), "unknownCollectionOwner");
|
|
757
|
+
assertFailure(await verify(page(), {
|
|
758
|
+
documentLoader,
|
|
759
|
+
documentUrl: new URL(gatewayUrl(gw1, pageId)),
|
|
760
|
+
referrer: linkFrom(collection, "first", { collectionContext: {
|
|
761
|
+
rootId: outboxId,
|
|
762
|
+
owner: null
|
|
763
|
+
} })
|
|
764
|
+
}), "unknownCollectionOwner");
|
|
765
|
+
});
|
|
766
|
+
test("verifyPortableObject() checks intermediate pages", async () => {
|
|
767
|
+
const documentLoader = createLoader({ [gatewayUrl(gw1, actorId)]: signedActor });
|
|
768
|
+
const collection = await OrderedCollection.fromJsonLd(outbox(), { contextLoader });
|
|
769
|
+
const intermediate = async (partOf) => await CollectionPage.fromJsonLd({
|
|
770
|
+
"@context": context$1,
|
|
771
|
+
id: `ap://${did}/actor/outbox?page=1`,
|
|
772
|
+
type: "OrderedCollectionPage",
|
|
773
|
+
partOf
|
|
774
|
+
}, { contextLoader });
|
|
775
|
+
const pageId = `ap://${did}/actor/outbox?page=2`;
|
|
776
|
+
const page = {
|
|
777
|
+
"@context": context$1,
|
|
778
|
+
id: pageId,
|
|
779
|
+
type: "OrderedCollectionPage",
|
|
780
|
+
partOf: outboxId,
|
|
781
|
+
orderedItems: []
|
|
782
|
+
};
|
|
783
|
+
const chain = (first) => linkFrom(first, "next", { referrer: linkFrom(collection, "first", { referrer: linkFrom(actorObject, "outbox") }) });
|
|
784
|
+
const options = {
|
|
785
|
+
documentLoader,
|
|
786
|
+
documentUrl: new URL(gatewayUrl(gw1, pageId)),
|
|
787
|
+
gateways: [new URL(gw1)]
|
|
788
|
+
};
|
|
789
|
+
const result = await verify(page, {
|
|
790
|
+
...options,
|
|
791
|
+
referrer: chain(await intermediate(outboxId))
|
|
792
|
+
});
|
|
793
|
+
ok(result.verified);
|
|
794
|
+
strictEqual(result.method, "gateway");
|
|
795
|
+
assertFailure(await verify(page, {
|
|
796
|
+
...options,
|
|
797
|
+
referrer: chain(await intermediate(`ap://${did}/actor/inbox`))
|
|
798
|
+
}), "collectionPageMismatch");
|
|
799
|
+
assertFailure(await verify(page, {
|
|
800
|
+
...options,
|
|
801
|
+
referrer: linkFrom(await intermediate(outboxId), "next", { truncated: true })
|
|
802
|
+
}), "unknownCollectionOwner");
|
|
803
|
+
});
|
|
804
|
+
test("verifyPortableObject() works with property accessors", async () => {
|
|
805
|
+
const itemId = `ap://${did}/notes/1`;
|
|
806
|
+
const note = await sign({
|
|
807
|
+
"@context": context$1,
|
|
808
|
+
id: itemId,
|
|
809
|
+
type: "Note",
|
|
810
|
+
attributedTo: actorId,
|
|
811
|
+
content: "Hello"
|
|
812
|
+
});
|
|
813
|
+
const page1Id = `ap://${did}/actor/outbox?page=1`;
|
|
814
|
+
const page2Id = `ap://${did}/actor/outbox?page=2`;
|
|
815
|
+
const page1 = {
|
|
816
|
+
id: page1Id,
|
|
817
|
+
type: "OrderedCollectionPage",
|
|
818
|
+
partOf: outboxId,
|
|
819
|
+
orderedItems: [{
|
|
820
|
+
id: itemId,
|
|
821
|
+
type: "Note",
|
|
822
|
+
content: "Forged"
|
|
823
|
+
}],
|
|
824
|
+
next: page2Id
|
|
825
|
+
};
|
|
826
|
+
const options = {
|
|
827
|
+
documentLoader: createLoader({
|
|
828
|
+
[gatewayUrl(gw1, actorId)]: signedActor,
|
|
829
|
+
[gatewayUrl(gw1, outboxId)]: outbox({ first: page1 }),
|
|
830
|
+
[gatewayUrl(gw1, page1Id)]: {
|
|
831
|
+
"@context": context$1,
|
|
832
|
+
...page1
|
|
833
|
+
},
|
|
834
|
+
[gatewayUrl(gw1, page2Id)]: {
|
|
835
|
+
"@context": context$1,
|
|
836
|
+
id: page2Id,
|
|
837
|
+
type: "OrderedCollectionPage",
|
|
838
|
+
partOf: outboxId,
|
|
839
|
+
orderedItems: [itemId]
|
|
840
|
+
},
|
|
841
|
+
[gatewayUrl(gw1, itemId)]: note
|
|
842
|
+
}),
|
|
843
|
+
contextLoader,
|
|
844
|
+
gateways: [gw1],
|
|
845
|
+
verifyPortableObject
|
|
846
|
+
};
|
|
847
|
+
const create = await Collection.fromJsonLd({
|
|
848
|
+
"@context": context$1,
|
|
849
|
+
type: "Collection",
|
|
850
|
+
items: [actorId]
|
|
851
|
+
}, { contextLoader });
|
|
852
|
+
const items = await Array.fromAsync(create.getItems(options));
|
|
853
|
+
strictEqual(items.length, 1);
|
|
854
|
+
ok(items[0] instanceof Person);
|
|
855
|
+
const outboxCollection = await items[0].getOutbox(options);
|
|
856
|
+
ok(outboxCollection instanceof OrderedCollection);
|
|
857
|
+
deepStrictEqual((await Array.fromAsync(traverseCollection(outboxCollection, options))).map((item) => item instanceof Note ? item.content : null), ["Hello", "Hello"]);
|
|
858
|
+
const evilLoader = createLoader({
|
|
859
|
+
[gatewayUrl(evil, actorId)]: signedActor,
|
|
860
|
+
[gatewayUrl(evil, outboxId)]: outbox()
|
|
861
|
+
});
|
|
862
|
+
strictEqual(await (await Person.fromJsonLd(signedActor, { contextLoader })).getOutbox({
|
|
863
|
+
...options,
|
|
864
|
+
documentLoader: evilLoader,
|
|
865
|
+
gateways: [evil]
|
|
866
|
+
}), null);
|
|
867
|
+
});
|
|
868
|
+
test("verifyPortableObject() lets pages inherit owners across signed and embedded collections", async () => {
|
|
869
|
+
const page1Id = `ap://${did}/actor/outbox?page=1`;
|
|
870
|
+
const page1 = {
|
|
871
|
+
"@context": context$1,
|
|
872
|
+
id: page1Id,
|
|
873
|
+
type: "OrderedCollectionPage",
|
|
874
|
+
partOf: outboxId,
|
|
875
|
+
orderedItems: []
|
|
876
|
+
};
|
|
877
|
+
const fetchPerson = async (options) => {
|
|
878
|
+
const wrapper = await Collection.fromJsonLd({
|
|
879
|
+
"@context": context$1,
|
|
880
|
+
type: "Collection",
|
|
881
|
+
items: [actorId]
|
|
882
|
+
}, { contextLoader });
|
|
883
|
+
const [person] = await Array.fromAsync(wrapper.getItems(options));
|
|
884
|
+
ok(person instanceof Person);
|
|
885
|
+
return person;
|
|
886
|
+
};
|
|
887
|
+
const signedOutbox = await sign(outbox({ first: page1Id }));
|
|
888
|
+
let documentLoader = createLoader({
|
|
889
|
+
[gatewayUrl(gw1, actorId)]: signedActor,
|
|
890
|
+
[gatewayUrl(gw1, outboxId)]: signedOutbox,
|
|
891
|
+
[gatewayUrl(gw2, page1Id)]: page1
|
|
892
|
+
});
|
|
893
|
+
let options = {
|
|
894
|
+
documentLoader,
|
|
895
|
+
contextLoader,
|
|
896
|
+
gateways: [gw1, gw2],
|
|
897
|
+
verifyPortableObject
|
|
898
|
+
};
|
|
899
|
+
let person = await fetchPerson(options);
|
|
900
|
+
let collection = await person.getOutbox(options);
|
|
901
|
+
ok(collection instanceof OrderedCollection);
|
|
902
|
+
ok(await collection.getFirst(options) instanceof CollectionPage);
|
|
903
|
+
const embeddingActor = await sign(actor({ outbox: {
|
|
904
|
+
id: outboxId,
|
|
905
|
+
type: "OrderedCollection",
|
|
906
|
+
first: page1Id
|
|
907
|
+
} }));
|
|
908
|
+
documentLoader = createLoader({
|
|
909
|
+
[gatewayUrl(gw1, actorId)]: embeddingActor,
|
|
910
|
+
[gatewayUrl(gw2, page1Id)]: page1,
|
|
911
|
+
[gatewayUrl(evil, page1Id)]: page1
|
|
912
|
+
});
|
|
913
|
+
options = {
|
|
914
|
+
...options,
|
|
915
|
+
documentLoader
|
|
916
|
+
};
|
|
917
|
+
person = await fetchPerson(options);
|
|
918
|
+
collection = await person.getOutbox(options);
|
|
919
|
+
ok(collection instanceof OrderedCollection);
|
|
920
|
+
ok(await collection.getFirst(options) instanceof CollectionPage);
|
|
921
|
+
deepStrictEqual(documentLoader.fetched, [
|
|
922
|
+
gatewayUrl(gw1, actorId),
|
|
923
|
+
gatewayUrl(gw1, page1Id),
|
|
924
|
+
gatewayUrl(gw2, page1Id)
|
|
925
|
+
]);
|
|
926
|
+
person = await fetchPerson(options);
|
|
927
|
+
collection = await person.getOutbox(options);
|
|
928
|
+
ok(collection instanceof OrderedCollection);
|
|
929
|
+
strictEqual(await collection.getFirst({
|
|
930
|
+
...options,
|
|
931
|
+
gateways: [evil]
|
|
932
|
+
}), null);
|
|
933
|
+
});
|
|
934
|
+
test("verifyPortableObject() reuses a verified actor for pages of signed collections", async () => {
|
|
935
|
+
const documentLoader = createLoader({});
|
|
936
|
+
const signed = await verify(await sign(outbox()), {
|
|
937
|
+
documentLoader,
|
|
938
|
+
referrer: linkFrom(actorObject, "outbox", { acceptance: "verified" })
|
|
939
|
+
});
|
|
940
|
+
ok(signed.verified);
|
|
941
|
+
strictEqual(signed.method, "proof");
|
|
942
|
+
const collection = await OrderedCollection.fromJsonLd(outbox(), { contextLoader });
|
|
943
|
+
const pageId = `ap://${did}/actor/outbox?page=1`;
|
|
944
|
+
const result = await verify({
|
|
945
|
+
"@context": context$1,
|
|
946
|
+
id: pageId,
|
|
947
|
+
type: "OrderedCollectionPage",
|
|
948
|
+
partOf: outboxId,
|
|
949
|
+
orderedItems: []
|
|
950
|
+
}, {
|
|
951
|
+
documentLoader,
|
|
952
|
+
documentUrl: new URL(gatewayUrl(gw1, pageId)),
|
|
953
|
+
referrer: linkFrom(collection, "first", { collectionContext: signed.collectionContext })
|
|
954
|
+
});
|
|
955
|
+
ok(result.verified);
|
|
956
|
+
strictEqual(result.method, "gateway");
|
|
957
|
+
deepStrictEqual(documentLoader.fetched, []);
|
|
958
|
+
});
|
|
959
|
+
test("verifyPortableObject() fetches the owner through attributedTo hints", async () => {
|
|
960
|
+
const documentLoader = createLoader({ [gatewayUrl(gw2, actorId)]: signedActor });
|
|
961
|
+
const result = await verify(outbox({ attributedTo: `${actorId}?@gateway=${encodeURIComponent(gw2)}` }), {
|
|
962
|
+
documentLoader,
|
|
963
|
+
documentUrl: new URL(gatewayUrl(gw1, outboxId)),
|
|
964
|
+
gatewayHints: [new URL(gw1)]
|
|
965
|
+
});
|
|
966
|
+
ok(result.verified);
|
|
967
|
+
strictEqual(result.method, "gateway");
|
|
968
|
+
deepStrictEqual(documentLoader.fetched, [gatewayUrl(gw2, actorId)]);
|
|
969
|
+
});
|
|
970
|
+
//#endregion
|
|
971
|
+
export {};
|