8000 Coalesce concurrent diagnostics log snapshots · Issue #291 · stratum-mining/sv2-ui · GitHub
[go: up one dir, main page]

Skip to content

Coalesce concurrent diagnostics log snapshots #291

Closed

Description

@lucasbalieiro

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


Finding

  • repo: stratum-mining/sv2-ui (https://github.com/stratum-mining/sv2-ui.git)
  • reviewed revision: 1a0845a2d56f3d2e964b4c68d6cbfa5a3d8794ba
  • title: Coalesce concurrent diagnostics log snapshots
  • severity: medium
  • location: server/src/logs/diagnostics.ts:103-117
  • cwe: CWE-400
  • scanner: llm-code-review
  • fingerprint: 38eae9365f4e11840544d34137e04241e820bbacca04fd3644100148922ca0e1

Description

readCollatedLogLines() starts a fresh Promise.all() of Docker log reads for every caller and has no shared in-flight snapshot or concurrency limit. Its production caller is the unauthenticated GET /api/logs/diagnostics route; Express listens on all interfaces by default, and the UI itself polls this route every three seconds. A reachable adversary can issue many simultaneous requests, causing each request to start one read per configured container. Every operation asks Docker for 200 records, parses and sorts the returned arrays, runs all diagnostic parsers, and is serialized independently, so parallel requests multiply Docker-socket work, heap use, and CPU until the management service becomes unavailable. The two-second Docker abort limits duration but does not cap the number of simultaneous requests. Single-flight snapshots by mode/provider (with cleanup after rejection) or impose a server-side concurrency/rate limit. The regression test holds the provider behind a gate, launches 32 JD-mode diagnostics calls, and expects a single two-container snapshot; HEAD invokes it 64 times. Prior #8050 is a separate race in ActivePoolTracker on /api/status; #4648 covers tail=all; dismissed #13066 covers per-record byte bounds. None coalesces this diagnostics path.

Proof of Concept

diff --git a/server/src/logs/diagnostics.test.ts b/server/src/logs/diagnostics.test.ts
--- a/server/src/logs/diagnostics.test.ts
+++ b/server/src/logs/diagnostics.test.ts
@@ -55,3 +55,33 @@ test('rethrows non-missing-container log failures', async () => {
     /docker socket disappeared/
   );
 });
+
+test('coalesces simultaneous diagnostics snapshots', async () => {
+  let readCount = 0;
+  let releaseReads!: () => void;
+  const readGate = new Promise<void>((resolve) => {
+    releaseReads = resolve;
+  });
+  const readLogs = async (): Promise<ContainerLogLine[]> => {
+    readCount += 1;
+    await readGate;
+    return [];
+  };
+
+  const requests = Array.from(
+    { length: 32 },
+    () => getLogDiagnostics('jd', true, readLogs)
+  );
+
+  try {
+    await new Promise<void>((resolve) => setImmediate(resolve));
+    assert.equal(
+      readCount,
+      2,
+      'concurrent callers should share one two-container Docker log snapshot'
+    );
+  } finally {
+    releaseReads();
+    await Promise.all(requests);
+  }
+});

Suggested Fix

diff --git a/server/src/logs/diagnostics.ts b/server/src/logs/diagnostics.ts
--- a/server/src/logs/diagnostics.ts
+++ b/server/src/logs/diagnostics.ts
@@ -21,6 +21,11 @@ export type LogProvider = (
   options?: { tail?: number }
 ) => Promise<ContainerLogLine[]>;
 
+const inFlightSnapshots = new WeakMap<
+  LogProvider,
+  Map<SetupMode | null, Promise<ContainerLogLine[]>>
+>();
+
 function isMissingContainerError(error: unknown): boolean {
   let current = error;
 
@@ -101,7 +106,18 @@ export async function readCollatedLogLines(
     return [];
   }
 
-  const logSets = await Promise.all(
+  let snapshots = inFlightSnapshots.get(readLogs);
+  if (!snapshots) {
+    snapshots = new Map();
+    inFlightSnapshots.set(readLogs, snapshots);
+  }
+
+  const inFlight = snapshots.get(mode);
+  if (inFlight) {
+    return inFlight;
+  }
+
+  const snapshot = Promise.all(
     containers.map(async (container) => {
       try {
         return await readLogs(container, { tail: RECENT_LOG_TAIL });
@@ -117,9 +133,17 @@ export async function readCollatedLogLines(
         throw error;
       }
     })
-  );
+  ).then((logSets) => logSets.flat().sort(sortLines));
+
+  snapshots.set(mode, snapshot);
 
-  return logSets.flat().sort(sortLines);
+  try {
+    return await snapshot;
+  } finally {
+    if (snapshots.get(mode) === snapshot) {
+      snapshots.delete(mode);
+    }
+  }
 }
 
 export async function getLogDiagnostics(

This finding was discovered by Project Loupe.

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:logs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0