{"id":"XE-0120","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"196","source_url":"https://github.com/xeprotocol/xe/issues/196","reporter_id":39,"title":"statechain sync: server enforces no byte cap and the client's 10 MiB cap is below the maximum legal page (64 × 256 KiB = 16 MiB) — permanent sync failure and unbounded encoding work per request","area":"p2p-sync","claimed_tier":"minor","assessed_tier":null,"verdict":null,"duplicate_of":null,"tracked":false,"internal_ref":"","internal_url":null,"status":"triaging","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-10-29T16:33:01.504339+00:00","updated_at":"2026-11-08T20:17:41.885067+00:00","source_created_at":"2026-10-29T16:32:01+00:00","told_at":"2026-11-08T20:18:50.573312+00:00","status_changed_at":"2026-11-08T20:18:37.475643+00:00","url":"https://bounty.xe.network/XE-0120","body":"**Suggested severity: Low (liveness + amplification; no funds at risk)**\n**Area:** statechain sync (`statechain/sync.go`, `statechain/types.go`)\n**Repo pin:** 9787859 (testnet-0005)\n\n## Summary\n\nThe statechain sync handler bounds the response by block count (`syncMaxBlocks = 10000`) and by a 60 s stream deadline, but **never by bytes**. `syncMaxResponseSize = 10 << 20` is referenced exactly once, on the client side (`statechain/sync.go:168`).\n\nConsensus allows a state block up to `MaxBlockSize = 262144` (`statechain/types.go:102`), and the page size is fixed at 64 (`sync.go:20`). So one *legal* page can be `64 × 262144 = 16,777,216` bytes, i.e. **1.6× the client's decode cap**. When that happens the client's `LimitReader` truncates mid-JSON, `json.Decoder.Decode` returns `unexpected EOF`, `requestSync` returns an error, and the node retries the identical request every 30 s forever — there is no smaller-page fallback and no server-side notion of a byte budget.\n\n## Code\n\n```go\n// statechain/sync.go:20-25\nsyncPageSize        = 64\nsyncMaxBlocks       = 10000\nsyncMaxResponseSize = 10 << 20   // used only by the client, line 168\n```\n\n```go\n// statechain/sync.go:120-141 — server: bounded by count, never by bytes\nfor idx := startIndex; idx <= tip.Index && totalSent < syncMaxBlocks; {\n\tpageCount := uint64(syncPageSize)\n\t...\n\tblocks, err := chain.GetBlocks(idx, pageCount)\n\t...\n\ttotalSent += len(blocks)\n\thasMore := idx+uint64(len(blocks)) <= tip.Index && totalSent < syncMaxBlocks\n\tif err := enc.Encode(syncResponse{Blocks: blocks, HasMore: hasMore}); err != nil {\n\t\treturn\n\t}\n\tidx += uint64(len(blocks))\n}\n```\n\n```go\n// statechain/sync.go:168 — client: the only place the cap exists\ndec := json.NewDecoder(io.LimitReader(s, syncMaxResponseSize))\n```\n\n```go\n// statechain/types.go:102\nMaxBlockSize = 262144\n```\n\n## Consequences\n\n1. **Permanent sync failure (liveness).** A node whose tip is just before a window of 64 large-but-legal blocks can never advance: every attempt decodes the same over-cap page and fails at the same byte. A fresh node hits it on its very first request (`1..64`), so it can never bootstrap. Because `sys.*` keys (phase, activations, oracle keyset, min_protocol_version) are frozen, that node keeps applying stale gating policy while the ledger advances — rejecting newly activated block types or accepting ones that should be gated.\n2. **Encoding work with no byte ceiling.** A single request with `tip_index = -1` makes the node walk up to 10,000 blocks and JSON-encode them to a peer that may read slowly or not at all; the only stop conditions are the count cap and the 60 s deadline. The inbound limiter is per peer ID at 1 request / 30 s, and peer IDs are cheap to mint (cf. #47: one IP can run several full-rate hosts), so many identities can keep a node encoding back-to-back.\n\nHonest limits: today's testnet blocks are small, so (1) is latent — the trigger is governance/oracle signers producing a run of large blocks, not an outside attacker. (2) is available now but bounded by the real chain size.\n\n## Suggested fix\n\n- Give `handleSyncStream` a byte budget (marshal each page, stop when the accumulated size would exceed `syncMaxResponseSize`, and set `has_more` accordingly).\n- Make the constants consistent: either `syncPageSize × MaxBlockSize <= syncMaxResponseSize`, or raise the client cap above the maximum legal page.\n- On a decode failure, retry with a smaller requested page instead of repeating the identical request forever.\n- Add a test that builds a maximum-size legal page and asserts both that the server stops before the client cap and that a client with the shipped limits can ingest it.\n","patch_refs":[{"id":204,"repo":"xeprotocol/xe","kind":"pr","ref":"204","title":"fix(statechain): enforce server byte budget and align client sync decode cap (fixes #196)","url":"https://github.com/xeprotocol/xe/pull/204","note":"","state":"open","author":"NePreby","added_by":"desk","created_at":"2026-10-29T17:00:15.617410+00:00"}],"thread":[{"id":1601,"source":"desk","source_ref":"reply:720","author":"Joseph Denne","team":true,"body":"Thanks for the report. We have confirmed the behaviour you describe in the statechain sync path and it is being handled; we will update this ticket when there is a fix to point at.","created_at":"2026-11-08T20:17:41.885067+00:00"}]}