From acc245b5ca5ea4100c5ef48273edbcb02a1e3a7f Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:04:38 +0530 Subject: [PATCH 1/2] Verify fully remote proofs against the freshest head The head was read once at the start of verify() and the batch length was compared against it after verification, so a concurrent update advancing storage in between made the result stale. Re-read the head after verifying and compare against that. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- lib/fully-remote-proof.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/fully-remote-proof.js b/lib/fully-remote-proof.js index 757b5a6f..291a0253 100644 --- a/lib/fully-remote-proof.js +++ b/lib/fully-remote-proof.js @@ -68,8 +68,6 @@ async function verify(storage, buffer, { referrer = null } = {}) { let roots = await Promise.all(rootPromises) if (roots.some(isNull)) roots = [] - const length = head ? head.length : 0 - if (!auth.manifest || !auth.manifest.signers.length) return null let batch = null @@ -102,7 +100,16 @@ async function verify(storage, buffer, { referrer = null } = {}) { result.key = auth.key result.discoveryKey = discoveryKey - result.newer = batch.length > length + + // Storage may have advanced while the proof was read and verified (e.g. a + // concurrent notification applied it), so compare against the freshest head + // instead of the possibly stale one read above. + rx = core.read() + const freshHeadPromise = rx.getHead() + rx.tryFlush() + const freshHead = await freshHeadPromise + + result.newer = batch.length > (freshHead ? freshHead.length : 0) result.length = batch.length result.block = proof.block From 1da36669cf251b8a5e6c6b04e1e0f23b9198907d Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:05:03 +0530 Subject: [PATCH 2/2] Test that newer reflects the freshest head Simulates a storage advance during proof verification by doctoring the first head read, then asserts newer is computed against the fresh head. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- test/fully-remote-proof.js | 51 +++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/test/fully-remote-proof.js b/test/fully-remote-proof.js index 787f1e6d..86ff6f59 100644 --- a/test/fully-remote-proof.js +++ b/test/fully-remote-proof.js @@ -4,7 +4,7 @@ const c = require('compact-encoding') const remote = require('../lib/fully-remote-proof.js') const messages = require('../lib/messages.js') -const { create } = require('./helpers') +const { create, replicate } = require('./helpers') test('fully remote proof - proof and verify', async function (t) { const core = await create(t) @@ -60,3 +60,52 @@ test('fully remote proof - block, hash or seek not covered by the upgrade', asyn const p = await remote.verify(store, await remote.proof(core, opts)) t.alike(p.block.value, b4a.from('d'), 'block past the upgrade still verifies') }) + +// Simulates the race from #791: the first head read inside verify() returns a +// stale head, as if a concurrent notification advanced storage right after it +// was read. +function wrapStoreWithStaleHead(store, staleLength) { + return new Proxy(store, { + get(target, prop, receiver) { + if (prop !== 'resumeCore') return Reflect.get(target, prop, receiver) + return async function (discoveryKey) { + const core = await target.resumeCore(discoveryKey) + if (core === null) return null + let first = true + const read = core.read.bind(core) + core.read = function (...args) { + const rx = read(...args) + if (first) { + first = false + const getHead = rx.getHead.bind(rx) + rx.getHead = async function (...headArgs) { + const head = await getHead(...headArgs) + if (head === null) return null + return { ...head, length: staleLength } + } + } + return rx + } + return core + } + } + }) +} + +test('fully remote proof - newer uses freshest head', async function (t) { + const a = await create(t) + await a.append(['a', 'b', 'c']) + + const b = await create(t, a.key) + replicate(a, b, t) + await b.get(2) + + const proof = await remote.proof(a) + + const store = wrapStoreWithStaleHead(b.state.storage.store, 2) + const p = await remote.verify(store, proof) + + t.ok(p !== null, 'proof verifies') + t.is(p.length, 3, 'proof length reported') + t.is(p.newer, false, 'storage already has the proof length, so it is not newer') +})