@fedify/fedify 2.1.31 → 2.1.32
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/attachment-arrays-DZ4JHJD8.mjs +111 -0
- package/dist/{builder-isMRh_I6.mjs → builder--sc-rozj.mjs} +3 -3
- package/dist/callback-BARPujV7.d.mts +2388 -0
- package/dist/{client-BIBzqEIp.d.cts → client-9DPcy_Yx.d.ts} +16 -0
- package/dist/{client-jE7oGvRh.d.ts → client-B3ruDgWg.d.cts} +16 -0
- package/dist/{client-BZr7TGyu.mjs → client-cpPaW7Ww.mjs} +86 -11
- package/dist/compat/attachment-arrays.test.d.mts +2 -0
- package/dist/compat/attachment-arrays.test.mjs +254 -0
- 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/{context-C0uGVzgk.d.ts → context-DE2YrQwM.d.ts} +30 -13
- package/dist/{context-CLaLxi49.d.cts → context-y6ZUQCMf.d.cts} +30 -13
- package/dist/{deno-CiaJqTcu.mjs → deno-L23b3Hba.mjs} +1 -1
- package/dist/{docloader-DOrwedT7.mjs → docloader-DUXr9k_O.mjs} +2 -2
- package/dist/federation/attachment-responses.test.d.mts +2 -0
- package/dist/federation/attachment-responses.test.mjs +182 -0
- package/dist/federation/builder.test.mjs +1 -1
- package/dist/federation/collection.test.mjs +1 -1
- package/dist/federation/delivery-attachments.test.d.mts +2 -0
- package/dist/federation/delivery-attachments.test.mjs +280 -0
- package/dist/federation/handler.test.mjs +208 -16
- package/dist/federation/idempotency.test.mjs +2 -2
- package/dist/federation/inbox.test.mjs +1 -1
- package/dist/federation/middleware.test.mjs +1185 -8
- package/dist/federation/mod.cjs +1 -1
- package/dist/federation/mod.d.cts +2 -2
- package/dist/federation/mod.d.ts +2 -2
- package/dist/federation/mod.js +1 -1
- package/dist/federation/negotiation.test.mjs +1 -1
- package/dist/federation/retry.test.mjs +1 -1
- package/dist/federation/send.test.mjs +283 -3
- package/dist/federation/temporal.test.mjs +25 -1
- package/dist/federation/webfinger.test.mjs +1 -1
- package/dist/{http-Crml3gei.js → http-BtbMAgTA.js} +6 -4
- package/dist/{http-9IFUxdkp.d.cts → http-C8qLHs_Z.d.cts} +3 -3
- package/dist/{http-CRiVlZd5.cjs → http-D5woFKA_.cjs} +6 -4
- package/dist/{http-SbfezERn.d.ts → http-D8WmcRAF.d.ts} +3 -3
- package/dist/{http-A9mP2uM7.mjs → http-HX6wVyJh.mjs} +6 -4
- package/dist/{inbox-KESLudq4.mjs → inbox-CL7A0HXT.mjs} +1 -1
- package/dist/{key-Dg6-DV_O.mjs → key-Dai3f5yI.mjs} +3 -2
- package/dist/{kv-cache-Cqmm-yQa.cjs → kv-cache-B0V_e231.cjs} +1 -1
- package/dist/{kv-cache-DQcyZL5T.js → kv-cache-BsDg-cT8.js} +1 -1
- package/dist/{ld-COjDN6Nb.mjs → ld-DN6x1iZ8.mjs} +153 -21
- package/dist/ld-signature-DoZcbsZR.mjs +23 -0
- package/dist/{middleware-BtDQUCcz.cjs → middleware-Cl6k5Hrm.cjs} +1 -1
- package/dist/{middleware-BbgR7xOI.cjs → middleware-CrfRuKCf.cjs} +561 -101
- package/dist/{middleware-COFlKeYn.mjs → middleware-DiquMgtt.mjs} +378 -89
- package/dist/{middleware-DIW5S_5L.js → middleware-DunNtbo9.js} +560 -101
- package/dist/{middleware-CuQvczZd.mjs → middleware-SnQS8eIb.mjs} +1 -1
- package/dist/{mod-LOEOKbYd.d.cts → mod-CehHJOV4.d.cts} +30 -13
- package/dist/{mod-CFRocFeQ.d.ts → mod-DLJpnqdw.d.ts} +1 -1
- package/dist/{mod-C7w-QVUv.d.ts → mod-Mh3oIuRK.d.ts} +30 -13
- package/dist/{mod-BRnwh2oy.d.ts → mod-REfljt0T.d.ts} +3 -3
- package/dist/{mod-Co7LEK9X.d.cts → mod-UvIGUHqW.d.cts} +3 -3
- package/dist/{mod-CGUH8Ujp.d.cts → mod-sTa1Zzxk.d.cts} +1 -1
- package/dist/mod.cjs +5 -5
- package/dist/mod.d.cts +8 -8
- package/dist/mod.d.ts +8 -8
- package/dist/mod.js +5 -5
- package/dist/nodeinfo/client.test.mjs +432 -5
- package/dist/nodeinfo/handler.test.mjs +1 -1
- package/dist/nodeinfo/mod.cjs +1 -1
- package/dist/nodeinfo/mod.d.cts +1 -1
- package/dist/nodeinfo/mod.d.ts +1 -1
- package/dist/nodeinfo/mod.js +1 -1
- package/dist/nodeinfo/types.test.mjs +1 -1
- package/dist/{owner-CFHq61cq.mjs → owner-DwyDorZI.mjs} +2 -2
- package/dist/{proof-0JkSC_pT.cjs → proof-Be-4K4jn.cjs} +193 -46
- package/dist/{proof-KSHlbPCG.mjs → proof-DYjmyrHh.mjs} +38 -13
- package/dist/{proof-DzksJFK7.js → proof-Du4-5SHY.js} +187 -46
- package/dist/{send-DaDe2YCb.mjs → send-BGSfQkIK.mjs} +92 -21
- package/dist/sig/http.test.mjs +2 -2
- package/dist/sig/key-types.test.d.mts +218 -0
- package/dist/sig/key-types.test.mjs +8 -0
- package/dist/sig/key.test.mjs +2 -2
- package/dist/sig/ld.test.mjs +573 -8
- package/dist/sig/mod.cjs +2 -2
- package/dist/sig/mod.d.cts +3 -3
- package/dist/sig/mod.d.ts +3 -3
- package/dist/sig/mod.js +2 -2
- package/dist/sig/owner.test.mjs +1 -1
- package/dist/sig/proof.test.mjs +95 -3
- package/dist/{temporal-DNGH8o2E.mjs → temporal-CNCchWly.mjs} +3 -5
- package/dist/testing/mod.d.mts +2 -2203
- package/dist/{types-DOvyymM4.cjs → types-B5wRhemi.cjs} +85 -10
- package/dist/{types-B1_pRFE_.js → types-CXy2lRVU.js} +86 -11
- package/dist/utils/docloader.test.mjs +2 -2
- package/dist/utils/mod.cjs +1 -1
- package/dist/utils/mod.d.cts +1 -1
- package/dist/utils/mod.d.ts +1 -1
- package/dist/utils/mod.js +1 -1
- package/package.json +6 -6
- /package/dist/{collection-BD6-SZ6O.mjs → collection-DnoYNwBC.mjs} +0 -0
- /package/dist/{negotiation-BehA2uul.mjs → negotiation-xb0QR3u_.mjs} +0 -0
- /package/dist/{retry-B_E3V_Dx.mjs → retry-CI1flGDD.mjs} +0 -0
- /package/dist/{types-DCP0WLdt.mjs → types-s4mCTGkT.mjs} +0 -0
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { Temporal } from "@js-temporal/polyfill";
|
|
2
2
|
import { URLPattern } from "urlpattern-polyfill";
|
|
3
|
-
import { c as fetchKey, f as validateCryptoKey, o as exportJwk, p as verifyKeyOwnership, s as fetchActorDocument, v as name, y as version } from "./http-
|
|
3
|
+
import { c as fetchKey, f as validateCryptoKey, o as exportJwk, p as verifyKeyOwnership, s as fetchActorDocument, v as name, y as version } from "./http-BtbMAgTA.js";
|
|
4
4
|
import { getLogger } from "@logtape/logtape";
|
|
5
5
|
import { Activity, CryptographicKey, DataIntegrityProof, Multikey, Object as Object$1, getTypeId, isActor } from "@fedify/vocab";
|
|
6
6
|
import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
|
|
7
7
|
import { encodeHex } from "byte-encodings/hex";
|
|
8
|
-
import { getDocumentLoader } from "@fedify/vocab-runtime";
|
|
9
|
-
import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
|
|
8
|
+
import { getDocumentLoader, preloadedContexts } from "@fedify/vocab-runtime";
|
|
10
9
|
import jsonld from "@fedify/vocab-runtime/jsonld";
|
|
10
|
+
import { asCryptoKey } from "@fedify/vocab-runtime/internal";
|
|
11
|
+
import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
|
|
11
12
|
import serialize from "json-canon";
|
|
12
13
|
//#region src/sig/ld.ts
|
|
13
14
|
const logger$2 = getLogger([
|
|
@@ -297,24 +298,110 @@ function assertSafeJsonLd(jsonLd) {
|
|
|
297
298
|
}
|
|
298
299
|
}
|
|
299
300
|
}
|
|
301
|
+
const signatureTermDefinition = { "signature": "https://w3id.org/security#signature" };
|
|
302
|
+
const signatureDefiningContexts = new Set(["https://w3id.org/identity/v1", "https://w3id.org/security/v1"]);
|
|
303
|
+
const signatureFreeContexts = getSignatureFreeContexts();
|
|
304
|
+
function getSignatureFreeContexts() {
|
|
305
|
+
const urls = /* @__PURE__ */ new Set();
|
|
306
|
+
for (const [url, document] of globalThis.Object.entries(preloadedContexts)) {
|
|
307
|
+
if (typeof document !== "object" || document == null) continue;
|
|
308
|
+
const context = document["@context"];
|
|
309
|
+
if (typeof context !== "object" || context == null || Array.isArray(context)) continue;
|
|
310
|
+
const record = context;
|
|
311
|
+
if (!globalThis.Object.prototype.hasOwnProperty.call(record, "signature")) urls.add(url);
|
|
312
|
+
}
|
|
313
|
+
return urls;
|
|
314
|
+
}
|
|
315
|
+
function getContextEntryEffect(entry) {
|
|
316
|
+
if (entry == null) return "resets";
|
|
317
|
+
if (typeof entry === "string") {
|
|
318
|
+
if (signatureDefiningContexts.has(entry)) return "defines";
|
|
319
|
+
if (signatureFreeContexts.has(entry)) return "absent";
|
|
320
|
+
return "unknown";
|
|
321
|
+
}
|
|
322
|
+
if (typeof entry !== "object") return "absent";
|
|
323
|
+
const record = entry;
|
|
324
|
+
if (globalThis.Object.prototype.hasOwnProperty.call(record, "signature")) {
|
|
325
|
+
const definition = record["signature"];
|
|
326
|
+
const contextProtected = record["@protected"] === true;
|
|
327
|
+
const imported = globalThis.Object.prototype.hasOwnProperty.call(record, "@import");
|
|
328
|
+
if (definition == null) {
|
|
329
|
+
if (contextProtected) return "defines";
|
|
330
|
+
return imported ? "unknown" : "resets";
|
|
331
|
+
}
|
|
332
|
+
if (typeof definition === "object" && !Array.isArray(definition)) {
|
|
333
|
+
const term = definition;
|
|
334
|
+
const protectedTerm = contextProtected || term["@protected"] === true;
|
|
335
|
+
if (globalThis.Object.prototype.hasOwnProperty.call(term, "@id") && term["@id"] == null) {
|
|
336
|
+
if (protectedTerm) return "defines";
|
|
337
|
+
return imported ? "unknown" : "resets";
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return "defines";
|
|
341
|
+
}
|
|
342
|
+
if (globalThis.Object.prototype.hasOwnProperty.call(record, "@import")) return "unknown";
|
|
343
|
+
return "absent";
|
|
344
|
+
}
|
|
345
|
+
function getSignatureTermState(context) {
|
|
346
|
+
if (context == null) return "absent";
|
|
347
|
+
if (!Array.isArray(context)) {
|
|
348
|
+
const effect = getContextEntryEffect(context);
|
|
349
|
+
return effect === "defines" ? "defined" : effect === "unknown" ? "unknown" : "absent";
|
|
350
|
+
}
|
|
351
|
+
let state = "absent";
|
|
352
|
+
for (const entry of context) {
|
|
353
|
+
const effect = getContextEntryEffect(entry);
|
|
354
|
+
if (effect === "defines") state = "defined";
|
|
355
|
+
else if (effect === "resets") state = "absent";
|
|
356
|
+
else if (effect === "unknown" && state !== "defined") state = "unknown";
|
|
357
|
+
}
|
|
358
|
+
return state;
|
|
359
|
+
}
|
|
360
|
+
function prepareSignatureDocument(jsonLd) {
|
|
361
|
+
if (typeof jsonLd !== "object" || jsonLd == null || Array.isArray(jsonLd)) return jsonLd;
|
|
362
|
+
const document = jsonLd;
|
|
363
|
+
if (document["proof"] != null) return { ...document };
|
|
364
|
+
const context = document["@context"];
|
|
365
|
+
if (getSignatureTermState(context) !== "absent") return { ...document };
|
|
366
|
+
const definition = { ...signatureTermDefinition };
|
|
367
|
+
return {
|
|
368
|
+
...document,
|
|
369
|
+
"@context": context == null ? [definition] : Array.isArray(context) ? [...context, definition] : [context, definition]
|
|
370
|
+
};
|
|
371
|
+
}
|
|
300
372
|
/**
|
|
301
373
|
* Attaches a LD signature to the given JSON-LD document.
|
|
374
|
+
*
|
|
375
|
+
* When the document's context does not already define the `signature`
|
|
376
|
+
* property, the returned document adds a minimal inline context for it.
|
|
377
|
+
* Without that definition, expanding the attached signature would produce a
|
|
378
|
+
* blank node predicate, which is not valid RDF. Documents that already carry
|
|
379
|
+
* a `proof` are left unchanged, since a new context definition would invalidate
|
|
380
|
+
* the proof. Documents whose context includes an unrecognized remote context
|
|
381
|
+
* are also left unchanged, since that context may already define `signature`.
|
|
302
382
|
* @param jsonLd The JSON-LD document to attach the signature to. It is not
|
|
303
383
|
* modified.
|
|
304
384
|
* @param signature The signature to attach.
|
|
305
385
|
* @returns The JSON-LD document with the attached signature.
|
|
306
386
|
* @throws {TypeError} If the input document is not a valid JSON-LD document.
|
|
387
|
+
* Top-level arrays are rejected, since a Linked Data
|
|
388
|
+
* Signature is attached to a single node.
|
|
307
389
|
* @since 1.0.0
|
|
308
390
|
*/
|
|
309
391
|
function attachSignature(jsonLd, signature) {
|
|
310
|
-
if (typeof jsonLd !== "object" || jsonLd == null) throw new TypeError("Failed to attach signature; invalid JSON-LD document.");
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
};
|
|
392
|
+
if (typeof jsonLd !== "object" || jsonLd == null || Array.isArray(jsonLd)) throw new TypeError("Failed to attach signature; invalid JSON-LD document.");
|
|
393
|
+
const output = prepareSignatureDocument(jsonLd);
|
|
394
|
+
output.signature = signature;
|
|
395
|
+
return output;
|
|
315
396
|
}
|
|
316
397
|
/**
|
|
317
398
|
* Creates a LD signature for the given JSON-LD document.
|
|
399
|
+
*
|
|
400
|
+
* If the document's context does not define the `signature` property, the
|
|
401
|
+
* signature is computed over the document with a minimal inline definition
|
|
402
|
+
* added. Use {@link attachSignature} to attach the signature to that exact
|
|
403
|
+
* document. Documents that already carry a `proof`, or whose context includes
|
|
404
|
+
* an unrecognized remote context, are hashed unchanged.
|
|
318
405
|
* @param jsonLd The JSON-LD document to sign.
|
|
319
406
|
* @param privateKey The private key to sign the document.
|
|
320
407
|
* @param keyId The ID of the public key that corresponds to the private key.
|
|
@@ -328,14 +415,17 @@ async function createSignature(jsonLd, privateKey, keyId, { contextLoader, creat
|
|
|
328
415
|
validateCryptoKey(privateKey, "private");
|
|
329
416
|
if (privateKey.algorithm.name !== "RSASSA-PKCS1-v1_5") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
|
|
330
417
|
const options = {
|
|
331
|
-
"@context": "https://w3id.org/identity/v1",
|
|
332
418
|
creator: keyId.href,
|
|
333
419
|
created: created?.toString() ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
334
420
|
};
|
|
335
|
-
const message = await hashJsonLd(
|
|
421
|
+
const message = await hashJsonLd({
|
|
422
|
+
"@context": "https://w3id.org/identity/v1",
|
|
423
|
+
...options
|
|
424
|
+
}, contextLoader) + await hashJsonLd(prepareSignatureDocument(jsonLd), contextLoader);
|
|
336
425
|
const messageBytes = new TextEncoder().encode(message);
|
|
337
|
-
const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", privateKey, messageBytes);
|
|
426
|
+
const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", asCryptoKey(privateKey), messageBytes);
|
|
338
427
|
return {
|
|
428
|
+
"@context": "https://w3id.org/security/v1",
|
|
339
429
|
...options,
|
|
340
430
|
type: "RsaSignature2017",
|
|
341
431
|
signatureValue: encodeBase64(signature)
|
|
@@ -374,20 +464,23 @@ async function signJsonLd(jsonLd, privateKey, keyId, options) {
|
|
|
374
464
|
}
|
|
375
465
|
});
|
|
376
466
|
}
|
|
377
|
-
/**
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
467
|
+
/** @internal */
|
|
468
|
+
function hasVerifiableSignature(jsonLd) {
|
|
469
|
+
if (typeof jsonLd !== "object" || jsonLd == null || Array.isArray(jsonLd) || !("signature" in jsonLd)) return false;
|
|
470
|
+
const signature = jsonLd.signature;
|
|
471
|
+
if (typeof signature !== "object" || signature == null || Array.isArray(signature)) return false;
|
|
472
|
+
return "type" in signature && signature.type === "RsaSignature2017" && "creator" in signature && typeof signature.creator === "string" && (!("created" in signature) || typeof signature.created === "string") && "signatureValue" in signature && typeof signature.signatureValue === "string";
|
|
473
|
+
}
|
|
474
|
+
/** @internal */
|
|
475
|
+
function describeSignatureShape(jsonLd) {
|
|
476
|
+
if (typeof jsonLd !== "object" || jsonLd == null || !("signature" in jsonLd)) return null;
|
|
477
|
+
const signature = jsonLd.signature;
|
|
478
|
+
const properties = typeof signature === "object" && signature != null ? globalThis.Object.keys(signature) : [];
|
|
479
|
+
return {
|
|
480
|
+
signatureKind: signature == null ? String(signature) : Array.isArray(signature) ? "array" : typeof signature,
|
|
481
|
+
signatureProperties: properties.sort().slice(0, 20).map((p) => p.slice(0, 80)),
|
|
482
|
+
signaturePropertyCount: properties.length
|
|
483
|
+
};
|
|
391
484
|
}
|
|
392
485
|
/**
|
|
393
486
|
* Detaches Linked Data Signatures from the given JSON-LD document.
|
|
@@ -404,6 +497,9 @@ function detachSignature(jsonLd) {
|
|
|
404
497
|
}
|
|
405
498
|
/**
|
|
406
499
|
* Verifies Linked Data Signatures of the given JSON-LD document.
|
|
500
|
+
* Unmapped properties discarded during JSON-LD processing are tolerated, but
|
|
501
|
+
* their values are not authenticated by the signature. Other unsafe
|
|
502
|
+
* canonicalization warnings, including relative IRIs, remain rejected.
|
|
407
503
|
* @param jsonLd The JSON-LD document to verify.
|
|
408
504
|
* @param options Options for verifying the signature.
|
|
409
505
|
* @returns The public key that signed the document or `null` if the signature
|
|
@@ -411,7 +507,11 @@ function detachSignature(jsonLd) {
|
|
|
411
507
|
* @since 1.0.0
|
|
412
508
|
*/
|
|
413
509
|
async function verifySignature(jsonLd, options = {}) {
|
|
414
|
-
if (!
|
|
510
|
+
if (!hasVerifiableSignature(jsonLd)) {
|
|
511
|
+
const shape = describeSignatureShape(jsonLd);
|
|
512
|
+
if (shape != null) logger$2.debug("Linked Data Signature has an unsupported shape: {signatureKind}; properties: {signatureProperties}.", shape);
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
415
515
|
const sig = jsonLd.signature;
|
|
416
516
|
let signature;
|
|
417
517
|
try {
|
|
@@ -423,7 +523,14 @@ async function verifySignature(jsonLd, options = {}) {
|
|
|
423
523
|
});
|
|
424
524
|
return null;
|
|
425
525
|
}
|
|
426
|
-
|
|
526
|
+
let creator;
|
|
527
|
+
try {
|
|
528
|
+
creator = new URL(sig.creator);
|
|
529
|
+
} catch {
|
|
530
|
+
logger$2.debug("Failed to verify; signature creator is not a valid URL.");
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
const { key, cached } = await fetchKey(creator, CryptographicKey, options);
|
|
427
534
|
if (key == null) return null;
|
|
428
535
|
const sigOpts = {
|
|
429
536
|
...sig,
|
|
@@ -434,7 +541,7 @@ async function verifySignature(jsonLd, options = {}) {
|
|
|
434
541
|
delete sigOpts.signatureValue;
|
|
435
542
|
let sigOptsHash;
|
|
436
543
|
try {
|
|
437
|
-
sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
|
|
544
|
+
sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader, "verify");
|
|
438
545
|
} catch (error) {
|
|
439
546
|
logger$2.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
|
|
440
547
|
signatureOptions: sigOpts,
|
|
@@ -446,7 +553,7 @@ async function verifySignature(jsonLd, options = {}) {
|
|
|
446
553
|
delete document.signature;
|
|
447
554
|
let docHash;
|
|
448
555
|
try {
|
|
449
|
-
docHash = await hashJsonLd(document, options.contextLoader);
|
|
556
|
+
docHash = await hashJsonLd(document, options.contextLoader, "verify");
|
|
450
557
|
} catch (error) {
|
|
451
558
|
logger$2.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
|
|
452
559
|
document,
|
|
@@ -463,7 +570,7 @@ async function verifySignature(jsonLd, options = {}) {
|
|
|
463
570
|
keyId: sig.creator,
|
|
464
571
|
...sig
|
|
465
572
|
});
|
|
466
|
-
const { key } = await fetchKey(
|
|
573
|
+
const { key } = await fetchKey(creator, CryptographicKey, {
|
|
467
574
|
...options,
|
|
468
575
|
keyCache: {
|
|
469
576
|
get: () => Promise.resolve(void 0),
|
|
@@ -485,6 +592,8 @@ async function verifySignature(jsonLd, options = {}) {
|
|
|
485
592
|
* Signatures. If the document is signed, this function verifies the signature
|
|
486
593
|
* and checks if the document is attributed to the owner of the public key.
|
|
487
594
|
* If the document is not signed, this function returns `false`.
|
|
595
|
+
* Unmapped properties discarded during JSON-LD processing are not
|
|
596
|
+
* authenticated, even when verification succeeds.
|
|
488
597
|
* @param jsonLd The JSON-LD document to verify.
|
|
489
598
|
* @param options Options for verifying the document.
|
|
490
599
|
* @returns `true` if the document is authentic; `false` otherwise.
|
|
@@ -499,11 +608,12 @@ async function verifyCompactJsonLd(jsonLd, options = {}) {
|
|
|
499
608
|
async function verifyJsonLdInternal(jsonLd, options, compact) {
|
|
500
609
|
return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("ld_signatures.verify", async (span) => {
|
|
501
610
|
try {
|
|
502
|
-
const
|
|
611
|
+
const hasLdSignature = hasVerifiableSignature(jsonLd);
|
|
612
|
+
const verificationOptions = hasLdSignature ? {
|
|
503
613
|
...options,
|
|
504
614
|
contextLoader: getNormalizationContextLoader(options.contextLoader)
|
|
505
615
|
} : options;
|
|
506
|
-
const compacted = compact ?
|
|
616
|
+
const compacted = compact ? hasLdSignature ? await compactJsonLd(jsonLd, options.contextLoader) : jsonLd : jsonLd;
|
|
507
617
|
const object = await Object$1.fromJsonLd(compacted, verificationOptions);
|
|
508
618
|
if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
|
|
509
619
|
span.setAttribute("activitypub.object.type", getTypeId(object).href);
|
|
@@ -537,10 +647,17 @@ async function verifyJsonLdInternal(jsonLd, options, compact) {
|
|
|
537
647
|
}
|
|
538
648
|
});
|
|
539
649
|
}
|
|
540
|
-
|
|
650
|
+
const tolerableVerificationWarnings = new Set(["blank node predicate", "invalid property"]);
|
|
651
|
+
function handleVerificationEvent({ event, next }) {
|
|
652
|
+
if (event.level === "warning" && !tolerableVerificationWarnings.has(event.code)) throw new TypeError(`Unsafe JSON-LD canonicalization warning: ${event.code}`);
|
|
653
|
+
next();
|
|
654
|
+
}
|
|
655
|
+
async function hashJsonLd(jsonLd, contextLoader, mode = "sign") {
|
|
541
656
|
const canon = await jsonld.canonize(jsonLd, {
|
|
542
657
|
format: "application/n-quads",
|
|
543
|
-
documentLoader: contextLoader ?? getDocumentLoader()
|
|
658
|
+
documentLoader: contextLoader ?? getDocumentLoader(),
|
|
659
|
+
safe: mode === "sign",
|
|
660
|
+
eventHandler: mode === "verify" ? handleVerificationEvent : void 0
|
|
544
661
|
});
|
|
545
662
|
const encoder = new TextEncoder();
|
|
546
663
|
return encodeHex(await crypto.subtle.digest("SHA-256", encoder.encode(canon)));
|
|
@@ -727,7 +844,10 @@ const logger = getLogger([
|
|
|
727
844
|
* @throws {TypeError} If the private key is invalid or unsupported.
|
|
728
845
|
* @since 0.10.0
|
|
729
846
|
*/
|
|
730
|
-
async function createProof(object, privateKey, keyId,
|
|
847
|
+
async function createProof(object, privateKey, keyId, options = {}) {
|
|
848
|
+
return (await createProofInternal(object, privateKey, keyId, options)).proof;
|
|
849
|
+
}
|
|
850
|
+
async function createProofInternal(object, privateKey, keyId, { contextLoader, context, created } = {}) {
|
|
731
851
|
validateCryptoKey(privateKey, "private");
|
|
732
852
|
if (privateKey.algorithm.name !== "Ed25519") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
|
|
733
853
|
const compactMsg = await object.clone({ proofs: [] }).toJsonLd({
|
|
@@ -753,14 +873,26 @@ async function createProof(object, privateKey, keyId, { contextLoader, context,
|
|
|
753
873
|
const digest = new Uint8Array(proofDigest.byteLength + msgDigest.byteLength);
|
|
754
874
|
digest.set(new Uint8Array(proofDigest), 0);
|
|
755
875
|
digest.set(new Uint8Array(msgDigest), proofDigest.byteLength);
|
|
756
|
-
const sig = await crypto.subtle.sign("Ed25519", privateKey, digest);
|
|
757
|
-
return
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
876
|
+
const sig = await crypto.subtle.sign("Ed25519", asCryptoKey(privateKey), digest);
|
|
877
|
+
return {
|
|
878
|
+
proof: new DataIntegrityProof({
|
|
879
|
+
cryptosuite: "eddsa-jcs-2022",
|
|
880
|
+
verificationMethod: keyId,
|
|
881
|
+
proofPurpose: "assertionMethod",
|
|
882
|
+
created: created ?? Temporal.Now.instant(),
|
|
883
|
+
proofValue: new Uint8Array(sig)
|
|
884
|
+
}),
|
|
885
|
+
jsonLd: compactMsg
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
function getJsonLdContextKey(value) {
|
|
889
|
+
const context = typeof value === "object" && value != null && !Array.isArray(value) ? value["@context"] ?? null : null;
|
|
890
|
+
return JSON.stringify(sortJsonKeys(context));
|
|
891
|
+
}
|
|
892
|
+
function sortJsonKeys(value) {
|
|
893
|
+
if (Array.isArray(value)) return value.map(sortJsonKeys);
|
|
894
|
+
if (typeof value !== "object" || value == null) return value;
|
|
895
|
+
return globalThis.Object.fromEntries(globalThis.Object.entries(value).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([key, item]) => [key, sortJsonKeys(item)]));
|
|
764
896
|
}
|
|
765
897
|
/**
|
|
766
898
|
* Signs the given object with the private key and returns the signed object.
|
|
@@ -776,9 +908,18 @@ async function signObject(object, privateKey, keyId, options = {}) {
|
|
|
776
908
|
return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("object_integrity_proofs.sign", { attributes: { "activitypub.object.type": getTypeId(object).href } }, async (span) => {
|
|
777
909
|
try {
|
|
778
910
|
if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
|
|
911
|
+
const { proof, jsonLd } = await createProofInternal(object, privateKey, keyId, options);
|
|
779
912
|
const existingProofs = [];
|
|
780
|
-
|
|
781
|
-
|
|
913
|
+
let hasProofs = object.proofIds.length > 0;
|
|
914
|
+
if (!hasProofs) for await (const _ of object.getProofs(options)) {
|
|
915
|
+
hasProofs = true;
|
|
916
|
+
break;
|
|
917
|
+
}
|
|
918
|
+
if (hasProofs) {
|
|
919
|
+
let sameContext = true;
|
|
920
|
+
if (options.context == null) sameContext = getJsonLdContextKey(await object.toJsonLd({ contextLoader: options.contextLoader })) === getJsonLdContextKey(jsonLd);
|
|
921
|
+
if (sameContext) for await (const existingProof of object.getProofs(options)) existingProofs.push(existingProof);
|
|
922
|
+
}
|
|
782
923
|
if (span.isRecording()) {
|
|
783
924
|
if (proof.cryptosuite != null) span.setAttribute("object_integrity_proofs.cryptosuite", proof.cryptosuite);
|
|
784
925
|
if (proof.verificationMethodId != null) span.setAttribute("object_integrity_proofs.key_id", proof.verificationMethodId.href);
|
|
@@ -950,4 +1091,4 @@ async function verifyObject(cls, jsonLd, options = {}) {
|
|
|
950
1091
|
return object;
|
|
951
1092
|
}
|
|
952
1093
|
//#endregion
|
|
953
|
-
export {
|
|
1094
|
+
export { wrapContextLoaderForJsonLd as S, isInvalidUrlTypeError as _, doesActorOwnKey as a, verifyJsonLd as b, assertSafeJsonLd as c, createSignature as d, describeSignatureShape as f, isClearlyMalformedContextReference as g, hasVerifiableSignature as h, verifyProof as i, attachSignature as l, getNormalizationContextLoader as m, signObject as n, getKeyOwner as o, detachSignature as p, verifyObject as r, InvalidContextReferenceError as s, createProof as t, compactJsonLd as u, signJsonLd as v, verifySignature as x, verifyCompactJsonLd as y };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import "@js-temporal/polyfill";
|
|
2
2
|
import "urlpattern-polyfill";
|
|
3
3
|
globalThis.addEventListener = () => {};
|
|
4
|
-
import { n as version, t as name } from "./deno-
|
|
5
|
-
import { r as doubleKnockScoped } from "./http-
|
|
4
|
+
import { n as version, t as name } from "./deno-L23b3Hba.mjs";
|
|
5
|
+
import { r as doubleKnockScoped } from "./http-HX6wVyJh.mjs";
|
|
6
6
|
import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
|
|
7
7
|
import { FetchError, UrlError } from "@fedify/vocab-runtime";
|
|
8
8
|
import { openDirectFetch, openPublicFetch } from "@fedify/vocab-runtime/internal";
|
|
@@ -40,7 +40,8 @@ function extractInboxes({ recipients, preferSharedInbox, excludeBaseUris }) {
|
|
|
40
40
|
* @param parameters The parameters for sending the activity.
|
|
41
41
|
* See also {@link SendActivityParameters}.
|
|
42
42
|
* @param deps The dependencies, for tests.
|
|
43
|
-
* @throws {
|
|
43
|
+
* @throws {FetchError} If the delivery times out before receiving a response.
|
|
44
|
+
* @throws {SendActivityError} If the inbox returns an unsuccessful response.
|
|
44
45
|
*/
|
|
45
46
|
function sendActivity(options, deps = {}) {
|
|
46
47
|
const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
|
|
@@ -67,7 +68,25 @@ function sendActivity(options, deps = {}) {
|
|
|
67
68
|
});
|
|
68
69
|
}
|
|
69
70
|
const MAX_ERROR_RESPONSE_BODY_BYTES = 1024;
|
|
70
|
-
|
|
71
|
+
const SEND_ACTIVITY_TIMEOUT_MS = 3e4;
|
|
72
|
+
function raceWithAbort(promise, signal) {
|
|
73
|
+
if (signal.aborted) return Promise.reject(signal.reason);
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const onAbort = () => {
|
|
76
|
+
signal.removeEventListener("abort", onAbort);
|
|
77
|
+
reject(signal.reason);
|
|
78
|
+
};
|
|
79
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
80
|
+
promise.then((value) => {
|
|
81
|
+
signal.removeEventListener("abort", onAbort);
|
|
82
|
+
resolve(value);
|
|
83
|
+
}, (error) => {
|
|
84
|
+
signal.removeEventListener("abort", onAbort);
|
|
85
|
+
reject(error);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
async function readLimitedResponseBody(response, maxBytes, signal) {
|
|
71
90
|
if (response.body == null) return "";
|
|
72
91
|
const reader = response.body.getReader();
|
|
73
92
|
const decoder = new TextDecoder();
|
|
@@ -76,7 +95,8 @@ async function readLimitedResponseBody(response, maxBytes) {
|
|
|
76
95
|
let truncated = false;
|
|
77
96
|
try {
|
|
78
97
|
while (true) {
|
|
79
|
-
|
|
98
|
+
signal.throwIfAborted();
|
|
99
|
+
const { done, value } = await raceWithAbort(reader.read(), signal);
|
|
80
100
|
if (done) break;
|
|
81
101
|
if (totalBytes + value.length > maxBytes) {
|
|
82
102
|
const remaining = maxBytes - totalBytes;
|
|
@@ -88,8 +108,10 @@ async function readLimitedResponseBody(response, maxBytes) {
|
|
|
88
108
|
totalBytes += value.length;
|
|
89
109
|
}
|
|
90
110
|
} finally {
|
|
91
|
-
if (truncated) reader.cancel().catch(() => {});
|
|
92
|
-
|
|
111
|
+
if (truncated || signal.aborted) reader.cancel().catch(() => {});
|
|
112
|
+
try {
|
|
113
|
+
reader.releaseLock();
|
|
114
|
+
} catch (_) {}
|
|
93
115
|
}
|
|
94
116
|
let result = chunks.join("");
|
|
95
117
|
if (truncated) result += "… (truncated)";
|
|
@@ -101,12 +123,24 @@ async function sendActivityInternal({ activity, activityId, keys, inbox, allowPr
|
|
|
101
123
|
"federation",
|
|
102
124
|
"outbox"
|
|
103
125
|
]);
|
|
104
|
-
const
|
|
126
|
+
const timeoutMs = deps.timeoutMs ?? SEND_ACTIVITY_TIMEOUT_MS;
|
|
127
|
+
const controller = new AbortController();
|
|
128
|
+
const { signal } = controller;
|
|
129
|
+
const openUnvalidatedHop = deps.openHop ?? (allowPrivateAddress ? openDirectFetch : (url) => openPublicFetch(url, { signal }));
|
|
105
130
|
let opened = false;
|
|
106
131
|
async function openHop(url) {
|
|
107
132
|
let session;
|
|
108
133
|
try {
|
|
109
|
-
|
|
134
|
+
signal.throwIfAborted();
|
|
135
|
+
const opening = openUnvalidatedHop(url);
|
|
136
|
+
opening.then((lateSession) => {
|
|
137
|
+
if (signal.aborted) closeAfterError(lateSession, logger);
|
|
138
|
+
}, () => {});
|
|
139
|
+
session = await raceWithAbort(opening, signal);
|
|
140
|
+
if (signal.aborted) {
|
|
141
|
+
closeAfterError(session, logger);
|
|
142
|
+
signal.throwIfAborted();
|
|
143
|
+
}
|
|
110
144
|
} catch (error) {
|
|
111
145
|
if (error instanceof UrlError && error.reason === "dns") {
|
|
112
146
|
logger.error("DNS resolution failed for URL: {url}", {
|
|
@@ -132,7 +166,8 @@ async function sendActivityInternal({ activity, activityId, keys, inbox, allowPr
|
|
|
132
166
|
method: "POST",
|
|
133
167
|
headers,
|
|
134
168
|
body: JSON.stringify(activity),
|
|
135
|
-
redirect: "manual"
|
|
169
|
+
redirect: "manual",
|
|
170
|
+
signal
|
|
136
171
|
});
|
|
137
172
|
let rsaKey = null;
|
|
138
173
|
for (const key of keys) if (key.privateKey.algorithm.name === "RSASSA-PKCS1-v1_5") {
|
|
@@ -147,18 +182,48 @@ async function sendActivityInternal({ activity, activityId, keys, inbox, allowPr
|
|
|
147
182
|
}))
|
|
148
183
|
});
|
|
149
184
|
let outcome;
|
|
185
|
+
const boundedSpecDeterminer = specDeterminer == null ? void 0 : {
|
|
186
|
+
determineSpec(origin) {
|
|
187
|
+
signal.throwIfAborted();
|
|
188
|
+
return raceWithAbort(Promise.resolve(specDeterminer.determineSpec(origin)), signal);
|
|
189
|
+
},
|
|
190
|
+
rememberSpec(origin, spec) {
|
|
191
|
+
if (signal.aborted) return;
|
|
192
|
+
try {
|
|
193
|
+
Promise.resolve(specDeterminer.rememberSpec(origin, spec)).catch((error) => logger.debug("Failed to remember the signature spec for {origin}: {error}", {
|
|
194
|
+
origin,
|
|
195
|
+
error
|
|
196
|
+
}));
|
|
197
|
+
} catch (error) {
|
|
198
|
+
logger.debug("Failed to remember the signature spec for {origin}: {error}", {
|
|
199
|
+
origin,
|
|
200
|
+
error
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const timer = setTimeout(() => controller.abort(new DOMException("The delivery timed out.", "TimeoutError")), timeoutMs);
|
|
150
206
|
try {
|
|
151
|
-
outcome = rsaKey == null ? await fetchWithValidatedRedirects(request, openHop, (response) => consumeSendActivityResponse(response, inbox)) : await doubleKnockScoped(request, rsaKey, {
|
|
207
|
+
outcome = rsaKey == null ? await fetchWithValidatedRedirects(request, openHop, (response) => consumeSendActivityResponse(response, inbox, signal), signal) : await doubleKnockScoped(request, rsaKey, {
|
|
152
208
|
tracerProvider,
|
|
153
|
-
specDeterminer
|
|
154
|
-
|
|
209
|
+
specDeterminer: boundedSpecDeterminer,
|
|
210
|
+
signal
|
|
211
|
+
}, openHop, (response) => consumeSendActivityResponse(response, inbox, signal));
|
|
155
212
|
} catch (error) {
|
|
156
|
-
|
|
213
|
+
let failureError = error;
|
|
214
|
+
if (signal.aborted && !(error instanceof SendActivityError)) {
|
|
215
|
+
const failure = new FetchError(inbox.href, `Timed out after ${timeoutMs} ms`);
|
|
216
|
+
failure.cause = signal.reason;
|
|
217
|
+
failureError = failure;
|
|
218
|
+
}
|
|
219
|
+
if (opened || signal.aborted) logger.error("Failed to send activity {activityId} to {inbox}:\n{error}", {
|
|
157
220
|
activityId,
|
|
158
221
|
inbox: inbox.href,
|
|
159
|
-
error
|
|
222
|
+
error: failureError
|
|
160
223
|
});
|
|
161
|
-
throw
|
|
224
|
+
throw failureError;
|
|
225
|
+
} finally {
|
|
226
|
+
clearTimeout(timer);
|
|
162
227
|
}
|
|
163
228
|
if (!outcome.ok) {
|
|
164
229
|
const { status, statusText, error } = outcome;
|
|
@@ -200,17 +265,18 @@ function cancelResponseBody(response, url) {
|
|
|
200
265
|
* session is open: the body of a successful response is cancelled, and at
|
|
201
266
|
* most 1 KiB of an error response body is read.
|
|
202
267
|
*/
|
|
203
|
-
async function consumeSendActivityResponse(response, inbox) {
|
|
268
|
+
async function consumeSendActivityResponse(response, inbox, signal) {
|
|
204
269
|
if (response.ok) {
|
|
205
270
|
cancelResponseBody(response, response.url || inbox.href);
|
|
206
271
|
return { ok: true };
|
|
207
272
|
}
|
|
208
273
|
let error;
|
|
209
274
|
try {
|
|
210
|
-
error = await readLimitedResponseBody(response, MAX_ERROR_RESPONSE_BODY_BYTES);
|
|
275
|
+
error = await readLimitedResponseBody(response, MAX_ERROR_RESPONSE_BODY_BYTES, signal);
|
|
211
276
|
} catch (_) {
|
|
212
277
|
error = "";
|
|
213
278
|
}
|
|
279
|
+
if (signal.aborted) error = "(timed out reading response body)";
|
|
214
280
|
return {
|
|
215
281
|
ok: false,
|
|
216
282
|
status: response.status,
|
|
@@ -229,7 +295,7 @@ async function consumeSendActivityResponse(response, inbox) {
|
|
|
229
295
|
* @param consume The function to consume the final response.
|
|
230
296
|
* @returns The value returned by `consume`.
|
|
231
297
|
*/
|
|
232
|
-
async function fetchWithValidatedRedirects(request, openHop, consume) {
|
|
298
|
+
async function fetchWithValidatedRedirects(request, openHop, consume, signal) {
|
|
233
299
|
const logger = getLogger([
|
|
234
300
|
"fedify",
|
|
235
301
|
"federation",
|
|
@@ -237,10 +303,14 @@ async function fetchWithValidatedRedirects(request, openHop, consume) {
|
|
|
237
303
|
]);
|
|
238
304
|
let body = await request.clone().text();
|
|
239
305
|
for (let redirects = 0;; redirects++) {
|
|
306
|
+
signal?.throwIfAborted();
|
|
240
307
|
const session = await openHop(request.url);
|
|
241
308
|
let response;
|
|
242
309
|
try {
|
|
243
|
-
response = await session.fetch(request, {
|
|
310
|
+
response = await session.fetch(request, {
|
|
311
|
+
redirect: "manual",
|
|
312
|
+
signal
|
|
313
|
+
});
|
|
244
314
|
} catch (error) {
|
|
245
315
|
await closeAfterError(session, logger);
|
|
246
316
|
throw error;
|
|
@@ -290,7 +360,8 @@ async function fetchWithValidatedRedirects(request, openHop, consume) {
|
|
|
290
360
|
method,
|
|
291
361
|
headers,
|
|
292
362
|
body,
|
|
293
|
-
redirect: "manual"
|
|
363
|
+
redirect: "manual",
|
|
364
|
+
signal: signal ?? request.signal
|
|
294
365
|
});
|
|
295
366
|
}
|
|
296
367
|
}
|
package/dist/sig/http.test.mjs
CHANGED
|
@@ -7,8 +7,8 @@ import { i as assertExists, t as assertStringIncludes } from "../std__assert-Dfl
|
|
|
7
7
|
import { r as assertFalse, t as assertThrows } from "../assert_throws-D4DNWrux.mjs";
|
|
8
8
|
import { t as assert } from "../assert-BhQd8QHP.mjs";
|
|
9
9
|
import { t as assertRejects } from "../assert_rejects-WyYSTDDX.mjs";
|
|
10
|
-
import { t as exportJwk } from "../key-
|
|
11
|
-
import { a as formatRfc9421SignatureParameters, c as signRequest, d as verifyRequestDetailed, i as formatRfc9421Signature, l as timingSafeEqual, n as doubleKnock, o as parseRfc9421Signature, r as doubleKnockScoped, s as parseRfc9421SignatureInput, t as createRfc9421SignatureBase, u as verifyRequest } from "../http-
|
|
10
|
+
import { t as exportJwk } from "../key-Dai3f5yI.mjs";
|
|
11
|
+
import { a as formatRfc9421SignatureParameters, c as signRequest, d as verifyRequestDetailed, i as formatRfc9421Signature, l as timingSafeEqual, n as doubleKnock, o as parseRfc9421Signature, r as doubleKnockScoped, s as parseRfc9421SignatureInput, t as createRfc9421SignatureBase, u as verifyRequest } from "../http-HX6wVyJh.mjs";
|
|
12
12
|
import { i as rsaPrivateKey2, l as rsaPublicKey5, o as rsaPublicKey1, s as rsaPublicKey2 } from "../keys-CtZLJq76.mjs";
|
|
13
13
|
import { t as KvKeyCache } from "../keycache-DahCaG_m.mjs";
|
|
14
14
|
import { t as MemoryKvStore } from "../kv-HvthQAHr.mjs";
|