@fedify/fedify 2.4.0-dev.1727 → 2.4.0-dev.1740
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-f5-YwHfj.mjs → builder-Bw6LVKGw.mjs} +2 -2
- package/dist/compat/transformers.test.mjs +1 -1
- package/dist/{deno-Deyoxesx.mjs → deno-tRdp8MCl.mjs} +1 -1
- package/dist/{docloader-B3kuDBg9.mjs → docloader-Dg_7gMC7.mjs} +2 -2
- package/dist/federation/builder.test.mjs +1 -1
- package/dist/federation/handler.test.mjs +3 -3
- 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.js +1 -1
- 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-BzRjeOky.cjs → federation-BJBpt_Am.cjs} +3 -3
- package/dist/{http-Bw6s_dOB.js → http-BIxGa6AG.js} +1 -1
- package/dist/{http-TjhZfiSN.mjs → http-Day1kcpT.mjs} +3 -3
- package/dist/{http-AyKngJ_h.cjs → http-DggG1scN.cjs} +1 -1
- package/dist/{key-b4kf4yZT.mjs → key-igh0AMsL.mjs} +2 -2
- package/dist/{kv-cache-CQsh-KUw.js → kv-cache-BIAB0HKx.js} +1 -1
- package/dist/{kv-cache-CpILvhUV.mjs → kv-cache-Bz6aQOOX.mjs} +1 -1
- package/dist/{kv-cache-BTVaZrf-.cjs → kv-cache-C4U_uBqD.cjs} +1 -1
- package/dist/{ld-CKujkAfG.mjs → ld-DnT9sCWa.mjs} +3 -3
- package/dist/{metrics-AlCGTDrr.mjs → metrics-DbdNnWIt.mjs} +1 -1
- package/dist/{middleware-LPbZz6-1.mjs → middleware-B6GweVcg.mjs} +1 -1
- package/dist/{middleware-CZcuedl-.js → middleware-CnsPXG0O.js} +3 -3
- package/dist/{middleware-C9Y5lLZN.mjs → middleware-DPIS4fsB.mjs} +12 -12
- package/dist/{mod-CuDDaIIt.d.cts → mod-CnGLNoVw.d.cts} +85 -3
- package/dist/{mod-D-IhOEOP.d.ts → mod-DzRXACM5.d.ts} +85 -3
- package/dist/mod.cjs +5 -4
- package/dist/mod.d.cts +3 -3
- package/dist/mod.d.ts +3 -3
- package/dist/mod.js +5 -5
- package/dist/nodeinfo/handler.test.mjs +1 -1
- package/dist/{owner-D1O8v8FB.mjs → owner-B3ef8GfJ.mjs} +2 -2
- package/dist/{proof-BTBolSnd.js → proof-BBLQXRWw.js} +603 -31
- package/dist/{proof-DcSm-2eG.cjs → proof-Cqk2TcLX.cjs} +607 -29
- package/dist/proof-DVoDZ8d7.mjs +890 -0
- package/dist/{send-DG7JG280.mjs → send-PbBSZ7Qk.mjs} +3 -3
- 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 +3 -2
- package/dist/sig/mod.d.cts +2 -2
- package/dist/sig/mod.d.ts +2 -2
- package/dist/sig/mod.js +3 -3
- package/dist/sig/owner.test.mjs +1 -1
- package/dist/sig/proof.test.mjs +956 -8
- package/dist/{temporal-DA3jx8x1.mjs → temporal-DLQIjuTn.mjs} +1 -1
- 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 +6 -6
- package/dist/proof-DeMR7Akd.mjs +0 -316
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Temporal } from "temporal-polyfill";
|
|
2
2
|
import "urlpattern-polyfill";
|
|
3
3
|
globalThis.addEventListener = () => {};
|
|
4
|
-
import { c as getNormalizationContextLoader } from "./ld-
|
|
4
|
+
import { c as getNormalizationContextLoader } from "./ld-DnT9sCWa.mjs";
|
|
5
5
|
import jsonld from "@fedify/vocab-runtime/jsonld";
|
|
6
6
|
//#region src/federation/temporal.ts
|
|
7
7
|
function isPlainObject(value) {
|
|
@@ -5,9 +5,9 @@ import { t as esm_default } from "../esm-BHXXx4Wx.mjs";
|
|
|
5
5
|
import { t as assertEquals } from "../assert_equals-CBlRfA-_.mjs";
|
|
6
6
|
import "../std__assert-CxFT0MNt.mjs";
|
|
7
7
|
import { t as assertRejects } from "../assert_rejects-wFbSPn1O.mjs";
|
|
8
|
-
import { l as verifyRequest } from "../http-
|
|
8
|
+
import { l as verifyRequest } from "../http-Day1kcpT.mjs";
|
|
9
9
|
import { i as rsaPrivateKey2 } from "../keys-DIJNHW-Y.mjs";
|
|
10
|
-
import { t as getAuthenticatedDocumentLoader } from "../docloader-
|
|
10
|
+
import { t as getAuthenticatedDocumentLoader } from "../docloader-Dg_7gMC7.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,7 +1,7 @@
|
|
|
1
1
|
import { Temporal } from "temporal-polyfill";
|
|
2
2
|
import { URLPattern } from "urlpattern-polyfill";
|
|
3
3
|
globalThis.addEventListener = () => {};
|
|
4
|
-
import { n as kvCache, t as MockKvStore } from "../kv-cache-
|
|
4
|
+
import { n as kvCache, t as MockKvStore } from "../kv-cache-Bz6aQOOX.mjs";
|
|
5
5
|
import { deepStrictEqual, throws } from "node:assert";
|
|
6
6
|
import { createTestMeterProvider, mockDocumentLoader, test } from "@fedify/fixture";
|
|
7
7
|
import { preloadedContexts } from "@fedify/vocab-runtime";
|
package/dist/utils/mod.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { URLPattern } = require("urlpattern-polyfill");
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const require_kv_cache = require("../kv-cache-
|
|
3
|
+
const require_kv_cache = require("../kv-cache-C4U_uBqD.cjs");
|
|
4
4
|
exports.getAuthenticatedDocumentLoader = require_kv_cache.getAuthenticatedDocumentLoader;
|
|
5
5
|
exports.kvCache = require_kv_cache.kvCache;
|
package/dist/utils/mod.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Temporal } from "temporal-polyfill";
|
|
2
2
|
import { URLPattern } from "urlpattern-polyfill";
|
|
3
|
-
import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../kv-cache-
|
|
3
|
+
import { n as getAuthenticatedDocumentLoader, t as kvCache } from "../kv-cache-BIAB0HKx.js";
|
|
4
4
|
export { getAuthenticatedDocumentLoader, kvCache };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/fedify",
|
|
3
|
-
"version": "2.4.0-dev.
|
|
3
|
+
"version": "2.4.0-dev.1740+32e9952c",
|
|
4
4
|
"description": "An ActivityPub server framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ActivityPub",
|
|
@@ -151,10 +151,10 @@
|
|
|
151
151
|
"structured-field-values": "^2.0.4",
|
|
152
152
|
"temporal-polyfill": "^1.0.1",
|
|
153
153
|
"urlpattern-polyfill": "^10.1.0",
|
|
154
|
-
"@fedify/uri-template": "2.4.0-dev.
|
|
155
|
-
"@fedify/vocab": "2.4.0-dev.
|
|
156
|
-
"@fedify/vocab-runtime": "2.4.0-dev.
|
|
157
|
-
"@fedify/webfinger": "2.4.0-dev.
|
|
154
|
+
"@fedify/uri-template": "2.4.0-dev.1740+32e9952c",
|
|
155
|
+
"@fedify/vocab": "2.4.0-dev.1740+32e9952c",
|
|
156
|
+
"@fedify/vocab-runtime": "2.4.0-dev.1740+32e9952c",
|
|
157
|
+
"@fedify/webfinger": "2.4.0-dev.1740+32e9952c"
|
|
158
158
|
},
|
|
159
159
|
"devDependencies": {
|
|
160
160
|
"@std/assert": "npm:@jsr/std__assert@^0.226.0",
|
|
@@ -167,7 +167,7 @@
|
|
|
167
167
|
"typescript": "^6.0.0",
|
|
168
168
|
"wrangler": "^4.17.0",
|
|
169
169
|
"@fedify/fixture": "2.0.0",
|
|
170
|
-
"@fedify/vocab-tools": "^2.4.0-dev.
|
|
170
|
+
"@fedify/vocab-tools": "^2.4.0-dev.1740+32e9952c"
|
|
171
171
|
},
|
|
172
172
|
"scripts": {
|
|
173
173
|
"build:self": "tsdown",
|
package/dist/proof-DeMR7Akd.mjs
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import { Temporal } from "temporal-polyfill";
|
|
2
|
-
import "urlpattern-polyfill";
|
|
3
|
-
globalThis.addEventListener = () => {};
|
|
4
|
-
import { n as version, t as name } from "./deno-Deyoxesx.mjs";
|
|
5
|
-
import { n as getDurationMs, r as getFederationMetrics, s as measureSignatureKeyFetch } from "./metrics-AlCGTDrr.mjs";
|
|
6
|
-
import { n as fetchKey, o as validateCryptoKey } from "./key-b4kf4yZT.mjs";
|
|
7
|
-
import { n as preloadedOnlyDocumentLoader } from "./public-audience--9hx7IGx.mjs";
|
|
8
|
-
import { r as normalizeOutgoingActivityJsonLd } from "./outgoing-jsonld-BQSHRLhg.mjs";
|
|
9
|
-
import { getLogger } from "@logtape/logtape";
|
|
10
|
-
import { Activity, DataIntegrityProof, Multikey, getTypeId } from "@fedify/vocab";
|
|
11
|
-
import { SpanStatusCode, trace } from "@opentelemetry/api";
|
|
12
|
-
import { haveSameFe34Origin } from "@fedify/vocab-runtime";
|
|
13
|
-
import { encodeHex } from "byte-encodings/hex";
|
|
14
|
-
import serialize from "json-canon";
|
|
15
|
-
//#region src/sig/proof.ts
|
|
16
|
-
/**
|
|
17
|
-
* Known Object Integrity Proof `cryptosuite` values, used to keep
|
|
18
|
-
* `object_integrity_proofs.cryptosuite` on a bounded set of spec-defined
|
|
19
|
-
* string values. Fedify currently signs and verifies only
|
|
20
|
-
* `eddsa-jcs-2022`; other values come in only from external proofs and are
|
|
21
|
-
* dropped from the metric attribute to avoid attacker-controlled
|
|
22
|
-
* cardinality.
|
|
23
|
-
*/
|
|
24
|
-
const OIP_KNOWN_CRYPTOSUITES = /* @__PURE__ */ new Set(["eddsa-jcs-2022"]);
|
|
25
|
-
const logger = getLogger([
|
|
26
|
-
"fedify",
|
|
27
|
-
"sig",
|
|
28
|
-
"proof"
|
|
29
|
-
]);
|
|
30
|
-
/**
|
|
31
|
-
* Checks if the given JSON-LD document has a DataIntegrityProof-like object,
|
|
32
|
-
* without fully deserializing it into vocabulary classes.
|
|
33
|
-
* @param jsonLd The JSON-LD document to check.
|
|
34
|
-
* @returns `true` if the document has a proof-like object; `false` otherwise.
|
|
35
|
-
* @since 2.2.0
|
|
36
|
-
*/
|
|
37
|
-
function hasProofLike(jsonLd) {
|
|
38
|
-
if (typeof jsonLd !== "object" || jsonLd == null) return false;
|
|
39
|
-
const record = jsonLd;
|
|
40
|
-
const proof = record.proof ?? record["https://w3id.org/security#proof"];
|
|
41
|
-
const getField = (source, compact, expanded) => source[compact] ?? source[expanded];
|
|
42
|
-
const isReference = (value) => {
|
|
43
|
-
if (typeof value === "string") return true;
|
|
44
|
-
if (Array.isArray(value)) return value.some(isReference);
|
|
45
|
-
return typeof value === "object" && value != null && ("id" in value && typeof value.id === "string" || "@id" in value && typeof value["@id"] === "string" || "@value" in value && typeof value["@value"] === "string");
|
|
46
|
-
};
|
|
47
|
-
const hasType = (value) => {
|
|
48
|
-
if (typeof value === "string") return value === "DataIntegrityProof" || value === "https://w3id.org/security#DataIntegrityProof";
|
|
49
|
-
if (Array.isArray(value)) return value.some(hasType);
|
|
50
|
-
return false;
|
|
51
|
-
};
|
|
52
|
-
const isProofLike = (value) => {
|
|
53
|
-
if (typeof value !== "object" || value == null) return false;
|
|
54
|
-
const proofRecord = value;
|
|
55
|
-
return hasType(proofRecord.type ?? proofRecord["@type"]) && isReference(getField(proofRecord, "verificationMethod", "https://w3id.org/security#verificationMethod")) && isReference(getField(proofRecord, "proofPurpose", "https://w3id.org/security#proofPurpose")) && isReference(getField(proofRecord, "proofValue", "https://w3id.org/security#proofValue"));
|
|
56
|
-
};
|
|
57
|
-
return Array.isArray(proof) ? proof.some(isProofLike) : isProofLike(proof);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Creates a proof for the given object.
|
|
61
|
-
* @param object The object to create a proof for.
|
|
62
|
-
* @param privateKey The private key to sign the proof with.
|
|
63
|
-
* @param keyId The key ID to use in the proof. It will be used by the verifier.
|
|
64
|
-
* @param options Additional options. See also {@link CreateProofOptions}.
|
|
65
|
-
* @returns The created proof.
|
|
66
|
-
* @throws {TypeError} If the private key is invalid or unsupported.
|
|
67
|
-
* @since 0.10.0
|
|
68
|
-
*/
|
|
69
|
-
async function createProof(object, privateKey, keyId, { contextLoader, context, created } = {}) {
|
|
70
|
-
validateCryptoKey(privateKey, "private");
|
|
71
|
-
if (privateKey.algorithm.name !== "Ed25519") throw new TypeError("Unsupported algorithm: " + privateKey.algorithm.name);
|
|
72
|
-
let compactMsg = await object.clone({ proofs: [] }).toJsonLd({
|
|
73
|
-
format: "compact",
|
|
74
|
-
contextLoader,
|
|
75
|
-
context
|
|
76
|
-
});
|
|
77
|
-
compactMsg = await normalizeOutgoingActivityJsonLd(compactMsg, contextLoader);
|
|
78
|
-
const msgCanon = serialize(compactMsg);
|
|
79
|
-
const encoder = new TextEncoder();
|
|
80
|
-
const msgBytes = encoder.encode(msgCanon);
|
|
81
|
-
const msgDigest = await crypto.subtle.digest("SHA-256", msgBytes);
|
|
82
|
-
created ??= Temporal.Now.instant();
|
|
83
|
-
const proofCanon = serialize({
|
|
84
|
-
"@context": compactMsg["@context"],
|
|
85
|
-
type: "DataIntegrityProof",
|
|
86
|
-
cryptosuite: "eddsa-jcs-2022",
|
|
87
|
-
verificationMethod: keyId.href,
|
|
88
|
-
proofPurpose: "assertionMethod",
|
|
89
|
-
created: created.toString()
|
|
90
|
-
});
|
|
91
|
-
const proofBytes = encoder.encode(proofCanon);
|
|
92
|
-
const proofDigest = await crypto.subtle.digest("SHA-256", proofBytes);
|
|
93
|
-
const digest = new Uint8Array(proofDigest.byteLength + msgDigest.byteLength);
|
|
94
|
-
digest.set(new Uint8Array(proofDigest), 0);
|
|
95
|
-
digest.set(new Uint8Array(msgDigest), proofDigest.byteLength);
|
|
96
|
-
const sig = await crypto.subtle.sign("Ed25519", privateKey, digest);
|
|
97
|
-
return new DataIntegrityProof({
|
|
98
|
-
cryptosuite: "eddsa-jcs-2022",
|
|
99
|
-
verificationMethod: keyId,
|
|
100
|
-
proofPurpose: "assertionMethod",
|
|
101
|
-
created: created ?? Temporal.Now.instant(),
|
|
102
|
-
proofValue: new Uint8Array(sig)
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Signs the given object with the private key and returns the signed object.
|
|
107
|
-
* @param object The object to create a proof for.
|
|
108
|
-
* @param privateKey The private key to sign the proof with.
|
|
109
|
-
* @param keyId The key ID to use in the proof. It will be used by the verifier.
|
|
110
|
-
* @param options Additional options. See also {@link SignObjectOptions}.
|
|
111
|
-
* @returns The signed object.
|
|
112
|
-
* @throws {TypeError} If the private key is invalid or unsupported.
|
|
113
|
-
* @since 0.10.0
|
|
114
|
-
*/
|
|
115
|
-
async function signObject(object, privateKey, keyId, options = {}) {
|
|
116
|
-
return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("object_integrity_proofs.sign", { attributes: { "activitypub.object.type": getTypeId(object).href } }, async (span) => {
|
|
117
|
-
try {
|
|
118
|
-
if (object.id != null) span.setAttribute("activitypub.object.id", object.id.href);
|
|
119
|
-
const existingProofs = [];
|
|
120
|
-
for await (const proof of object.getProofs(options)) existingProofs.push(proof);
|
|
121
|
-
const proof = await createProof(object, privateKey, keyId, options);
|
|
122
|
-
if (span.isRecording()) {
|
|
123
|
-
if (proof.cryptosuite != null) span.setAttribute("object_integrity_proofs.cryptosuite", proof.cryptosuite);
|
|
124
|
-
if (proof.verificationMethodId != null) span.setAttribute("object_integrity_proofs.key_id", proof.verificationMethodId.href);
|
|
125
|
-
if (proof.proofValue != null) span.setAttribute("object_integrity_proofs.signature", encodeHex(proof.proofValue));
|
|
126
|
-
}
|
|
127
|
-
return object.clone({ proofs: [...existingProofs, proof] });
|
|
128
|
-
} catch (error) {
|
|
129
|
-
span.setStatus({
|
|
130
|
-
code: SpanStatusCode.ERROR,
|
|
131
|
-
message: String(error)
|
|
132
|
-
});
|
|
133
|
-
throw error;
|
|
134
|
-
} finally {
|
|
135
|
-
span.end();
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Verifies the given proof for the object.
|
|
141
|
-
* @param jsonLd The JSON-LD object to verify the proof for. If it contains
|
|
142
|
-
* any proofs, they will be ignored.
|
|
143
|
-
* @param proof The proof to verify.
|
|
144
|
-
* @param options Additional options. See also {@link VerifyProofOptions}.
|
|
145
|
-
* @returns The public key that was used to sign the proof, or `null` if the
|
|
146
|
-
* proof is invalid.
|
|
147
|
-
* @since 0.10.0
|
|
148
|
-
*/
|
|
149
|
-
async function verifyProof(jsonLd, proof, options = {}) {
|
|
150
|
-
return await (options.tracerProvider ?? trace.getTracerProvider()).getTracer(name, version).startActiveSpan("object_integrity_proofs.verify", async (span) => {
|
|
151
|
-
const start = performance.now();
|
|
152
|
-
let verified = false;
|
|
153
|
-
let threw = false;
|
|
154
|
-
const cryptosuite = proof.cryptosuite != null && OIP_KNOWN_CRYPTOSUITES.has(proof.cryptosuite) ? proof.cryptosuite : void 0;
|
|
155
|
-
if (span.isRecording()) {
|
|
156
|
-
if (proof.cryptosuite != null) span.setAttribute("object_integrity_proofs.cryptosuite", proof.cryptosuite);
|
|
157
|
-
if (proof.verificationMethodId != null) span.setAttribute("object_integrity_proofs.key_id", proof.verificationMethodId.href);
|
|
158
|
-
if (proof.proofValue != null) span.setAttribute("object_integrity_proofs.signature", encodeHex(proof.proofValue));
|
|
159
|
-
}
|
|
160
|
-
try {
|
|
161
|
-
const key = await verifyProofInternal(jsonLd, proof, options);
|
|
162
|
-
if (key == null) span.setStatus({ code: SpanStatusCode.ERROR });
|
|
163
|
-
else verified = true;
|
|
164
|
-
return key;
|
|
165
|
-
} catch (error) {
|
|
166
|
-
threw = true;
|
|
167
|
-
span.setStatus({
|
|
168
|
-
code: SpanStatusCode.ERROR,
|
|
169
|
-
message: String(error)
|
|
170
|
-
});
|
|
171
|
-
throw error;
|
|
172
|
-
} finally {
|
|
173
|
-
const classified = threw ? "error" : verified ? "verified" : "rejected";
|
|
174
|
-
getFederationMetrics(options.meterProvider).recordSignatureVerificationDuration(getDurationMs(start), "object_integrity", classified, { cryptosuite });
|
|
175
|
-
span.end();
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
async function verifyProofInternal(jsonLd, proof, options) {
|
|
180
|
-
if (typeof jsonLd !== "object" || jsonLd == null || Array.isArray(jsonLd) || proof.cryptosuite !== "eddsa-jcs-2022" || proof.verificationMethodId == null || proof.proofPurpose !== "assertionMethod" || proof.proofValue == null || proof.created == null) return null;
|
|
181
|
-
const publicKeyPromise = measureSignatureKeyFetch(options.meterProvider, "object_integrity", () => fetchKey(proof.verificationMethodId, Multikey, options));
|
|
182
|
-
const proofConfig = {
|
|
183
|
-
"@context": jsonLd["@context"],
|
|
184
|
-
type: "DataIntegrityProof",
|
|
185
|
-
cryptosuite: proof.cryptosuite,
|
|
186
|
-
verificationMethod: proof.verificationMethodId.href,
|
|
187
|
-
proofPurpose: proof.proofPurpose,
|
|
188
|
-
created: proof.created.toString()
|
|
189
|
-
};
|
|
190
|
-
const encoder = new TextEncoder();
|
|
191
|
-
const proofBytes = encoder.encode(serialize(proofConfig));
|
|
192
|
-
const proofDigest = await crypto.subtle.digest("SHA-256", proofBytes);
|
|
193
|
-
const msg = { ...jsonLd };
|
|
194
|
-
if ("proof" in msg) delete msg.proof;
|
|
195
|
-
if ("https://w3id.org/security#proof" in msg) delete msg["https://w3id.org/security#proof"];
|
|
196
|
-
let fetchedKey;
|
|
197
|
-
try {
|
|
198
|
-
fetchedKey = await publicKeyPromise;
|
|
199
|
-
} catch (error) {
|
|
200
|
-
logger.debug("Failed to get the key (verificationMethod) for the proof:\n{proof}", {
|
|
201
|
-
proof,
|
|
202
|
-
keyId: proof.verificationMethodId.href,
|
|
203
|
-
error
|
|
204
|
-
});
|
|
205
|
-
return null;
|
|
206
|
-
}
|
|
207
|
-
const publicKey = fetchedKey.key;
|
|
208
|
-
if (publicKey == null) {
|
|
209
|
-
logger.debug("Failed to get the key (verificationMethod) for the proof:\n{proof}", {
|
|
210
|
-
proof,
|
|
211
|
-
keyId: proof.verificationMethodId.href
|
|
212
|
-
});
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
if (publicKey.publicKey.algorithm.name !== "Ed25519") {
|
|
216
|
-
if (fetchedKey.cached) {
|
|
217
|
-
logger.debug("The cached key (verificationMethod) for the proof is not a valid Ed25519 key:\n{keyId}; retrying with the freshly fetched key...", {
|
|
218
|
-
proof,
|
|
219
|
-
keyId: proof.verificationMethodId.href
|
|
220
|
-
});
|
|
221
|
-
return await verifyProofInternal(jsonLd, proof, {
|
|
222
|
-
...options,
|
|
223
|
-
keyCache: {
|
|
224
|
-
get: () => Promise.resolve(void 0),
|
|
225
|
-
set: async (keyId, key) => await options.keyCache?.set(keyId, key)
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
}
|
|
229
|
-
logger.debug("The fetched key (verificationMethod) for the proof is not a valid Ed25519 key:\n{keyId}", {
|
|
230
|
-
proof,
|
|
231
|
-
keyId: proof.verificationMethodId.href
|
|
232
|
-
});
|
|
233
|
-
return null;
|
|
234
|
-
}
|
|
235
|
-
const digest = new Uint8Array(proofDigest.byteLength + 32);
|
|
236
|
-
digest.set(new Uint8Array(proofDigest), 0);
|
|
237
|
-
const proofValue = proof.proofValue;
|
|
238
|
-
const verifyCandidate = async (candidate) => {
|
|
239
|
-
const msgBytes = encoder.encode(serialize(candidate));
|
|
240
|
-
const msgDigest = await crypto.subtle.digest("SHA-256", msgBytes);
|
|
241
|
-
digest.set(new Uint8Array(msgDigest), proofDigest.byteLength);
|
|
242
|
-
return await crypto.subtle.verify("Ed25519", publicKey.publicKey, proofValue.slice(), digest);
|
|
243
|
-
};
|
|
244
|
-
if (await verifyCandidate(msg)) return publicKey;
|
|
245
|
-
const normalized = await normalizeOutgoingActivityJsonLd(msg, preloadedOnlyDocumentLoader);
|
|
246
|
-
if (normalized !== msg && await verifyCandidate(normalized)) return publicKey;
|
|
247
|
-
if (fetchedKey.cached) {
|
|
248
|
-
logger.debug("Failed to verify the proof with the cached key {keyId}; retrying with the freshly fetched key...", {
|
|
249
|
-
keyId: proof.verificationMethodId.href,
|
|
250
|
-
proof
|
|
251
|
-
});
|
|
252
|
-
return await verifyProofInternal(jsonLd, proof, {
|
|
253
|
-
...options,
|
|
254
|
-
keyCache: {
|
|
255
|
-
get: () => Promise.resolve(void 0),
|
|
256
|
-
set: async (keyId, key) => await options.keyCache?.set(keyId, key)
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
logger.debug("Failed to verify the proof with the fetched key {keyId}:\n{proof}", {
|
|
261
|
-
keyId: proof.verificationMethodId.href,
|
|
262
|
-
proof
|
|
263
|
-
});
|
|
264
|
-
return null;
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Verifies the given object. It will verify all the proofs in the object,
|
|
268
|
-
* and succeed only if all the proofs are valid and all attributions and
|
|
269
|
-
* actors are authenticated by the proofs.
|
|
270
|
-
* @template T The type of the object to verify.
|
|
271
|
-
* @param cls The class of the object to verify. It must be a subclass of
|
|
272
|
-
* the {@link Object}.
|
|
273
|
-
* @param jsonLd The JSON-LD object to verify. It's assumed that the object
|
|
274
|
-
* is a compacted JSON-LD representation of a `T` with `@context`.
|
|
275
|
-
* @param options Additional options. See also {@link VerifyObjectOptions}.
|
|
276
|
-
* @returns The object if it's verified, or `null` if it's not.
|
|
277
|
-
* @throws {TypeError} If the object is invalid or unsupported.
|
|
278
|
-
* @since 0.10.0
|
|
279
|
-
*/
|
|
280
|
-
async function verifyObject(cls, jsonLd, options = {}) {
|
|
281
|
-
const logger = getLogger([
|
|
282
|
-
"fedify",
|
|
283
|
-
"sig",
|
|
284
|
-
"proof"
|
|
285
|
-
]);
|
|
286
|
-
const object = await cls.fromJsonLd(jsonLd, options);
|
|
287
|
-
const attributions = new Set(object.attributionIds.map((uri) => uri.href));
|
|
288
|
-
if (object instanceof Activity) for (const uri of object.actorIds) attributions.add(uri.href);
|
|
289
|
-
for await (const proof of object.getProofs(options)) {
|
|
290
|
-
const key = await verifyProof(jsonLd, proof, options);
|
|
291
|
-
if (key === null) return null;
|
|
292
|
-
if (proof.verificationMethodId == null) return null;
|
|
293
|
-
if (key.controllerId == null) {
|
|
294
|
-
logger.debug("Key {keyId} does not have a controller.", { keyId: key.id?.href });
|
|
295
|
-
continue;
|
|
296
|
-
}
|
|
297
|
-
deleteAuthenticatedAttribution(attributions, key.controllerId, proof.verificationMethodId);
|
|
298
|
-
}
|
|
299
|
-
if (attributions.size > 0) {
|
|
300
|
-
logger.debug("Some attributions are not authenticated by the proofs: {attributions}.", { attributions: [...attributions] });
|
|
301
|
-
return null;
|
|
302
|
-
}
|
|
303
|
-
return object;
|
|
304
|
-
}
|
|
305
|
-
function deleteAuthenticatedAttribution(attributions, controllerId, verificationMethodId) {
|
|
306
|
-
const controllerHasCryptographicOrigin = hasCryptographicOrigin(controllerId.href);
|
|
307
|
-
const verificationMethodMatchesController = controllerHasCryptographicOrigin && hasCryptographicOrigin(verificationMethodId.href) && haveSameFe34Origin(controllerId, verificationMethodId);
|
|
308
|
-
if (!controllerHasCryptographicOrigin || verificationMethodMatchesController) attributions.delete(controllerId.href);
|
|
309
|
-
if (!verificationMethodMatchesController) return;
|
|
310
|
-
for (const attribution of [...attributions]) if (hasCryptographicOrigin(attribution) && haveSameFe34Origin(controllerId, attribution)) attributions.delete(attribution);
|
|
311
|
-
}
|
|
312
|
-
function hasCryptographicOrigin(iri) {
|
|
313
|
-
return /^did:/i.test(iri) || /^ap(?:\+ef61)?:\/\//i.test(iri);
|
|
314
|
-
}
|
|
315
|
-
//#endregion
|
|
316
|
-
export { verifyProof as a, verifyObject as i, hasProofLike as n, signObject as r, createProof as t };
|