#1850·linkwarden

Remote Browser TOCTOU SSRF & Unbounded Worker Memory DoS

Author: codeCraft-RitikCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbug

Overview

In apps/worker/lib/protectPageRequests.ts and apps/worker/lib/preservationScheme/handleScreenshotAndPdf.ts:

  1. Remote Browser TOCTOU / SSRF Rebinding: When running with PLAYWRIGHT_WS_URL (the standard Docker/production architecture for remote Chromium browsers), assertUrlIsSafeForServerSideFetch(request.url()) validates the URL on the Linkwarden worker host. Once validated, route.continue() instructs the remote Chromium instance to fetch the asset. Chromium performs its own DNS lookup. An attacker using DNS rebinding (low TTL DNS pointing to a public IP first, then resolving to 127.0.0.1 or 169.254.169.254) bypasses SSRF restrictions and accesses internal networks and cloud instance metadata.
  2. Worker Memory Exhaustion (OOM DoS): In local browser mode, all sub-resources on an archived page are intercepted and fetched via safeFetch, followed by await response.buffer(). There is no response size limit or streaming cap. Any webpage embedding large assets (such as multiple 4K videos or large binary blobs) forces the worker process to buffer hundreds of megabytes directly into Node.js heap memory, leading to JavaScript heap out of memory process crashes.
  3. Leaked Interval in autoScroll: In handleScreenshotAndPdf.ts, when timeoutPromise resolves in Promise.race([scrollingPromise, timeoutPromise]), clearInterval(scrollDown) is never called. The timer continues executing window.scrollBy every 100ms in the page background while screenshots and PDFs are actively captured, causing visual tearing and resource leakage.

Impact

  • Security: Private network and cloud metadata access via remote Playwright browser workers.
  • Reliability: Uncontrolled Node.js worker crashes resulting in stuck queues and unarchived links for all users.
  • Preservation Quality: Corrupted screenshots and fragmented PDF archives due to scrolling races during rendering.

Affected Files & Locations

  • apps/worker/lib/protectPageRequests.ts (Lines 27–65)
  • apps/worker/lib/preservationScheme/handleScreenshotAndPdf.ts (Lines 96–119)
typescript
// apps/worker/lib/protectPageRequests.ts
27: if (remoteBrowser) {
28:   try {
29:     await assertUrlIsSafeForServerSideFetch(request.url());
30:     await route.continue(); // ❌ TOCTOU: Remote browser re-resolves DNS independently!
31:   } catch (error) { ... }
...
45: const response = await safeFetch(request.url(), { ... });
...
56: await route.fulfill({
57:   status: response.status,
58:   headers: responseHeaders,
59:   body: await response.buffer(), // ❌ UNBOUNDED BUFFER: Can exhaust Node.js heap!
60: });
typescript
// apps/worker/lib/preservationScheme/handleScreenshotAndPdf.ts
96: const autoScroll = async (AUTOSCROLL_TIMEOUT: number) => {
97:   const timeoutPromise = new Promise<void>((resolve) => {
98:     setTimeout(() => resolve(), AUTOSCROLL_TIMEOUT * 1000);
99:   });
100:  const scrollingPromise = new Promise<void>((resolve) => {
...
106:    let scrollDown = setInterval(() => {
...
111:        clearInterval(scrollDown);
...
115:    }, 100);
116:  });
117:
118:  await Promise.race([scrollingPromise, timeoutPromise]); // ❌ If timeoutPromise wins, scrollDown is NEVER CLEARED!
119: };

Steps to Reproduce

  1. Worker OOM: Add a link to a webpage that contains <img src="https://example.com/large-100mb-file.bin" /> multiple times.
  2. Watch the worker logs as response.buffer() consumes memory until the Node process terminates with: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
  3. Timer Leak: Bookmark an infinite-scroll page (e.g., social feed) with AUTOSCROLL_TIMEOUT=5. Observe that DOM scrolling events continue firing after the 5-second timeout during screenshot capture.

Proposed Fix

  1. In protectPageRequests.ts: Enforce a maximum sub-resource buffer size (e.g., 15MB) and abort oversized streams:
diff
--- a/apps/worker/lib/protectPageRequests.ts
+++ b/apps/worker/lib/protectPageRequests.ts
@@ -38,6 +38,15 @@ export default async function protectPageRequests(context: BrowserContext) {
     }

     try {
+      const MAX_RESOURCE_SIZE = 15 * 1024 * 1024; // 15MB cap per sub-resource
       const headers = request.headers();
       delete headers["accept-encoding"];
       delete headers["content-length"];
@@ -56,7 +65,13 @@ export default async function protectPageRequests(context: BrowserContext) {
+      const buffer = await response.buffer();
+      if (buffer.length > MAX_RESOURCE_SIZE) {
+        await route.abort("blockedbyclient");
+        return;
+      }
+
       await route.fulfill({
         status: response.status,
         headers: responseHeaders,
-        body: await response.buffer(),
+        body: buffer,
       });
  1. In handleScreenshotAndPdf.ts: Clear the timer handle in a finally block:
diff
--- a/apps/worker/lib/preservationScheme/handleScreenshotAndPdf.ts
+++ b/apps/worker/lib/preservationScheme/handleScreenshotAndPdf.ts
@@ -96,6 +96,8 @@ const handleScreenshotAndPdf = async (
 const autoScroll = async (AUTOSCROLL_TIMEOUT: number) => {
+  let scrollDown: ReturnType<typeof setInterval> | undefined;
+
   const timeoutPromise = new Promise<void>((resolve) => {
     setTimeout(() => {
       resolve();
@@ -105,7 +107,7 @@ const autoScroll = async (AUTOSCROLL_TIMEOUT: number) => {
   const scrollingPromise = new Promise<void>((resolve) => {
     let totalHeight = 0;
     let distance = 100;
-    let scrollDown = setInterval(() => {
+    scrollDown = setInterval(() => {
       let scrollHeight = document.body.scrollHeight;
       window.scrollBy(0, distance);
       totalHeight += distance;
@@ -115,7 +117,11 @@ const autoScroll = async (AUTOSCROLL_TIMEOUT: number) => {
     }, 100);
   });

-  await Promise.race([scrollingPromise, timeoutPromise]);
+  try {
+    await Promise.race([scrollingPromise, timeoutPromise]);
+  } finally {
+    if (scrollDown) clearInterval(scrollDown);
+  }
 };