Bounty
XE-0120 · Bounty

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

Triaging claimed Minor xe#196 filed 2026-10-29 16:32 UTC by hayokakoweda75

Report

Suggested severity: Low (liveness + amplification; no funds at risk) Area: statechain sync (statechain/sync.go, statechain/types.go) Repo pin: 9787859 (testnet-0005)

Summary

The 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).

Consensus 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.

Code

// statechain/sync.go:20-25
syncPageSize        = 64
syncMaxBlocks       = 10000
syncMaxResponseSize = 10 << 20   // used only by the client, line 168
// statechain/sync.go:120-141 — server: bounded by count, never by bytes
for idx := startIndex; idx <= tip.Index && totalSent < syncMaxBlocks; {
	pageCount := uint64(syncPageSize)
	...
	blocks, err := chain.GetBlocks(idx, pageCount)
	...
	totalSent += len(blocks)
	hasMore := idx+uint64(len(blocks)) <= tip.Index && totalSent < syncMaxBlocks
	if err := enc.Encode(syncResponse{Blocks: blocks, HasMore: hasMore}); err != nil {
		return
	}
	idx += uint64(len(blocks))
}
// statechain/sync.go:168 — client: the only place the cap exists
dec := json.NewDecoder(io.LimitReader(s, syncMaxResponseSize))
// statechain/types.go:102
MaxBlockSize = 262144

Consequences

  1. 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.
  2. 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.

Honest 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.

Suggested fix

  • Give handleSyncStream a byte budget (marshal each page, stop when the accumulated size would exceed syncMaxResponseSize, and set has_more accordingly).
  • Make the constants consistent: either syncPageSize × MaxBlockSize <= syncMaxResponseSize, or raise the client cap above the maximum legal page.
  • On a decode failure, retry with a smaller requested page instead of repeating the identical request forever.
  • 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.

Thread

Joseph Denneteam2026-11-08 20:17 UTCdesk

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.