8000 dashboard: a malformed counter in a monitoring response never breaks the dashboard · Issue #311 · stratum-mining/sv2-ui · GitHub
[go: up one dir, main page]

Skip to content

dashboard: a malformed counter in a monitoring response never breaks the dashboard #311

Open

Description

@lucasbalieiro

Affected layers

  • Frontend (src/): affected. mergeMetricEntries and mergeShareStatsEntries in src/hooks/usePersistentDashboardMetrics.ts pass live counters straight to Math.max. The loader that reads persisted state runs every counter through normalizeCount, which keeps only finite numbers, so live data is the one path without that check. The counters come unchanged from the monitoring responses: tileMetricEntries copies blocks_found and best_diff from each channel, and UnifiedDashboard copies shares_acknowledged, shares_submitted, shares_rejected and shares_rejected_by_reason from the server channels. These fields are taken from sv2-apps, which serializes them from unsigned integers, so an unmodified JDC or tProxy cannot put anything but numbers there.
  • Backend (server/): not affected. The two monitoring proxies in server/src/index.ts forward the upstream body as text without parsing it, so nothing there throws; they do not filter it either.
  • shared/: not affected. shared/openapi.json declares each of these counters as a non-negative integer; the defect is that the dashboard assumes the contract instead of checking it.

Problem

A counter such as {"toString":null} is valid JSON and passes response.json() in usePoolData. Math.max converts each argument to a number, and converting that object throws a TypeError. One channel with one such field is enough, including a channel whose only odd field is a rejection reason count such as shares_rejected_by_reason.stale.

The merge runs inside the state updater that usePersistentMetric and usePersistentShareStatsEntries schedule from an effect. The only try/catch around it covers the localStorage write, and App.tsx mounts no error boundary, so the exception reaches React and takes the whole dashboard down. The same response arrives again on the next poll and after a reload, so the dashboard stays down until the response changes. Other non-number values are not rejected either: a counter sent as the string "12" is coerced and stored as 12.

Impact

The operator loses the dashboard, not the mining service. It needs an adversary who can put malformed JSON into a monitoring response: control of the JDC or tProxy monitoring service, for example from a compromised mining container, or tampering on the path between the backend and it. Whether an ordinary miner or pool can make sv2-apps emit such a value is unknown, and the finding does not claim it. Severity low.

Expected outcome

  • A monitoring response in which a counter field is not a finite number never breaks the dashboard. The dashboard keeps rendering, the affected counter keeps the value it already had, and the other entries in the same response are merged as usual.
  • Live counters are held to the same checks as persisted ones: a value the loader would reject when reading localStorage is rejected when it arrives live, instead of being coerced.
  • Regression tests cover both merges, with entries built from a representative /server/channels payload in which one field is replaced by a malformed value, as docs/monitoring-api-compatibility.md asks.

Possible approach (non-binding)

Route every live counter through the normalizeCount already used at load time, or a stricter variant that reports an invalid value as absent rather than as 0. With the running-maximum merge both choices keep the stored value, since 0 never exceeds it. The tests belong in src/hooks/usePersistentDashboardMetrics.test.ts; the channel fixtures in src/lib/dashboardTileMetrics.test.ts show how to build the entries from the upstream channel shape. The Loupe PoC lists the five cases.

Loupe findings

Related issues and PRs

Loupe export for #311 (finding #90)

Finding

  • repo: stratum-mining/sv2-ui (https://github.com/stratum-mining/sv2-ui.git)
  • reviewed revision: ed3ad984d07e114b2c2ebc1cbd8f7cc8face03e8
  • title: Reject malformed live counters before numeric coercion
  • severity: low
  • location: src/hooks/usePersistentDashboardMetrics.ts:282-302
  • cwe: CWE-20
  • scanner: llm-code-review
  • fingerprint: ab5605ff7a928ba5847481125d5eb0bb36efb06d08643787431e8f8361fd8515

Description

Live telemetry bypasses the type validation used when loading persisted counters. mergeShareStatsEntries passes incoming reason counts and acknowledged/submitted/rejected values directly to Math.max; mergeMetricEntries does the same at line 216. A small, valid JSON value such as {"toString":null} in any of these numeric fields makes numeric coercion throw TypeError: Cannot convert object to primitive value.

The monitoring proxies in server/src/index.ts forward response bodies unchanged, usePoolData.ts returns response.json() without runtime validation, and UnifiedDashboard.tsx copies these fields into the merge inputs. Thus an adversary controlling or tampering with a monitoring API response can crash the dashboard state update using one otherwise ordinary channel, including by changing only shares_rejected_by_reason.stale. The merge runs outside the storage try/catch at lines 392-402, and App.tsx supplies no error boundary; the expected React consequence is loss of the dashboard until the offending response stops. This is UI availability impact, not mining-service compromise.

Exploitability assumption: the adversary must be able to supply malformed monitoring JSON. Whether an ordinary miner or pool can induce this through the out-of-tree monitoring implementation is unknown; no such capability is claimed. Browser/React error handling was not exercised. With tsx unavailable, the five node:test regression cases were run against the actual pure functions using Node's built-in TypeScript stripping: all fail on HEAD and pass when the coercions are replaced in memory with existing normalizeCount calls. Prior #8071, #13076, #13077, #19015 and #893 concern resource/cardinality failures, not this constant-size coercion exception.

Proof of Concept

diff --git a/src/hooks/usePersistentDashboardMetrics.live-counters.test.ts b/src/hooks/usePersistentDashboardMetrics.live-counters.test.ts
new file mode 100644
--- /dev/null
+++ b/src/hooks/usePersistentDashboardMetrics.live-counters.test.ts
@@ -0,0 +1,68 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+
+import {
+  mergeMetricEntries,
+  mergeShareStatsEntries,
+  type PersistedMetricEntry,
+  type PersistedShareStatsEntry,
+} from './usePersistentDashboardMetrics.js';
+
+const key = 'translator:server:extended:1:worker';
+
+test('malformed live metric counters cannot throw during a dashboard update', () => {
+  const entries: PersistedMetricEntry[] = JSON.parse(
+    '[{"key":"translator:server:extended:1:worker","value":{"toString":null}}]',
+  );
+  const previous = { [key]: 7 };
+  let next = previous;
+
+  assert.doesNotThrow(() => {
+    next = mergeMetricEntries(previous, entries).next;
+  });
+  assert.equal(next[key], 7, 'an invalid sample must preserve the existing count');
+});
+
+for (const field of ['acknowledged', 'submitted', 'rejected'] as const) {
+  test(`malformed live ${field} cannot throw during a dashboard update`, () => {
+    const previous = {
+      [key]: { acknowledged: 7, submitted: 8, rejected: 1, rejectedByReason: {} },
+    };
+    const entry: PersistedShareStatsEntry = {
+      key,
+      acknowledged: 7,
+      submitted: 8,
+      rejected: 1,
+      [field]: JSON.parse('{"toString":null}'),
+    };
+    let next = previous;
+
+    assert.doesNotThrow(() => {
+      next = mergeShareStatsEntries(previous, [entry]).next;
+    });
+    assert.equal(next[key][field], previous[key][field]);
+  });
+}
+
+test('malformed live rejection counts cannot crash an otherwise valid channel', () => {
+  const channel = JSON.parse(
+    '{"channel_id":1,"user_identity":"worker","shares_acknowledged":7,' +
+    '"shares_submitted":8,"shares_rejected":1,' +
+    '"shares_rejected_by_reason":{"stale":{"toString":null}}}',
+  );
+  // Use the same field mapping as UnifiedDashboard's server-channel telemetry.
+  const entries: PersistedShareStatsEntry[] = [{
+    key: `translator:server:extended:${channel.channel_id}:${channel.user_identity}`,
+    acknowledged: channel.shares_acknowledged,
+    submitted: channel.shares_submitted,
+    rejected: channel.shares_rejected,
+    rejectedByReason: channel.shares_rejected_by_reason,
+  }];
+  let result: ReturnType<typeof mergeShareStatsEntries> | undefined;
+
+  assert.doesNotThrow(() => {
+    result = mergeShareStatsEntries({}, entries);
+  });
+  assert.equal(result?.next[key].submitted, 8);
+  assert.equal(result?.next[key].rejectedByReason.stale ?? 0, 0);
+});

Suggested Fix

diff --git a/src/hooks/usePersistentDashboardMetrics.ts b/src/hooks/usePersistentDashboardMetrics.ts
--- a/src/hooks/usePersistentDashboardMetrics.ts
+++ b/src/hooks/usePersistentDashboardMetrics.ts
@@ -213,7 +213,7 @@ export function mergeMetricEntries(
   );
 
   boundedEntries.forEach(({ key, value }) => {
-    const normalizedValue = Math.max(0, value);
+    const normalizedValue = normalizeCount(value);
     if ((next[key] ?? 0) < normalizedValue) {
       next[key] = normalizedValue;
       changed = true;
@@ -280,7 +280,7 @@ export function mergeShareStatsEntries(
     const incomingReasonNames = new Set(incomingReasons.map(([reason]) => reason));
 
     for (const [reason, count] of incomingReasons) {
-      const normalizedCount = Math.max(0, count);
+      const normalizedCount = normalizeCount(count);
       if ((rejectedByReason[reason] ?? 0) < normalizedCount) {
         if (
           !(reason in rejectedByReason) &&
@@ -297,9 +297,9 @@ export function mergeShareStatsEntries(
     }
 
     const nextEntry = {
-      acknowledged: Math.max(current.acknowledged, Math.max(0, entry.acknowledged)),
-      submitted: Math.max(current.submitted, Math.max(0, entry.submitted)),
-      rejected: Math.max(current.rejected, Math.max(0, entry.rejected)),
+      acknowledged: Math.max(current.acknowledged, normalizeCount(entry.acknowledged)),
+      submitted: Math.max(current.submitted, normalizeCount(entry.submitted)),
+      rejected: Math.max(current.rejected, normalizeCount(entry.rejected)),
       rejectedByReason,
     };
 

This finding was discovered by Project Loupe.

Activity

  1. added
    area:dashboard
    and removed on Oct 14, 2026
  2. changed the title [-]Reject malformed live counters before numeric coercion[/-] [+]dashboard: a malformed counter in a monitoring response never breaks the dashboard[/+] 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:dashboard

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0