@fedify/fedify 2.2.2 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/{builder-Bw2K7xaq.mjs → builder-CaVN56-q.mjs} +2 -2
  2. package/dist/compat/mod.d.cts +1 -1
  3. package/dist/compat/mod.d.ts +1 -1
  4. package/dist/compat/transformers.test.mjs +1 -1
  5. package/dist/{context-BPMgyX7m.d.ts → context-BU-1O90h.d.ts} +48 -6
  6. package/dist/{context-DwkhwUX9.d.cts → context-DVA8wHZ0.d.cts} +48 -6
  7. package/dist/{deno-DCXSCeB3.mjs → deno-DMg4SgCb.mjs} +1 -1
  8. package/dist/{docloader-BqX3RfO-.mjs → docloader-Da15YRxG.mjs} +2 -2
  9. package/dist/federation/builder.test.mjs +1 -1
  10. package/dist/federation/handler.test.mjs +1363 -43
  11. package/dist/federation/idempotency.test.mjs +2 -2
  12. package/dist/federation/middleware.test.mjs +1584 -80
  13. package/dist/federation/mod.cjs +1 -1
  14. package/dist/federation/mod.d.cts +2 -2
  15. package/dist/federation/mod.d.ts +2 -2
  16. package/dist/federation/mod.js +1 -1
  17. package/dist/federation/retry.test.mjs +1 -1
  18. package/dist/federation/send.test.mjs +3 -3
  19. package/dist/federation/temporal.test.d.mts +2 -0
  20. package/dist/federation/temporal.test.mjs +71 -0
  21. package/dist/federation/webfinger.test.mjs +1 -1
  22. package/dist/{http-FZD1s7yO.js → http-BPPaA2uz.js} +1 -1
  23. package/dist/{http-Bslazr8_.mjs → http-C_edJspG.mjs} +2 -2
  24. package/dist/{http-DhcDuRya.cjs → http-Cl0Q2bUO.cjs} +1 -1
  25. package/dist/{key-Dk8-s0bs.mjs → key-BAQuZEU1.mjs} +1 -1
  26. package/dist/{kv-cache-BYBaA6NP.js → kv-cache-C4DGZ_t4.js} +1 -1
  27. package/dist/{kv-cache-Csre9hbs.cjs → kv-cache-DmGi6uC-.cjs} +1 -1
  28. package/dist/ld-tusP_XxG.mjs +573 -0
  29. package/dist/{middleware-DBU9HFil.cjs → middleware-0V-9qj7m.cjs} +399 -73
  30. package/dist/{middleware-BEw9z4kK.js → middleware-Ar1QOOPG.js} +396 -71
  31. package/dist/{middleware-AO-0hf_I.mjs → middleware-D9k0Knum.mjs} +314 -78
  32. package/dist/{middleware-D5H3hLTL.cjs → middleware-OQPBzyvx.cjs} +1 -1
  33. package/dist/{middleware-CDwTn0jH.mjs → middleware-madKLp2f.mjs} +1 -1
  34. package/dist/{mod-CNAHY39V.d.ts → mod-BVt6iTmH.d.ts} +1 -1
  35. package/dist/{mod-Bi6WOdti.d.cts → mod-q-NFLW6B.d.cts} +1 -1
  36. package/dist/mod.cjs +4 -4
  37. package/dist/mod.d.cts +2 -2
  38. package/dist/mod.d.ts +2 -2
  39. package/dist/mod.js +4 -4
  40. package/dist/nodeinfo/handler.test.mjs +1 -1
  41. package/dist/{owner-SWOTNh3d.mjs → owner-DRHNR5YO.mjs} +2 -2
  42. package/dist/{proof-BMoWLd-F.mjs → proof-DLhLRv3m.mjs} +2 -2
  43. package/dist/{proof-e42uuEG9.cjs → proof-DfrItHmh.cjs} +351 -3
  44. package/dist/{proof-BT_TJd2j.js → proof-SQ4cQs3A.js} +298 -4
  45. package/dist/{send-ir10TETC.mjs → send-C7tim5U9.mjs} +2 -2
  46. package/dist/sig/http.test.mjs +2 -2
  47. package/dist/sig/key.test.mjs +1 -1
  48. package/dist/sig/ld.test.mjs +558 -2
  49. package/dist/sig/mod.cjs +2 -2
  50. package/dist/sig/mod.js +2 -2
  51. package/dist/sig/owner.test.mjs +1 -1
  52. package/dist/sig/proof.test.mjs +1 -1
  53. package/dist/temporal-LL61Ddf2.mjs +95 -0
  54. package/dist/testing/mod.d.mts +48 -6
  55. package/dist/utils/docloader.test.mjs +2 -2
  56. package/dist/utils/mod.cjs +1 -1
  57. package/dist/utils/mod.js +1 -1
  58. package/package.json +6 -6
  59. package/dist/ld-BPA4jaBc.mjs +0 -279
  60. /package/dist/{retry-bMXBL97A.mjs → retry-v_sGLH1d.mjs} +0 -0
@@ -0,0 +1,95 @@
1
+ import { Temporal } from "@js-temporal/polyfill";
2
+ import "urlpattern-polyfill";
3
+ globalThis.addEventListener = () => {};
4
+ import { c as getNormalizationContextLoader } from "./ld-tusP_XxG.mjs";
5
+ import jsonld from "@fedify/vocab-runtime/jsonld";
6
+ //#region src/federation/temporal.ts
7
+ function isPlainObject(value) {
8
+ return typeof value === "object" && value != null && !Array.isArray(value);
9
+ }
10
+ function normalizeDateTimeLiteral(value) {
11
+ return value.substring(19).match(/[Z+-]/) ? value : value + "Z";
12
+ }
13
+ function isMalformedDateTimeLiteral(value) {
14
+ if (typeof value !== "string") return false;
15
+ try {
16
+ Temporal.Instant.from(normalizeDateTimeLiteral(value));
17
+ return false;
18
+ } catch {
19
+ return true;
20
+ }
21
+ }
22
+ function isMalformedDurationLiteral(value) {
23
+ if (typeof value !== "string") return false;
24
+ try {
25
+ Temporal.Duration.from(value);
26
+ return false;
27
+ } catch {
28
+ return true;
29
+ }
30
+ }
31
+ const TEMPORAL_DATE_TIME_IRIS = new Set([
32
+ "https://www.w3.org/ns/activitystreams#deleted",
33
+ "https://www.w3.org/ns/activitystreams#endTime",
34
+ "https://www.w3.org/ns/activitystreams#published",
35
+ "https://www.w3.org/ns/activitystreams#startTime",
36
+ "https://www.w3.org/ns/activitystreams#updated",
37
+ "http://purl.org/dc/terms/created",
38
+ "https://w3id.org/security#created"
39
+ ]);
40
+ const TEMPORAL_DURATION_IRIS = new Set(["https://www.w3.org/ns/activitystreams#duration"]);
41
+ const QUESTION_CLOSED_IRI = "https://www.w3.org/ns/activitystreams#closed";
42
+ const XSD_DATE_TIME_IRI = "http://www.w3.org/2001/XMLSchema#dateTime";
43
+ function hasMalformedExpandedDateTimeLiteral(value) {
44
+ if (Array.isArray(value)) return value.some(hasMalformedExpandedDateTimeLiteral);
45
+ return isPlainObject(value) && "@value" in value && isMalformedDateTimeLiteral(value["@value"]);
46
+ }
47
+ function hasMalformedExpandedQuestionClosedLiteral(value) {
48
+ if (Array.isArray(value)) return value.some(hasMalformedExpandedQuestionClosedLiteral);
49
+ if (!isPlainObject(value) || !("@value" in value)) return false;
50
+ const literal = value["@value"];
51
+ if (typeof literal === "boolean") return false;
52
+ if (typeof literal !== "string") return false;
53
+ if (value["@type"] !== XSD_DATE_TIME_IRI) return false;
54
+ if (new Date(literal).toString() === "Invalid Date") return false;
55
+ return isMalformedDateTimeLiteral(literal);
56
+ }
57
+ function hasMalformedExpandedDurationLiteral(value) {
58
+ if (Array.isArray(value)) return value.some(hasMalformedExpandedDurationLiteral);
59
+ return isPlainObject(value) && "@value" in value && isMalformedDurationLiteral(value["@value"]);
60
+ }
61
+ function hasMalformedKnownTemporalLiteralInternal(value, visited) {
62
+ if (Array.isArray(value)) return value.some((item) => hasMalformedKnownTemporalLiteralInternal(item, visited));
63
+ if (!isPlainObject(value)) return false;
64
+ if (visited.has(value)) return false;
65
+ visited.add(value);
66
+ if ("@value" in value) return false;
67
+ for (const [key, child] of Object.entries(value)) {
68
+ if (TEMPORAL_DATE_TIME_IRIS.has(key)) {
69
+ if (hasMalformedExpandedDateTimeLiteral(child)) return true;
70
+ continue;
71
+ }
72
+ if (key === QUESTION_CLOSED_IRI) {
73
+ if (hasMalformedExpandedQuestionClosedLiteral(child)) return true;
74
+ continue;
75
+ }
76
+ if (TEMPORAL_DURATION_IRIS.has(key)) {
77
+ if (hasMalformedExpandedDurationLiteral(child)) return true;
78
+ continue;
79
+ }
80
+ if (hasMalformedKnownTemporalLiteralInternal(child, visited)) return true;
81
+ }
82
+ return false;
83
+ }
84
+ async function hasMalformedKnownTemporalLiteral(value, contextLoader) {
85
+ try {
86
+ return hasMalformedKnownTemporalLiteralInternal(await jsonld.expand(value, {
87
+ documentLoader: getNormalizationContextLoader(contextLoader),
88
+ keepFreeFloatingNodes: true
89
+ }), /* @__PURE__ */ new Set());
90
+ } catch {
91
+ return false;
92
+ }
93
+ }
94
+ //#endregion
95
+ export { hasMalformedKnownTemporalLiteral as t };
@@ -617,6 +617,42 @@ interface InboxMessage {
617
617
  readonly id: ReturnType<typeof crypto.randomUUID>;
618
618
  readonly baseUrl: string;
619
619
  readonly activity: unknown;
620
+ /**
621
+ * The normalized JSON-LD representation of a signed inbox activity that
622
+ * Fedify already compacted successfully while accepting the request. Queue
623
+ * workers can reuse this producer-side parse cache under stricter loader or
624
+ * network constraints without changing the raw payload preserved for
625
+ * forwarding.
626
+ *
627
+ * This may exist even when {@link ldSignatureVerified} is `false`, because
628
+ * fallback-authenticated traffic and already-queued backlog items can still
629
+ * depend on the cached normalized form to avoid re-fetching remote custom
630
+ * contexts during worker processing.
631
+ *
632
+ * This is optional for backward compatibility with messages that were
633
+ * queued by older Fedify versions or that were already in a queue before
634
+ * upgrading.
635
+ *
636
+ * Fedify keeps this on the queued message itself instead of an external
637
+ * sidecar because generic queue backends do not provide reliable lifecycle
638
+ * guarantees for auxiliary storage across retries and redeliveries.
639
+ *
640
+ * @internal
641
+ */
642
+ readonly normalizedActivity?: unknown;
643
+ /**
644
+ * Whether the producer actually verified the Linked Data Signature before
645
+ * queueing this message. This lets workers distinguish verified LDS replay
646
+ * from other authenticated inbox traffic that merely happened to include a
647
+ * signature block. This provenance marker is separate from the optional
648
+ * normalizedActivity parse cache.
649
+ *
650
+ * `undefined` preserves backward compatibility with older queued messages
651
+ * that predate this marker.
652
+ *
653
+ * @internal
654
+ */
655
+ readonly ldSignatureVerified?: boolean;
620
656
  readonly started: string;
621
657
  readonly attempt: number;
622
658
  readonly identifier: string | null;
@@ -1993,9 +2029,12 @@ interface InboxContext<TContextData> extends Context<TContextData> {
1993
2029
  * Forwards a received activity to the recipients' inboxes. The forwarded
1994
2030
  * activity will be signed in HTTP Signatures by the forwarder, but its
1995
2031
  * payload will not be modified, i.e., Linked Data Signatures and Object
1996
- * Integrity Proofs will not be added. Therefore, if the activity is not
1997
- * signed (i.e., it has neither Linked Data Signatures nor Object Integrity
1998
- * Proofs), the recipient probably will not trust the activity.
2032
+ * Integrity Proofs will not be added. Even when Fedify internally
2033
+ * normalizes a Linked Data Signature activity for parsing, this method still
2034
+ * forwards the original received payload so the sender's signatures/proofs
2035
+ * are preserved as-is. Therefore, if the activity is not signed (i.e., it
2036
+ * has neither Linked Data Signatures nor Object Integrity Proofs), the
2037
+ * recipient probably will not trust the activity.
1999
2038
  * @param forwarder The forwarder's identifier or the forwarder's username
2000
2039
  * or the forwarder's key pair(s).
2001
2040
  * @param recipients The recipients of the activity.
@@ -2011,9 +2050,12 @@ interface InboxContext<TContextData> extends Context<TContextData> {
2011
2050
  * Forwards a received activity to the recipients' inboxes. The forwarded
2012
2051
  * activity will be signed in HTTP Signatures by the forwarder, but its
2013
2052
  * payload will not be modified, i.e., Linked Data Signatures and Object
2014
- * Integrity Proofs will not be added. Therefore, if the activity is not
2015
- * signed (i.e., it has neither Linked Data Signatures nor Object Integrity
2016
- * Proofs), the recipient probably will not trust the activity.
2053
+ * Integrity Proofs will not be added. Even when Fedify internally
2054
+ * normalizes a Linked Data Signature activity for parsing, this method still
2055
+ * forwards the original received payload so the sender's signatures/proofs
2056
+ * are preserved as-is. Therefore, if the activity is not signed (i.e., it
2057
+ * has neither Linked Data Signatures nor Object Integrity Proofs), the
2058
+ * recipient probably will not trust the activity.
2017
2059
  * @param forwarder The forwarder's identifier or the forwarder's username.
2018
2060
  * @param recipients In this case, it must be `"followers"`.
2019
2061
  * @param options Options for forwarding the activity.
@@ -5,9 +5,9 @@ import { t as esm_default } from "../esm-DVILvP5e.mjs";
5
5
  import { t as assertEquals } from "../assert_equals-Ew3jOFa3.mjs";
6
6
  import "../std__assert-CRDpx_HF.mjs";
7
7
  import { t as assertRejects } from "../assert_rejects-B-qJtC9Z.mjs";
8
- import { l as verifyRequest } from "../http-Bslazr8_.mjs";
8
+ import { l as verifyRequest } from "../http-C_edJspG.mjs";
9
9
  import { i as rsaPrivateKey2 } from "../keys-DGu1NFwu.mjs";
10
- import { t as getAuthenticatedDocumentLoader } from "../docloader-BqX3RfO-.mjs";
10
+ import { t as getAuthenticatedDocumentLoader } from "../docloader-Da15YRxG.mjs";
11
11
  import { mockDocumentLoader, test } from "@fedify/fixture";
12
12
  import { UrlError } from "@fedify/vocab-runtime";
13
13
  //#region src/utils/docloader.test.ts
@@ -1,6 +1,6 @@
1
1
  const { Temporal } = require("@js-temporal/polyfill");
2
2
  const { URLPattern } = require("urlpattern-polyfill");
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
4
- const require_kv_cache = require("../kv-cache-Csre9hbs.cjs");
4
+ const require_kv_cache = require("../kv-cache-DmGi6uC-.cjs");
5
5
  exports.getAuthenticatedDocumentLoader = require_kv_cache.getAuthenticatedDocumentLoader;
6
6
  exports.kvCache = require_kv_cache.kvCache;
package/dist/utils/mod.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { Temporal } from "@js-temporal/polyfill";
2
2
  import { URLPattern } from "urlpattern-polyfill";
3
- import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../kv-cache-BYBaA6NP.js";
3
+ import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../kv-cache-C4DGZ_t4.js";
4
4
  export { getAuthenticatedDocumentLoader, kvCache };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/fedify",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "An ActivityPub server framework",
5
5
  "keywords": [
6
6
  "ActivityPub",
@@ -153,9 +153,9 @@
153
153
  "uri-template-router": "^1.0.0",
154
154
  "url-template": "^3.1.1",
155
155
  "urlpattern-polyfill": "^10.1.0",
156
- "@fedify/vocab": "2.2.2",
157
- "@fedify/webfinger": "2.2.2",
158
- "@fedify/vocab-runtime": "2.2.2"
156
+ "@fedify/vocab": "2.2.3",
157
+ "@fedify/vocab-runtime": "2.2.3",
158
+ "@fedify/webfinger": "2.2.3"
159
159
  },
160
160
  "devDependencies": {
161
161
  "@std/assert": "npm:@jsr/std__assert@^0.226.0",
@@ -167,8 +167,8 @@
167
167
  "tsx": "^4.19.4",
168
168
  "typescript": "^6.0.0",
169
169
  "wrangler": "^4.17.0",
170
- "@fedify/vocab-tools": "^2.2.2",
171
- "@fedify/fixture": "2.0.0"
170
+ "@fedify/fixture": "2.0.0",
171
+ "@fedify/vocab-tools": "^2.2.3"
172
172
  },
173
173
  "scripts": {
174
174
  "build:self": "tsdown",
@@ -1,279 +0,0 @@
1
- import "@js-temporal/polyfill";
2
- import "urlpattern-polyfill";
3
- globalThis.addEventListener = () => {};
4
- import { n as version, t as name } from "./deno-DCXSCeB3.mjs";
5
- import { n as fetchKey, o as validateCryptoKey } from "./key-Dk8-s0bs.mjs";
6
- import { Activity, CryptographicKey, Object as Object$1, getTypeId } from "@fedify/vocab";
7
- import { SpanStatusCode, trace } from "@opentelemetry/api";
8
- import { getDocumentLoader } from "@fedify/vocab-runtime";
9
- import { getLogger } from "@logtape/logtape";
10
- import { decodeBase64, encodeBase64 } from "byte-encodings/base64";
11
- import { encodeHex } from "byte-encodings/hex";
12
- import jsonld from "@fedify/vocab-runtime/jsonld";
13
- //#region src/sig/ld.ts
14
- const logger = getLogger([
15
- "fedify",
16
- "sig",
17
- "ld"
18
- ]);
19
- /**
20
- * Attaches a LD signature to the given JSON-LD document.
21
- * @param jsonLd The JSON-LD document to attach the signature to. It is not
22
- * modified.
23
- * @param signature The signature to attach.
24
- * @returns The JSON-LD document with the attached signature.
25
- * @throws {TypeError} If the input document is not a valid JSON-LD document.
26
- * @since 1.0.0
27
- */
28
- function attachSignature(jsonLd, signature) {
29
- if (typeof jsonLd !== "object" || jsonLd == null) throw new TypeError("Failed to attach signature; invalid JSON-LD document.");
30
- return {
31
- ...jsonLd,
32
- signature
33
- };
34
- }
35
- /**
36
- * Creates a LD signature for the given JSON-LD document.
37
- * @param jsonLd The JSON-LD document to sign.
38
- * @param privateKey The private key to sign the document.
39
- * @param keyId The ID of the public key that corresponds to the private key.
40
- * @param options Additional options for creating the signature.
41
- * See also {@link CreateSignatureOptions}.
42
- * @return The created signature.
43
- * @throws {TypeError} If the private key is invalid or unsupported.
44
- * @since 1.0.0
45
- */
46
- async function createSignature(jsonLd, privateKey, keyId, { contextLoader, created } = {}) {
47
- validateCryptoKey(privateKey, "private");
48
- if (privateKey.algorithm.name !== "RSASSA-PKCS1-v1_5") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
49
- const options = {
50
- "@context": "https://w3id.org/identity/v1",
51
- creator: keyId.href,
52
- created: created?.toString() ?? (/* @__PURE__ */ new Date()).toISOString()
53
- };
54
- const message = await hashJsonLd(options, contextLoader) + await hashJsonLd(jsonLd, contextLoader);
55
- const messageBytes = new TextEncoder().encode(message);
56
- const signature = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", privateKey, messageBytes);
57
- return {
58
- ...options,
59
- type: "RsaSignature2017",
60
- signatureValue: encodeBase64(signature)
61
- };
62
- }
63
- /**
64
- * Signs the given JSON-LD document with the private key and returns the signed
65
- * JSON-LD document.
66
- * @param jsonLd The JSON-LD document to sign.
67
- * @param privateKey The private key to sign the document.
68
- * @param keyId The key ID to use in the signature. It will be used by the
69
- * verifier to fetch the corresponding public key.
70
- * @param options Additional options for signing the document.
71
- * See also {@link SignJsonLdOptions}.
72
- * @returns The signed JSON-LD document.
73
- * @throws {TypeError} If the private key is invalid or unsupported.
74
- * @since 1.0.0
75
- */
76
- async function signJsonLd(jsonLd, privateKey, keyId, options) {
77
- return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("ld_signatures.sign", { attributes: { "ld_signatures.key_id": keyId.href } }, async (span) => {
78
- try {
79
- const signature = await createSignature(jsonLd, privateKey, keyId, options);
80
- if (span.isRecording()) {
81
- span.setAttribute("ld_signatures.type", signature.type);
82
- span.setAttribute("ld_signatures.signature", encodeHex(decodeBase64(signature.signatureValue)));
83
- }
84
- return attachSignature(jsonLd, signature);
85
- } catch (error) {
86
- span.setStatus({
87
- code: SpanStatusCode.ERROR,
88
- message: String(error)
89
- });
90
- throw error;
91
- } finally {
92
- span.end();
93
- }
94
- });
95
- }
96
- /**
97
- * Checks if the given JSON-LD document has a Linked Data Signature-like
98
- * object, without restricting it to a single suite-specific shape.
99
- * @param jsonLd The JSON-LD document to check.
100
- * @returns `true` if the document has a signature-like object; `false`
101
- * otherwise.
102
- * @since 2.2.0
103
- */
104
- function hasSignatureLike(jsonLd) {
105
- if (typeof jsonLd !== "object" || jsonLd == null) return false;
106
- const signature = jsonLd.signature;
107
- const hasReference = (value) => {
108
- if (typeof value === "string") return true;
109
- if (Array.isArray(value)) return value.some(hasReference);
110
- return typeof value === "object" && value != null && ("id" in value && typeof value.id === "string" || "@id" in value && typeof value["@id"] === "string");
111
- };
112
- const hasSignatureObject = (value) => {
113
- if (typeof value !== "object" || value == null) return false;
114
- const signatureRecord = value;
115
- return (typeof signatureRecord.type === "string" || Array.isArray(signatureRecord.type) && signatureRecord.type.some((item) => typeof item === "string")) && (hasReference(signatureRecord.creator) || hasReference(signatureRecord.verificationMethod)) && (typeof signatureRecord.signatureValue === "string" || typeof signatureRecord.jws === "string");
116
- };
117
- return Array.isArray(signature) ? signature.some(hasSignatureObject) : hasSignatureObject(signature);
118
- }
119
- /**
120
- * Checks if the given JSON-LD document has a Linked Data Signature.
121
- * @param jsonLd The JSON-LD document to check.
122
- * @returns `true` if the document has a signature; `false` otherwise.
123
- * @since 1.0.0
124
- */
125
- function hasSignature(jsonLd) {
126
- if (typeof jsonLd !== "object" || jsonLd == null) return false;
127
- if ("signature" in jsonLd) {
128
- const signature = jsonLd.signature;
129
- if (typeof signature !== "object" || signature == null) return false;
130
- 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";
131
- }
132
- return false;
133
- }
134
- /**
135
- * Detaches Linked Data Signatures from the given JSON-LD document.
136
- * @param jsonLd The JSON-LD document to modify.
137
- * @returns The modified JSON-LD document. If the input document does not
138
- * contain a signature, the original document is returned.
139
- * @since 1.0.0
140
- */
141
- function detachSignature(jsonLd) {
142
- if (typeof jsonLd !== "object" || jsonLd == null) return jsonLd;
143
- const doc = { ...jsonLd };
144
- delete doc.signature;
145
- return doc;
146
- }
147
- /**
148
- * Verifies Linked Data Signatures of the given JSON-LD document.
149
- * @param jsonLd The JSON-LD document to verify.
150
- * @param options Options for verifying the signature.
151
- * @returns The public key that signed the document or `null` if the signature
152
- * is invalid or the key is not found.
153
- * @since 1.0.0
154
- */
155
- async function verifySignature(jsonLd, options = {}) {
156
- if (!hasSignature(jsonLd)) return null;
157
- const sig = jsonLd.signature;
158
- let signature;
159
- try {
160
- signature = decodeBase64(sig.signatureValue);
161
- } catch (error) {
162
- logger.debug("Failed to verify; invalid base64 signatureValue: {signatureValue}", {
163
- ...sig,
164
- error
165
- });
166
- return null;
167
- }
168
- const { key, cached } = await fetchKey(new URL(sig.creator), CryptographicKey, options);
169
- if (key == null) return null;
170
- const sigOpts = {
171
- ...sig,
172
- "@context": "https://w3id.org/identity/v1"
173
- };
174
- delete sigOpts.type;
175
- delete sigOpts.id;
176
- delete sigOpts.signatureValue;
177
- let sigOptsHash;
178
- try {
179
- sigOptsHash = await hashJsonLd(sigOpts, options.contextLoader);
180
- } catch (error) {
181
- logger.warn("Failed to verify; failed to hash the signature options: {signatureOptions}\n{error}", {
182
- signatureOptions: sigOpts,
183
- error
184
- });
185
- return null;
186
- }
187
- const document = { ...jsonLd };
188
- delete document.signature;
189
- let docHash;
190
- try {
191
- docHash = await hashJsonLd(document, options.contextLoader);
192
- } catch (error) {
193
- logger.warn("Failed to verify; failed to hash the document: {document}\n{error}", {
194
- document,
195
- error
196
- });
197
- return null;
198
- }
199
- const encoder = new TextEncoder();
200
- const message = sigOptsHash + docHash;
201
- const messageBytes = encoder.encode(message);
202
- if (await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes)) return key;
203
- if (cached) {
204
- logger.debug("Failed to verify with the cached key {keyId}; signature {signatureValue} is invalid. Retrying with the freshly fetched key...", {
205
- keyId: sig.creator,
206
- ...sig
207
- });
208
- const { key } = await fetchKey(new URL(sig.creator), CryptographicKey, {
209
- ...options,
210
- keyCache: {
211
- get: () => Promise.resolve(void 0),
212
- set: async (keyId, key) => await options.keyCache?.set(keyId, key)
213
- }
214
- });
215
- if (key == null) return null;
216
- return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key.publicKey, signature.slice(), messageBytes) ? key : null;
217
- }
218
- logger.debug("Failed to verify with the fetched key {keyId}; signature {signatureValue} is invalid. Check if the key is correct or if the signed message is correct. The message to sign is:\n{message}", {
219
- keyId: sig.creator,
220
- ...sig,
221
- message
222
- });
223
- return null;
224
- }
225
- /**
226
- * Verify the authenticity of the given JSON-LD document using Linked Data
227
- * Signatures. If the document is signed, this function verifies the signature
228
- * and checks if the document is attributed to the owner of the public key.
229
- * If the document is not signed, this function returns `false`.
230
- * @param jsonLd The JSON-LD document to verify.
231
- * @param options Options for verifying the document.
232
- * @returns `true` if the document is authentic; `false` otherwise.
233
- */
234
- async function verifyJsonLd(jsonLd, options = {}) {
235
- return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("ld_signatures.verify", async (span) => {
236
- try {
237
- const object = await Object$1.fromJsonLd(jsonLd, options);
238
- if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
239
- span.setAttribute("activitypub.object.type", getTypeId(object).href);
240
- if (typeof jsonLd === "object" && jsonLd != null && "signature" in jsonLd && typeof jsonLd.signature === "object" && jsonLd.signature != null) {
241
- if ("creator" in jsonLd.signature && typeof jsonLd.signature.creator === "string") span.setAttribute("ld_signatures.key_id", jsonLd.signature.creator);
242
- if ("signatureValue" in jsonLd.signature && typeof jsonLd.signature.signatureValue === "string") span.setAttribute("ld_signatures.signature", jsonLd.signature.signatureValue);
243
- if ("type" in jsonLd.signature && typeof jsonLd.signature.type === "string") span.setAttribute("ld_signatures.type", jsonLd.signature.type);
244
- }
245
- const attributions = new Set(object.attributionIds.map((uri) => uri.href));
246
- if (object instanceof Activity) for (const uri of object.actorIds) attributions.add(uri.href);
247
- const key = await verifySignature(jsonLd, options);
248
- if (key == null) return false;
249
- if (key.ownerId == null) {
250
- logger.debug("Key {keyId} has no owner.", { keyId: key.id?.href });
251
- return false;
252
- }
253
- attributions.delete(key.ownerId.href);
254
- if (attributions.size > 0) {
255
- logger.debug("Some attributions are not authenticated by the Linked Data Signatures: {attributions}.", { attributions: [...attributions] });
256
- return false;
257
- }
258
- return true;
259
- } catch (error) {
260
- span.setStatus({
261
- code: SpanStatusCode.ERROR,
262
- message: String(error)
263
- });
264
- throw error;
265
- } finally {
266
- span.end();
267
- }
268
- });
269
- }
270
- async function hashJsonLd(jsonLd, contextLoader) {
271
- const canon = await jsonld.canonize(jsonLd, {
272
- format: "application/n-quads",
273
- documentLoader: contextLoader ?? getDocumentLoader()
274
- });
275
- const encoder = new TextEncoder();
276
- return encodeHex(await crypto.subtle.digest("SHA-256", encoder.encode(canon)));
277
- }
278
- //#endregion
279
- export { signJsonLd as a, hasSignatureLike as i, createSignature as n, verifyJsonLd as o, detachSignature as r, verifySignature as s, attachSignature as t };
File without changes