8000 Bound container output before buffering it in memory · Issue #289 · stratum-mining/sv2-ui · GitHub
[go: up one dir, main page]

Skip to content

Bound container output before buffering it in memory #289

Closed

Description

@lucasbalieiro

tracking https://github.com/project-loupe/audit-sv2-ui/issues/25


Finding

  • repo: stratum-mining/sv2-ui (https://github.com/stratum-mining/sv2-ui.git)
  • reviewed revision: ba9d73547e751247c4aa79257b54d3d706f42620
  • title: Bound container output before buffering it in memory
  • severity: medium
  • location: server/src/sv2-app-config-compatibility.ts:140-143
  • cwe: CWE-400
  • scanner: llm-code-review
  • fingerprint: 2dd5fea4723baf0c2bdb7b6450d72273eb979f6932efb79161f00f9f32775756

Description

runConfigCommand pipes both stdout and stderr from docker run and appends every chunk to a single JavaScript string without any size limit. The child is deliberately allowed to execute for 2.5 seconds, and cleanup can wait another 10 seconds. A selected compatibility image that continuously writes output can therefore make the Node process allocate memory until V8 aborts, rather than returning a bounded compatibility failure. This is externally triggerable because the command executes the selected container image and treats its output as untrusted parser diagnostics; the current image selections are registry references rather than local binaries. In CI this can kill the compatibility job or exhaust a self-hosted runner, blocking validation and release workflows. The PoC places a fake docker executable first in PATH; its run command emits a backpressure-aware 1 MiB stream. It launches the real compatibility script with a 64 MiB heap and asserts that the script terminates normally without an out-of-memory diagnostic. HEAD fails because lines 141-143 retain the entire stream. A fix should retain only a capped diagnostic tail/head and optionally terminate the child once the diagnostic limit is exceeded.

Proof of Concept

diff --git a/server/src/sv2-app-config-compatibility.test.ts b/server/src/sv2-app-config-compatibility.test.ts
new file mode 100644
--- /dev/null
+++ b/server/src/sv2-app-config-compatibility.test.ts
@@ -0,0 +1,49 @@
+import assert from 'node:assert/strict';
+import { spawn } from 'node:child_process';
+import { chmod, mkdtemp, rm, writeFile } from 'node:fs/promises';
+import os from 'node:os';
+import path from 'node:path';
+import { test } from 'node:test';
+import { fileURLToPath } from 'node:url';
+
+test('bounds output collected from a compatibility-check container', async () => {
+  const fakeBinDir = await mkdtemp(path.join(os.tmpdir(), 'sv2-ui-fake-docker-'));
+  const dockerPath = path.join(fakeBinDir, 'docker');
+  const repoRoot = fileURLToPath(new URL('../..', import.meta.url));
+
+  try {
+    await writeFile(dockerPath, `#!/usr/bin/env node
+if (process.argv[2] !== 'run') process.exit(0);
+const chunk = 'x'.repeat(1024 * 1024);
+function writeChunk() {
+  if (process.stdout.write(chunk)) setImmediate(writeChunk);
+  else process.stdout.once('drain', writeChunk);
+}
+process.on('SIGINT', () => process.exit(0));
+writeChunk();
+`);
+    await chmod(dockerPath, 0o755);
+
+    const result = await new Promise<{ code: number | null; signal: NodeJS.Signals | null; stderr: string }>((resolve, reject) => {
+      const child = spawn(process.execPath, [
+        '--max-old-space-size=64',
+        '--import',
+        'tsx',
+        'server/src/sv2-app-config-compatibility.ts',
+      ], {
+        cwd: repoRoot,
+        env: { ...process.env, PATH: `${fakeBinDir}:${process.env.PATH ?? ''}` },
+        stdio: ['ignore', 'ignore', 'pipe'],
+      });
+      let stderr = '';
+      child.stderr.on('data', (chunk: Buffer) => { stderr += chunk.toString(); });
+      child.once('error', reject);
+      child.once('exit', (code, signal) => resolve({ code, signal, stderr }));
+    });
+
+    assert.equal(result.signal, null, result.stderr);
+    assert.doesNotMatch(result.stderr, /heap out of memory|reached heap limit/i);
+  } finally {
+    await rm(fakeBinDir, { recursive: true, force: true });
+  }
+});

Suggested Fix

diff --git a/server/src/sv2-app-config-compatibility.ts b/server/src/sv2-app-config-compatibility.ts
--- a/server/src/sv2-app-config-compatibility.ts
+++ b/server/src/sv2-app-config-compatibility.ts
@@ -20,6 +20,7 @@ import type { SetupData } from './types.js';
 
 const execFileAsync = promisify(execFile);
 const CONFIG_ACCEPTANCE_WINDOW_MS = 2_500;
+const MAX_CONFIG_OUTPUT_LENGTH = 1024 * 1024;
 const TEST_POOL_PORT = 34254;
 const MALFORMED_CONFIG = 'invalid = [\n';
 
@@ -138,8 +139,14 @@ async function runConfigCommand(
   ];
   const child = spawn('docker', args, { stdio: ['ignore', 'pipe', 'pipe'] });
   let output = '';
-  child.stdout.on('data', (chunk: Buffer) => { output += chunk.toString(); });
-  child.stderr.on('data', (chunk: Buffer) => { output += chunk.toString(); });
+  const appendOutput = (chunk: Buffer): void => {
+    const remaining = MAX_CONFIG_OUTPUT_LENGTH - output.length;
+    if (remaining > 0) {
+      output += chunk.toString().slice(0, remaining);
+    }
+  };
+  child.stdout.on('data', appendOutput);
+  child.stderr.on('data', appendOutput);
 
   const exited = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve, reject) => {
     child.once('error', reject);

This finding was discovered by Project Loupe.

Activity

  1. added
    area:docker
    and removed on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:docker

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0