diff options
Diffstat (limited to 'names')
| -rw-r--r-- | names/migrate-check.js | 28 | ||||
| -rw-r--r-- | names/migrate-check.test.js | 41 | ||||
| -rw-r--r-- | names/server.js | 6 |
3 files changed, 75 insertions, 0 deletions
diff --git a/names/migrate-check.js b/names/migrate-check.js new file mode 100644 index 0000000..d4995f4 --- /dev/null +++ b/names/migrate-check.js @@ -0,0 +1,28 @@ +// /migrate-grant's second check. The bearer token proves the caller is +// coinos.io, not that the name's own account asked. So the old account must +// have just handed itself over: coinos.io stamps `v3migrated` (a timestamp) +// only when the account's own session posts the hand-off, and the grant +// follows within seconds. A bug upstream then can't give someone else's name +// to a stranger, and a name handed over long ago can't be quietly re-granted. +// +// Not `migrated`: coinos.io has set that on every new account since 2023, so +// nearly every name passes it. +// Returns null when the grant may go ahead, else [status, error]. +export const FRESH_MS = 5 * 60_000; + +export async function migrateGrantRefusal(name, fetchFn = fetch, now = Date.now()) { + let u; + try { + const r = await fetchFn(`https://coinos.io/api/users/${encodeURIComponent(name)}`, { signal: AbortSignal.timeout(8000) }); + if (r.status === 404) return [404, 'no such coinos.io account']; + if (r.status !== 200) throw new Error(`coinos.io said ${r.status}`); + u = await r.json(); + } catch { + return [502, "couldn't check the coinos.io account, try again"]; + } + if (!u || String(u.username || '').toLowerCase() !== String(name).toLowerCase()) return [404, 'no such coinos.io account']; + const at = Number(u.v3migrated); + if (!Number.isFinite(at) || at > now + 60_000 || now - at > FRESH_MS) + return [409, 'that coinos.io account has not just migrated']; + return null; +} diff --git a/names/migrate-check.test.js b/names/migrate-check.test.js new file mode 100644 index 0000000..a1ad725 --- /dev/null +++ b/names/migrate-check.test.js @@ -0,0 +1,41 @@ +// bun test names/migrate-check.test.js +import { test, expect } from 'bun:test'; +import { migrateGrantRefusal, FRESH_MS } from './migrate-check.js'; + +const NOW = 1_800_000_000_000; +const coinos = (users, status) => async (url) => { + const name = decodeURIComponent(url.split('/').pop()); + if (status) return new Response('', { status }); + const u = users[name.toLowerCase()]; + return u ? Response.json(u) : new Response('{}', { status: 404 }); +}; +const users = { + alice: { username: 'Alice', migrated: true, v3migrated: NOW - 5_000 }, + // every account created since 2023 carries migrated: true; it proves nothing + victim: { username: 'victim', migrated: true }, + bob: { username: 'bob' }, + stale: { username: 'stale', migrated: true, v3migrated: NOW - FRESH_MS - 1 }, + future: { username: 'future', v3migrated: NOW + 10 * 60_000 }, + junk: { username: 'junk', v3migrated: 'yes' }, +}; +const refusal = (n, f = coinos(users)) => migrateGrantRefusal(n, f, NOW); + +test('an account that just handed itself over may hand its name over', async () => { + expect(await refusal('alice')).toBeNull(); +}); +test('the legacy `migrated` flag alone is not enough', async () => { + expect((await refusal('victim'))[0]).toBe(409); + expect((await refusal('bob'))[0]).toBe(409); +}); +test('a hand-off that is stale, from the future, or not a timestamp is refused', async () => { + expect((await refusal('stale'))[0]).toBe(409); + expect((await refusal('future'))[0]).toBe(409); + expect((await refusal('junk'))[0]).toBe(409); +}); +test('a name with no coinos.io account is refused', async () => { + expect((await refusal('nobody'))[0]).toBe(404); +}); +test("coinos.io down means no grant, and it says to try again", async () => { + expect((await refusal('alice', coinos(users, 502)))[0]).toBe(502); + expect((await refusal('alice', async () => { throw new Error('net'); }))[0]).toBe(502); +}); diff --git a/names/server.js b/names/server.js index 0a8a9f1..3e687c3 100644 --- a/names/server.js +++ b/names/server.js @@ -38,6 +38,7 @@ import { registrationIdentity } from './ownership.js'; import { openStore } from './store.js'; import { withdrawService } from './withdraw.js'; import { accountOf, accountBlockers, forgetAccount } from './forget.js'; +import { migrateGrantRefusal } from './migrate-check.js'; const CFG = JSON.parse(readFileSync(process.env.NAMES_CONFIG || join(import.meta.dir, 'config.json'), 'utf8')); @@ -2104,6 +2105,11 @@ Bun.serve({ try { b = await req.json(); } catch { return json({ error: 'bad body' }, 400); } const name = String(b.name || '').toLowerCase(); if (!NAME_RE.test(name)) return json({ error: 'bad name' }, 400); + const refused = await migrateGrantRefusal(name); + if (refused) { + log(`migrate-grant refused: ${name}@coinos.io (${refused[1]})`); + return json({ error: refused[1] }, refused[0]); + } let pubkey = /^[0-9a-f]{64}$/.test(String(b.pubkey || '')) ? b.pubkey : null; let manager = null; if (!pubkey && b.to) { |
