@hot-updater/plugin-core 1.2.0-rc.2 → 1.2.0-rc.21
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/coreApi.d.cts +19 -4
- package/dist/coreApi.d.mts +19 -4
- package/dist/engine/core/schema.cjs +17 -1
- package/dist/engine/core/schema.d.cts +9 -0
- package/dist/engine/core/schema.d.mts +9 -0
- package/dist/engine/core/schema.mjs +17 -1
- package/dist/engine/createEngine.cjs +1 -2
- package/dist/engine/createEngine.d.cts +1 -6
- package/dist/engine/createEngine.d.mts +1 -6
- package/dist/engine/createEngine.mjs +1 -2
- package/dist/engine/database/aggregateBatching.cjs +24 -116
- package/dist/engine/database/aggregateBatching.mjs +24 -116
- package/dist/engine/database/database.cjs +1 -4
- package/dist/engine/database/database.mjs +1 -4
- package/dist/index.cjs +1 -7
- package/dist/index.d.cts +3 -4
- package/dist/index.d.mts +3 -4
- package/dist/index.mjs +2 -3
- package/dist/types/databaseConfig.d.cts +8 -19
- package/dist/types/databaseConfig.d.mts +8 -19
- package/dist/types/databaseRows.d.cts +5 -0
- package/dist/types/databaseRows.d.mts +5 -0
- package/dist/types/index.cjs +3 -0
- package/dist/types/index.d.cts +98 -59
- package/dist/types/index.d.mts +98 -59
- package/dist/types/index.mjs +3 -1
- package/dist/types/models.d.cts +9 -1
- package/dist/types/models.d.mts +9 -1
- package/dist/types/public.d.cts +2 -2
- package/dist/types/public.d.mts +2 -2
- package/package.json +3 -9
- package/dist/remoteBundleSigning.cjs +0 -357
- package/dist/remoteBundleSigning.d.cts +0 -38
- package/dist/remoteBundleSigning.d.mts +0 -38
- package/dist/remoteBundleSigning.mjs +0 -352
|
@@ -1,352 +0,0 @@
|
|
|
1
|
-
//#region src/remoteBundleSigning.ts
|
|
2
|
-
const REMOTE_BUNDLE_SIGNING_PATH = "/_hot-updater/signing";
|
|
3
|
-
const REMOTE_BUNDLE_SIGNING_PROTOCOL_VERSION = 1;
|
|
4
|
-
const REMOTE_BUNDLE_SIGNING_ALGORITHM = "RSA-SHA256";
|
|
5
|
-
const REMOTE_BUNDLE_SIGNING_TOKEN_HEADER = "x-hot-updater-signing-token";
|
|
6
|
-
const MESSAGE_BYTES = 32;
|
|
7
|
-
const REQUEST_BODY_LIMIT = 4096;
|
|
8
|
-
const RESPONSE_BODY_LIMIT = 32768;
|
|
9
|
-
const SIGNATURE_BYTES_LIMIT = 16384;
|
|
10
|
-
const TOKEN_BYTES_LIMIT = 4096;
|
|
11
|
-
const REQUEST_TIMEOUT_MS = 3e4;
|
|
12
|
-
const JSON_CONTENT_TYPE_PATTERN = /^application\/json(?:\s*;|$)/iu;
|
|
13
|
-
const BASE64_PATTERN = /^(?:[A-Za-z\d+/]{4})*(?:[A-Za-z\d+/]{2}==|[A-Za-z\d+/]{3}=)?$/u;
|
|
14
|
-
const PUBLIC_KEY_PATTERN = /^-----BEGIN PUBLIC KEY-----\s+([A-Za-z\d+/=\s]+)\s+-----END PUBLIC KEY-----$/u;
|
|
15
|
-
/**
|
|
16
|
-
* The name of the errors `remoteSigning` throws with a reason it wrote itself,
|
|
17
|
-
* which never holds the token or what an upstream library threw. The CLI
|
|
18
|
-
* shows these, and redacts any other signing adapter error, by this name.
|
|
19
|
-
*/
|
|
20
|
-
const REMOTE_SIGNING_ERROR_NAME = "HotUpdaterRemoteSigningError";
|
|
21
|
-
const ERROR_RESPONSE_BODY_LIMIT = 4096;
|
|
22
|
-
const ERROR_MESSAGE_LENGTH_LIMIT = 300;
|
|
23
|
-
const MISSING_TOKEN_MESSAGE = "remoteSigning requires signingToken or HOT_UPDATER_SIGNING_TOKEN.";
|
|
24
|
-
/** A failure whose reason is safe to show: it names no secret. */
|
|
25
|
-
var RemoteSigningFailure = class extends Error {};
|
|
26
|
-
const remoteSigningError = (summary, error) => {
|
|
27
|
-
if (!(error instanceof RemoteSigningFailure)) return new Error(summary);
|
|
28
|
-
const named = /* @__PURE__ */ new Error(`${summary} ${error.message}`);
|
|
29
|
-
named.name = REMOTE_SIGNING_ERROR_NAME;
|
|
30
|
-
return named;
|
|
31
|
-
};
|
|
32
|
-
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
33
|
-
const hasExactKeys = (value, expected) => {
|
|
34
|
-
const actual = Object.keys(value).sort();
|
|
35
|
-
const sortedExpected = [...expected].sort();
|
|
36
|
-
return actual.length === sortedExpected.length && actual.every((key, index) => key === sortedExpected[index]);
|
|
37
|
-
};
|
|
38
|
-
const bytesToBase64 = (value) => {
|
|
39
|
-
let binary = "";
|
|
40
|
-
for (let offset = 0; offset < value.byteLength; offset += 32768) binary += String.fromCharCode(...value.subarray(offset, offset + 32768));
|
|
41
|
-
return btoa(binary);
|
|
42
|
-
};
|
|
43
|
-
const base64ToBytes = (value) => {
|
|
44
|
-
if (!value || !BASE64_PATTERN.test(value)) return null;
|
|
45
|
-
try {
|
|
46
|
-
const binary = atob(value);
|
|
47
|
-
const result = Uint8Array.from(binary, (character) => character.charCodeAt(0));
|
|
48
|
-
return bytesToBase64(result) === value ? result : null;
|
|
49
|
-
} catch {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
const bytesToHex = (value) => Array.from(value, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
54
|
-
const toArrayBuffer = (value) => new Uint8Array(value).buffer;
|
|
55
|
-
const canonicalizePublicKey = async (publicKey) => {
|
|
56
|
-
const match = PUBLIC_KEY_PATTERN.exec(publicKey.trim());
|
|
57
|
-
const der = match?.[1] ? base64ToBytes(match[1].replaceAll(/\s/gu, "")) : null;
|
|
58
|
-
if (!der) throw new Error("invalid public key");
|
|
59
|
-
const cryptoKey = await crypto.subtle.importKey("spki", toArrayBuffer(der), {
|
|
60
|
-
hash: "SHA-256",
|
|
61
|
-
name: "RSASSA-PKCS1-v1_5"
|
|
62
|
-
}, true, ["verify"]);
|
|
63
|
-
const modulusLength = Reflect.get(cryptoKey.algorithm, "modulusLength");
|
|
64
|
-
if (![
|
|
65
|
-
2048,
|
|
66
|
-
3072,
|
|
67
|
-
4096
|
|
68
|
-
].includes(modulusLength)) throw new Error("unsupported public key");
|
|
69
|
-
const canonicalDer = new Uint8Array(await crypto.subtle.exportKey("spki", cryptoKey));
|
|
70
|
-
const lines = bytesToBase64(canonicalDer).match(/.{1,64}/gu) ?? [];
|
|
71
|
-
return {
|
|
72
|
-
cryptoKey,
|
|
73
|
-
keyId: bytesToHex(new Uint8Array(await crypto.subtle.digest("SHA-256", canonicalDer))),
|
|
74
|
-
publicKey: `-----BEGIN PUBLIC KEY-----\n${lines.join("\n")}\n-----END PUBLIC KEY-----\n`
|
|
75
|
-
};
|
|
76
|
-
};
|
|
77
|
-
const assertSafePath = (path) => {
|
|
78
|
-
if (!path.startsWith("/") || path.includes("\\") || path.includes("//")) throw new Error("invalid path");
|
|
79
|
-
for (const segment of path.split("/")) {
|
|
80
|
-
let decoded;
|
|
81
|
-
try {
|
|
82
|
-
decoded = decodeURIComponent(segment);
|
|
83
|
-
} catch {
|
|
84
|
-
throw new Error("invalid path");
|
|
85
|
-
}
|
|
86
|
-
if (decoded === "." || decoded === ".." || decoded.includes("/") || decoded.includes("\\") || decoded.includes("\0")) throw new Error("invalid path");
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
const invalidEndpoint = () => {
|
|
90
|
-
const error = /* @__PURE__ */ new Error("Remote bundle signing requires a valid endpoint.");
|
|
91
|
-
error.name = REMOTE_SIGNING_ERROR_NAME;
|
|
92
|
-
return error;
|
|
93
|
-
};
|
|
94
|
-
const normalizeEndpoint = (endpoint) => {
|
|
95
|
-
let url;
|
|
96
|
-
try {
|
|
97
|
-
url = new URL(endpoint);
|
|
98
|
-
} catch {
|
|
99
|
-
throw invalidEndpoint();
|
|
100
|
-
}
|
|
101
|
-
const rawPath = /^https?:\/\/[^/?#]*(\/[^?#]*)?/iu.exec(endpoint)?.[1] ?? "/";
|
|
102
|
-
try {
|
|
103
|
-
assertSafePath(rawPath);
|
|
104
|
-
} catch {
|
|
105
|
-
throw invalidEndpoint();
|
|
106
|
-
}
|
|
107
|
-
const isLoopback = url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "[::1]";
|
|
108
|
-
if (url.protocol !== "https:" && !(url.protocol === "http:" && isLoopback) || url.username || url.password || url.search || url.hash) throw invalidEndpoint();
|
|
109
|
-
const prefix = url.pathname.replace(/\/+$/u, "");
|
|
110
|
-
url.pathname = `${prefix}${REMOTE_BUNDLE_SIGNING_PATH}`;
|
|
111
|
-
return url.href;
|
|
112
|
-
};
|
|
113
|
-
const normalizeHandlerPath = (endpointPath) => {
|
|
114
|
-
try {
|
|
115
|
-
assertSafePath(endpointPath);
|
|
116
|
-
const url = new URL(endpointPath, "https://hot-updater.invalid");
|
|
117
|
-
if (url.origin !== "https://hot-updater.invalid" || url.search || url.hash || url.pathname !== endpointPath || !endpointPath.endsWith("/_hot-updater/signing")) throw new Error("invalid path");
|
|
118
|
-
return endpointPath;
|
|
119
|
-
} catch {
|
|
120
|
-
throw new Error("Bundle signing handler requires a valid endpoint path.");
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
const readLimitedBody = async (body, contentLength, limit) => {
|
|
124
|
-
if (contentLength !== null) {
|
|
125
|
-
if (!/^\d+$/u.test(contentLength) || Number(contentLength) > limit) throw new Error("body too large");
|
|
126
|
-
}
|
|
127
|
-
if (!body) return "";
|
|
128
|
-
const reader = body.getReader();
|
|
129
|
-
const chunks = [];
|
|
130
|
-
let size = 0;
|
|
131
|
-
try {
|
|
132
|
-
while (true) {
|
|
133
|
-
const { done, value } = await reader.read();
|
|
134
|
-
if (done) break;
|
|
135
|
-
size += value.byteLength;
|
|
136
|
-
if (size > limit) {
|
|
137
|
-
await reader.cancel();
|
|
138
|
-
throw new Error("body too large");
|
|
139
|
-
}
|
|
140
|
-
chunks.push(value);
|
|
141
|
-
}
|
|
142
|
-
} finally {
|
|
143
|
-
reader.releaseLock();
|
|
144
|
-
}
|
|
145
|
-
const joined = new Uint8Array(size);
|
|
146
|
-
let offset = 0;
|
|
147
|
-
for (const chunk of chunks) {
|
|
148
|
-
joined.set(chunk, offset);
|
|
149
|
-
offset += chunk.byteLength;
|
|
150
|
-
}
|
|
151
|
-
return new TextDecoder("utf-8", { fatal: true }).decode(joined);
|
|
152
|
-
};
|
|
153
|
-
const parseJsonBody = async (body, headers, limit) => {
|
|
154
|
-
if (!JSON_CONTENT_TYPE_PATTERN.test(headers.get("content-type") ?? "")) throw new Error("invalid content type");
|
|
155
|
-
return JSON.parse(await readLimitedBody(body, headers.get("content-length"), limit));
|
|
156
|
-
};
|
|
157
|
-
const parseMetadata = async (value) => {
|
|
158
|
-
if (!isRecord(value) || !hasExactKeys(value, [
|
|
159
|
-
"algorithm",
|
|
160
|
-
"keyId",
|
|
161
|
-
"protocolVersion",
|
|
162
|
-
"publicKey"
|
|
163
|
-
]) || value.protocolVersion !== 1 || value.algorithm !== "RSA-SHA256" || typeof value.keyId !== "string" || !/^[a-f\d]{64}$/u.test(value.keyId) || typeof value.publicKey !== "string") throw new Error("invalid metadata");
|
|
164
|
-
const resolved = await canonicalizePublicKey(value.publicKey);
|
|
165
|
-
if (resolved.keyId !== value.keyId) throw new Error("invalid metadata");
|
|
166
|
-
return resolved;
|
|
167
|
-
};
|
|
168
|
-
const remoteRequest = async (fetchImplementation, endpoint, resolveToken, init) => {
|
|
169
|
-
const token = await resolveToken();
|
|
170
|
-
if (typeof token !== "string" || !token.trim()) throw new Error("missing token");
|
|
171
|
-
let response;
|
|
172
|
-
try {
|
|
173
|
-
response = await fetchImplementation(endpoint, {
|
|
174
|
-
...init,
|
|
175
|
-
headers: {
|
|
176
|
-
Accept: "application/json",
|
|
177
|
-
...init.headers,
|
|
178
|
-
[REMOTE_BUNDLE_SIGNING_TOKEN_HEADER]: token
|
|
179
|
-
},
|
|
180
|
-
redirect: "error",
|
|
181
|
-
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
|
|
182
|
-
});
|
|
183
|
-
} catch {
|
|
184
|
-
throw new RemoteSigningFailure(`Could not reach ${endpoint}.`);
|
|
185
|
-
}
|
|
186
|
-
if (response.status !== 200) {
|
|
187
|
-
const refusal = await describeRefusal(response, token);
|
|
188
|
-
throw new RemoteSigningFailure(`${endpoint} answered ${refusal}${/[.!?]$/u.test(refusal) ? "" : "."}`);
|
|
189
|
-
}
|
|
190
|
-
return parseJsonBody(response.body, response.headers, RESPONSE_BODY_LIMIT);
|
|
191
|
-
};
|
|
192
|
-
/**
|
|
193
|
-
* A refusal's status, with the `{ error }` or `{ error: { code, message } }`
|
|
194
|
-
* a signing server answers it with, such as a scope refusal.
|
|
195
|
-
* The token is masked and the message shortened to one line.
|
|
196
|
-
*/
|
|
197
|
-
const describeRefusal = async (response, token) => {
|
|
198
|
-
let code;
|
|
199
|
-
let message;
|
|
200
|
-
try {
|
|
201
|
-
const body = await parseJsonBody(response.body, response.headers, ERROR_RESPONSE_BODY_LIMIT);
|
|
202
|
-
const error = isRecord(body) ? body.error : void 0;
|
|
203
|
-
if (typeof error === "string") message = error;
|
|
204
|
-
else if (isRecord(error)) {
|
|
205
|
-
code = error.code;
|
|
206
|
-
message = error.message;
|
|
207
|
-
}
|
|
208
|
-
} catch {}
|
|
209
|
-
const status = typeof code === "string" && /^[a-z][a-z0-9_]{0,63}$/u.test(code) ? `${response.status} ${code}` : String(response.status);
|
|
210
|
-
if (typeof message !== "string") return status;
|
|
211
|
-
const text = message.split(token).join("********").replace(/\p{Cc}+/gu, " ").trim();
|
|
212
|
-
if (!text) return status;
|
|
213
|
-
return `${status}: ${text.length > ERROR_MESSAGE_LENGTH_LIMIT ? `${text.slice(0, ERROR_MESSAGE_LENGTH_LIMIT)}…` : text}`;
|
|
214
|
-
};
|
|
215
|
-
/** Signs through a server implementing the Hot Updater signing protocol. */
|
|
216
|
-
const remoteSigning = ({ endpoint, fetch: fetchImplementation = globalThis.fetch, signingToken }) => {
|
|
217
|
-
if (typeof fetchImplementation !== "function") throw new Error("Remote bundle signing requires fetch.");
|
|
218
|
-
let normalizedEndpoint;
|
|
219
|
-
const endpointUrl = () => normalizedEndpoint ??= normalizeEndpoint(endpoint);
|
|
220
|
-
let resolvedKey;
|
|
221
|
-
const resolveToken = async () => {
|
|
222
|
-
const token = typeof signingToken === "function" ? await signingToken() : signingToken ?? process.env.HOT_UPDATER_SIGNING_TOKEN;
|
|
223
|
-
if (!token?.trim()) throw new RemoteSigningFailure(MISSING_TOKEN_MESSAGE);
|
|
224
|
-
return token;
|
|
225
|
-
};
|
|
226
|
-
const getResolvedKey = () => {
|
|
227
|
-
if (resolvedKey) return resolvedKey;
|
|
228
|
-
const url = endpointUrl();
|
|
229
|
-
resolvedKey = remoteRequest(fetchImplementation, url, resolveToken, { method: "GET" }).then(parseMetadata).catch((error) => {
|
|
230
|
-
resolvedKey = void 0;
|
|
231
|
-
throw remoteSigningError("Failed to load the remote bundle signing public key.", error);
|
|
232
|
-
});
|
|
233
|
-
return resolvedKey;
|
|
234
|
-
};
|
|
235
|
-
return {
|
|
236
|
-
name: "remoteSigning",
|
|
237
|
-
async getPublicKey() {
|
|
238
|
-
const { publicKey } = await getResolvedKey();
|
|
239
|
-
return { publicKey };
|
|
240
|
-
},
|
|
241
|
-
async sign({ message }) {
|
|
242
|
-
if (!(message instanceof Uint8Array) || message.byteLength !== MESSAGE_BYTES) throw new Error("Remote bundle signing messages must be exactly 32 bytes.");
|
|
243
|
-
const resolved = await getResolvedKey();
|
|
244
|
-
let value;
|
|
245
|
-
try {
|
|
246
|
-
value = await remoteRequest(fetchImplementation, endpointUrl(), resolveToken, {
|
|
247
|
-
body: JSON.stringify({
|
|
248
|
-
algorithm: REMOTE_BUNDLE_SIGNING_ALGORITHM,
|
|
249
|
-
keyId: resolved.keyId,
|
|
250
|
-
message: bytesToBase64(message),
|
|
251
|
-
protocolVersion: 1
|
|
252
|
-
}),
|
|
253
|
-
headers: { "Content-Type": "application/json" },
|
|
254
|
-
method: "POST"
|
|
255
|
-
});
|
|
256
|
-
} catch (error) {
|
|
257
|
-
throw remoteSigningError("Remote bundle signer failed to sign the message.", error);
|
|
258
|
-
}
|
|
259
|
-
if (!isRecord(value) || !hasExactKeys(value, [
|
|
260
|
-
"algorithm",
|
|
261
|
-
"keyId",
|
|
262
|
-
"protocolVersion",
|
|
263
|
-
"signature"
|
|
264
|
-
]) || value.protocolVersion !== 1 || value.algorithm !== "RSA-SHA256" || value.keyId !== resolved.keyId || typeof value.signature !== "string") throw new Error("Remote bundle signer returned an invalid signature.");
|
|
265
|
-
const signature = base64ToBytes(value.signature);
|
|
266
|
-
if (!signature || signature.byteLength > SIGNATURE_BYTES_LIMIT) throw new Error("Remote bundle signer returned an invalid signature.");
|
|
267
|
-
if (!await crypto.subtle.verify("RSASSA-PKCS1-v1_5", resolved.cryptoKey, toArrayBuffer(signature), toArrayBuffer(message))) throw new Error("Remote bundle signer returned an unverifiable signature.");
|
|
268
|
-
return { signature: new Uint8Array(signature) };
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
};
|
|
272
|
-
const jsonResponse = (value, status = 200, headers) => Response.json(value, {
|
|
273
|
-
headers: {
|
|
274
|
-
"Cache-Control": "private, no-store",
|
|
275
|
-
...headers
|
|
276
|
-
},
|
|
277
|
-
status
|
|
278
|
-
});
|
|
279
|
-
const isTokenSized = (value) => new TextEncoder().encode(value).byteLength <= TOKEN_BYTES_LIMIT;
|
|
280
|
-
const tokensMatch = async (expected, actual) => {
|
|
281
|
-
if (!expected || actual === null || !isTokenSized(expected) || !isTokenSized(actual)) return false;
|
|
282
|
-
const [expectedHash, actualHash] = await Promise.all([crypto.subtle.digest("SHA-256", new TextEncoder().encode(expected)), crypto.subtle.digest("SHA-256", new TextEncoder().encode(actual))]);
|
|
283
|
-
const left = new Uint8Array(expectedHash);
|
|
284
|
-
const right = new Uint8Array(actualHash);
|
|
285
|
-
let difference = 0;
|
|
286
|
-
for (let index = 0; index < left.byteLength; index += 1) difference |= left[index] ^ right[index];
|
|
287
|
-
return difference === 0;
|
|
288
|
-
};
|
|
289
|
-
/** Handles the portable managed signing protocol in Web API runtimes. */
|
|
290
|
-
const createBundleSigningHandler = async ({ endpointPath = REMOTE_BUNDLE_SIGNING_PATH, publicKey, request, sign, token }) => {
|
|
291
|
-
const normalizedEndpointPath = normalizeHandlerPath(endpointPath);
|
|
292
|
-
const url = new URL(request.url);
|
|
293
|
-
if (url.pathname !== normalizedEndpointPath) return null;
|
|
294
|
-
if (url.search || url.hash) return jsonResponse({ error: "Bad request" }, 400);
|
|
295
|
-
const presented = request.headers.get(REMOTE_BUNDLE_SIGNING_TOKEN_HEADER);
|
|
296
|
-
let accepted;
|
|
297
|
-
try {
|
|
298
|
-
accepted = typeof token === "function" ? presented !== null && presented.length > 0 && isTokenSized(presented) && await token(presented) === true : await tokensMatch(token, presented);
|
|
299
|
-
} catch {
|
|
300
|
-
return jsonResponse({ error: "Signing service unavailable" }, 503);
|
|
301
|
-
}
|
|
302
|
-
if (!accepted) return jsonResponse({ error: "Unauthorized" }, 401);
|
|
303
|
-
if (request.method !== "GET" && request.method !== "POST") return jsonResponse({ error: "Method not allowed" }, 405, { Allow: "GET, POST" });
|
|
304
|
-
let resolved;
|
|
305
|
-
try {
|
|
306
|
-
resolved = await canonicalizePublicKey(typeof publicKey === "function" ? await publicKey() : publicKey);
|
|
307
|
-
} catch {
|
|
308
|
-
return jsonResponse({ error: "Signing service unavailable" }, 503);
|
|
309
|
-
}
|
|
310
|
-
const metadata = {
|
|
311
|
-
algorithm: REMOTE_BUNDLE_SIGNING_ALGORITHM,
|
|
312
|
-
keyId: resolved.keyId,
|
|
313
|
-
protocolVersion: 1,
|
|
314
|
-
publicKey: resolved.publicKey
|
|
315
|
-
};
|
|
316
|
-
if (request.method === "GET") return jsonResponse(metadata);
|
|
317
|
-
let body;
|
|
318
|
-
try {
|
|
319
|
-
body = await parseJsonBody(request.body, request.headers, REQUEST_BODY_LIMIT);
|
|
320
|
-
} catch {
|
|
321
|
-
return jsonResponse({ error: "Bad request" }, 400);
|
|
322
|
-
}
|
|
323
|
-
if (!isRecord(body) || !hasExactKeys(body, [
|
|
324
|
-
"algorithm",
|
|
325
|
-
"keyId",
|
|
326
|
-
"message",
|
|
327
|
-
"protocolVersion"
|
|
328
|
-
]) || body.protocolVersion !== 1 || body.algorithm !== "RSA-SHA256" || body.keyId !== resolved.keyId || typeof body.message !== "string") return jsonResponse({ error: "Bad request" }, 400);
|
|
329
|
-
const message = base64ToBytes(body.message);
|
|
330
|
-
if (!message || message.byteLength !== MESSAGE_BYTES) return jsonResponse({ error: "Bad request" }, 400);
|
|
331
|
-
let signature;
|
|
332
|
-
try {
|
|
333
|
-
const result = await sign(new Uint8Array(message));
|
|
334
|
-
if (!(result instanceof Uint8Array) || result.byteLength === 0 || result.byteLength > SIGNATURE_BYTES_LIMIT) throw new Error("invalid signature");
|
|
335
|
-
signature = result;
|
|
336
|
-
} catch {
|
|
337
|
-
return jsonResponse({ error: "Signing service unavailable" }, 503);
|
|
338
|
-
}
|
|
339
|
-
try {
|
|
340
|
-
if (!await crypto.subtle.verify("RSASSA-PKCS1-v1_5", resolved.cryptoKey, toArrayBuffer(signature), toArrayBuffer(message))) return jsonResponse({ error: "Signing service unavailable" }, 503);
|
|
341
|
-
} catch {
|
|
342
|
-
return jsonResponse({ error: "Signing service unavailable" }, 503);
|
|
343
|
-
}
|
|
344
|
-
return jsonResponse({
|
|
345
|
-
algorithm: REMOTE_BUNDLE_SIGNING_ALGORITHM,
|
|
346
|
-
keyId: resolved.keyId,
|
|
347
|
-
protocolVersion: 1,
|
|
348
|
-
signature: bytesToBase64(signature)
|
|
349
|
-
});
|
|
350
|
-
};
|
|
351
|
-
//#endregion
|
|
352
|
-
export { REMOTE_BUNDLE_SIGNING_ALGORITHM, REMOTE_BUNDLE_SIGNING_PATH, REMOTE_BUNDLE_SIGNING_PROTOCOL_VERSION, REMOTE_BUNDLE_SIGNING_TOKEN_HEADER, createBundleSigningHandler, remoteSigning };
|