8000 Paginate server-channel responses used by monitoring modes · Issue #338 · stratum-mining/sv2-ui · GitHub
[go: up one dir, main page]

Skip to content

Paginate server-channel responses used by monitoring modes #338

Closed as duplicate

Description

@lucasbalieiro

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


Finding

  • repo: stratum-mining/sv2-ui (https://github.com/stratum-mining/sv2-ui.git)
  • reviewed revision: b610455ac2d1e017c1df5db8787c0b2e505eabdc
  • title: Paginate server-channel responses used by monitoring modes
  • severity: low
  • location: src/hooks/usePoolData.ts:167-170
  • scanner: llm-code-review
  • fingerprint: 15de83e348a60403a439eaf4972795351bf3afda43b9b1f44246090088055f67

Description

Both primary pool-channel loading (serverChannelsQuery) and the translator-specific helper issue exactly one /server/channels?offset=0&limit=100 request and treat its arrays as complete. The checked-in OpenAPI contract marks this endpoint paginated with a maximum page size of 100, while ServerChannelsResponse provides total_extended and total_standard; neither total is consulted. In a supported non-aggregated configuration, a large downstream worker population can produce more than 100 upstream channels. An adversarial miner able to create enough worker/channel connections can therefore move records to later pages that are never displayed. UnifiedDashboard computes share acknowledgements/rejections, best difficulty, block entries, and channel identities from these truncated arrays, producing a monitoring blind spot and incorrect persistent telemetry. The same omission exists in useTranslatorServerChannels at line 239, so JD mode's translator view is also affected; these are two mode-specific uses of the same server-channel endpoint and should share one paginating loader. The PoC performs that behavior-preserving extraction and proves a 101-channel response still returns only 100 on HEAD. Searches for serverChannelsQuery pagination total_extended, useTranslatorServerChannels limit 100, and ServerChannelsResponse pagination usePoolData found no prior report. Finding #894 is unrelated path encoding.

Proof of Concept

diff --git a/src/hooks/usePoolData.ts b/src/hooks/usePoolData.ts
--- a/src/hooks/usePoolData.ts
+++ b/src/hooks/usePoolData.ts
@@ -138,6 +138,12 @@ async function fetchAllSv2Clients(baseUrl: string): Promise<Sv2ClientWithChannels
 
   return clientsWithChannels;
 }
+
+export function fetchAllServerChannels(baseUrl: string): Promise<ServerChannelsResponse> {
+  return fetchWithTimeout<ServerChannelsResponse>(
+    `${baseUrl}/server/channels?offset=0&limit=100`
+  );
+}
 
 /**
  * Hook to fetch Pool connection data.
@@ -166,7 +173,7 @@ export function usePoolData(templateMode: TemplateMode = null) {
   // Fetch upstream server channels (Pool connection: shares, best diff)
   const serverChannelsQuery = useQuery({
     queryKey: ['server-channels', mode],
-    queryFn: () => fetchWithTimeout<ServerChannelsResponse>(`${baseUrl}/server/channels?offset=0&limit=100`),
+    queryFn: () => fetchAllServerChannels(baseUrl),
     refetchInterval: 3000,
   });
 
@@ -236,7 +243,7 @@ export function useTranslatorServerChannels(enabled = true) {
 
   return useQuery({
     queryKey: ['translator-server-channels'],
-    queryFn: () => fetchWithTimeout<ServerChannelsResponse>(`${endpoints.translator.base}/server/channels?offset=0&limit=100`),
+    queryFn: () => fetchAllServerChannels(endpoints.translator.base),
     refetchInterval: enabled ? 3000 : false,
     enabled,
   });
diff --git a/src/hooks/usePoolData.test.ts b/src/hooks/usePoolData.test.ts
new file mode 100644
--- /dev/null
+++ b/src/hooks/usePoolData.test.ts
@@ -0,0 +1,53 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+
+import { fetchAllServerChannels } from './usePoolData.js';
+
+test('server-channel loading follows pagination until every channel is represented', async () => {
+  const originalFetch = globalThis.fetch;
+  const firstPage = Array.from({ length: 100 }, (_, channel_id) => ({ channel_id }));
+
+  globalThis.fetch = (async (input: RequestInfo | URL) => {
+    const url = String(input);
+    let body: unknown;
+
+    if (url === '/jdc-api/v1/server/channels?offset=0&limit=100') {
+      body = {
+        offset: 0,
+        limit: 100,
+        total_extended: 101,
+        total_standard: 0,
+        extended_channels: firstPage,
+        standard_channels: [],
+      };
+    } else if (url === '/jdc-api/v1/server/channels?offset=100&limit=100') {
+      body = {
+        offset: 100,
+        limit: 100,
+        total_extended: 101,
+        total_standard: 0,
+        extended_channels: [{ channel_id: 100 }],
+        standard_channels: [],
+      };
+    } else {
+      throw new Error(`unexpected request: ${url}`);
+    }
+
+    return new Response(JSON.stringify(body), {
+      status: 200,
+      headers: { 'Content-Type': 'application/json' },
+    });
+  }) as typeof globalThis.fetch;
+
+  try {
+    const channels = await fetchAllServerChannels('/jdc-api/v1');
+    assert.equal(
+      channels.extended_channels.length,
+      101,
+      'the channel advertised by total_extended on the second page must be represented',
+    );
+    assert.equal(channels.extended_channels.at(-1)?.channel_id, 100);
+  } finally {
+    globalThis.fetch = originalFetch;
+  }
+});
+

Suggested Fix

diff --git a/src/hooks/usePoolData.ts b/src/hooks/usePoolData.ts
--- a/src/hooks/usePoolData.ts
+++ b/src/hooks/usePoolData.ts
@@ -80,3 +80,36 @@ async function fetchWithTimeout<T>(url: string, timeoutMs = 5000): Promise<T> {
 }
 
+/**
+ * Fetch all upstream server channels.
+ */
+async function fetchAllServerChannels(baseUrl: string): Promise<ServerChannelsResponse> {
+  const pageLimit = 100;
+  const firstPage = await fetchWithTimeout<ServerChannelsResponse>(
+    `${baseUrl}/server/channels?offset=0&limit=${pageLimit}`
+  );
+  const allChannels = {
+    ...firstPage,
+    extended_channels: [...firstPage.extended_channels],
+    standard_channels: [...firstPage.standard_channels],
+  };
+  const totalChannels = firstPage.total_extended + firstPage.total_standard;
+
+  for (
+    let offset = pageLimit;
+    offset < totalChannels && (
+      allChannels.extended_channels.length < firstPage.total_extended ||
+      allChannels.standard_channels.length < firstPage.total_standard
+    );
+    offset += pageLimit
+  ) {
+    const page = await fetchWithTimeout<ServerChannelsResponse>(
+      `${baseUrl}/server/channels?offset=${offset}&limit=${pageLimit}`
+    );
+    allChannels.extended_channels.push(...page.extended_channels);
+    allChannels.standard_channels.push(...page.standard_channels);
+  }
+
+  return allChannels;
+}
+
 /**
@@ -167,5 +200,5 @@ export function usePoolData(templateMode: TemplateMode = null) {
   const serverChannelsQuery = useQuery({
     queryKey: ['server-channels', mode],
-    queryFn: () => fetchWithTimeout<ServerChannelsResponse>(`${baseUrl}/server/channels?offset=0&limit=100`),
+    queryFn: () => fetchAllServerChannels(baseUrl),
     refetchInterval: 3000,
   });
@@ -236,7 +269,7 @@ export function useTranslatorServerChannels(enabled = true) {
 
   return useQuery({
     queryKey: ['translator-server-channels'],
-    queryFn: () => fetchWithTimeout<ServerChannelsResponse>(`${endpoints.translator.base}/server/channels?offset=0&limit=100`),
+    queryFn: () => fetchAllServerChannels(endpoints.translator.base),
     refetchInterval: enabled ? 3000 : false,
     enabled,
   });

Related issues and PRs

This finding was discovered by Project Loupe.

Activity

  1. added
    area:dashboard
    and removed on Oct 14, 2026
  2. GitGab19 commented on Oct 14, 2026

    @GitGab19
    Member

    Closed as a duplicate of #330, which now lists this issue's Loupe finding and keeps its export in a collapsible block. The fix lands there.

  3. added
    duplicate
    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:dashboardduplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0