Node.js based forum software built for the modern web
fix: enforce response size during streaming and require Digest for inbox requests
Julian Lamcee29dc548db
diff --git a/src/activitypub/signatures.js b/src/activitypub/signatures.jsindex 1cd00b6e46..fefe53ab92 100644--- a/src/activitypub/signatures.js+++ b/src/activitypub/signatures.js@@ -95,7 +95,12 @@ Signatures.verify = async (req, fetchPublicKeyFn) => {return false;}- // Digest check for requests with body (POST/PUT)+ const hasBody = req.rawBody || req.body;+ if (hasBody && !headers.digest) {+ winston.warn('[activitypub/signatures] Digest header required for requests with a body');+ return false;+ }+if (headers.digest) {const bodyData = req.rawBody || (typeof req.body === 'string' ? req.body : JSON.stringify(req.body));if (!bodyData) return false;@@ -155,6 +160,15 @@ async function tryVerifyDraft(req, fetchPublicKeyFn) {return false;}+ if (req.headers.digest) {+ const signedHeaders = (parsed.value.params?.headers ?? [])+ .map(h => h.toLowerCase());+ if (!signedHeaders.includes('digest')) {+ winston.warn('[activitypub/signatures] Digest header present but not included in signed headers (draft)');+ return false;+ }+ }+// Fetch public key PEMconst publicKeyPem = await fetchPublicKeyFn(parsed.value.keyId, req.ip);if (!publicKeyPem) {@@ -195,6 +209,15 @@ async function tryVerifyRFC9421(req, fetchPublicKeyFn) {return false;}+ if (req.headers.digest) {+ const signedHeaders = ((parsed.value?.params?.headers ?? []) || [])+ .map(h => h.toLowerCase());+ if (!signedHeaders.includes('digest')) {+ winston.warn('[activitypub/signatures] Digest header present but not included in signed headers (RFC 9421)');+ return false;+ }+ }+// Fetch public key PEMconst publicKeyPem = await fetchPublicKeyFn(keyId, req.ip);if (!publicKeyPem) {diff --git a/src/request.js b/src/request.jsindex 1ef5ee2cc6..5b955a766e 100644--- a/src/request.js+++ b/src/request.js@@ -141,6 +141,7 @@ async function call(url, method, { body, timeout, jar, sizeLimit = 10 * 1024 * 1...config.headers,},signal: timeout > 0 ? AbortSignal.timeout(timeout) : undefined,+ size: sizeLimit,dispatcher: manualDispatcher,};if (body instanceof FormData) {@@ -177,10 +178,17 @@ async function call(url, method, { body, timeout, jar, sizeLimit = 10 * 1024 * 1const { headers } = response;const contentType = headers.get('content-type');const isJSON = contentType && jsonTest.test(contentType);- // eslint-disable-next-line no-await-in-loop- const buffer = await response.arrayBuffer();- // Enforce response size limit to prevent memory exhaustion+ let buffer;+ try {+ buffer = await response.arrayBuffer();+ } catch (err) {+ // undici throws TypeError when streaming size is exceeded; rethrow+ if (err.name === 'TypeError' && String(err.message).includes('exceeded')) {+ throw new Error(`Response size exceeded limit (${sizeLimit} bytes)`);+ }+ throw err;+ }if (buffer.byteLength > sizeLimit) {throw new Error(`Response size (${buffer.byteLength} bytes) exceeds limit (${sizeLimit} bytes)`);}