Bounty
XE-0916 · Bounty

Web dao.js compact string values hash differently after Go store and gossip re-encoding

Closed · duplicate —claimed Medium web filed 2026-11-06 16:51 UTC by byfor8

Report

Summary

The embedded dao.js tool is supposed to submit state-chain blocks that hash the same after Go storage and gossip as they did at admission. JS JSON.stringify leaves <>& compact; Go encoding/json default SetEscapeHTML re-encodes on PutStateBlock (store/badger_store.go:1741-1748), MemStore copyStateBlock (store/mem_store.go:865-876), and gossip Publish (net/gossip.go:324-330), while MarshalCanonical (statechain/encoding.go:47-51) hashed the compact bytes. NewChain.replay at statechain/chain.go:51-86 then VerifyBlockHash at :68 fails on the escaped stored bytes. Submit is POST /api/statechain/blocks (api/handler.go:938-950) via node.SubmitStateChainBlock (node/node.go:2289-2293) from the FS embedded at web/embed.go:5 and served at api/ui.go:63-64. GitHub #45 is statechain gossip admitting any 64-hex with a non-empty signature list, and XE-0465/XE-0569 is Python compact_json leaving U+2028 raw; the poison here is the browser dao tool plus Go SetEscapeHTML on the submitter's own node.

Affected components

web/assets/dao.js:202-243 onGenerateHash, :362-399 serializeBlockJson; web/assets/statechain-block.js:40 hash of textarea bytes; web/embed.go:5; api/ui.go:63-64; api/handler.go:210, :938-950 handleSubmitStateChainBlock; node/node.go:2289-2293 SubmitStateChainBlock; statechain/types.go:14-17 Op.Value; statechain/encoding.go:10 MarshalCanonical, op-value encode at :47-51; statechain/chain.go:51-86 replay, :68 VerifyBlockHash, :155 AddBlock size marshal, :207; store/badger_store.go:1741-1748 PutStateBlock; store/mem_store.go:865-876 copyStateBlock; net/gossip.go:324-330 Publish at xe 02e9641b0ac64437f57e09d63301f88cce61a802.

Impact

The submitter's node admits the compact block, persists escaped bytes, then cannot restart until that block is gone. Gossip peers never store it. If the block collected threshold signatures before the mismatch surfaced, that resolution is lost. The web dao tool is the submission path, and funds stay put.

PoC

package round2b

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"testing"
	"unicode/utf8"

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

// TestPoC_B1 drives AddBlock of a DAO state-chain block whose op value is the
// compact JSON string dao.js would submit (unescaped <, >, &), then reloads
// the stored bytes the way NewChain.replay does.
func TestPoC_B1(t *testing.T) {
	pass, detail := runB1()
	if pass {
		fmt.Println("PASS:", detail)
		return
	}
	fmt.Println("FAIL:", detail)
	os.Exit(1)
}

func runB1() (bool, string) {
	core.SetNetworkID("")

	dao1, err := core.GenerateKeyPair()
	if err != nil {
		return false, "dao1: " + err.Error()
	}
	dao2, err := core.GenerateKeyPair()
	if err != nil {
		return false, "dao2: " + err.Error()
	}
	genesis, err := genesisDAO([]string{dao1.PubKeyHex(), dao2.PubKeyHex()}, 2)
	if err != nil {
		return false, "genesis: " + err.Error()
	}

	// Compact JSON string as dao.js serializeBlockJson would emit for a
	// parsed string containing <, >, &. JS JSON.stringify does not HTML-escape.
	jsSubmitted := json.RawMessage(`"<hello>&world>"`)
	if !bytes.Contains(jsSubmitted, []byte("<")) || !bytes.Contains(jsSubmitted, []byte(">")) || !bytes.Contains(jsSubmitted, []byte("&")) {
		return false, "fixture lost compact <>&"
	}

	ms := store.NewMemStore()
	chain, err := statechain.NewChain(ms, genesis)
	if err != nil {
		return false, "NewChain: " + err.Error()
	}

	block, err := buildDAOBlock(chain, "dao.note", jsSubmitted, dao1, dao2)
	if err != nil {
		return false, "build: " + err.Error()
	}
	admitHash := block.Hash
	if err := statechain.VerifyBlockHash(block); err != nil {
		return false, "pre-admit hash: " + err.Error()
	}

	if err := chain.AddBlock(block); err != nil {
		return false, "AddBlock rejected compact <>& value (admission should succeed): " + err.Error()
	}
	logf("ADMIT: index=%d hash=%s value_raw=%s", block.Index, fmtShort(admitHash), string(jsSubmitted))

	stored, err := ms.GetStateBlock(block.Index)
	if err != nil || stored == nil {
		return false, fmt.Sprintf("GetStateBlock: %v stored=%v", err, stored)
	}
	storedVal := string(stored.Ops[0].Value)
	goMarshaled, err := json.Marshal(block)
	if err != nil {
		return false, "json.Marshal(block): " + err.Error()
	}

	htmlEscaped := bytes.Contains(goMarshaled, []byte(`\u003c`)) ||
		bytes.Contains(goMarshaled, []byte(`\u003e`)) ||
		bytes.Contains(goMarshaled, []byte(`\u0026`))
	storedEscaped := strings.Contains(storedVal, `\u003c`) ||
		strings.Contains(storedVal, `\u003e`) ||
		strings.Contains(storedVal, `\u0026`)

	logf("STORE json.Marshal contains HTML escapes: %v", htmlEscaped)
	logf("STORE GetStateBlock value bytes: %s", storedVal)
	logf("STORE value escaped: %v (admission value was %s)", storedEscaped, string(jsSubmitted))

	storedHashErr := statechain.VerifyBlockHash(stored)
	logf("REPLAY VerifyBlockHash(stored): %v", storedHashErr)

	_, replayErr := statechain.NewChain(ms, genesis)
	logf("RESTART NewChain.replay: %v", replayErr)

	// Gossip Publish path: json.Marshal(StateChainMsg{Block}) then peer Unmarshal + VerifyBlockHash.
	type stateChainMsg struct {
		Block *statechain.Block `json:"block"`
	}
	gossipBytes, err := json.Marshal(stateChainMsg{Block: block})
	if err != nil {
		return false, "gossip marshal: " + err.Error()
	}
	var peerMsg stateChainMsg
	if err := json.Unmarshal(gossipBytes, &peerMsg); err != nil {
		return false, "gossip unmarshal: " + err.Error()
	}
	peerHashErr := statechain.VerifyBlockHash(peerMsg.Block)
	logf("GOSSIP peer VerifyBlockHash after Go marshal: %v", peerHashErr)
	logf("GOSSIP payload snippet: %s", snippetAround(gossipBytes, `\u003c`))

	// U+2028: JS JSON.stringify already emits \u2028. Also try an actual LINE SEPARATOR in RawMessage.
	u2028escaped := json.RawMessage(`"line\u2028sep"`)
	u2028raw := json.RawMessage("\"line" + string(rune(0x2028)) + "sep\"")
	encEsc, _ := json.Marshal(&statechain.Op{Action: "set", Key: "k", Value: u2028escaped})
	encRaw, _ := json.Marshal(&statechain.Op{Action: "set", Key: "k", Value: u2028raw})
	logf("U+2028 already-escaped RawMessage marshal=%s", encEsc)
	logf("U+2028 actual-char RawMessage marshal=%s utf8=%v", encRaw, utf8.Valid(u2028raw))

	// CONTROL: no special chars, restart must succeed.
	msCtrl := store.NewMemStore()
	ctrlChain, err := statechain.NewChain(msCtrl, genesis)
	if err != nil {
		return false, "control NewChain: " + err.Error()
	}
	ctrlVal := json.RawMessage(`"hello world"`)
	ctrlBlock, err := buildDAOBlock(ctrlChain, "dao.note", ctrlVal, dao1, dao2)
	if err != nil {
		return false, "control build: " + err.Error()
	}
	if err := ctrlChain.AddBlock(ctrlBlock); err != nil {
		return false, "control AddBlock: " + err.Error()
	}
	_, ctrlReplay := statechain.NewChain(msCtrl, genesis)
	logf("CONTROL replay (no <>&): %v", ctrlReplay)

	if !htmlEscaped || !storedEscaped {
		return false, fmt.Sprintf("Go marshal did not HTML-escape stored value (htmlEscaped=%v storedEscaped=%v storedVal=%s)", htmlEscaped, storedEscaped, storedVal)
	}
	if storedHashErr == nil {
		return false, "stored bytes still hashed; expected VerifyBlockHash mismatch after HTML escape"
	}
	if replayErr == nil {
		return false, "NewChain.replay succeeded; expected restart failure on escaped stored bytes"
	}
	if peerHashErr == nil {
		return false, "gossip peer accepted re-marshalled block; expected hash mismatch"
	}
	if ctrlReplay != nil {
		return false, "control replay failed: " + ctrlReplay.Error()
	}
	return true, fmt.Sprintf(
		"compact <>& admitted hash=%s; stored value %s; replay=%v; gossip peer=%v; control replay ok",
		fmtShort(admitHash), storedVal, replayErr, peerHashErr,
	)
}

func snippetAround(b []byte, needle string) string {
	s := string(b)
	i := strings.Index(s, needle)
	if i < 0 {
		if len(s) > 180 {
			return s[:180] + "..."
		}
		return s
	}
	lo := i - 40
	if lo < 0 {
		lo = 0
	}
	hi := i + 60
	if hi > len(s) {
		hi = len(s)
	}
	return s[lo:hi]
}
stored value "\u003chello\u003e\u0026world\u003e"
replay=NewChain: replay: block 1: hash mismatch
PASS: compact <>& admitted; stored bytes HTML-escaped; replay and gossip peer hash-mismatch

Suggested fix

Disable SetEscapeHTML on state-block marshal, or hash after a canonical compact encode shared by JS and Go. Store the submitted bytes instead of re-marshalling.

Thread

XE deskteam2026-11-14 22:53 UTCdesk

Thanks. This is the same finding as https://bounty.xe.network/XE-0465, which was filed first; the programme pays the first verifiable submission, so this one closes as a duplicate. If you believe it is a different bug, file a new report that links https://bounty.xe.network/XE-0916 and says what differs; it is read like any other.