Node.js based forum software built for the modern web
fix(request): add response size limit to prevent memory exhaustion
Julian Lam0f2cc7024e79
diff --git a/src/activitypub/index.js b/src/activitypub/index.jsindex b17799b92e..8026fd0da5 100644--- a/src/activitypub/index.js+++ b/src/activitypub/index.js@@ -257,9 +257,10 @@ ActivityPub.fetchPublicKey = async (uri, ip) => {try {// Use request.get with built-in SSRF protections- // Set reasonable timeout+ // Set reasonable timeout and response size limitconst { body } = await request.get(uri, {timeout: 5000, // 5 seconds+ sizeLimit: 1024 * 1024, // 1MB — public keys are typically <10KBheaders: {'accept': ActivityPub._constants.acceptableTypes.at(1),},diff --git a/src/request.js b/src/request.jsindex 576626f6eb..17be455803 100644--- a/src/request.js+++ b/src/request.js@@ -111,7 +111,7 @@ const dispatcher = new NodeBBAgent({});const manualDispatcher = new Dispatcher1Wrapper(dispatcher);-async function call(url, method, { body, timeout, jar, ...config } = {}) {+async function call(url, method, { body, timeout, jar, sizeLimit = 10 * 1024 * 1024, ...config } = {}) {const originalUrl = url;let currentUrl = url;let redirectCount = 0; // Add redirect counter@@ -174,7 +174,14 @@ async function call(url, method, { body, timeout, jar, ...config } = {}) {const contentType = headers.get('content-type');const isJSON = contentType && jsonTest.test(contentType);// eslint-disable-next-line no-await-in-loop- let respBody = await response.text();+ const buffer = await response.arrayBuffer();++ // Enforce response size limit to prevent memory exhaustion+ if (buffer.byteLength > sizeLimit) {+ throw new Error(`Response size (${buffer.byteLength} bytes) exceeds limit (${sizeLimit} bytes)`);+ }++ let respBody = new TextDecoder().decode(buffer);if (isJSON && respBody) {try {