| … | … | @@ -0,0 +1,124 @@ |
| 1 | // Making TOFU safe on a surface that is NOT loopback. |
| 2 | // |
| 3 | // ⛔ THE PREMISE THAT BREAKS. browsers.js says it plainly: the safety of first-claim TOFU "rests |
| 4 | // ENTIRELY on the surface binding localhost… exposing it before claiming it hands it to whoever is on |
| 5 | // the network. It is a first-run convenience, not an authorisation model." A bridge's operator page has |
| 6 | // to be reachable from the network, so "the first browser to prove a key" becomes "the first stranger |
| 7 | // who finds the port". |
| 8 | // |
| 9 | // ⛔ AND THE RACE STOPS BEING THEORETICAL. The same header admits `claim` reads `unclaimed()` and then |
| 10 | // writes: "two browsers arriving together can both read 'nobody registered'". On loopback that gap is |
| 11 | // one round trip. On a public port it is something an attacker can drive on purpose. |
| 12 | // |
| 13 | // A bootstrap token the CALLER prints to its own service log is a secret only the operator can read, |
| 14 | // so requiring it for the FIRST claim binds that claim to them without inventing an authorisation |
| 15 | // model. A latch makes the check-and-register atomic. |
| 16 | // |
| 17 | // ⚠ No token configured means NOT REQUIRED — the loopback default, unchanged, which is why every |
| 18 | // existing browsers test still passes untouched. |
| 19 | |
| 20 | import test from 'brittle' |
| 21 | import Corestore from 'corestore' |
| 22 | import fs from '#node:fs' |
| 23 | import os from '#node:os' |
| 24 | import { join } from '#node:path' |
| 25 | import { an } from 'anproto' |
| 26 | import { Browsers } from '../lib/browsers.js' |
| 27 | |
| 28 | const tmp = () => fs.mkdtempSync(join(os.tmpdir(), 'favela-bootstrap-')) |
| 29 | const TOKEN = 'b7f4c1a9e2d05638b7f4c1a9e2d05638' |
| 30 | |
| 31 | async function open (opts) { |
| 32 | const b = new Browsers(new Corestore(tmp()), opts) |
| 33 | await b.ready() |
| 34 | return b |
| 35 | } |
| 36 | |
| 37 | async function browser () { |
| 38 | const keypair = await an.gen() |
| 39 | return { |
| 40 | publicKey: an.publicKey(keypair), |
| 41 | async answer (b) { |
| 42 | const { challenge } = b.challenge() |
| 43 | return { challenge, signature: await an.sign(await an.hash(challenge), keypair) } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | test('★★★ with a bootstrap token set, a proved key alone cannot claim', async (t) => { |
| 49 | const b = await open({ bootstrap: TOKEN }) |
| 50 | const who = await browser() |
| 51 | |
| 52 | // ⚠ The answer is taken BEFORE the arrow: `await` inside a non-async arrow is a parse error, not a |
| 53 | // runtime one, so it takes the whole file down rather than the test. |
| 54 | const proof = await who.answer(b) |
| 55 | await t.exception( |
| 56 | () => b.claim({ publicKey: who.publicKey, ...proof }), |
| 57 | /bootstrap token/, |
| 58 | 'proving the key is not enough on a surface strangers can reach') |
| 59 | |
| 60 | t.ok(await b.unclaimed(), 'and the refusal changed nothing — the instance is still unclaimed') |
| 61 | }) |
| 62 | |
| 63 | test('★★★ a wrong token is refused and costs nothing', async (t) => { |
| 64 | const b = await open({ bootstrap: TOKEN }) |
| 65 | const who = await browser() |
| 66 | |
| 67 | const proof = await who.answer(b) |
| 68 | await t.exception( |
| 69 | () => b.claim({ publicKey: who.publicKey, ...proof, bootstrap: 'b7f4c1a9e2d05638b7f4c1a9e2d05639' }), |
| 70 | /bootstrap token/, |
| 71 | 'one byte different is still wrong') |
| 72 | t.ok(await b.unclaimed(), 'nothing was written') |
| 73 | }) |
| 74 | |
| 75 | test('★★★ the right token claims it, once', async (t) => { |
| 76 | const b = await open({ bootstrap: TOKEN }) |
| 77 | const first = await browser() |
| 78 | |
| 79 | const firstProof = await first.answer(b) |
| 80 | const out = await b.claim({ publicKey: first.publicKey, ...firstProof, bootstrap: TOKEN }) |
| 81 | t.ok(out.claimed) |
| 82 | t.absent(await b.unclaimed(), 'the instance is claimed') |
| 83 | |
| 84 | // ⛔ It closes PERMANENTLY. Holding the token does not reopen it — the token gates the first claim, |
| 85 | // it is not a standing key to the instance. |
| 86 | const second = await browser() |
| 87 | const secondProof = await second.answer(b) |
| 88 | await t.exception( |
| 89 | () => b.claim({ publicKey: second.publicKey, ...secondProof, bootstrap: TOKEN }), |
| 90 | /already claimed/, |
| 91 | 'a second claimant with the same token is still refused') |
| 92 | }) |
| 93 | |
| 94 | test('★★★ two claimants arriving together — exactly one wins', async (t) => { |
| 95 | // The race the header admits. Both prove their key, both present the token, both arrive before |
| 96 | // either has written. Without the latch both read "unclaimed" and both register. |
| 97 | const b = await open({ bootstrap: TOKEN }) |
| 98 | const a1 = await browser() |
| 99 | const a2 = await browser() |
| 100 | |
| 101 | const proofs = [ |
| 102 | { publicKey: a1.publicKey, ...(await a1.answer(b)), bootstrap: TOKEN }, |
| 103 | { publicKey: a2.publicKey, ...(await a2.answer(b)), bootstrap: TOKEN } |
| 104 | ] |
| 105 | const results = await Promise.allSettled(proofs.map((p) => b.claim(p))) |
| 106 | |
| 107 | const won = results.filter((r) => r.status === 'fulfilled') |
| 108 | const lost = results.filter((r) => r.status === 'rejected') |
| 109 | t.is(won.length, 1, 'exactly one claim succeeded') |
| 110 | t.is(lost.length, 1, 'and the other was told the instance was already claimed') |
| 111 | // ⚠ Guarded. Without the latch BOTH claims succeed and `lost` is empty — measured — so an |
| 112 | // unguarded `lost[0].reason` throws and takes the whole FILE down instead of failing this one |
| 113 | // assertion. A test that crashes on the defect it exists to catch reports nothing. |
| 114 | const why = lost.length ? String((lost[0].reason && lost[0].reason.message) || lost[0].reason) : '(nobody lost — both claims won)' |
| 115 | t.ok(/already claimed/.test(why), why) |
| 116 | }) |
| 117 | |
| 118 | test('★★ with no token configured nothing changes — the loopback default', async (t) => { |
| 119 | const b = await open() |
| 120 | const who = await browser() |
| 121 | const proof = await who.answer(b) |
| 122 | const out = await b.claim({ publicKey: who.publicKey, ...proof }) |
| 123 | t.ok(out.claimed, 'first-claim TOFU still works where the surface is bound to localhost') |
| 124 | }) |