{"id":"XE-17331","kind":"bounty","visibility":"public","source":"web","source_repo":"","source_ref":"","source_url":"","reporter_id":2799,"title":"api: GET /accounts offset paging skips and duplicates accounts under concurrent balance changes (no cursor, re-sort per page)","area":"node-api","claimed_tier":"medium","assessed_tier":null,"verdict":"duplicate","duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-17T12:07:13.901147+00:00","updated_at":"2026-11-23T01:01:14.149714+00:00","source_created_at":null,"told_at":"2026-11-23T01:01:14.178759+00:00","status_changed_at":"2026-11-23T01:01:14.158521+00:00","url":"https://bounty.xe.network/XE-17331","body":"# GET /accounts offset paging skips and duplicates accounts under concurrent balance changes (no cursor, re-sort per page)\r\n\r\n## Summary\r\n\r\n`GET /accounts` pages with a numeric offset over a list that is re-sorted by live XE balance on every request. Any send/receive that changes any account's XE balance between two page fetches re-orders the whole list, shifting the offset window: an account crossing the page boundary is returned twice or never. There is no snapshot, no cursor, and no `total` anywhere in the path, so a consumer walking the enumeration (explorer, indexer, SDK, supply census) silently misses or double-counts accounts.\r\n\r\nThis is the `GET /accounts` instance of the offset-paging defect class filed as XE-17296 for `GET /pending` — but with a strictly stronger trigger: `/pending` shifts only on insert/delete of the key set, while `/accounts` re-sorts on every balance change anywhere on the network, i.e. on every balance-changing block.\r\n\r\n## Target\r\n\r\n- Repo: `xeprotocol/xe` @ `8cc209d65c16089158da8fabd392628b71fe0711` (remote HEAD, verified 2026-11-17).\r\n- Files: `api/handler.go:714-716` (`handleGetAllAccounts`), `node/node.go:1037-1056` (`GetAccountsPage`), `core/account_summary.go:110-122` (`AccountsByXEBalance`), `:40-73` (`refreshSummariesLocked`), `api/handler.go:640-656` (`parsePagination`).\r\n\r\n## Code path\r\n\r\n```go\r\n// api/handler.go:714-716\r\nfunc (h *Handler) handleGetAllAccounts(w http.ResponseWriter, r *http.Request) {\r\n    offset, limit := parsePagination(r)\r\n    writeJSON(w, http.StatusOK, h.node.GetAccountsPage(offset, limit))\r\n}\r\n\r\n// node/node.go:1037-1056\r\nfunc (n *Node) GetAccountsPage(offset, limit int) []AccountSummary {\r\n    all := n.Ledger.AccountsByXEBalance()\r\n    if offset >= len(all) { return []AccountSummary{} }\r\n    end := min(offset+limit, len(all))\r\n    out := make([]AccountSummary, 0, end-offset)\r\n    for _, s := range all[offset:end] { out = append(out, ...) }\r\n    return out\r\n}\r\n\r\n// core/account_summary.go:110-122\r\nfunc (l *Ledger) AccountsByXEBalance() []*AccountSummary {\r\n    l.summaryMu.Lock()\r\n    defer l.summaryMu.Unlock()\r\n    l.refreshSummariesLocked()\r\n    if l.summaryByXE == nil {\r\n        l.summaryByXE = slices.SortedFunc(maps.Values(l.summaries), func(a, b *AccountSummary) int {\r\n            if c := cmp.Compare(b.xe, a.xe); c != 0 { return c }   // XE balance DESC\r\n            return strings.Compare(a.Account, b.Account)\r\n        })\r\n    }\r\n    return l.summaryByXE\r\n}\r\n```\r\n\r\nThe sort key is a **mutable value**: `refreshSummariesLocked` (`:40-73`) rebuilds the affected summaries whenever any account is dirty and ends with `l.summaryByXE, l.summaryByID = nil, nil` — the cached order is discarded and the next request re-sorts from scratch. Each page request therefore materialises a fresh ordering of the live collection and slices it by numeric offset, with no snapshot held across pages, no cursor, and no `total` in the bare-array response. A balance change anywhere between page N and page N+1 re-orders the list and shifts every entry after the change point by ±1: the walk skips one account and duplicates another.\r\n\r\n`parsePagination` (`api/handler.go:640-656`) takes only `offset`/`limit`; no cursor parameter exists anywhere in the accounts path.\r\n\r\n## Reproduction (read-only, no crafted input)\r\n\r\n1. `GET https://test.network/api/v1/accounts?limit=100&offset=0` — 100 accounts, strictly XE-balance descending (verified live 2026-11-17: top balances 41,000,000,000,000, 996,941,000,000, 322,000,000).\r\n2. Wait for any balance-changing block to land, then `GET .../accounts?limit=100&offset=100`.\r\n3. Diff the boundary: an account whose balance changed between the two fetches — or any account ranked after it — can appear in both pages or in neither.\r\n\r\nDeterministic variant: two consecutive full walks during normal network activity disagree at the boundary whenever a balance changed mid-walk. (Live scale on testnet-0006: ~1,300 accounts, default limit 100 → a 13-page walk; every balance-changing block is a re-sort event.)\r\n\r\n## Impact\r\n\r\nMedium — same class and tier as XE-17296. No direct fund loss: the defect corrupts enumeration, not balances. Anyone paginating `/accounts` — explorers, indexers, the SDK, a supply census — silently misses accounts or counts them twice, with no `total` to detect the loss. The trigger is the network's normal operation (any send/receive), not an attacker action, and the re-sort happens on every balance-changing block, so the hazard is exercised far more often than the insert-only variants on other endpoints.\r\n\r\n## Suggested fix\r\n\r\nSnapshot the sorted list under `summaryMu` for the duration of a walk, or add a keyset cursor (e.g. `(xe_balance, address)` — the sort key is already total), or at minimum return a `total` plus a stable-ordering guarantee so consumers can detect a shifted walk.\r\n\r\n## Distinctness\r\n\r\n- **XE-17296** (Medium, filed this cycle) is `GET /pending` only — different endpoint, different trigger (pending-set insert/delete vs balance re-sort). Its body does not mention `/accounts`.\r\n- **XE-16443** (High) is the five endpoints loading the full collection before paginating — a performance defect (cost), not a correctness defect (wrong enumeration). Different mechanism, different fix.\r\n- **XE-0058 / XE-14068** are silent truncation (bare array, no `total`, 100-item default) — a detection problem, not the skip/duplicate mechanism. The missing `total` is cited here only as why the loss is undetectable.\r\n- **XE-11844** (Minor, docs) claims `/accounts` paging IS stable because the handler sorts before slicing — verified live with two back-to-back pages at rest. It does not consider the concurrent-mutation case at all: its \"stable\" claim holds only while no balance changes between the fetches, which is exactly the gap this report closes. Its suggested fix (document `/accounts` as sorting before slicing) would enshrine the wrong stability claim. Different defect (docs error vs enumeration correctness), different fix.\r\n- **XE-5929 / XE-13614 / XE-14166** are docs staleness about the sort claim, not the paging hazard.\r\n- **XE-13265 / XE-8285** are per-request cost (full load / full chain read), not correctness.\r\n- Corpus searches over all 2,869 public tickets: `offset` / `cursor` / `pagination` / `skip` + `accounts` — no ticket names an offset-paging skip/duplicate on `/accounts`. Sibling endpoints with the same root cause (`/frontiers`, `/conflicts`, `/leases`, `/reputation`, `/statechain/kv`, `/directory`) were audited in the same sweep; they are recorded separately as follow-up candidates, not claimed here.\r\n\r\n## Honest limitations\r\n\r\n- **Live observation:** three back-to-back two-page walks (~40 s, 2026-11-17) showed no shift — the boundary accounts' balances did not change inside those windows. The defect is proven in code (re-sort on every dirty account + offset slicing with no snapshot), and the shift requires a balance-changing block landing between the two page fetches; on a quiet testnet that is intermittent, on a busy network it is routine. No live skip/duplicate was captured in this run.\r\n- The walk is 13 pages at the default limit; the hazard needs a balance change between two specific page fetches, so a single walk may or may not exhibit it — the defect is in the paging contract, not in any single response.","patch_refs":[],"thread":[{"id":19446,"source":"desk","source_ref":"reply:17123","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, file a new report that links https://bounty.xe.network/XE-17331 and says what differs; it is read like any other.","created_at":"2026-11-23T01:01:14.149714+00:00"}]}