aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-12-02 10:37:33 -0700
committerAdam Soltys <[email protected]>2026-12-02 10:37:33 -0700
commit957599974b453b9b13653b821658fd0a76d01809 (patch)
tree3c43e9a272bbc406afda3bf0f434ffa6c9d02250
parent24738a235e87e7e17ee8a721609e0cbb6613218e (diff)
downloadcoinosv3-server-957599974b453b9b13653b821658fd0a76d01809.tar.gz
coinosv3-server-957599974b453b9b13653b821658fd0a76d01809.zip
names: /migrate-grant only hands over a name whose coinos.io account has migrated.
"check with the coinos-server agent on desk about the migrate hole. We should fix the issue and get it reenabled" (Adam, 2026-12-02). The bearer token proves the caller is coinos.io, not that the name's own account asked for the grant. The grant now also asks coinos.io for the account and goes ahead only when it exists and is flagged migrated; the old account's session flags itself just before it calls the grant, so a legitimate migration passes. It refuses with 404 when there's no such account, 409 when it hasn't migrated, and 502 when coinos.io can't be asked (the caller retries). New migrate-check.test.js passes. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6WoqaQDYdpWkatPCar
-rw-r--r--names/migrate-check.js19
-rw-r--r--names/migrate-check.test.js26
-rw-r--r--names/server.js3
3 files changed, 48 insertions, 0 deletions
diff --git a/names/migrate-check.js b/names/migrate-check.js
new file mode 100644
index 0000000..d0248a6
--- /dev/null
+++ b/names/migrate-check.js
@@ -0,0 +1,19 @@
+// /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
+// already be flagged migrated (its own session flags itself just before the
+// grant). A bug upstream then can't hand someone else's name to a stranger.
+// Returns null when the grant may go ahead, else [status, error].
+export async function migrateGrantRefusal(name, fetchFn = fetch) {
+ 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'];
+ if (u.migrated !== true) return [409, 'that coinos.io account has not migrated'];
+ return null;
+}
diff --git a/names/migrate-check.test.js b/names/migrate-check.test.js
new file mode 100644
index 0000000..ba90d1d
--- /dev/null
+++ b/names/migrate-check.test.js
@@ -0,0 +1,26 @@
+// bun test names/migrate-check.test.js
+import { test, expect } from 'bun:test';
+import { migrateGrantRefusal } from './migrate-check.js';
+
+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 }, victim: { username: 'victim', migrated: false }, bob: { username: 'bob' } };
+
+test('a migrated account may hand its name over', async () => {
+ expect(await migrateGrantRefusal('alice', coinos(users))).toBeNull();
+});
+test("someone else's name, not flagged migrated, is refused", async () => {
+ expect((await migrateGrantRefusal('victim', coinos(users)))[0]).toBe(409);
+ expect((await migrateGrantRefusal('bob', coinos(users)))[0]).toBe(409);
+});
+test('a name with no coinos.io account is refused', async () => {
+ expect((await migrateGrantRefusal('nobody', coinos(users)))[0]).toBe(404);
+});
+test("coinos.io down means no grant, and it says to try again", async () => {
+ expect((await migrateGrantRefusal('alice', coinos(users, 502)))[0]).toBe(502);
+ expect((await migrateGrantRefusal('alice', async () => { throw new Error('net'); }))[0]).toBe(502);
+});
diff --git a/names/server.js b/names/server.js
index 0a8a9f1..a8f7464 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,8 @@ 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) 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) {