From 6f1131fff99c757cc97a397957181f2206dbd1e2 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 17:53:24 -0700 Subject: [PATCH 01/18] feat: flag duplicates, then unique-index identity --- .../migrations/0077_identity_uniqueness.sql | 174 ++++++++++ .../identity-uniqueness-migration.test.ts | 296 ++++++++++++++++++ 2 files changed, 470 insertions(+) create mode 100644 backend/src/db/migrations/0077_identity_uniqueness.sql create mode 100644 backend/test/identity-uniqueness-migration.test.ts diff --git a/backend/src/db/migrations/0077_identity_uniqueness.sql b/backend/src/db/migrations/0077_identity_uniqueness.sql new file mode 100644 index 000000000..b11781942 --- /dev/null +++ b/backend/src/db/migrations/0077_identity_uniqueness.sql @@ -0,0 +1,174 @@ +-- One person, one account: identity uniqueness for ORCID and email +-- (ADR 0043, epic #1250 phase #1254). +-- +-- WHAT IS BROKEN. Nothing has ever stopped one person from holding two live +-- accounts. `users.email` carries an exact-case UNIQUE constraint (0026), so +-- `Ada@Lab.org` and `ada@lab.org` are two accounts; `users.orcid` carries no +-- constraint at all, and the only ORCID uniqueness that exists is +-- UNIQUE(provider, provider_subject) on `oauth_identities`, which the ORCID +-- finalize route checks. That check is not enough, and production proves it: +-- +-- id 42 robert.oostenveld@donders.ru.nl orcid 0000-0002-1974-1293 +-- orcid_verified=1, NO oauth_identities row, no name, no GitHub +-- id 43 r.oostenveld@donders.ru.nl orcid 0000-0002-1974-1293 +-- orcid_verified=1, HAS the oauth_identities row and the ORCID name +-- +-- The identity row left row 42 (an unlink, or an identity insert whose +-- rollback did not run) while `users.orcid`/`orcid_verified` stayed behind on +-- it, so the second sign-up saw an unclaimed iD and made a second account. +-- +-- WHY THIS MIGRATION CANNOT JUST ADD THE INDEXES. A plain +-- `CREATE UNIQUE INDEX ON users(orcid)` fails on rows 42/43 and takes the +-- whole deploy with it. Deleting or merging a row to make the index buildable +-- is not this migration's call either: merging accounts is manual, and 42 is +-- somebody's real account. +-- +-- THE STRATEGY. Add `identity_conflict`, flag every NON-CANONICAL duplicate +-- with it, and make the unique indexes PARTIAL so a flagged row is invisible +-- to them. Nothing is deleted, nothing is merged, and the index is buildable +-- on any catalog whatever it contains -- the flag absorbs exactly the rows +-- that would otherwise fail it. A flagged row still logs in, still owns its +-- datasets, still reads normally; the ONLY thing it loses is its claim on the +-- identifier. `GET /admin/users/duplicates` is how an admin sees the groups, +-- and `POST /admin/users/:id/clear-identity-conflict` clears a flag ONLY once +-- the collision is actually gone (409 with the colliding rows otherwise), so +-- the flag cannot be cleared back into a state the index would refuse. +-- +-- NOT ONE TRANSACTION (same reasoning as 0075's header: wrangler hands the +-- file to executeSql as one string whose transactional scope is not +-- guaranteed on the remote path). Statements 2-5 are individually idempotent +-- and each narrows its own predicate by succeeding, so re-running finishes a +-- partial application. Statement 1, the ALTER, is the exception: SQLite has no +-- `ADD COLUMN IF NOT EXISTS`, so a re-run after it landed fails on it. That is +-- the standing shape for every column-adding migration here (0037, 0062), and +-- the recovery is the same: if the ALTER already landed, re-run statements 2 +-- onward. Do not hand-repair the flags. + +-- (1) The flag. 0 = this row's identifiers are its own; 1 = at least one of +-- them is claimed by another live row and this row is not the canonical +-- holder. NOT NULL DEFAULT 0 so every existing and future row starts clean and +-- the partial-index predicate is never NULL. +ALTER TABLE users ADD COLUMN identity_conflict INTEGER NOT NULL DEFAULT 0; + +-- (2) Canonicalise the ORCID check digit before anything compares iDs. +-- An ORCID iD's last character is a checksum that is `X` when it is not a +-- digit, and uppercase is the canonical spelling. The index in (4) compares +-- `orcid` exactly, so a stored lowercase `x` would read as a different iD +-- from the same person's uppercase one and slip past both the flagging below +-- and the index. Every write path now uppercases it (services/identity.ts), +-- which closes the future; this closes the past. +-- +-- The GLOB is deliberately narrow: it matches ONLY a bare iD ending in a +-- lowercase `x`, so a row holding a full `https://orcid.org/...` URI (or +-- anything else unexpected) is left exactly as it is rather than being +-- uppercased into mojibake by a blanket UPPER(). +UPDATE users + SET orcid = UPPER(orcid) + WHERE orcid GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]x'; + +-- (3a) Flag non-canonical ORCID holders. +-- +-- CANONICAL = the row that holds the `oauth_identities` row for that iD, and +-- the lowest id only when no row does. That ordering is the whole point in the +-- 42/43 case: 43 is the row that can actually sign in with the iD and carries +-- the ORCID name, and 42 is the orphan the identity row left behind. Picking +-- the lowest id would have flagged the working account and left the orphan +-- holding the identifier. +-- +-- At most one row can be identity-backed for a given iD +-- (UNIQUE(provider, provider_subject) on oauth_identities), so the LIMIT 1 +-- picks from a set of at most one and the choice is deterministic. +-- +-- A singleton -- one live row holding an iD nobody else holds -- IS its own +-- canonical row, so `id <> canonical` is false and it is not flagged. There is +-- no separate "is this a duplicate at all" clause because that one is +-- redundant with this one. +-- +-- The subqueries read `id`, `orcid`, `deleted_at` and `oauth_identities`, none +-- of which this statement writes, so the result does not depend on the order +-- SQLite visits rows in. +UPDATE users + SET identity_conflict = 1, + updated_at = datetime('now') + WHERE deleted_at IS NULL + AND identity_conflict = 0 + AND orcid IS NOT NULL + AND TRIM(orcid) <> '' + AND id <> COALESCE( + (SELECT c.id + FROM users c + JOIN oauth_identities oi + ON oi.user_id = c.id + AND oi.provider = 'orcid' + AND oi.provider_subject = c.orcid + WHERE c.deleted_at IS NULL + AND c.orcid = users.orcid + LIMIT 1), + (SELECT MIN(m.id) + FROM users m + WHERE m.deleted_at IS NULL + AND m.orcid = users.orcid)); + +-- (3b) Flag non-canonical email holders, case-insensitively. +-- +-- CANONICAL = the lowest live id in the group. There is no identity row to +-- prefer here and no other principled tiebreak: the oldest account is the one +-- most likely to be the one in use. +-- +-- `m.identity_conflict = 0` makes this statement compose with (3a) instead of +-- fighting it. Without it, a pair that collides on BOTH iD and email -- where +-- (3a) flagged the LOWER id because the identity row sits on the higher one -- +-- would then have its higher id flagged here too, leaving the group with no +-- canonical row at all and neither row able to hold the identifier. +-- +-- Reading a column this statement also writes is safe here, and the reason is +-- worth stating because it is not obvious: this statement only ever flags rows +-- that are NOT the minimum, so the minimum it computes stays unflagged and the +-- MIN is stable no matter which order rows are visited in. (The migration test +-- pins this with a three-row case-variant group.) +-- +-- If (3a) flagged EVERY row in an email group, the subquery is NULL, `id <> +-- NULL` is NULL, and nothing is flagged here -- the group keeps whatever (3a) +-- decided rather than being flagged twice over. +UPDATE users + SET identity_conflict = 1, + updated_at = datetime('now') + WHERE deleted_at IS NULL + AND identity_conflict = 0 + AND id <> (SELECT MIN(m.id) + FROM users m + WHERE m.deleted_at IS NULL + AND m.identity_conflict = 0 + AND m.email = users.email COLLATE NOCASE); + +-- (4) The ORCID index. Partial on three counts: NULL/blank iDs are not an +-- identity (most CLI-era rows have none and multiple NULLs would collide even +-- in a plain unique index only if SQLite treated NULLs as equal -- it does +-- not, but the blank string is not so forgiving, hence the TRIM); tombstones +-- are not live accounts; and a flagged row has already lost its claim. +-- +-- The TRIM predicate matches (3a)'s exactly. That correspondence is +-- load-bearing rather than tidy: if the index admitted a row the flagging pass +-- skipped, the CREATE could fail on a catalog the flags said was clean. +CREATE UNIQUE INDEX IF NOT EXISTS idx_users_orcid_live_unique + ON users(orcid) + WHERE orcid IS NOT NULL + AND TRIM(orcid) <> '' + AND deleted_at IS NULL + AND identity_conflict = 0; + +-- (5) The email index, case-insensitive. This is a strictly stronger +-- constraint than the exact-case table-level UNIQUE from 0026, which stays -- +-- both are enforced, and a case-variant collision now fails at THIS index. +-- +-- SQLite reports such a failure as `UNIQUE constraint failed: users.email` +-- (the column, not the index name), which is what the existing column-scoped +-- catch clauses in signup and /auth/email/change/verify already match on. +CREATE UNIQUE INDEX IF NOT EXISTS idx_users_email_live_unique + ON users(email COLLATE NOCASE) + WHERE deleted_at IS NULL + AND identity_conflict = 0; + +-- Cheap lookup for the duplicate report's "show me the flagged rows" half. +CREATE INDEX IF NOT EXISTS idx_users_identity_conflict + ON users(identity_conflict); diff --git a/backend/test/identity-uniqueness-migration.test.ts b/backend/test/identity-uniqueness-migration.test.ts new file mode 100644 index 000000000..793f5b4c5 --- /dev/null +++ b/backend/test/identity-uniqueness-migration.test.ts @@ -0,0 +1,296 @@ +/** + * Integration test for migration 0077_identity_uniqueness.sql + * (ADR 0043, epic #1250 phase #1254). + * + * Real engine, no mocks: every migration EXCEPT 0077 is applied to an + * in-memory bun:sqlite database, rows are seeded at the pre-0077 schema, then + * 0077 is applied and its rules are asserted against the real SQL. + * + * The fixture is deliberately the production shape plus the two things + * production does not happen to contain, because a rule that only handles the + * catalog as it stands today is a rule that breaks on the next duplicate: + * + * - an ORCID duplicate where the IDENTITY-BACKED row is NOT the lowest id + * (production rows 42/43, whose canonical row is the higher one); + * - a three-row case-variant email group (the lowest id wins, and the two + * losers must not each pick a different survivor); + * - a pair that collides on BOTH identifiers at once, where the two rules + * could otherwise flag every row in the group and leave the identifier + * held by nobody; + * - a legacy lowercase `x` check digit, which the exact-comparison index + * would not otherwise see as the same iD; + * - clean rows and a tombstone, which must come out untouched. + * + * What is asserted is not just "the right rows are flagged" but the property + * that makes the migration deploy-safe: after it runs, EVERY live group has + * exactly one unflagged row, both indexes exist, a later duplicate INSERT is + * refused, and a flagged row is still readable. + */ + +import { Database } from "bun:sqlite"; +import { describe, expect, test } from "bun:test"; +import { readFileSync, readdirSync } from "node:fs"; +import { join } from "node:path"; + +const MIGRATIONS_DIR = join(import.meta.dir, "../src/db/migrations"); +const TARGET = "0077_identity_uniqueness.sql"; + +const SHARED_ORCID = "0000-0002-1974-1293"; +const LOWERCASE_X_ORCID = "0000-0001-5109-353x"; +const UPPERCASE_X_ORCID = "0000-0001-5109-353X"; +const CLEAN_ORCID = "0000-0003-1111-2222"; +const BOTH_ORCID = "0000-0004-4444-5555"; + +/** Apply every migration up to (not including) the target. */ +function dbBeforeTarget(): Database { + const db = new Database(":memory:"); + for (const file of readdirSync(MIGRATIONS_DIR) + .filter((f) => f.endsWith(".sql") && f < TARGET) + .sort()) { + db.exec(readFileSync(join(MIGRATIONS_DIR, file), "utf-8")); + } + return db; +} + +function applyTarget(db: Database): void { + db.exec(readFileSync(join(MIGRATIONS_DIR, TARGET), "utf-8")); +} + +interface SeedRow { + email: string; + orcid?: string | null; + github?: string | null; + /** Give this row the oauth_identities row for its own iD. */ + identityBacked?: boolean; + deleted?: boolean; +} + +/** Seed rows in array order so ids are predictable, and return them by email. */ +function seed(db: Database, rows: SeedRow[]): Map { + const insert = db.prepare( + `INSERT INTO users (email, status, signup_source, orcid, orcid_verified, github_username, deleted_at) + VALUES (?, 'verified', 'web', ?, ?, ?, ?)`, + ); + const ids = new Map(); + for (const r of rows) { + insert.run( + r.email, + r.orcid ?? null, + r.orcid ? 1 : 0, + r.github ?? null, + r.deleted ? "2026-01-01 00:00:00" : null, + ); + const row = db + .query<{ id: number }, [string]>("SELECT id FROM users WHERE email = ?") + .get(r.email); + if (!row) throw new Error(`seed failed for ${r.email}`); + ids.set(r.email, row.id); + if (r.identityBacked && r.orcid) { + db.prepare( + "INSERT INTO oauth_identities (user_id, provider, provider_subject) VALUES (?, 'orcid', ?)", + ).run(row.id, r.orcid); + } + } + return ids; +} + +/** + * The fixture, seeded and migrated. Ids are assigned in the order below, so + * "the orphan is the LOWER id" is a fact about this fixture, not a hope. + */ +function migratedFixture(): { db: Database; ids: Map } { + const db = dbBeforeTarget(); + const ids = seed(db, [ + // The production 42/43 shape: the orphan first (lower id), the + // identity-backed row second (higher id). + { email: "robert.oostenveld@donders.ru.nl", orcid: SHARED_ORCID }, + { email: "r.oostenveld@donders.ru.nl", orcid: SHARED_ORCID, identityBacked: true }, + // A three-row case-variant email group. + { email: "Ada@Lab.org" }, + { email: "ada@lab.org" }, + { email: "ADA@LAB.ORG" }, + // Legacy lowercase check digit vs its canonical spelling. + { email: "x-lower@example.org", orcid: LOWERCASE_X_ORCID }, + { email: "x-upper@example.org", orcid: UPPERCASE_X_ORCID }, + // Collides on BOTH: same iD (identity row on the HIGHER id, so the ORCID + // rule flags the lower one) and the same address case-insensitively (so + // the email rule would, on its own, flag the higher one instead). + { email: "Both@Example.org", orcid: BOTH_ORCID }, + { email: "both@example.org", orcid: BOTH_ORCID, identityBacked: true }, + // Clean rows: an untouched account, and a tombstone whose iD duplicates it. + { email: "clean@example.org", orcid: CLEAN_ORCID, github: "Octocat" }, + { email: "deleted+99@deleted.invalid", orcid: CLEAN_ORCID, deleted: true }, + ]); + applyTarget(db); + return { db, ids }; +} + +function flagOf(db: Database, email: string): number { + const row = db + .query<{ identity_conflict: number }, [string]>( + "SELECT identity_conflict FROM users WHERE email = ?", + ) + .get(email); + if (!row) throw new Error(`no row for ${email}`); + return row.identity_conflict; +} + +describe("migration 0077: the flag", () => { + test("the ORCID duplicate flags the ORPHAN, not the lowest id", () => { + const { db, ids } = migratedFixture(); + // Guard the fixture's own premise: the identity-backed row IS the higher + // id, so "lowest id wins" and "identity row wins" give different answers. + expect(ids.get("r.oostenveld@donders.ru.nl")).toBeGreaterThan( + ids.get("robert.oostenveld@donders.ru.nl") as number, + ); + expect(flagOf(db, "robert.oostenveld@donders.ru.nl")).toBe(1); + expect(flagOf(db, "r.oostenveld@donders.ru.nl")).toBe(0); + }); + + test("the case-variant email group keeps the lowest id and flags the rest", () => { + const { db } = migratedFixture(); + expect(flagOf(db, "Ada@Lab.org")).toBe(0); + expect(flagOf(db, "ada@lab.org")).toBe(1); + expect(flagOf(db, "ADA@LAB.ORG")).toBe(1); + }); + + test("a lowercase check digit is canonicalised and then treated as the same iD", () => { + const { db } = migratedFixture(); + const stored = db + .query<{ orcid: string }, [string]>("SELECT orcid FROM users WHERE email = ?") + .get("x-lower@example.org"); + expect(stored?.orcid).toBe(UPPERCASE_X_ORCID); + // Now that the two spellings are one value, the pair is a duplicate and + // the lower id keeps it. Without statement (2) neither would be flagged + // and the index would compare them as different people. + expect(flagOf(db, "x-lower@example.org")).toBe(0); + expect(flagOf(db, "x-upper@example.org")).toBe(1); + }); + + test("clean rows and tombstones are untouched", () => { + const { db } = migratedFixture(); + expect(flagOf(db, "clean@example.org")).toBe(0); + // The tombstone shares the clean row's iD, and must neither be flagged + // nor cause the clean row to be. + expect(flagOf(db, "deleted+99@deleted.invalid")).toBe(0); + }); + + test("a pair colliding on BOTH identifiers still leaves one row holding them", () => { + const { db } = migratedFixture(); + // The two rules disagree about which row to flag here (ORCID says the + // lower id, email says the higher). If they were independent, both rows + // would end up flagged and the identifier would be held by nobody. + const lower = flagOf(db, "Both@Example.org"); + const upper = flagOf(db, "both@example.org"); + expect([lower, upper].filter((f) => f === 0)).toHaveLength(1); + // Specifically: the ORCID rule runs first and wins, so the identity-backed + // row survives -- the same precedence as the 42/43 case. + expect(upper).toBe(0); + expect(lower).toBe(1); + }); + + test("every live duplicate group ends with exactly one unflagged row", () => { + // The whole-catalog property, asserted over the fixture rather than row by + // row: a rule that fixes the rows a test names while leaving a group + // behind is exactly the failure this migration exists to prevent. + const { db } = migratedFixture(); + const orcidGroups = db + .query<{ n: number }, []>( + `SELECT COUNT(*) AS n FROM ( + SELECT orcid FROM users + WHERE deleted_at IS NULL AND orcid IS NOT NULL AND identity_conflict = 0 + GROUP BY orcid HAVING COUNT(*) > 1)`, + ) + .get(); + expect(orcidGroups?.n).toBe(0); + const emailGroups = db + .query<{ n: number }, []>( + `SELECT COUNT(*) AS n FROM ( + SELECT email FROM users + WHERE deleted_at IS NULL AND identity_conflict = 0 + GROUP BY email COLLATE NOCASE HAVING COUNT(*) > 1)`, + ) + .get(); + expect(emailGroups?.n).toBe(0); + }); +}); + +describe("migration 0077: the indexes", () => { + test("both partial unique indexes exist, with their COLLATE and predicates", () => { + // SQLite/D1 support a partial UNIQUE index whose key carries a COLLATE. + // Reading the stored DDL rather than just the name proves the index that + // exists is the one intended: an index without the COLLATE would let a + // case variant through, and one without the predicate could not have been + // created over this fixture at all. + const { db } = migratedFixture(); + const rows = db + .query<{ name: string; sql: string }, []>( + "SELECT name, sql FROM sqlite_master WHERE type = 'index' AND name LIKE 'idx_users_%_live_unique'", + ) + .all(); + const byName = new Map(rows.map((r) => [r.name, r.sql])); + expect([...byName.keys()].sort()).toEqual([ + "idx_users_email_live_unique", + "idx_users_orcid_live_unique", + ]); + const emailSql = byName.get("idx_users_email_live_unique") as string; + expect(emailSql).toContain("UNIQUE INDEX"); + expect(emailSql).toContain("COLLATE NOCASE"); + expect(emailSql).toContain("identity_conflict = 0"); + expect(emailSql).toContain("deleted_at IS NULL"); + const orcidSql = byName.get("idx_users_orcid_live_unique") as string; + expect(orcidSql).toContain("UNIQUE INDEX"); + expect(orcidSql).toContain("identity_conflict = 0"); + expect(orcidSql).toContain("deleted_at IS NULL"); + }); + + test("a later duplicate INSERT is refused on each identifier", () => { + const { db } = migratedFixture(); + const insert = db.prepare( + "INSERT INTO users (email, status, signup_source, orcid) VALUES (?, 'verified', 'web', ?)", + ); + expect(() => insert.run("someone-else@example.org", CLEAN_ORCID)).toThrow( + /UNIQUE constraint failed: users\.orcid/, + ); + expect(() => insert.run("CLEAN@Example.ORG", null)).toThrow( + /UNIQUE constraint failed: users\.email/, + ); + }); + + test("a duplicate that is FLAGGED can still be inserted (that is the escape hatch)", () => { + // This is what makes the migration deploy-safe: the flag is how a real + // duplicate coexists with a unique index instead of failing the deploy. + const { db } = migratedFixture(); + expect(() => + db + .prepare( + "INSERT INTO users (email, status, signup_source, orcid, identity_conflict) VALUES (?, 'verified', 'web', ?, 1)", + ) + .run("flagged-dupe@example.org", CLEAN_ORCID), + ).not.toThrow(); + }); + + test("a flagged row is still fully readable", () => { + // A flagged account is not quarantined: it signs in, owns datasets, and + // appears in listings. Only its claim on the identifier is gone. + const { db } = migratedFixture(); + const row = db + .query<{ id: number; email: string; orcid: string; status: string }, [string]>( + "SELECT id, email, orcid, status FROM users WHERE email = ?", + ) + .get("robert.oostenveld@donders.ru.nl"); + expect(row?.orcid).toBe(SHARED_ORCID); + expect(row?.status).toBe("verified"); + }); + + test("multiple rows with no ORCID at all do not collide", () => { + const { db } = migratedFixture(); + const insert = db.prepare( + "INSERT INTO users (email, status, signup_source, orcid) VALUES (?, 'verified', 'cli', NULL)", + ); + expect(() => { + insert.run("no-orcid-1@example.org"); + insert.run("no-orcid-2@example.org"); + }).not.toThrow(); + }); +}); From 44584f029cd2146ec286a59ca05de86ff44ea5a7 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 17:53:29 -0700 Subject: [PATCH 02/18] feat: one module for identity rules and codes --- backend/src/services/identity.ts | 214 +++++++++++++++++++++++++++++++ backend/src/services/profile.ts | 5 +- shared/contract/identity.ts | 134 +++++++++++++++++++ shared/contract/index.ts | 1 + 4 files changed, 353 insertions(+), 1 deletion(-) create mode 100644 backend/src/services/identity.ts create mode 100644 shared/contract/identity.ts diff --git a/backend/src/services/identity.ts b/backend/src/services/identity.ts new file mode 100644 index 000000000..7cb1da9a5 --- /dev/null +++ b/backend/src/services/identity.ts @@ -0,0 +1,214 @@ +/** + * Identity uniqueness: one person, one account (#1254, epic #1250; ADR 0043). + * + * An ORCID iD, an email address (case-insensitively) or a GitHub handle may + * back at most one LIVE account. Migration 0077 enforces the first two in the + * database with partial unique indexes; `idx_users_github` (0012) has always + * enforced the third. This module is the application half: the normalisation + * every write must apply before it stores an identifier, and the pre-flight + * lookups that turn a would-be constraint violation into a typed refusal that + * tells the person what to do instead. + * + * WHY ONE MODULE. The rules were previously spelled out at each call site and + * they disagreed. `users.email` was written verbatim by CLI signup (never + * lowercased) and lowercased by the web routes; ORCID finalize checked + * `oauth_identities` but not `users.orcid`; the GitHub "@" strip existed only + * in `services/profile.ts`. Every one of those gaps is a way to end up with + * two accounts for one person, which is what production rows 42 and 43 are. + * + * NOTHING HERE DELETES OR MERGES. A refusal points at the account that already + * exists; merging two accounts stays a manual, human decision. + */ + +import { z } from "zod"; +import { + IDENTITY_CONFLICT_MESSAGES, + type IdentityConflictCode, +} from "../../../shared/contract/identity.js"; +import { ORCID_ID_PATTERN } from "../../../shared/contract/publication.js"; + +/** + * Body of a typed identity refusal. + * + * `code` is what the website switches on; `message` is the sentence a human + * reads. `error` is set by the CALLER, not here, because the two families of + * route disagree about what belongs in it and both are right: the + * browser-facing routes have always put the CODE in `error` (the website reads + * it there, and `email_in_use` predates this phase), while the CLI-facing + * signup puts a short human label there because that is the field the CLI + * prints. Neither is changed by #1254. + */ +export function identityRefusal(code: IdentityConflictCode): { + code: IdentityConflictCode; + message: string; +} { + return { code, message: IDENTITY_CONFLICT_MESSAGES[code] }; +} + +/** + * Canonical stored form of an email address: trimmed and lowercased. + * + * Lowercasing the local part is technically lossy (RFC 5321 leaves its case + * significant), and it is the right trade anyway: no provider anyone signs up + * with treats `Ada@` and `ada@` as two people, while storing both is exactly + * how one person ends up with two accounts. This is also what the web routes + * have always done via their Zod transform, so the CLI path was the outlier. + */ +export function normalizeEmail(raw: string): string { + return raw.trim().toLowerCase(); +} + +/** + * The email field every auth route validates, with normalisation running + * BEFORE `.email()` rather than after it. + * + * Order matters and the five hand-rolled copies this replaces all had it + * backwards: `z.string().email().transform(trim+lowercase)` rejects a pasted + * `" ada@lab.org "` outright, because the trim never runs. Normalising first + * accepts it and stores the canonical form -- which is the whole rule (ADR + * 0043), not a convenience. + */ +export const emailFieldSchema = z.preprocess( + (v) => (typeof v === "string" ? normalizeEmail(v) : v), + z.string().email().max(320), +); + +/** + * Canonical stored form of an ORCID iD, or `null` when the input holds no + * well-formed iD. + * + * Accepts a bare iD or a full `https://orcid.org/` URI. The check digit is + * uppercased: `X` is the canonical spelling of a non-numeric checksum, and the + * unique index compares `users.orcid` exactly, so a lowercase `x` would read + * as a different person's iD. Migration 0077 canonicalises the existing rows + * for the same reason. + * + * Deliberately NOT `orcid-auth.ts`'s `normalizeOrcidId`: that one matches only + * an uppercase `X` and would reject `...353x` outright rather than fixing it. + */ +export function normalizeOrcid(raw: string | null | undefined): string | null { + if (!raw) return null; + const upper = raw.trim().toUpperCase(); + const match = upper.match(/(\d{4}-\d{4}-\d{4}-\d{3}[\dX])$/); + if (!match) return null; + return ORCID_ID_PATTERN.test(match[1]) ? match[1] : null; +} + +/** + * Canonical stored form of a GitHub handle: trimmed with a leading `@` + * stripped, because people paste their handle that way. + * + * Case is NOT folded: GitHub's own canonical login has case (`Octocat`), and + * `idx_users_github` is already `COLLATE NOCASE`, so the comparison is + * case-insensitive without the stored value having to be flattened. + */ +export function normalizeGithubHandle(raw: string): string { + return raw.trim().replace(/^@/, ""); +} + +// --------------------------------------------------------------------------- +// Typed refusals +// --------------------------------------------------------------------------- + +/** + * The account that already holds an identifier. Only non-identifying fields + * beyond the id: a refusal must not become an oracle that dumps someone + * else's email back to an anonymous caller. + */ +export interface IdentityHolder { + id: number; + username: string | null; +} + +/** + * Whether a caught D1/SQLite error is a UNIQUE violation on one of the user + * identifier columns. + * + * SQLite names the COLUMN in the message even when the violated index is a + * named partial one (verified against 0077's two indexes), so + * `UNIQUE constraint failed: users.email` covers both the 0026 table-level + * constraint and `idx_users_email_live_unique`. Column-scoped on purpose: a + * UNIQUE hit on some other column must not be reported to a user as "that + * address is taken". + */ +export function isUniqueViolationOn( + err: unknown, + column: "email" | "orcid" | "username" | "github_username", +): boolean { + const msg = err instanceof Error ? err.message : String(err); + return msg.includes("UNIQUE constraint failed") && msg.includes(`users.${column}`); +} + +// --------------------------------------------------------------------------- +// Pre-flight lookups +// --------------------------------------------------------------------------- + +/** + * Live account holding an ORCID iD, through EITHER `users.orcid` or an + * `oauth_identities` row. + * + * The union is the point. `oauth_identities` alone -- the only check ORCID + * finalize did before #1254 -- misses a row whose identity row was removed + * while `users.orcid` stayed behind, and that row is production id 42. A + * tombstoned row is not a holder: the tombstone NULLs `orcid`, and an identity + * row that outlives its user cannot be signed into. + */ +export const ORCID_HOLDER_SQL = `SELECT u.id, u.username + FROM users u + WHERE u.deleted_at IS NULL + AND ( + u.orcid = ? + OR EXISTS (SELECT 1 FROM oauth_identities oi + WHERE oi.user_id = u.id + AND oi.provider = 'orcid' + AND oi.provider_subject = ?) + ) + ORDER BY u.id + LIMIT 1`; + +/** Live account holding an email address, compared case-insensitively. */ +export const EMAIL_HOLDER_SQL = `SELECT id, username FROM users + WHERE deleted_at IS NULL AND email = ? COLLATE NOCASE + ORDER BY id LIMIT 1`; + +/** Live account holding a GitHub handle, compared case-insensitively. */ +export const GITHUB_HOLDER_SQL = `SELECT id, username FROM users + WHERE deleted_at IS NULL AND github_username = ? COLLATE NOCASE + ORDER BY id LIMIT 1`; + +/** + * Find the live account holding `orcid`, if any. Pass `exceptUserId` when the + * caller is checking on behalf of an existing account (a link or a relink), so + * the account's own iD is not reported as a conflict with itself. + */ +export async function findOrcidHolder( + db: D1Database, + orcid: string, + exceptUserId?: number, +): Promise { + const row = await db.prepare(ORCID_HOLDER_SQL).bind(orcid, orcid).first(); + if (!row) return null; + return exceptUserId !== undefined && row.id === exceptUserId ? null : row; +} + +/** Find the live account holding `email` (case-insensitively), if any. */ +export async function findEmailHolder( + db: D1Database, + email: string, + exceptUserId?: number, +): Promise { + const row = await db.prepare(EMAIL_HOLDER_SQL).bind(email).first(); + if (!row) return null; + return exceptUserId !== undefined && row.id === exceptUserId ? null : row; +} + +/** Find the live account holding `github` (case-insensitively), if any. */ +export async function findGithubHolder( + db: D1Database, + github: string, + exceptUserId?: number, +): Promise { + const row = await db.prepare(GITHUB_HOLDER_SQL).bind(github).first(); + if (!row) return null; + return exceptUserId !== undefined && row.id === exceptUserId ? null : row; +} diff --git a/backend/src/services/profile.ts b/backend/src/services/profile.ts index f92fd85f6..f81976465 100644 --- a/backend/src/services/profile.ts +++ b/backend/src/services/profile.ts @@ -28,6 +28,7 @@ * answers 409 `name_is_orcid_canonical` for the rest. */ +import { normalizeGithubHandle } from "./identity"; import { type UsernameFormatError, validateUsernameFormat } from "./username"; /** @@ -86,7 +87,9 @@ export function normalizeProfilePatch(input: ProfilePatchInput): NormalizeResult const patch: ProfilePatch = {}; if (input.github_username !== undefined) { - const handle = input.github_username.trim().replace(/^@/, ""); + // One spelling of the handle rule, shared with signup and the duplicate + // report (services/identity.ts, ADR 0043). + const handle = normalizeGithubHandle(input.github_username); if (handle.length === 0) { patch.github_username = null; } else if (!GITHUB_HANDLE_RE.test(handle)) { diff --git a/shared/contract/identity.ts b/shared/contract/identity.ts new file mode 100644 index 000000000..04590a064 --- /dev/null +++ b/shared/contract/identity.ts @@ -0,0 +1,134 @@ +/** + * Identity-uniqueness wire vocabulary (#1254, epic #1250; ADR 0043). + * + * One person, one account: an ORCID iD, an email address (case-insensitively) + * or a GitHub handle may back at most one live NEMAR account. When a sign-up, + * an ORCID link, or an email change would break that, the route refuses with + * one of the codes below rather than creating the second account. + * + * These are RENDERED, not just logged: the website's Settings and sign-up + * screens switch on the code to decide what to show, so the set is declared + * once here and consumed as a type by both halves -- the same pattern as + * `publicationBlockReasonSchema` in publication.ts. + * + * Zero deps beyond zod (extraction-ready for @nemar/contract). + */ + +import { z } from "zod"; + +/** + * Why an identity write was refused. + * + * - `orcid_in_use` -- the iD is already on a live account through + * `users.orcid`. This is the case nothing checked before #1254, and it is + * how production rows 42/43 came to exist. + * - `orcid_already_linked` -- the iD already has an `oauth_identities` row. + * Kept distinct from `orcid_in_use` because it is the pre-existing code the + * ORCID finalize route has always returned and the website already handles + * it; the two mean the same thing to a user and carry the same message. + * - `orcid_linked_other` -- (redirect flows only) the finished iD backs a + * DIFFERENT account than the session's. Pre-existing. + * - `email_in_use` -- the address is on a live account, compared + * case-insensitively. Pre-existing code, newly also covering case variants. + * - `github_in_use` -- the handle is on a live account, case-insensitively. + * - `identity_conflict_remains` -- an admin tried to clear a row's + * `identity_conflict` flag while the collision that earned it is still + * there. The response names the rows that still collide. + */ +export const identityConflictCodeSchema = z.enum([ + "orcid_in_use", + "orcid_already_linked", + "orcid_linked_other", + "email_in_use", + "github_in_use", + "identity_conflict_remains", +]); +export type IdentityConflictCode = z.infer; + +/** Where a person changes each identifier once they are signed in. */ +export const IDENTITY_SETTINGS_URL = "https://nemar.org/settings"; + +/** + * The "what to do about it" half of every identity refusal. + * + * Each one names the self-service fix on the account that ALREADY exists, + * because that is the only fix that does not need an admin: sign in to it and + * change the email, change the GitHub username, or unlink/re-link the ORCID + * iD. Merging two accounts is manual and is deliberately not offered here as + * if it were a button. + * + * In the CONTRACT rather than in the backend because three clients render + * them: the website (from `code`), the CLI (which shows the sentence directly, + * since a code means nothing in a terminal), and the backend's own JSON + * `message`. One wording, three surfaces. + */ +export const IDENTITY_CONFLICT_MESSAGES: Record = { + orcid_in_use: `That ORCID iD already belongs to a NEMAR account. Sign in to that account instead; if you want the iD on a different account, unlink it there first (Settings, ${IDENTITY_SETTINGS_URL}).`, + orcid_already_linked: `That ORCID iD already belongs to a NEMAR account. Sign in to that account instead; if you want the iD on a different account, unlink it there first (Settings, ${IDENTITY_SETTINGS_URL}).`, + orcid_linked_other: `That ORCID iD is linked to a different NEMAR account. Unlink it there first (Settings, ${IDENTITY_SETTINGS_URL}), then link it here.`, + email_in_use: `That email address already belongs to a NEMAR account. Sign in to that account instead, or change its address first (Settings, ${IDENTITY_SETTINGS_URL}).`, + github_in_use: `That GitHub account is already linked to a NEMAR account. Sign in to that account instead, or change its GitHub username first (Settings, ${IDENTITY_SETTINGS_URL}).`, + identity_conflict_remains: + "The collision that flagged this account is still there. Resolve it on the other account first (change its email or GitHub username, or unlink its ORCID iD), or delete that account, then clear the flag.", +}; + +/** Which identifier a duplicate group is keyed on. */ +export const identityKindSchema = z.enum(["orcid", "email", "github"]); +export type IdentityKind = z.infer; + +/** One live account inside a duplicate group (`GET /admin/users/duplicates`). */ +export const duplicateAccountSchema = z + .object({ + id: z.number().int(), + username: z.string().nullable(), + email: z.string(), + created_at: z.string(), + /** Whether this row holds the `oauth_identities` row for its ORCID iD -- + * the thing that decides which row is canonical in an ORCID group. */ + has_oauth_identity: z.boolean(), + dataset_count: z.number().int(), + /** The 0077 flag: 1 means this row has already lost its claim on at least + * one identifier and is invisible to the partial unique indexes. */ + identity_conflict: z.number().int(), + /** True for the one row in THIS group that keeps the identifier. */ + canonical: z.boolean(), + }) + .passthrough(); +export type DuplicateAccount = z.infer; + +/** A set of live accounts sharing one normalised identifier. */ +export const duplicateGroupSchema = z + .object({ + kind: identityKindSchema, + /** The shared value, normalised (email lowercased, GitHub handle + * lowercased, ORCID as stored). Never a secret. */ + value: z.string(), + canonical_user_id: z.number().int(), + accounts: z.array(duplicateAccountSchema), + }) + .passthrough(); +export type DuplicateGroup = z.infer; + +/** `GET /admin/users/duplicates`. */ +export const duplicateReportSchema = z + .object({ + groups: z.array(duplicateGroupSchema), + group_count: z.number().int(), + /** Live rows carrying `identity_conflict = 1`. This can exceed the number + * of non-canonical rows in `groups` only if a flag outlived its + * collision, which is exactly what the clear endpoint is for. */ + flagged_count: z.number().int(), + }) + .passthrough(); +export type DuplicateReport = z.infer; + +/** `POST /admin/users/:id/clear-identity-conflict` on success. */ +export const clearIdentityConflictResponseSchema = z + .object({ + ok: z.literal(true), + id: z.number().int(), + /** False when the row was already clear -- a no-op, not an error. */ + cleared: z.boolean(), + }) + .passthrough(); +export type ClearIdentityConflictResponse = z.infer; diff --git a/shared/contract/index.ts b/shared/contract/index.ts index 8c2b93f21..0081fddfa 100644 --- a/shared/contract/index.ts +++ b/shared/contract/index.ts @@ -14,3 +14,4 @@ export * from "./version.js"; export * from "./user.js"; export * from "./dataset.js"; export * from "./publication.js"; +export * from "./identity.js"; From 733e1618a7e1c5345d9f159f75ee615994f3e57a Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 17:53:34 -0700 Subject: [PATCH 03/18] feat: refuse a duplicate identity at every write --- backend/src/routes/auth-orcid.ts | 135 +++-- backend/src/routes/auth-web.ts | 43 +- backend/src/routes/auth.ts | 119 +++- backend/test/identity-refusals-route.test.ts | 564 +++++++++++++++++++ 4 files changed, 773 insertions(+), 88 deletions(-) create mode 100644 backend/test/identity-refusals-route.test.ts diff --git a/backend/src/routes/auth-orcid.ts b/backend/src/routes/auth-orcid.ts index b1c50469e..562b01e2e 100644 --- a/backend/src/routes/auth-orcid.ts +++ b/backend/src/routes/auth-orcid.ts @@ -24,6 +24,14 @@ * the finished iD is unclaimed; without relink mode a second, different iD * is refused (`orcid_already_have`), as before. * + * Identity uniqueness (#1254, ADR 0043): an iD backs at most one LIVE account, + * and "backs" means `users.orcid` OR an `oauth_identities` row -- not just the + * identity table, which is all this file checked before. Finalize, link and + * relink all consult `findOrcidHolder`; unlink now clears `users.orcid` too, + * so an unlinked account stops claiming an iD it can no longer prove. The + * refusal codes are `orcid_in_use`, `orcid_already_linked`, `orcid_linked_other` + * and `email_in_use` (shared/contract/identity.ts). + * * Host/cookie model: these routes run on api.nemar.org but the browser reaches * them through the app.nemar.org same-origin proxy (like /auth/code/*), which * mirrors our Set-Cookie. Cookies therefore carry Domain=WEB_SESSION_COOKIE_DOMAIN @@ -37,6 +45,14 @@ import { auditLogStatement } from "../db/audit-log"; import { timingSafeEqual } from "../lib/constant-time"; import { webSessionMiddleware } from "../middleware/webSession"; import { issueEmailVerificationCode } from "../services/email-verification"; +import { + emailFieldSchema, + findEmailHolder, + findOrcidHolder, + identityRefusal, + normalizeEmail, + normalizeOrcid, +} from "../services/identity"; import { type OauthMode, PENDING_COOKIE_NAME, @@ -74,22 +90,14 @@ const STATE_TTL_SECONDS = 10 * 60; const PENDING_TTL_MS = 15 * 60 * 1000; const emailSchema = z.object({ - email: z - .string() - .email() - .max(320) - .transform((e) => e.trim().toLowerCase()), + email: emailFieldSchema, }); // Finalize a brand-new ORCID signup. Mirrors the CLI signup's required fields // (#835): city/country are required for export-control screening, affiliation // optional. Name comes from ORCID, never the form. const finalizeSchema = z.object({ - email: z - .string() - .email() - .max(320) - .transform((e) => e.trim().toLowerCase()), + email: emailFieldSchema, affiliation: z.string().max(200).optional(), city: z.string().min(1, "City is required").max(120), country: z.string().min(1, "Country is required").max(120), @@ -380,9 +388,18 @@ authOrcidRoutes.get("/orcid/callback", webSessionMiddleware, async (c) => { afterResponse(c, refreshUserName(c.env, webUser.id, orcid)); return redirect(`${frontend}${state.next}`, [clearState]); } - // link_new: the finished iD is unclaimed. If this account already has a - // *different* iD, only an explicit relink flow may swap it (#913); - // any other mode keeps the historical refusal. + // link_new says only that no oauth_identities row claims the iD. That + // is not the same as unclaimed (#1254, ADR 0043): a row whose identity + // row was removed while `users.orcid` stayed behind still holds the iD + // for citation and for `nemar admin duplicates`, and linking over it is + // how one person ends up backing two accounts (production rows 42/43). + // Checked here rather than inside the relink branch so it covers both a + // first link and an explicit relink -- both write `users.orcid`. + const orcidHolder = await findOrcidHolder(c.env.DB, orcid, webUser.id); + if (orcidHolder) return fail("orcid_in_use", state.next); + + // If this account already has a *different* iD, only an explicit relink + // flow may swap it (#913); any other mode keeps the historical refusal. const mine = await c.env.DB.prepare( "SELECT provider_subject FROM oauth_identities WHERE user_id = ? AND provider = 'orcid' LIMIT 1", ) @@ -474,30 +491,51 @@ authOrcidRoutes.post("/orcid/finalize", zValidator("json", finalizeSchema), asyn return c.json({ error: "orcid_pending_expired" }, 401); } - const { email, affiliation, city, country } = c.req.valid("json"); + const { affiliation, city, country } = c.req.valid("json"); + // The Zod field already trims and lowercases; re-normalising here is what + // makes the rule the SCHEMA's rather than this route's, so a future schema + // change cannot quietly store a mixed-case address (ADR 0043). + const email = normalizeEmail(c.req.valid("json").email); + // Unreachable as a repair, and kept as a guarantee: `verifyPending` + // already rejects a token whose iD is not in canonical form, so this + // cannot fire for a cookie the callback minted. What it buys is that the + // value this route STORES is canonical by construction rather than by + // trusting an upstream check to stay where it is. + const orcid = normalizeOrcid(pending.orcid); + if (!orcid) { + console.error(`[auth-orcid] finalize: pending token carried a malformed iD ${pending.orcid}`); + c.header("Set-Cookie", clearPending); + return c.json({ error: "orcid_pending_expired" }, 401); + } // Email collision: never auto-link onto an existing account (takeover // vector). Make the user sign in with their existing method, then link - // ORCID from settings. - const existing = await c.env.DB.prepare( - "SELECT id FROM users WHERE email = ? AND deleted_at IS NULL LIMIT 1", - ) - .bind(email) - .first<{ id: number }>(); - if (existing) { - return c.json({ error: "email_in_use" }, 409); + // ORCID from settings. Case-insensitive since #1254: `Ada@Lab.org` and + // `ada@lab.org` are one person, and storing both is one of the two ways + // this catalog grew duplicate accounts. + const emailHolder = await findEmailHolder(c.env.DB, email); + if (emailHolder) { + return c.json({ error: "email_in_use", ...identityRefusal("email_in_use") }, 409); } - // Same ORCID may already back an account if the user double-submitted or - // raced the form; UNIQUE(provider, provider_subject) is the backstop. - const identExists = await c.env.DB.prepare( - "SELECT user_id FROM oauth_identities WHERE provider = 'orcid' AND provider_subject = ? LIMIT 1", - ) - .bind(pending.orcid) - .first<{ user_id: number }>(); - if (identExists) { + // The iD may already back an account through `users.orcid` even with no + // oauth_identities row -- an unlink, or an identity insert whose rollback + // did not run, leaves exactly that shape, and it is why production rows 42 + // and 43 both carry 0000-0002-1974-1293. Checking only the identity table + // (all this route did before #1254) is what let the second sign-up + // through. The two codes are kept apart because `orcid_already_linked` is + // the pre-existing one the website already handles; they mean the same + // thing to the person reading them and carry the same message. + const orcidHolder = await findOrcidHolder(c.env.DB, orcid); + if (orcidHolder) { + const identExists = await c.env.DB.prepare( + "SELECT user_id FROM oauth_identities WHERE provider = 'orcid' AND provider_subject = ? LIMIT 1", + ) + .bind(orcid) + .first<{ user_id: number }>(); + const code = identExists ? "orcid_already_linked" : "orcid_in_use"; c.header("Set-Cookie", clearPending); - return c.json({ error: "orcid_already_linked" }, 409); + return c.json({ error: code, ...identityRefusal(code) }, 409); } // New web account: username/github_username/password_hash stay NULL @@ -517,7 +555,7 @@ authOrcidRoutes.post("/orcid/finalize", zValidator("json", finalizeSchema), asyn affiliation, city, country, approved_at, service_access) VALUES (?, ?, 1, 'pending', 0, 'web', ?, ?, ?, NULL, 0)`, ) - .bind(email, pending.orcid, affiliation || null, city, country) + .bind(email, orcid, affiliation || null, city, country) .run(); const userId = insert.meta?.last_row_id; if (!userId) { @@ -535,7 +573,7 @@ authOrcidRoutes.post("/orcid/finalize", zValidator("json", finalizeSchema), asyn `INSERT INTO oauth_identities (user_id, provider, provider_subject, provider_email, display_name, last_login_at) VALUES (?, 'orcid', ?, NULL, ?, datetime('now'))`, ) - .bind(userId, pending.orcid, pending.name) + .bind(userId, orcid, pending.name) .run(); } catch (identErr) { console.error("[auth-orcid] finalize: identity insert failed; rolling back user", identErr); @@ -546,7 +584,10 @@ authOrcidRoutes.post("/orcid/finalize", zValidator("json", finalizeSchema), asyn console.error("[auth-orcid] finalize: failed to roll back orphan user", delErr), ); c.header("Set-Cookie", clearPending); - return c.json({ error: "orcid_already_linked" }, 409); + return c.json( + { error: "orcid_already_linked", ...identityRefusal("orcid_already_linked") }, + 409, + ); } const { cookieIdRaw, maxAgeSeconds } = await issueSession( @@ -562,7 +603,7 @@ authOrcidRoutes.post("/orcid/finalize", zValidator("json", finalizeSchema), asyn append: true, }); // Canonical name from ORCID, after the response (best-effort; never blocks signup). - afterResponse(c, refreshUserName(c.env, userId, pending.orcid)); + afterResponse(c, refreshUserName(c.env, userId, orcid)); // Mail the first verification code to the address just collected. The // account exists and is signed in either way: an undeliverable code @@ -613,16 +654,34 @@ authOrcidRoutes.post("/orcid/unlink", webSessionMiddleware, async (c) => { const webUser = c.var.webUser; if (!webUser) return c.json({ error: "Authentication required" }, 401); - // Drop the verified link but keep users.orcid (the citation-facing value). + // Drop the link COMPLETELY: the identity row, `orcid_verified`, AND + // `users.orcid` (#1254, ADR 0043). + // + // Keeping `users.orcid` used to be the deliberate choice -- it is the + // citation-facing value, and dropping it loses whatever a DOI-discovery pass + // had attached to the account. That reasoning does not survive the shape it + // produces: a row that still claims an iD it can no longer sign in with, + // which is invisible to every check that looked at `oauth_identities`, and + // which silently blocks (or, before this phase, silently duplicated) the + // person's next sign-up. Production rows 42/43 are that shape. + // + // The citation value is RE-DERIVABLE and the identity is not: linking the iD + // again refills `users.orcid` (`linkIdentity`), and so does the DOI + // enrichment pass that discovered it in the first place. Losing a + // re-derivable column beats keeping a claim on an identifier the account + // cannot prove. + // // Email-code login still works, so removing the only OAuth identity never - // locks the account out. The two writes go in one D1 batch so we never leave + // locks the account out. The writes go in one D1 batch so we never leave // orcid_verified=1 with the identity row already gone (or vice versa). try { await c.env.DB.batch([ c.env.DB.prepare( "DELETE FROM oauth_identities WHERE user_id = ? AND provider = 'orcid'", ).bind(webUser.id), - c.env.DB.prepare("UPDATE users SET orcid_verified = 0 WHERE id = ?").bind(webUser.id), + c.env.DB.prepare("UPDATE users SET orcid = NULL, orcid_verified = 0 WHERE id = ?").bind( + webUser.id, + ), ]); } catch (err) { console.error("[auth-orcid] unlink failed", err); diff --git a/backend/src/routes/auth-web.ts b/backend/src/routes/auth-web.ts index fe14a51a2..1e129c078 100644 --- a/backend/src/routes/auth-web.ts +++ b/backend/src/routes/auth-web.ts @@ -74,6 +74,7 @@ import { } from "../services/email-verification"; import { validateGitHubUsername } from "../services/github"; import { getDatasetsToken } from "../services/github-auth"; +import { emailFieldSchema, findEmailHolder, identityRefusal } from "../services/identity"; import { type ProfilePatchInput, githubHandleChanged, @@ -97,19 +98,11 @@ import { type Bindings, type UserRole, type Variables, parseRole } from "../type export const authWebRoutes = new Hono<{ Bindings: Bindings; Variables: Variables }>(); const emailSchema = z.object({ - email: z - .string() - .email() - .max(320) - .transform((e) => e.trim().toLowerCase()), + email: emailFieldSchema, }); const verifySchema = z.object({ - email: z - .string() - .email() - .max(320) - .transform((e) => e.trim().toLowerCase()), + email: emailFieldSchema, code: z.string().regex(/^\d{6}$/, "code must be 6 digits"), remember: z.boolean(), }); @@ -971,11 +964,7 @@ authWebRoutes.patch( // --------------------------------------------------------------- const emailChangeVerifySchema = z.object({ - email: z - .string() - .email() - .max(320) - .transform((e) => e.trim().toLowerCase()), + email: emailFieldSchema, code: z.string().regex(/^\d{6}$/, "code must be 6 digits"), }); @@ -1150,12 +1139,13 @@ authWebRoutes.post( // is bounded: caller must hold a session, the route sits in the // auth-ip bucket (10/min/IP, rateLimit.ts), and the per-user cap // below throttles how fast one account can cycle targets. - const collision = await db - .prepare("SELECT id FROM users WHERE email = ? AND deleted_at IS NULL LIMIT 1") - .bind(email) - .first<{ id: number }>(); + // Case-insensitive since #1254 (ADR 0043): migration 0077's partial + // unique index refuses a case-variant of a live address, so a request + // that only checked exact case would mail a code for a target the + // verify step could never write. + const collision = await findEmailHolder(db, email); if (collision) { - return c.json({ error: "email_in_use" }, 409); + return c.json({ error: "email_in_use", ...identityRefusal("email_in_use") }, 409); } // #1008 analogue: the non-production D1 mirrors real production users, @@ -1289,14 +1279,13 @@ authWebRoutes.post( return c.json({ error: "same_email" }, 409); } // Re-check the collision: an account for this address may have been - // created between request and verify. The users.email UNIQUE - // constraint below is the authoritative backstop for the write race. - const collision = await db - .prepare("SELECT id FROM users WHERE email = ? AND deleted_at IS NULL LIMIT 1") - .bind(email) - .first<{ id: number }>(); + // created between request and verify. Case-insensitive (ADR 0043), and + // the users.email UNIQUE constraints below -- the 0026 table-level one + // AND 0077's partial `idx_users_email_live_unique` -- remain the + // authoritative backstop for the write race. + const collision = await findEmailHolder(db, email); if (collision) { - return c.json({ error: "email_in_use" }, 409); + return c.json({ error: "email_in_use", ...identityRefusal("email_in_use") }, 409); } const row = await db diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index 79e8a8e9a..37f8e3178 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -24,6 +24,14 @@ import { } from "../services/email"; import { validateGitHubUsername } from "../services/github"; import { getDatasetsToken } from "../services/github-auth"; +import { + findEmailHolder, + findOrcidHolder, + identityRefusal, + normalizeEmail, + normalizeGithubHandle, + normalizeOrcid, +} from "../services/identity"; import { fetchOrcidName, orcidPubBase } from "../services/orcid-auth"; import { hashPassword, validatePasswordStrength, verifyPassword } from "../services/password"; import { @@ -178,16 +186,27 @@ const signupSchema = z.object({ /^[a-zA-Z0-9_-]+$/, "Username can only contain letters, numbers, underscores, and hyphens", ), - email: z.string().email("Invalid email address"), + // Normalised BEFORE validation (ADR 0043) so the stored value can only ever + // be canonical: an address is trimmed and lowercased, and a GitHub handle + // loses a pasted leading "@" instead of failing the format check over it. + // `preprocess` rather than `.transform()` because the rules have to run + // before `.email()`/`.regex()`, not after them. + email: z.preprocess( + (v) => (typeof v === "string" ? normalizeEmail(v) : v), + z.string().email("Invalid email address"), + ), password: z.string().min(12, "Password must be at least 12 characters").max(128), - github_username: z - .string() - .min(1, "GitHub username is required") - .max(39, "GitHub username is too long") - .regex( - /^[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/, - "GitHub username must start and end with a letter or number, and can only contain letters, numbers, and hyphens", - ), + github_username: z.preprocess( + (v) => (typeof v === "string" ? normalizeGithubHandle(v) : v), + z + .string() + .min(1, "GitHub username is required") + .max(39, "GitHub username is too long") + .regex( + /^[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/, + "GitHub username must start and end with a letter or number, and can only contain letters, numbers, and hyphens", + ), + ), description: z .string() .min( @@ -196,7 +215,11 @@ const signupSchema = z.object({ ) .max(500, "Description must be at most 500 characters"), // ORCID is now required: it's the canonical source for the user's name (#835). - orcid: orcidIdSchema, + // Uppercased first so an iD typed with a lowercase `x` check digit is + // accepted and STORED canonically -- the partial unique index from migration + // 0077 compares `users.orcid` exactly, so a case variant would read as a + // different person's iD (ADR 0043). + orcid: z.preprocess((v) => (typeof v === "string" ? (normalizeOrcid(v) ?? v) : v), orcidIdSchema), // Supplied only when the ORCID record hides its name (#1255): the server // still reads ORCID first, so these are a fallback, never an override. given_name: z.string().trim().min(1).max(100).optional(), @@ -242,13 +265,19 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { ); } - // NOTE: the signup de-dup checks below (username / email / github_username) - // deliberately do NOT filter `deleted_at IS NULL` — they must see ALL rows, - // including tombstones, to honor the UNIQUE constraints. Re-signup with a + // NOTE: the username / github_username de-dup checks below deliberately do + // NOT filter `deleted_at IS NULL` — they must see ALL rows, including + // tombstones, to honor the table-wide UNIQUE constraints. Re-signup with a // deleted user's old email/username/github is enabled by the tombstone // MASKING (email -> deleted+@deleted.invalid, username/github -> NULL), // which frees those values, NOT by excluding deleted rows here. See the // DELETE /admin/users/by-id/:id tombstone + migration 0037. + // + // The email and ORCID checks are the exception and are live-rows-only, + // because the constraints they mirror are the PARTIAL indexes from 0077 + // (`deleted_at IS NULL AND identity_conflict = 0`) rather than table-wide + // ones. Masking still does the freeing; the predicate just matches the + // index the write will actually hit. // Check if username already exists const existingUsername = await db @@ -260,14 +289,28 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { return c.json({ error: "Username already taken" }, 409); } - // Check if email already exists - const existingEmail = await db - .prepare("SELECT id FROM users WHERE email = ?") - .bind(email) - .first(); + // Check if email already exists, case-insensitively (ADR 0043). The + // exact-case check this replaces let `Ada@Lab.org` and `ada@lab.org` + // become two accounts for one person; migration 0077's partial unique + // index now refuses that write outright, and this turns the refusal into + // a message that says what to do about it. + const existingEmail = await findEmailHolder(db, email); if (existingEmail) { - return c.json({ error: "Email already registered" }, 409); + return c.json({ error: "Email already registered", ...identityRefusal("email_in_use") }, 409); + } + + // Check if the ORCID iD already backs an account (#1254). Nothing checked + // this before: `users.orcid` carried no constraint at all, and the only + // ORCID uniqueness in the system lived on `oauth_identities`, which a CLI + // signup never writes. So the CLI was a way to mint a second account for + // an iD that already had one. `findOrcidHolder` looks at both. + const existingOrcid = await findOrcidHolder(db, orcid); + if (existingOrcid) { + return c.json( + { error: "ORCID iD already registered", ...identityRefusal("orcid_in_use") }, + 409, + ); } // Direct dup check on the raw input (case-insensitive). Common case: @@ -280,7 +323,13 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { .bind(github_username) .first(); if (directDupGithub) { - return c.json({ error: "GitHub account already linked to another user" }, 409); + return c.json( + { + error: "GitHub account already linked to another user", + ...identityRefusal("github_in_use"), + }, + 409, + ); } // Validate GitHub username exists. This is also where we recover the @@ -322,7 +371,13 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { .bind(githubUser.login) .first(); if (canonicalDup) { - return c.json({ error: "GitHub account already linked to another user" }, 409); + return c.json( + { + error: "GitHub account already linked to another user", + ...identityRefusal("github_in_use"), + }, + 409, + ); } } @@ -477,10 +532,28 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { return c.json({ error: "Username already taken" }, 409); } if (msg.includes("users.email")) { - return c.json({ error: "Email already registered" }, 409); + return c.json( + { error: "Email already registered", ...identityRefusal("email_in_use") }, + 409, + ); + } + // 0077's partial index reports the COLUMN, not the index name, so this + // catches a concurrent signup that claimed the iD between the check + // above and this insert. + if (msg.includes("users.orcid")) { + return c.json( + { error: "ORCID iD already registered", ...identityRefusal("orcid_in_use") }, + 409, + ); } if (msg.includes("users.github_username")) { - return c.json({ error: "GitHub account already linked to another user" }, 409); + return c.json( + { + error: "GitHub account already linked to another user", + ...identityRefusal("github_in_use"), + }, + 409, + ); } console.error("Unhandled UNIQUE constraint column in signup:", msg); return c.json({ error: "An account with these details already exists" }, 409); diff --git a/backend/test/identity-refusals-route.test.ts b/backend/test/identity-refusals-route.test.ts new file mode 100644 index 000000000..81b7c1b4a --- /dev/null +++ b/backend/test/identity-refusals-route.test.ts @@ -0,0 +1,564 @@ +/** + * Real route tests for identity uniqueness (#1254, epic #1250; ADR 0043). + * + * One person, one account: an ORCID iD, an email address (case-insensitively) + * or a GitHub handle backs at most one live account. Migration 0077 enforces + * it in the database; these tests drive the four APPLICATION entry points that + * have to refuse before the database has to, plus the unlink that stops an + * account claiming an iD it can no longer prove. + * + * Real engine throughout: bun:sqlite behind realD1 with every migration + * applied, real Hono dispatch via `app.request()`, real zod validation, real + * bcrypt at signup, real session issuance via `issueSession()`, real HMAC code + * hashing via the production `hashAuthCode`, and real pending-cookie signing + * via `signPending`. The two external boundaries -- ORCID's public record API + * and GitHub's /users/:login -- are a local `Bun.serve()` reached through the + * bindings production already uses to point at a mirror. No mocks. + * + * ENTRY POINTS, NOT HELPERS. Every case below goes through the HTTP route, so + * it can catch the thing that actually went wrong in production: a check that + * existed but looked at the wrong table. `findOrcidHolder` returning the right + * row proves nothing if finalize never calls it. + */ + +import type { Database } from "bun:sqlite"; +import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test"; +import type { Server } from "bun"; +import { Hono } from "hono"; +import { authRoutes } from "../src/routes/auth"; +import { authOrcidRoutes } from "../src/routes/auth-orcid"; +import { authWebRoutes } from "../src/routes/auth-web"; +import { PENDING_COOKIE_NAME, encodeState, signPending } from "../src/services/orcid-auth"; +import { issueSession } from "../src/services/web-session"; +import type { Bindings, Variables } from "../src/types/bindings"; +import { freshDb, realD1 } from "./helpers/d1"; + +const ORIGIN = "https://nemar.org"; +const APP = "https://app.nemar.org"; +const ENCRYPTION_KEY = "identity-refusal-test-encryption-key-0123"; +const CSRF = "csrf-identity-refusal-test"; + +const HELD_ORCID = "0000-0002-1974-1293"; +const OTHER_ORCID = "0000-0002-1825-0097"; +/** Valid iD whose check digit is X, used to prove the uppercase rule. */ +const X_ORCID = "0000-0001-5109-353X"; + +let db: Database; +let app: Hono<{ Bindings: Bindings; Variables: Variables }>; +/** Serves ORCID personal-details, ORCID /oauth/token, and GitHub /users/:login. */ +let external: Server; +let externalBase: string; +/** Which iD the local ORCID token endpoint hands back for the next callback. */ +let tokenOrcid = OTHER_ORCID; + +beforeAll(() => { + external = Bun.serve({ + port: 0, + fetch(req) { + const url = new URL(req.url); + if (url.pathname === "/oauth/token") { + return Response.json({ orcid: tokenOrcid, name: "Ada Lovelace", access_token: "unused" }); + } + if (url.pathname.endsWith("/personal-details")) { + return Response.json({ + name: { "given-names": { value: "Ada" }, "family-name": { value: "Lovelace" } }, + }); + } + const gh = url.pathname.match(/^\/users\/(.+)$/); + if (gh) return Response.json({ login: gh[1], id: 4242 }); + return new Response("not found", { status: 404 }); + }, + }); + externalBase = `http://localhost:${external.port}`; + (globalThis as { NEMAR_GITHUB_API_URL?: string }).NEMAR_GITHUB_API_URL = externalBase; +}); + +afterAll(() => { + (globalThis as { NEMAR_GITHUB_API_URL?: string }).NEMAR_GITHUB_API_URL = undefined; + external.stop(true); +}); + +function env(): Bindings { + return { + DB: realD1(db), + ENVIRONMENT: "test", + ENCRYPTION_KEY, + ORCID_CLIENT_ID: "APP-TEST", + ORCID_CLIENT_SECRET: "test-secret", + ORCID_API_BASE: externalBase, + ORCID_PUB_API_BASE: externalBase, + APP_BASE_URL: APP, + WEB_SESSION_COOKIE_DOMAIN: "", + GITHUB_ADMIN_PAT: "test-pat-not-used-against-a-real-host", + API_BASE_URL: "http://localhost:8787", + } as unknown as Bindings; +} + +beforeEach(() => { + db = freshDb(); + tokenOrcid = OTHER_ORCID; + app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); + app.route("/auth", authRoutes); + app.route("/auth", authOrcidRoutes); + app.route("/auth", authWebRoutes); +}); + +// -------------------------------------------------------------------------- +// Seeding +// -------------------------------------------------------------------------- + +interface SeedOpts { + orcid?: string | null; + /** Give the row the oauth_identities row for its iD. */ + identityBacked?: boolean; + github?: string | null; + username?: string | null; + deleted?: boolean; +} + +function seedUser(email: string, opts: SeedOpts = {}): number { + db.run( + `INSERT INTO users (username, email, status, signup_source, email_verified, + orcid, orcid_verified, github_username, deleted_at) + VALUES (?, ?, 'verified', 'web', 1, ?, ?, ?, ?)`, + [ + opts.username ?? null, + email, + opts.orcid ?? null, + opts.orcid ? 1 : 0, + opts.github ?? null, + opts.deleted ? "2026-01-01 00:00:00" : null, + ], + ); + const row = db.query<{ id: number }, [string]>("SELECT id FROM users WHERE email = ?").get(email); + if (!row) throw new Error(`seed failed for ${email}`); + if (opts.identityBacked && opts.orcid) { + db.run( + "INSERT INTO oauth_identities (user_id, provider, provider_subject) VALUES (?, 'orcid', ?)", + [row.id, opts.orcid], + ); + } + return row.id; +} + +function userByEmail(email: string) { + return db + .query< + { + id: number; + email: string; + orcid: string | null; + orcid_verified: number; + github_username: string | null; + }, + [string] + >("SELECT id, email, orcid, orcid_verified, github_username FROM users WHERE email = ?") + .get(email); +} + +async function sessionCookie(userId: number): Promise { + const { cookieIdRaw } = await issueSession(env(), userId, false, null, null, "orcid"); + return `nemar_session=${cookieIdRaw}`; +} + +// -------------------------------------------------------------------------- +// CLI signup +// -------------------------------------------------------------------------- + +function signup(body: Record = {}): Promise { + return app.request( + "/auth/signup", + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + username: "newuser", + email: "newuser@example.org", + password: "Correct-Horse-Battery-9", + github_username: "newuser", + description: "I would like to deposit EEG datasets collected in our lab.", + orcid: OTHER_ORCID, + city: "San Diego", + country: "USA", + ...body, + }), + }, + env(), + ); +} + +describe("CLI signup refuses a duplicate identity", () => { + test("a case-variant of a live address is refused with the Settings message", async () => { + seedUser("Ada@Lab.org"); + const res = await signup({ email: "ADA@lab.ORG" }); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string; message: string }; + expect(body.code).toBe("email_in_use"); + // The `error` string is unchanged (the CLI prints it and pins it), and the + // actionable sentence rides alongside in `message`. + expect(body.error).toBe("Email already registered"); + expect(body.message).toContain("nemar.org/settings"); + expect(db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM users").get()?.n).toBe(2); + }); + + test("an iD held only through users.orcid is refused (the 42/43 hole)", async () => { + // No oauth_identities row: exactly the shape a CLI signup could never see + // before #1254, because nothing checked users.orcid at all. + seedUser("holder@example.org", { orcid: HELD_ORCID }); + const res = await signup({ orcid: HELD_ORCID }); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string; message: string }; + expect(body.code).toBe("orcid_in_use"); + expect(body.message).toContain("nemar.org/settings"); + }); + + test("an iD held only through oauth_identities is refused too", async () => { + // The mirror image: users.orcid is NULL and the identity row carries it. + const id = seedUser("linked@example.org"); + db.run( + "INSERT INTO oauth_identities (user_id, provider, provider_subject) VALUES (?, 'orcid', ?)", + [id, HELD_ORCID], + ); + const res = await signup({ orcid: HELD_ORCID }); + expect(res.status).toBe(409); + expect(((await res.json()) as { code: string }).code).toBe("orcid_in_use"); + }); + + test("a case-variant GitHub handle is refused", async () => { + seedUser("gh@example.org", { github: "Octocat" }); + const res = await signup({ github_username: "octocat" }); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string }; + expect(body.code).toBe("github_in_use"); + expect(body.error).toBe("GitHub account already linked to another user"); + }); + + test("a tombstoned row does not block re-signup with its freed address", async () => { + // The tombstone MASKS the address rather than holding it, so the live-only + // predicate the email check now uses must not resurrect it as a collision. + seedUser("deleted+7@deleted.invalid", { deleted: true }); + const res = await signup({ email: "newuser@example.org" }); + expect(res.status).toBe(201); + }); + + test("an iD held by a TOMBSTONED row does not block a new signup", async () => { + seedUser("deleted+8@deleted.invalid", { orcid: HELD_ORCID, deleted: true }); + const res = await signup({ orcid: HELD_ORCID }); + expect(res.status).toBe(201); + }); +}); + +describe("CLI signup normalises what it stores", () => { + test("a mixed-case address is stored lowercase", async () => { + const res = await signup({ email: "Ada.Lovelace@Example.ORG" }); + expect(res.status).toBe(201); + expect(userByEmail("ada.lovelace@example.org")).toBeTruthy(); + }); + + test("a pasted @handle is accepted and stored without the @", async () => { + // Before #1254 the regex rejected the leading "@" outright, so a pasted + // handle failed validation rather than being cleaned up. + const res = await signup({ github_username: "@octocat" }); + expect(res.status).toBe(201); + expect(userByEmail("newuser@example.org")?.github_username).toBe("octocat"); + }); + + test("a lowercase check digit is accepted and stored uppercase", async () => { + const res = await signup({ orcid: X_ORCID.toLowerCase() }); + expect(res.status).toBe(201); + expect(userByEmail("newuser@example.org")?.orcid).toBe(X_ORCID); + }); + + test("a full ORCID URI is reduced to the bare iD", async () => { + const res = await signup({ orcid: `https://orcid.org/${OTHER_ORCID}` }); + expect(res.status).toBe(201); + expect(userByEmail("newuser@example.org")?.orcid).toBe(OTHER_ORCID); + }); +}); + +// -------------------------------------------------------------------------- +// ORCID finalize +// -------------------------------------------------------------------------- + +async function finalize(orcid: string, body: Record = {}): Promise { + const pending = await signPending( + { orcid, name: "Ada Lovelace", exp: Date.now() + 60_000 }, + ENCRYPTION_KEY, + ); + return app.request( + "/auth/orcid/finalize", + { + method: "POST", + headers: { + Origin: ORIGIN, + Cookie: `${PENDING_COOKIE_NAME}=${pending}`, + "content-type": "application/json", + }, + body: JSON.stringify({ + email: "brandnew@example.org", + city: "San Diego", + country: "USA", + ...body, + }), + }, + env(), + ); +} + +describe("ORCID finalize refuses a duplicate identity", () => { + test("an iD held only through users.orcid is refused as orcid_in_use", async () => { + // THE production bug: row 42 holds the iD with no identity row, so the + // identity-table check finalize used to do said "unclaimed" and made a + // second account for the same person. + seedUser("holder@example.org", { orcid: HELD_ORCID }); + const res = await finalize(HELD_ORCID); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string; message: string }; + expect(body.error).toBe("orcid_in_use"); + expect(body.code).toBe("orcid_in_use"); + expect(body.message).toContain("nemar.org/settings"); + expect(db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM users").get()?.n).toBe(2); + }); + + test("an iD held through oauth_identities keeps the pre-existing code", async () => { + // `orcid_already_linked` is what the website already handles; #1254 must + // not rename it out from under it. + seedUser("linked@example.org", { orcid: HELD_ORCID, identityBacked: true }); + const res = await finalize(HELD_ORCID); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string; message: string }; + expect(body.error).toBe("orcid_already_linked"); + expect(body.message).toContain("nemar.org/settings"); + }); + + test("a case-variant of a live address is refused", async () => { + seedUser("Ada@Lab.org"); + const res = await finalize(OTHER_ORCID, { email: "ADA@LAB.ORG" }); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string; message: string }; + expect(body.error).toBe("email_in_use"); + expect(body.message).toContain("nemar.org/settings"); + }); + + test("a clean signup lands, with the address trimmed and lowercased", async () => { + // The iD is already canonical here and cannot be otherwise: `verifyPending` + // rejects a token whose iD is not in canonical form, so finalize's own + // `normalizeOrcid` is a belt-and-braces guarantee about what gets STORED, + // not a repair for user input. The typed-iD path is covered on signup. + // + // The address is the interesting half: `" Brand.New@Example.ORG "` used to + // 400, because the schema ran `.email()` before its own trim. + const res = await finalize(X_ORCID, { email: " Brand.New@Example.ORG " }); + expect(res.status).toBe(200); + const row = userByEmail("brand.new@example.org"); + expect(row).toBeTruthy(); + expect(row?.orcid).toBe(X_ORCID); + // The identity row must carry the SAME canonical spelling, or the two + // halves of "who holds this iD" disagree. + const ident = db + .query<{ provider_subject: string }, [number]>( + "SELECT provider_subject FROM oauth_identities WHERE user_id = ?", + ) + .get(row?.id as number); + expect(ident?.provider_subject).toBe(X_ORCID); + }); +}); + +// -------------------------------------------------------------------------- +// ORCID link / relink, through the callback +// -------------------------------------------------------------------------- + +function callback(cookie: string, mode: "login" | "link" | "relink"): Promise { + return app.request( + `/auth/orcid/callback?code=authcode&state=${CSRF}`, + { + headers: { + Cookie: `nemar_oauth_state=${encodeState({ csrf: CSRF, mode, next: "/settings" })}; ${cookie}`, + }, + }, + env(), + ); +} + +function redirectError(res: Response): string | null { + const location = res.headers.get("Location"); + return location ? new URL(location).searchParams.get("error") : null; +} + +describe("ORCID link refuses an iD another account holds", () => { + test("link is refused when the iD sits on another row's users.orcid", async () => { + // decideLinkOutcome sees no identity row and says `link_new`; the new + // users.orcid check is the only thing standing between that and two rows + // claiming one iD. + seedUser("holder@example.org", { orcid: OTHER_ORCID }); + const mine = seedUser("me@example.org"); + const res = await callback(await sessionCookie(mine), "link"); + expect(res.status).toBe(302); + expect(redirectError(res)).toBe("orcid_in_use"); + expect(userByEmail("me@example.org")?.orcid).toBeNull(); + }); + + test("link is refused with the pre-existing code when another row is identity-backed", async () => { + seedUser("holder@example.org", { orcid: OTHER_ORCID, identityBacked: true }); + const mine = seedUser("me@example.org"); + const res = await callback(await sessionCookie(mine), "link"); + expect(redirectError(res)).toBe("orcid_linked_other"); + }); + + test("relink is refused by the same check, not waved through by the mode", async () => { + // #913 lets an explicit relink swap an account's own iD. It must not + // become a way to take an iD off another account. + seedUser("holder@example.org", { orcid: OTHER_ORCID }); + const mine = seedUser("me@example.org", { orcid: HELD_ORCID, identityBacked: true }); + const res = await callback(await sessionCookie(mine), "relink"); + expect(redirectError(res)).toBe("orcid_in_use"); + expect(userByEmail("me@example.org")?.orcid).toBe(HELD_ORCID); + }); + + test("linking an unclaimed iD still works", async () => { + // The guard against a check that refuses everything. + const mine = seedUser("me@example.org"); + const res = await callback(await sessionCookie(mine), "link"); + expect(redirectError(res)).toBeNull(); + expect(userByEmail("me@example.org")?.orcid).toBe(OTHER_ORCID); + }); + + test("re-linking the iD this account already holds is not a conflict", async () => { + const mine = seedUser("me@example.org", { orcid: OTHER_ORCID, identityBacked: true }); + const res = await callback(await sessionCookie(mine), "login"); + expect(redirectError(res)).toBeNull(); + expect(userByEmail("me@example.org")?.orcid).toBe(OTHER_ORCID); + }); +}); + +// -------------------------------------------------------------------------- +// Unlink +// -------------------------------------------------------------------------- + +describe("ORCID unlink releases the iD", () => { + test("unlink clears users.orcid as well as orcid_verified", async () => { + // Keeping users.orcid is what produced row 42: an account claiming an iD + // it can no longer sign in with, invisible to the identity-table check. + const id = seedUser("me@example.org", { orcid: HELD_ORCID, identityBacked: true }); + const res = await app.request( + "/auth/orcid/unlink", + { method: "POST", headers: { Origin: ORIGIN, Cookie: await sessionCookie(id) } }, + env(), + ); + expect(res.status).toBe(200); + const row = userByEmail("me@example.org"); + expect(row?.orcid).toBeNull(); + expect(row?.orcid_verified).toBe(0); + expect( + db + .query<{ n: number }, [number]>( + "SELECT COUNT(*) AS n FROM oauth_identities WHERE user_id = ?", + ) + .get(id)?.n, + ).toBe(0); + }); + + test("the released iD is then free for another account to claim", async () => { + // The point of clearing it, stated as behaviour rather than as a column + // value: before #1254 this signup would have been refused forever by an + // account that no longer had the link. + const id = seedUser("me@example.org", { orcid: HELD_ORCID, identityBacked: true }); + await app.request( + "/auth/orcid/unlink", + { method: "POST", headers: { Origin: ORIGIN, Cookie: await sessionCookie(id) } }, + env(), + ); + const res = await signup({ orcid: HELD_ORCID }); + expect(res.status).toBe(201); + }); +}); + +// -------------------------------------------------------------------------- +// Email change +// -------------------------------------------------------------------------- + +describe("email change refuses a target another account holds", () => { + test("request refuses a case-variant of a live address", async () => { + seedUser("Taken@Nemar.test"); + const id = seedUser("me@nemar.test"); + const res = await app.request( + "/auth/email/change/request", + { + method: "POST", + headers: { + Origin: ORIGIN, + Cookie: await sessionCookie(id), + "content-type": "application/json", + }, + body: JSON.stringify({ email: "TAKEN@nemar.TEST" }), + }, + env(), + ); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string; message: string }; + expect(body.error).toBe("email_in_use"); + expect(body.message).toContain("nemar.org/settings"); + }); + + test("verify refuses a case-variant claimed between request and verify", async () => { + // The whole flow, driven for real: ask for a code against a free address, + // let someone else claim a case-variant of it, then redeem the code. The + // re-check at verify is the only thing between that and a write the + // partial unique index would reject. + const id = seedUser("me@nemar.test"); + const cookie = await sessionCookie(id); + const requested = await app.request( + "/auth/email/change/request", + { + method: "POST", + headers: { Origin: ORIGIN, Cookie: cookie, "content-type": "application/json" }, + body: JSON.stringify({ email: "moved@nemar.test" }), + }, + env(), + ); + expect(requested.status).toBe(200); + const code = ((await requested.json()) as { dev_code?: string }).dev_code; + expect(code).toMatch(/^\d{6}$/); + + seedUser("Moved@Nemar.Test"); + + const verified = await app.request( + "/auth/email/change/verify", + { + method: "POST", + headers: { Origin: ORIGIN, Cookie: cookie, "content-type": "application/json" }, + body: JSON.stringify({ email: "moved@nemar.test", code }), + }, + env(), + ); + expect(verified.status).toBe(409); + expect(((await verified.json()) as { code: string }).code).toBe("email_in_use"); + expect(userByEmail("me@nemar.test")).toBeTruthy(); + }); + + test("an uncontested change still lands, lowercased", async () => { + const id = seedUser("me@nemar.test"); + const cookie = await sessionCookie(id); + const requested = await app.request( + "/auth/email/change/request", + { + method: "POST", + headers: { Origin: ORIGIN, Cookie: cookie, "content-type": "application/json" }, + body: JSON.stringify({ email: "Moved@Nemar.Test" }), + }, + env(), + ); + const code = ((await requested.json()) as { dev_code?: string }).dev_code as string; + const verified = await app.request( + "/auth/email/change/verify", + { + method: "POST", + headers: { Origin: ORIGIN, Cookie: cookie, "content-type": "application/json" }, + body: JSON.stringify({ email: "MOVED@nemar.test", code }), + }, + env(), + ); + expect(verified.status).toBe(200); + expect(userByEmail("moved@nemar.test")?.id).toBe(id); + }); +}); From 777fc884054e85f5e56e045150959091b711f248 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 17:53:38 -0700 Subject: [PATCH 04/18] feat: admin duplicate report and flag clearing --- backend/src/routes/admin/index.ts | 9 + backend/src/routes/admin/user-duplicates.ts | 278 +++++++++++++++ backend/src/routes/admin/users.ts | 15 +- backend/test/user-duplicates-route.test.ts | 361 ++++++++++++++++++++ src/commands/admin.ts | 141 ++++++++ src/lib/api/admin.ts | 34 ++ test/admin-duplicates-cli.test.ts | 250 ++++++++++++++ test/admin-route-inventory.unit.test.ts | 7 + test/api-export-surface.unit.test.ts | 4 + 9 files changed, 1097 insertions(+), 2 deletions(-) create mode 100644 backend/src/routes/admin/user-duplicates.ts create mode 100644 backend/test/user-duplicates-route.test.ts create mode 100644 test/admin-duplicates-cli.test.ts diff --git a/backend/src/routes/admin/index.ts b/backend/src/routes/admin/index.ts index c1cdff89b..98f502492 100644 --- a/backend/src/routes/admin/index.ts +++ b/backend/src/routes/admin/index.ts @@ -17,6 +17,7 @@ import { registerImportRoutes } from "./imports"; import { registerNoticeRoutes } from "./notices"; import { registerPublishRoutes } from "./publish"; import type { AdminRouter } from "./shared"; +import { registerUserDuplicateRoutes } from "./user-duplicates"; import { registerUserNameRoutes } from "./user-names"; import { registerUserUsernameRoutes } from "./user-usernames"; import { registerUsersRoutes } from "./users"; @@ -30,6 +31,14 @@ export const adminRoutes: AdminRouter = new Hono(); adminRoutes.use("*", authMiddleware); adminRoutes.use("*", adminMiddleware); +// BEFORE registerUsersRoutes, and it has to be. Hono runs every handler whose +// pattern matches, in REGISTRATION order, and `GET /users/:username` matches +// `/users/duplicates` too -- registered second, the duplicate report would +// never be reached, because the username lookup 404s first. (`POST +// /users/backfill-names` has no such neighbour, which is why it can sit after.) +// The cost is that an account literally named `duplicates` is unreachable +// through `GET /admin/users/:username`; the same trade backfill-names makes. +registerUserDuplicateRoutes(adminRoutes); registerUsersRoutes(adminRoutes); registerUserNameRoutes(adminRoutes); registerUserUsernameRoutes(adminRoutes); diff --git a/backend/src/routes/admin/user-duplicates.ts b/backend/src/routes/admin/user-duplicates.ts new file mode 100644 index 000000000..5b74e4992 --- /dev/null +++ b/backend/src/routes/admin/user-duplicates.ts @@ -0,0 +1,278 @@ +/** + * Admin routes: the duplicate-account report and the flag it reports on + * (#1254, epic #1250; ADR 0043). + * + * GET /admin/users/duplicates + * POST /admin/users/:id/clear-identity-conflict + * + * Migration 0077 made an ORCID iD and an email address unique across live + * accounts, and absorbed the catalog's existing duplicates into a per-row + * `identity_conflict` flag rather than failing the deploy or deleting anybody. + * That flag is deliberately invisible to a user, so these two endpoints are the + * whole operator interface for it: one to SEE the groups, one to clear a flag + * after a collision is actually resolved. + * + * NEITHER ROUTE MERGES OR DELETES ANYTHING. Merging two accounts means moving + * datasets, DOIs, S3 credentials and a GitHub collaborator set between owners, + * and getting it wrong is not recoverable; it stays a deliberate manual + * operation. The self-service fix comes first anyway: the person signs in to + * the surviving account and changes its email, its GitHub username, or its + * ORCID link in Settings. + * + * Lives in its own domain file rather than in users.ts for the same reason + * user-names.ts does: one concern, and it keeps out of the way of the account + * listing work happening in that file. + * + * SAFETY (the shared dev/prod users table, AGENTS.md): the report is read-only, + * the clear route writes one integer on one row plus an audit entry, and + * neither emails anyone, touches GitHub, S3, or a DOI. + */ + +import type { + DuplicateAccount, + DuplicateGroup, + IdentityKind, +} from "../../../../shared/contract/identity.js"; +import { auditLogStatement } from "../../db/audit-log"; +import { identityRefusal } from "../../services/identity"; +import type { AdminRouter } from "./shared"; + +/** + * Every live account plus the two facts the grouping needs that are not + * columns on `users`: whether the row holds the `oauth_identities` row for its + * own iD (which decides who is canonical in an ORCID group, per 0077) and how + * many non-sandbox datasets it owns (which is what an operator actually weighs + * when deciding which of two accounts survives). + * + * Unpaginated on purpose: `users` is ~600 rows, the grouping is O(n) in the + * application, and a paged report could split a duplicate group across two + * pages -- which is the one thing this report must never do. + */ +export const DUPLICATE_CANDIDATES_SQL = `SELECT + u.id, + u.username, + u.email, + u.github_username, + u.orcid, + u.created_at, + u.identity_conflict, + CASE WHEN EXISTS ( + SELECT 1 FROM oauth_identities oi + WHERE oi.user_id = u.id + AND oi.provider = 'orcid' + AND oi.provider_subject = u.orcid + ) THEN 1 ELSE 0 END AS has_oauth_identity, + (SELECT COUNT(*) FROM datasets d + WHERE d.owner_user_id = u.id AND d.is_sandbox = 0) AS dataset_count + FROM users u + WHERE u.deleted_at IS NULL + ORDER BY u.id`; + +interface CandidateRow { + id: number; + username: string | null; + email: string; + github_username: string | null; + orcid: string | null; + created_at: string; + identity_conflict: number; + has_oauth_identity: number; + dataset_count: number; +} + +/** + * The grouping key for each identifier, or `null` when the row does not carry + * that identifier at all. + * + * Email and GitHub fold case because their uniqueness rules do + * (`idx_users_email_live_unique` is `COLLATE NOCASE`, and so is + * `idx_users_github` from 0012). ORCID does NOT fold case, because + * `idx_users_orcid_live_unique` compares exactly -- migration 0077 + * canonicalises the stored check digit and every write path normalises it, so + * an exact comparison is complete, and folding here would report a group the + * index does not consider a collision. + */ +function keyFor(row: CandidateRow, kind: IdentityKind): string | null { + if (kind === "orcid") { + const v = row.orcid?.trim(); + return v ? v : null; + } + if (kind === "email") { + const v = row.email?.trim(); + return v ? v.toLowerCase() : null; + } + const v = row.github_username?.trim(); + return v ? v.toLowerCase() : null; +} + +/** + * Which row keeps the identifier, mirroring migration 0077's rules exactly. + * + * ORCID: the row holding the `oauth_identities` row, and the lowest id only + * when none does. That ordering is the whole point in the production case -- + * id 43 can sign in with the iD and carries the ORCID name, id 42 is the + * orphan the identity row left behind, and picking the lowest id would have + * crowned the orphan. + * + * Email and GitHub: the lowest id. There is no identity row to prefer and no + * other principled tiebreak; the oldest account is the likelier live one. + * + * If this and the migration ever disagree, the report tells an operator to + * resolve the wrong row -- so the correspondence is asserted directly by the + * route test against a fixture the migration itself flagged. + */ +function canonicalIdOf(rows: CandidateRow[], kind: IdentityKind): number { + if (kind === "orcid") { + const backed = rows.find((r) => r.has_oauth_identity === 1); + if (backed) return backed.id; + } + return rows.reduce((min, r) => (r.id < min ? r.id : min), rows[0].id); +} + +function toAccount(row: CandidateRow, canonicalId: number): DuplicateAccount { + return { + id: row.id, + username: row.username, + email: row.email, + created_at: row.created_at, + has_oauth_identity: row.has_oauth_identity === 1, + dataset_count: row.dataset_count, + identity_conflict: row.identity_conflict, + canonical: row.id === canonicalId, + }; +} + +/** Build every duplicate group across all three identifiers. Exported so the + * clear route below and the tests both read the same grouping. */ +export function buildDuplicateGroups(rows: CandidateRow[]): DuplicateGroup[] { + const groups: DuplicateGroup[] = []; + for (const kind of ["orcid", "email", "github"] as const) { + const byKey = new Map(); + for (const row of rows) { + const key = keyFor(row, kind); + if (key === null) continue; + const bucket = byKey.get(key); + if (bucket) bucket.push(row); + else byKey.set(key, [row]); + } + for (const [value, bucket] of byKey) { + if (bucket.length < 2) continue; + const canonicalId = canonicalIdOf(bucket, kind); + groups.push({ + kind, + value, + canonical_user_id: canonicalId, + accounts: bucket.map((r) => toAccount(r, canonicalId)), + }); + } + } + // Stable order so a diff between two runs is meaningful: by identifier + // (orcid, email, github as listed above), then by the shared value. + return groups; +} + +export function registerUserDuplicateRoutes(admin: AdminRouter): void { + /** + * GET /admin/users/duplicates - live accounts sharing an identifier. + * + * Static segment, so Hono's RegExpRouter matches this before + * `GET /users/:username` (static outranks param) -- the same property + * `POST /users/backfill-names` already relies on. + */ + admin.get("/users/duplicates", async (c) => { + try { + const { results } = await c.env.DB.prepare(DUPLICATE_CANDIDATES_SQL).all(); + const rows = results ?? []; + const groups = buildDuplicateGroups(rows); + return c.json({ + groups, + group_count: groups.length, + // Counted over ALL live rows, not over `groups`: a flag that outlived + // its collision (someone fixed the other account) shows up here as a + // number larger than the groups explain, which is precisely the signal + // that a flag is ready to be cleared. + flagged_count: rows.filter((r) => r.identity_conflict === 1).length, + }); + } catch (err) { + console.error("[admin/user-duplicates] report failed", err); + return c.json({ error: "Failed to build duplicate report" }, 500); + } + }); + + /** + * POST /admin/users/:id/clear-identity-conflict - un-flag a resolved row. + * + * Refuses (409) while the collision that earned the flag is still there, + * naming the rows that still collide. That refusal is not politeness: the + * partial unique indexes would reject the row the moment it became visible + * to them, so clearing early would either fail loudly on the UPDATE or -- + * worse, if the collision were on a column the row does not currently write + * -- leave the catalog one write away from a constraint error nobody + * expected. + * + * Id-keyed, not username-keyed: a web/ORCID row has `username = NULL` + * (#1012), and the duplicate rows are disproportionately those. + */ + admin.post("/users/:id/clear-identity-conflict", async (c) => { + const id = Number.parseInt(c.req.param("id"), 10); + if (!Number.isInteger(id) || id <= 0) { + return c.json({ error: "Invalid user id" }, 400); + } + + try { + const { results } = await c.env.DB.prepare(DUPLICATE_CANDIDATES_SQL).all(); + const rows = results ?? []; + const target = rows.find((r) => r.id === id); + if (!target) { + // Live rows only, so a tombstoned id lands here too -- correctly: a + // tombstone is already invisible to both partial indexes and has no + // flag worth clearing. + return c.json({ error: "User not found" }, 404); + } + + // Every group this row is in, minus the ones where it is alone. + const colliding = buildDuplicateGroups(rows) + .filter((g) => g.accounts.some((a) => a.id === id)) + .map((g) => ({ + kind: g.kind, + value: g.value, + user_ids: g.accounts.filter((a) => a.id !== id).map((a) => a.id), + })); + + if (colliding.length > 0) { + return c.json( + { + error: "identity_conflict_remains", + ...identityRefusal("identity_conflict_remains"), + colliding, + }, + 409, + ); + } + + if (target.identity_conflict === 0) { + // Already clear. A no-op, not an error: an operator who resolved the + // collision by deleting the OTHER row may find this one was never + // flagged in the first place, and a 409 there would read as a failure. + return c.json({ ok: true, id, cleared: false }); + } + + await c.env.DB.batch([ + c.env.DB.prepare( + "UPDATE users SET identity_conflict = 0, updated_at = datetime('now') WHERE id = ?", + ).bind(id), + auditLogStatement(c.env.DB, { + userId: c.get("user")?.id ?? null, + action: "identity_conflict_cleared", + resourceType: "user", + resourceId: String(id), + details: JSON.stringify({ email: target.email, orcid: target.orcid }), + }), + ]); + return c.json({ ok: true, id, cleared: true }); + } catch (err) { + console.error("[admin/user-duplicates] clear failed", err); + return c.json({ error: "Failed to clear identity conflict" }, 500); + } + }); +} diff --git a/backend/src/routes/admin/users.ts b/backend/src/routes/admin/users.ts index 23a5e9a9c..d5060985c 100644 --- a/backend/src/routes/admin/users.ts +++ b/backend/src/routes/admin/users.ts @@ -37,6 +37,7 @@ import { isNonProductionEnv } from "../../services/environment"; import { removeCollaborator } from "../../services/github"; import { getDatasetsToken } from "../../services/github-auth"; import { revokeUserIamAccess } from "../../services/iam"; +import { normalizeGithubHandle, normalizeOrcid } from "../../services/identity"; import { errorMessage } from "../../services/repo-metadata"; import { type Bindings, type Variables, isDemotion, parseRole } from "../../types/bindings"; import type { AdminRouter } from "./shared"; @@ -1535,9 +1536,19 @@ export function registerUsersRoutes(admin: AdminRouter): void { // Shared iD validator, not a bare length check: `max(19)` accepted // any 19-character string as an ORCID, which is how a fixture row // could carry an iD the rest of the system would never accept. - orcid: orcidIdSchema.optional(), + // Canonicalised the same way every real write is (ADR 0043), so a + // fixture cannot seed a row shape the production paths can no longer + // produce -- a lowercase `x` check digit or an `@handle`. + orcid: z + .preprocess((v) => (typeof v === "string" ? (normalizeOrcid(v) ?? v) : v), orcidIdSchema) + .optional(), orcid_verified: z.boolean().optional(), - github_username: z.string().max(39).optional(), + github_username: z + .preprocess( + (v) => (typeof v === "string" ? normalizeGithubHandle(v) : v), + z.string().max(39), + ) + .optional(), city: z.string().max(200).optional(), country: z.string().max(200).optional(), affiliation: z.string().max(300).optional(), diff --git a/backend/test/user-duplicates-route.test.ts b/backend/test/user-duplicates-route.test.ts new file mode 100644 index 000000000..71bd59ddd --- /dev/null +++ b/backend/test/user-duplicates-route.test.ts @@ -0,0 +1,361 @@ +/** + * Real route tests for the duplicate-account report and the identity-conflict + * flag it reports on (#1254, epic #1250; ADR 0043). + * + * GET /admin/users/duplicates + * POST /admin/users/:id/clear-identity-conflict + * + * Real engine, no mocks: bun:sqlite behind realD1 with every migration applied + * (INCLUDING 0077, so the rows the report describes are flagged by the real + * migration rather than by the test), real Hono dispatch through the real + * admin router, and the production auth + admin middleware reached with a real + * hashed API token. + * + * THE CORRESPONDENCE IS THE POINT. The report's canonical rule and the + * migration's canonical rule have to agree, or the report tells an operator to + * resolve the wrong row. So the fixture is seeded BEFORE 0077 runs and the + * assertion compares the report's `canonical` marks against the flags 0077 + * actually wrote -- rather than against a second hand-written copy of the rule, + * which would pass however both drifted. + */ + +import { Database } from "bun:sqlite"; +import { beforeEach, describe, expect, test } from "bun:test"; +import { readFileSync, readdirSync } from "node:fs"; +import { join } from "node:path"; +import { Hono } from "hono"; +import { adminRoutes } from "../src/routes/admin"; +import { hashApiKey } from "../src/services/token"; +import type { Bindings, Variables } from "../src/types/bindings"; +import { realD1 } from "./helpers/d1"; + +const MIGRATIONS_DIR = join(import.meta.dir, "../src/db/migrations"); +const TARGET = "0077_identity_uniqueness.sql"; +const ADMIN_KEY = "nemar_test_admin_key_for_duplicates"; + +const SHARED_ORCID = "0000-0002-1974-1293"; +const ADMIN_ORCID = "0000-0003-7777-8888"; + +let db: Database; +let app: Hono<{ Bindings: Bindings; Variables: Variables }>; + +function env(): Bindings { + return { DB: realD1(db), ENVIRONMENT: "test" } as unknown as Bindings; +} + +function migrationsBefore(target: string): string[] { + return readdirSync(MIGRATIONS_DIR) + .filter((f) => f.endsWith(".sql") && f < target) + .sort(); +} + +/** Every migration except 0077, so the fixture can be seeded pre-flag. */ +function dbBeforeTarget(): Database { + const fresh = new Database(":memory:"); + for (const file of migrationsBefore(TARGET)) { + fresh.exec(readFileSync(join(MIGRATIONS_DIR, file), "utf-8")); + } + return fresh; +} + +interface SeedOpts { + orcid?: string | null; + identityBacked?: boolean; + github?: string | null; + username?: string | null; + deleted?: boolean; + role?: string; +} + +function seedUser(email: string, opts: SeedOpts = {}): number { + db.run( + `INSERT INTO users (username, email, status, signup_source, email_verified, + orcid, orcid_verified, github_username, role, deleted_at) + VALUES (?, ?, 'verified', 'web', 1, ?, ?, ?, ?, ?)`, + [ + opts.username ?? null, + email, + opts.orcid ?? null, + opts.orcid ? 1 : 0, + opts.github ?? null, + opts.role ?? "member", + opts.deleted ? "2026-01-01 00:00:00" : null, + ], + ); + const row = db.query<{ id: number }, [string]>("SELECT id FROM users WHERE email = ?").get(email); + if (!row) throw new Error(`seed failed for ${email}`); + if (opts.identityBacked && opts.orcid) { + db.run( + "INSERT INTO oauth_identities (user_id, provider, provider_subject) VALUES (?, 'orcid', ?)", + [row.id, opts.orcid], + ); + } + return row.id; +} + +async function seedAdminToken(userId: number): Promise { + db.run( + "INSERT INTO tokens (user_id, api_key_hash, api_key_prefix) VALUES (?, ?, 'nemar_te')", + [userId, await hashApiKey(ADMIN_KEY)], + ); +} + +function get(path: string): Promise { + return app.request(path, { headers: { Authorization: `Bearer ${ADMIN_KEY}` } }, env()); +} + +function post(path: string): Promise { + return app.request( + path, + { method: "POST", headers: { Authorization: `Bearer ${ADMIN_KEY}` } }, + env(), + ); +} + +function flagOf(id: number): number { + return ( + db + .query<{ identity_conflict: number }, [number]>( + "SELECT identity_conflict FROM users WHERE id = ?", + ) + .get(id)?.identity_conflict ?? -1 + ); +} + +interface ReportBody { + groups: { + kind: string; + value: string; + canonical_user_id: number; + accounts: { + id: number; + username: string | null; + email: string; + created_at: string; + has_oauth_identity: boolean; + dataset_count: number; + identity_conflict: number; + canonical: boolean; + }[]; + }[]; + group_count: number; + flagged_count: number; +} + +/** Ids the fixture assigns, in seed order. */ +let orphanId: number; +let holderId: number; +let adaLowId: number; +let adaHighId: number; +let adminId: number; + +beforeEach(async () => { + db = dbBeforeTarget(); + // The production 42/43 shape: the ORPHAN gets the lower id, the + // identity-backed row the higher one, so "lowest id" and "identity row" + // disagree and the report has to pick the same winner 0077 does. + orphanId = seedUser("robert.oostenveld@donders.ru.nl", { orcid: SHARED_ORCID }); + holderId = seedUser("r.oostenveld@donders.ru.nl", { + orcid: SHARED_ORCID, + identityBacked: true, + username: null, + }); + adaLowId = seedUser("Ada@Lab.org", { username: "ada" }); + adaHighId = seedUser("ada@lab.org"); + adminId = seedUser("root@nemar.org", { + username: "root", + role: "admin", + orcid: ADMIN_ORCID, + identityBacked: true, + }); + // A dataset on the surviving ORCID row, so `dataset_count` is not uniformly + // zero and a transposed count would be visible. + db.run( + `INSERT INTO datasets (dataset_id, name, owner_user_id, is_sandbox) + VALUES ('nm099001', 'A dataset', ?, 0)`, + [holderId], + ); + db.exec(readFileSync(join(MIGRATIONS_DIR, TARGET), "utf-8")); + await seedAdminToken(adminId); + + app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); + app.route("/admin", adminRoutes); +}); + +describe("GET /admin/users/duplicates", () => { + test("groups both duplicate identifiers and leaves the clean row out", async () => { + const res = await get("/admin/users/duplicates"); + expect(res.status).toBe(200); + const body = (await res.json()) as ReportBody; + expect(body.group_count).toBe(2); + const kinds = body.groups.map((g) => g.kind).sort(); + expect(kinds).toEqual(["email", "orcid"]); + // The admin row shares nothing, so it must not appear in any group. + const listed = new Set(body.groups.flatMap((g) => g.accounts.map((a) => a.id))); + expect(listed.has(adminId)).toBe(false); + }); + + test("the canonical mark matches the row migration 0077 left unflagged", async () => { + // Correspondence asserted against the migration's OWN output, not against + // a second copy of the rule: if the two ever disagree, this fails. + const res = await get("/admin/users/duplicates"); + const body = (await res.json()) as ReportBody; + for (const group of body.groups) { + for (const account of group.accounts) { + expect(account.canonical).toBe(flagOf(account.id) === 0); + } + expect(group.canonical_user_id).toBe( + group.accounts.find((a) => a.canonical)?.id as number, + ); + } + // Named explicitly too, so a report that marked NOTHING canonical (and so + // trivially matched a catalog where every row was flagged) still fails. + const orcidGroup = body.groups.find((g) => g.kind === "orcid"); + expect(orcidGroup?.canonical_user_id).toBe(holderId); + const emailGroup = body.groups.find((g) => g.kind === "email"); + expect(emailGroup?.canonical_user_id).toBe(adaLowId); + }); + + test("each row carries the facts an operator decides on", async () => { + const res = await get("/admin/users/duplicates"); + const body = (await res.json()) as ReportBody; + const orcidGroup = body.groups.find((g) => g.kind === "orcid"); + const survivor = orcidGroup?.accounts.find((a) => a.id === holderId); + const orphan = orcidGroup?.accounts.find((a) => a.id === orphanId); + expect(survivor?.has_oauth_identity).toBe(true); + expect(orphan?.has_oauth_identity).toBe(false); + expect(survivor?.dataset_count).toBe(1); + expect(orphan?.dataset_count).toBe(0); + expect(orphan?.identity_conflict).toBe(1); + expect(typeof survivor?.created_at).toBe("string"); + }); + + test("flagged_count counts live flagged rows", async () => { + const res = await get("/admin/users/duplicates"); + const body = (await res.json()) as ReportBody; + // The ORCID orphan and the higher-id Ada. + expect(body.flagged_count).toBe(2); + }); + + test("the email group is keyed case-insensitively on the normalised value", async () => { + const res = await get("/admin/users/duplicates"); + const body = (await res.json()) as ReportBody; + const emailGroup = body.groups.find((g) => g.kind === "email"); + expect(emailGroup?.value).toBe("ada@lab.org"); + expect(emailGroup?.accounts.map((a) => a.id).sort()).toEqual([adaLowId, adaHighId].sort()); + }); + + test("a tombstoned row is not a duplicate", async () => { + seedUser("deleted+90@deleted.invalid", { orcid: SHARED_ORCID, deleted: true }); + const res = await get("/admin/users/duplicates"); + const body = (await res.json()) as ReportBody; + const orcidGroup = body.groups.find((g) => g.kind === "orcid"); + expect(orcidGroup?.accounts).toHaveLength(2); + }); + + test("a GitHub duplicate is reported too, case-insensitively", async () => { + // 0012's table-wide unique NOCASE index means this should be impossible in + // practice; the grouping exists so the report describes all three + // identifiers rather than two. Seeded past the index to prove the grouping + // is real code rather than a dead branch. + db.exec("DROP INDEX idx_users_github"); + seedUser("gh-a@example.org", { github: "Octocat" }); + seedUser("gh-b@example.org", { github: "octocat" }); + const res = await get("/admin/users/duplicates"); + const body = (await res.json()) as ReportBody; + const githubGroup = body.groups.find((g) => g.kind === "github"); + expect(githubGroup?.value).toBe("octocat"); + expect(githubGroup?.accounts).toHaveLength(2); + }); + + test("a clean catalog reports nothing", async () => { + db.run("DELETE FROM users WHERE id IN (?, ?)", [orphanId, adaHighId]); + const res = await get("/admin/users/duplicates"); + const body = (await res.json()) as ReportBody; + expect(body.groups).toEqual([]); + expect(body.group_count).toBe(0); + expect(body.flagged_count).toBe(0); + }); + + test("a non-admin cannot read it", async () => { + const memberId = seedUser("member@example.org", { username: "member" }); + db.run("UPDATE tokens SET user_id = ? WHERE user_id = ?", [memberId, adminId]); + const res = await get("/admin/users/duplicates"); + expect(res.status).toBe(403); + }); +}); + +describe("POST /admin/users/:id/clear-identity-conflict", () => { + test("refuses while the collision remains, naming the colliding rows", async () => { + const res = await post(`/admin/users/${orphanId}/clear-identity-conflict`); + expect(res.status).toBe(409); + const body = (await res.json()) as { + error: string; + code: string; + message: string; + colliding: { kind: string; value: string; user_ids: number[] }[]; + }; + expect(body.error).toBe("identity_conflict_remains"); + expect(body.code).toBe("identity_conflict_remains"); + expect(body.message).toContain("Resolve it on the other account"); + expect(body.colliding).toHaveLength(1); + expect(body.colliding[0].kind).toBe("orcid"); + expect(body.colliding[0].user_ids).toEqual([holderId]); + // And nothing was written. + expect(flagOf(orphanId)).toBe(1); + }); + + test("succeeds once the other row is soft-deleted", async () => { + db.run("UPDATE users SET deleted_at = datetime('now'), orcid = NULL WHERE id = ?", [holderId]); + const res = await post(`/admin/users/${orphanId}/clear-identity-conflict`); + expect(res.status).toBe(200); + const body = (await res.json()) as { ok: boolean; id: number; cleared: boolean }; + expect(body).toEqual({ ok: true, id: orphanId, cleared: true }); + expect(flagOf(orphanId)).toBe(0); + }); + + test("the cleared row then holds the identifier for real", async () => { + // The flag is not cosmetic: clearing it puts the row back INTO the partial + // unique index, so a third claim on the iD is refused from then on. + db.run("UPDATE users SET deleted_at = datetime('now'), orcid = NULL WHERE id = ?", [holderId]); + await post(`/admin/users/${orphanId}/clear-identity-conflict`); + expect(() => + db.run( + "INSERT INTO users (email, status, signup_source, orcid) VALUES ('third@example.org', 'verified', 'web', ?)", + [SHARED_ORCID], + ), + ).toThrow(/UNIQUE constraint failed: users\.orcid/); + }); + + test("writes an audit row", async () => { + db.run("UPDATE users SET deleted_at = datetime('now'), orcid = NULL WHERE id = ?", [holderId]); + await post(`/admin/users/${orphanId}/clear-identity-conflict`); + const row = db + .query<{ action: string; resource_id: string; user_id: number }, []>( + "SELECT action, resource_id, user_id FROM audit_log WHERE action = 'identity_conflict_cleared'", + ) + .get(); + expect(row?.resource_id).toBe(String(orphanId)); + expect(row?.user_id).toBe(adminId); + }); + + test("an already-clear row is a no-op, not an error", async () => { + const res = await post(`/admin/users/${adminId}/clear-identity-conflict`); + expect(res.status).toBe(200); + expect((await res.json()) as { cleared: boolean }).toEqual({ + ok: true, + id: adminId, + cleared: false, + }); + }); + + test("an unknown id is a 404", async () => { + const res = await post("/admin/users/999999/clear-identity-conflict"); + expect(res.status).toBe(404); + }); + + test("a non-numeric id is a 400", async () => { + const res = await post("/admin/users/not-a-number/clear-identity-conflict"); + expect(res.status).toBe(400); + }); +}); diff --git a/src/commands/admin.ts b/src/commands/admin.ts index 7016fad69..cfd86fed5 100644 --- a/src/commands/admin.ts +++ b/src/commands/admin.ts @@ -28,6 +28,7 @@ import { Command } from "commander"; import inquirer from "inquirer"; import ora, { type Ora } from "ora"; import { formatBytesCli } from "../../shared/bytes.js"; +import type { DuplicateGroup } from "../../shared/contract/index.js"; import { type AvailabilityReport, type AvailabilityReportResult, @@ -61,6 +62,7 @@ import { bulkDeleteDatasets, changeUserRole, changeVisibility, + clearIdentityConflict, createConceptDoi, createExemplar, dataIntegritySweep, @@ -78,6 +80,7 @@ import { getFleetDrift, getImportStatus, getSummaryCoverage, + getUserDuplicates, hedSweep, hedSweepReset, listUsers, @@ -6745,3 +6748,141 @@ backfillUsernamesCommand }); adminCommand.addCommand(backfillUsernamesCommand); + +// ============================================================================ +// Duplicate accounts (#1254, epic #1250; ADR 0043) +// ============================================================================ + +/** + * `nemar admin duplicates` — live accounts sharing an ORCID iD, an email + * address, or a GitHub handle, plus the `--clear` half that un-flags a row + * once its collision is gone. + * + * A TOP-LEVEL COMMAND rather than a flag on `nemar admin users`: the listing + * is one row per account and this is one row per GROUP, so folding it in would + * mean two incompatible table shapes behind one command. It also reports on + * something the listing has no column for -- `identity_conflict`, the flag + * migration 0077 uses to keep a duplicate out of the unique indexes without + * deleting anybody. + * + * This command NEVER merges or deletes. It says which rows collide and which + * one holds the identifier; resolving it is the person's own Settings change, + * or an explicit account deletion by an admin. + */ +const duplicatesCommand = new Command("duplicates").description( + "Report live accounts sharing an ORCID iD, email, or GitHub handle", +); + +/** One line per account inside a group. */ +function printDuplicateAccount(account: DuplicateGroup["accounts"][number]): void { + const who = account.username ?? chalk.dim("(no username)"); + const marks: string[] = []; + if (account.canonical) marks.push(chalk.green("canonical")); + if (account.identity_conflict === 1) marks.push(chalk.yellow("flagged")); + if (account.has_oauth_identity) marks.push(chalk.dim("orcid-login")); + const suffix = marks.length > 0 ? ` [${marks.join(", ")}]` : ""; + const meta = `created ${account.created_at} datasets=${account.dataset_count}`; + console.log( + ` id ${String(account.id).padEnd(5)} ${who} <${account.email}> ${meta}${suffix}`, + ); +} + +duplicatesCommand + .option("--json", "Output raw JSON instead of the human report") + .option( + "--clear ", + "Clear one account's identity-conflict flag (refuses while the collision remains)", + ) + .action(async (options: { json?: boolean; clear?: string }) => { + if (!requireAuth()) return; + + if (options.clear !== undefined) { + const id = Number.parseInt(options.clear, 10); + if (!Number.isInteger(id) || id <= 0) { + console.error(chalk.red(`Invalid user id: ${options.clear}`)); + process.exit(1); + return; + } + const spinner = ora(`Clearing identity-conflict flag on user ${id}...`).start(); + try { + const res = await clearIdentityConflict(id); + spinner.stop(); + if (options.json) { + console.log(JSON.stringify(res, null, 2)); + return; + } + console.log( + res.cleared + ? chalk.green(`Cleared the identity-conflict flag on user ${id}.`) + : chalk.dim(`User ${id} was not flagged; nothing to clear.`), + ); + } catch (err) { + // A 409 here is the normal answer, not a crash: the collision is + // still there. Print the API's own sentence, which names what to fix. + spinner.fail( + err instanceof ApiError && err.statusCode === 409 + ? "Still colliding; flag not cleared" + : "Failed to clear the identity-conflict flag", + ); + console.error(chalk.red(errorDetail(err))); + process.exit(1); + } + return; + } + + const spinner = ora("Checking for duplicate accounts...").start(); + let report: Awaited>; + try { + report = await getUserDuplicates(); + spinner.stop(); + } catch (err) { + spinner.fail("Duplicate report failed"); + console.error(chalk.red(errorDetail(err))); + process.exit(1); + return; + } + + if (options.json) { + console.log(JSON.stringify(report, null, 2)); + return; + } + + console.log(); + if (report.groups.length === 0) { + console.log(chalk.green("No duplicate accounts.")); + // A flag with no group behind it is a resolved collision waiting to be + // cleared, and it is invisible in the groups list by definition — so it + // gets said out loud rather than left to be inferred from a zero. + if (report.flagged_count > 0) { + console.log( + chalk.yellow( + `${report.flagged_count} account(s) still carry an identity-conflict flag with no remaining collision.`, + ), + ); + console.log(chalk.dim(" Clear each with 'nemar admin duplicates --clear '.")); + } + return; + } + + console.log( + chalk.cyan( + `${report.group_count} duplicate group(s); ${report.flagged_count} account(s) flagged`, + ), + ); + for (const group of report.groups) { + console.log(); + console.log(` ${chalk.bold(group.kind)} ${group.value}`); + for (const account of group.accounts) printDuplicateAccount(account); + } + console.log(); + console.log( + chalk.dim( + "The fix is self-service on the surviving account: change its email or GitHub username, or unlink/re-link its ORCID iD, in Settings on nemar.org. Merging two accounts is manual.", + ), + ); + // Exit non-zero so a scheduled run surfaces a duplicate instead of + // scrolling past as a successful command. + process.exitCode = 1; + }); + +adminCommand.addCommand(duplicatesCommand); diff --git a/src/lib/api/admin.ts b/src/lib/api/admin.ts index 8228d43ca..7f0d6f827 100644 --- a/src/lib/api/admin.ts +++ b/src/lib/api/admin.ts @@ -11,7 +11,11 @@ import { type AdminUsersListResponse, type BackfillUsernameOutcome, type BackfillVerifyOutcome, + type ClearIdentityConflictResponse, + type DuplicateReport, adminUsersListResponseSchema, + clearIdentityConflictResponseSchema, + duplicateReportSchema, } from "../../../shared/contract/index.js"; import type { BackfillNameOutcome } from "../../../shared/contract/publication.js"; import { request } from "./client.js"; @@ -1593,3 +1597,33 @@ export async function backfillUsernames(options?: { true, ); } + +// ============================================================================ +// Identity uniqueness (#1254, epic #1250; ADR 0043) +// ============================================================================ + +/** + * Duplicate-account report: live accounts sharing an ORCID iD, an email + * address, or a GitHub handle. + * + * Validated against the shared contract rather than cast, for the reason + * stated at the top of this file: the report drives an operator's decision + * about which of two real accounts survives, so a silent shape drift here is + * worse than a loud parse failure. + */ +export async function getUserDuplicates(): Promise { + return request("/admin/users/duplicates", {}, true, duplicateReportSchema); +} + +/** + * Clear a row's `identity_conflict` flag. 409s while the collision is still + * there, which is the expected answer until someone has actually resolved it. + */ +export async function clearIdentityConflict(id: number): Promise { + return request( + `/admin/users/${id}/clear-identity-conflict`, + { method: "POST" }, + true, + clearIdentityConflictResponseSchema, + ); +} diff --git a/test/admin-duplicates-cli.test.ts b/test/admin-duplicates-cli.test.ts new file mode 100644 index 000000000..23809cfcb --- /dev/null +++ b/test/admin-duplicates-cli.test.ts @@ -0,0 +1,250 @@ +/** + * CLI `nemar admin duplicates` (#1254, epic #1250; ADR 0043), driven through + * the real entry point (`bun run src/index.ts admin duplicates`). + * + * The duplicate report is the whole operator interface for the + * `identity_conflict` flag migration 0077 introduces -- the flag is invisible + * to a user and to every other listing -- so what this command prints, and + * what it exits with, is the behaviour under test. + * + * Harness mirrors test/auth-status-upload-access.test.ts: a real subprocess + * CLI invocation against a real local HTTP server via TEST_API_URL, with an + * isolated NEMAR_CONFIG_DIR and a real on-disk config store. The server serves + * the real wire shapes, and the CLI's real contract validation runs against + * them. No mocks. + */ + +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { spawn } from "bun"; + +const CLI_ENTRY = join(import.meta.dir, "..", "src", "index.ts"); +const REPO_ROOT = join(import.meta.dir, ".."); + +const REPORT_WITH_GROUP = { + groups: [ + { + kind: "orcid", + value: "0000-0002-1974-1293", + canonical_user_id: 43, + accounts: [ + { + id: 42, + username: null, + email: "robert.oostenveld@donders.ru.nl", + created_at: "2026-08-04 18:57:00", + has_oauth_identity: false, + dataset_count: 0, + identity_conflict: 1, + canonical: false, + }, + { + id: 43, + username: null, + email: "r.oostenveld@donders.ru.nl", + created_at: "2026-08-04 19:18:00", + has_oauth_identity: true, + dataset_count: 3, + identity_conflict: 0, + canonical: true, + }, + ], + }, + ], + group_count: 1, + flagged_count: 1, +}; + +const EMPTY_REPORT = { groups: [], group_count: 0, flagged_count: 0 }; + +interface ServerOptions { + report?: unknown; + /** Response for POST /admin/users/:id/clear-identity-conflict. */ + clear?: { status: number; body: unknown }; +} + +function startServer(options: ServerOptions) { + const requests: string[] = []; + const server = Bun.serve({ + port: 0, + fetch(req) { + const url = new URL(req.url); + requests.push(`${req.method} ${url.pathname}`); + if (url.pathname === "/notices") return Response.json({ notices: [] }); + if (url.pathname === "/datasets/facets") return Response.json({}); + if (url.pathname === "/admin/users/duplicates") { + return Response.json(options.report ?? EMPTY_REPORT); + } + if (url.pathname.endsWith("/clear-identity-conflict")) { + const c = options.clear ?? { status: 200, body: { ok: true, id: 42, cleared: true } }; + return Response.json(c.body, { status: c.status }); + } + return Response.json({ error: "not found" }, { status: 404 }); + }, + }); + return { url: `http://localhost:${server.port}`, requests, stop: () => server.stop(true) }; +} + +let configDir: string; + +beforeEach(() => { + configDir = mkdtempSync(join(tmpdir(), "nemar-admin-duplicates-cli-")); + writeFileSync( + join(configDir, "config.json"), + JSON.stringify({ + activeAccount: "root", + accounts: { root: { apiKey: "test-admin-key", username: "root", role: "admin" } }, + }), + ); +}); + +afterEach(() => { + rmSync(configDir, { recursive: true, force: true }); +}); + +async function runCli(args: string[], testApiUrl: string) { + const env = { + ...process.env, + NEMAR_CONFIG_DIR: configDir, + TEST_API_URL: testApiUrl, + NEMAR_NO_UPDATE_CHECK: "1", + NO_COLOR: "1", + }; + env.FORCE_COLOR = undefined; + env.CLICOLOR_FORCE = undefined; + const proc = spawn({ + cmd: ["bun", "run", CLI_ENTRY, ...args], + cwd: REPO_ROOT, + env, + stdin: "ignore", + stdout: "pipe", + stderr: "pipe", + }); + const stdout = await new Response(proc.stdout).text(); + const stderr = await new Response(proc.stderr).text(); + const exitCode = await proc.exited; + return { stdout, stderr, exitCode }; +} + +describe("nemar admin duplicates", () => { + test("prints the group, marks the canonical row, and exits non-zero", async () => { + const server = startServer({ report: REPORT_WITH_GROUP }); + try { + const result = await runCli(["admin", "duplicates"], server.url); + expect(result.stdout).toContain("1 duplicate group(s); 1 account(s) flagged"); + expect(result.stdout).toContain("orcid 0000-0002-1974-1293"); + expect(result.stdout).toContain("id 42"); + expect(result.stdout).toContain("[flagged]"); + expect(result.stdout).toContain("[canonical, orcid-login]"); + expect(result.stdout).toContain("datasets=3"); + // The report is a finding, not a status line: a scheduled run has to be + // able to notice it without parsing stdout. + expect(result.exitCode).toBe(1); + } finally { + server.stop(); + } + }); + + test("a clean catalog says so and exits zero", async () => { + const server = startServer({ report: EMPTY_REPORT }); + try { + const result = await runCli(["admin", "duplicates"], server.url); + expect(result.stdout).toContain("No duplicate accounts."); + expect(result.exitCode).toBe(0); + } finally { + server.stop(); + } + }); + + test("a flag with no group behind it is called out, not left as a zero", async () => { + // Someone resolved the collision on the other account; the flag survives + // and is invisible in the groups list by definition. + const server = startServer({ + report: { groups: [], group_count: 0, flagged_count: 2 }, + }); + try { + const result = await runCli(["admin", "duplicates"], server.url); + expect(result.stdout).toContain("No duplicate accounts."); + expect(result.stdout).toContain("2 account(s) still carry an identity-conflict flag"); + expect(result.stdout).toContain("--clear "); + } finally { + server.stop(); + } + }); + + test("--json emits the raw report and nothing else", async () => { + const server = startServer({ report: REPORT_WITH_GROUP }); + try { + const result = await runCli(["admin", "duplicates", "--json"], server.url); + const parsed = JSON.parse(result.stdout); + expect(parsed.group_count).toBe(1); + expect(parsed.groups[0].canonical_user_id).toBe(43); + expect(result.stdout).not.toContain("duplicate group(s)"); + } finally { + server.stop(); + } + }); + + test("--clear reports success and never fetches the report", async () => { + const server = startServer({ clear: { status: 200, body: { ok: true, id: 42, cleared: true } } }); + try { + const result = await runCli(["admin", "duplicates", "--clear", "42"], server.url); + expect(result.exitCode).toBe(0); + expect(result.stdout).toContain("Cleared the identity-conflict flag on user 42"); + expect(server.requests).toContain("POST /admin/users/42/clear-identity-conflict"); + expect(server.requests).not.toContain("GET /admin/users/duplicates"); + } finally { + server.stop(); + } + }); + + test("--clear on an unflagged row says nothing to clear", async () => { + const server = startServer({ clear: { status: 200, body: { ok: true, id: 7, cleared: false } } }); + try { + const result = await runCli(["admin", "duplicates", "--clear", "7"], server.url); + expect(result.exitCode).toBe(0); + expect(result.stdout).toContain("was not flagged"); + } finally { + server.stop(); + } + }); + + test("a 409 prints the server's own sentence and exits non-zero", async () => { + // The expected answer while the collision stands, so it has to say what to + // fix rather than looking like a crash. + const server = startServer({ + clear: { + status: 409, + body: { + error: "identity_conflict_remains", + code: "identity_conflict_remains", + message: + "The collision that flagged this account is still there. Resolve it on the other account first.", + colliding: [{ kind: "orcid", value: "0000-0002-1974-1293", user_ids: [43] }], + }, + }, + }); + try { + const result = await runCli(["admin", "duplicates", "--clear", "42"], server.url); + expect(result.exitCode).toBe(1); + expect(`${result.stdout}${result.stderr}`).toContain("Still colliding"); + expect(`${result.stdout}${result.stderr}`).toContain("identity_conflict_remains"); + } finally { + server.stop(); + } + }); + + test("a non-numeric --clear id is rejected before any request", async () => { + const server = startServer({}); + try { + const result = await runCli(["admin", "duplicates", "--clear", "abc"], server.url); + expect(result.exitCode).toBe(1); + expect(`${result.stdout}${result.stderr}`).toContain("Invalid user id"); + expect(server.requests.some((r) => r.includes("clear-identity-conflict"))).toBe(false); + } finally { + server.stop(); + } + }); +}); diff --git a/test/admin-route-inventory.unit.test.ts b/test/admin-route-inventory.unit.test.ts index 9d8fac576..0df47cc03 100644 --- a/test/admin-route-inventory.unit.test.ts +++ b/test/admin-route-inventory.unit.test.ts @@ -45,6 +45,13 @@ const EXPECTED_ENTRIES: Record = { "POST /users/backfill-names": 2, // #1253, epic #1250 (ADR 0042): username backfill (zValidator + handler). "POST /users/backfill-usernames": 2, + // #1254, epic #1250 (ADR 0043): the duplicate-account report and the + // identity-conflict flag it reports on. `/users/duplicates` is reachable + // ONLY because registerUserDuplicateRoutes runs before registerUsersRoutes + // -- `GET /users/:username` matches it too, and the fallback router takes + // the first matching handler in registration order (pinned below). + "GET /users/duplicates": 1, + "POST /users/:id/clear-identity-conflict": 1, // DOI / enrichment "POST /datasets/:id/doi/concept": 2, diff --git a/test/api-export-surface.unit.test.ts b/test/api-export-surface.unit.test.ts index 111084369..e48524154 100644 --- a/test/api-export-surface.unit.test.ts +++ b/test/api-export-surface.unit.test.ts @@ -101,6 +101,7 @@ const MODULE_EXPORTS: Record = { "bulkDeleteDatasets", "changeUserRole", "changeVisibility", + "clearIdentityConflict", "createConceptDoi", "createExemplar", "dataIntegritySweep", @@ -118,6 +119,7 @@ const MODULE_EXPORTS: Record = { "getFleetDrift", "getImportStatus", "getSummaryCoverage", + "getUserDuplicates", "hedSweep", "hedSweepReset", "importDataset", @@ -174,9 +176,11 @@ const POST_SPLIT_ADDITIONS = [ "backfillUserNames", // #1255, epic #1250: POST /admin/users/backfill-names "backfillUsernames", // #1253, epic #1250: POST /admin/users/backfill-usernames "checkOrcidName", // #1255, epic #1250: GET /auth/orcid-name pre-signup lookup + "clearIdentityConflict", // #1254, ADR 0043: POST /admin/users/:id/clear-identity-conflict "doctorFix", // #1130, CLI wrapper for POST /admin/doctor/fix "doctorScan", // #1130, CLI wrapper for POST /admin/doctor/scan "getFacets", // #1149, epic #1144 phase 5b: GET /datasets/facets for shell completion + "getUserDuplicates", // #1254, ADR 0043: GET /admin/users/duplicates "publishZarrCatalog", // #1062, epic #1181 phase 2: POST /admin/zarr-catalog/publish "requestUploadAccess", // #1253, epic #1250: POST /users/me/upload-access/request "recordingStatsSweep", // #1194, CLI wrapper for POST /admin/datasets/recording-stats-sweep From da38244c6ff6e43380d4b2d4d6a80cac1282ddf0 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 17:53:43 -0700 Subject: [PATCH 05/18] feat: nemar auth profile --- backend/src/routes/users.ts | 20 +++ shared/contract/user.ts | 15 +++ src/commands/auth.ts | 138 +++++++++++++++++++- test/auth-profile-cli.test.ts | 228 ++++++++++++++++++++++++++++++++++ 4 files changed, 400 insertions(+), 1 deletion(-) create mode 100644 test/auth-profile-cli.test.ts diff --git a/backend/src/routes/users.ts b/backend/src/routes/users.ts index 565ff80c8..9779295f0 100644 --- a/backend/src/routes/users.ts +++ b/backend/src/routes/users.ts @@ -49,8 +49,13 @@ userRoutes.get("/me", async (c) => { .prepare( ` SELECT + status, created_at, approved_at, + email_verified, + orcid_verified, + given_name, + family_name, sandbox_completed, sandbox_completed_at, sandbox_dataset_id, @@ -62,8 +67,13 @@ userRoutes.get("/me", async (c) => { ) .bind(user.id, user.id) .first<{ + status: string; created_at: string; approved_at: string; + email_verified: number; + orcid_verified: number; + given_name: string | null; + family_name: string | null; sandbox_completed: number; sandbox_completed_at: string | null; sandbox_dataset_id: string | null; @@ -97,6 +107,16 @@ userRoutes.get("/me", async (c) => { github_username: user.github_username, role: user.role, orcid: user.orcid || null, + // Added for `nemar auth profile` (#1254): the command's whole job is to + // say what each identifier currently IS and where to change it, and + // "email: ada@lab.org" with no word on whether that inbox is confirmed + // is the half-answer that sends someone to support. Purely additive -- + // the contract marks all five optional, so an older CLI is unaffected. + status: userDetails?.status, + email_verified: !!userDetails?.email_verified, + orcid_verified: !!userDetails?.orcid_verified, + given_name: userDetails?.given_name ?? null, + family_name: userDetails?.family_name ?? null, created_at: userDetails?.created_at, approved_at: userDetails?.approved_at, dataset_count: userDetails?.dataset_count || 0, diff --git a/shared/contract/user.ts b/shared/contract/user.ts index 44992f62d..3dc8522f9 100644 --- a/shared/contract/user.ts +++ b/shared/contract/user.ts @@ -31,6 +31,21 @@ export const userSchema = z * an older backend does not send it; `undefined` is "unknown", not "no". */ service_access: z.boolean().optional(), + /** + * Account tier and identifier-verification state, for `nemar auth profile` + * (#1254, ADR 0043). + * + * All five are `.optional()` for the same reason `service_access` is: a + * CLI talking to a backend deployed before #1254 receives no such key, and + * absence must render as "unknown" rather than as a confident "no". That + * matters most for `email_verified` -- telling someone their confirmed + * inbox is unconfirmed sends them to redeem a code they do not need. + */ + status: z.string().optional(), + email_verified: z.boolean().optional(), + orcid_verified: z.boolean().optional(), + given_name: z.string().nullable().optional(), + family_name: z.string().nullable().optional(), }) .passthrough(); export type ContractUser = z.infer; diff --git a/src/commands/auth.ts b/src/commands/auth.ts index b27de8b59..4abd35cef 100644 --- a/src/commands/auth.ts +++ b/src/commands/auth.ts @@ -38,7 +38,7 @@ import { retrieveKey, signup, } from "../lib/api/auth.js"; -import { ApiError, MaintenanceError } from "../lib/api/errors.js"; +import { ApiError, MaintenanceError, errorDetail } from "../lib/api/errors.js"; import { printStepFailure } from "../lib/cli-output.js"; import { DEFAULT_API_URL, @@ -1421,3 +1421,139 @@ Examples: $ nemar auth request-upload-access $ nemar auth request-upload-access --why "Sharing our lab's 64-channel EEG study of motor imagery"`, ); + +// ============================================================================ +// Profile (#1254, epic #1250; ADR 0043) +// ============================================================================ + +/** Rendered for a field the backend sent as null/empty. */ +const NOT_SET = "not set"; + +/** + * `verified` / `not verified` / `unknown` for a tri-state flag. + * + * `undefined` is a backend that predates #1254, and it renders as unknown + * rather than as "not verified" for the same reason `Upload access` does (ADR + * 0040): telling someone their confirmed inbox is unconfirmed sends them to + * redeem a code they do not need. + */ +function verifiedMark(flag: boolean | undefined): string { + if (flag === undefined) return chalk.dim("verification state unknown"); + return flag ? chalk.green("verified") : chalk.yellow("not verified"); +} + +/** + * `nemar auth profile` — what this account's identifiers currently are, and + * where each one is changed. + * + * Separate from `nemar auth status`, which answers "am I logged in and can I + * upload" from the CONFIG CACHE. This one always fetches: it is the command + * someone runs because they are about to change an identifier, and a cached + * answer is exactly the wrong thing to hand them there. + * + * The footer is the load-bearing half. Identity uniqueness means a duplicate + * account is refused rather than created (ADR 0043), so the message a person + * hits is "that iD already belongs to an account" -- and the only useful next + * sentence is where to go and change it. Email, GitHub and ORCID are all + * self-service in Settings today; username and name are not, and saying so is + * better than leaving someone hunting for a field that does not exist. + */ +export async function profileAction(): Promise { + if (!isAuthenticated()) { + console.log(chalk.yellow("Not authenticated")); + console.log(); + console.log(" Run 'nemar auth login' to authenticate"); + return; + } + + const spinner = ora("Fetching account profile...").start(); + let user: Awaited>; + try { + user = await getCurrentUser(); + spinner.stop(); + } catch (error) { + spinner.fail(`Could not fetch your profile: ${errorDetail(error)}`); + if (error instanceof ApiError && error.statusCode === 401) { + console.log(chalk.dim(" Run 'nemar auth login' with a current API key.")); + } + process.exitCode = 1; + return; + } + + const fullName = [user.given_name, user.family_name].filter(Boolean).join(" "); + + console.log(); + console.log(chalk.bold("Account")); + console.log(` Username: ${user.username ? chalk.cyan(user.username) : chalk.dim(NOT_SET)}`); + console.log(` Name: ${fullName ? fullName : chalk.dim(NOT_SET)}`); + console.log(` Email: ${user.email} ${verifiedMark(user.email_verified)}`); + console.log( + ` GitHub: ${user.github_username ? `@${user.github_username}` : chalk.dim(NOT_SET)}`, + ); + console.log( + ` ORCID: ${user.orcid ? `${user.orcid} ${verifiedMark(user.orcid_verified)}` : chalk.dim(NOT_SET)}`, + ); + console.log(` Role: ${user.role}`); + + console.log(); + console.log(chalk.bold("Access")); + console.log(` Tier: ${user.status ?? chalk.dim("unknown")}`); + // Same three states as `nemar auth status`, and for the same reason: a + // backend that predates ADR 0040 sends nothing, and "not granted" is the one + // wrong answer to give someone who already holds the grant. + if (user.service_access === true) { + console.log(` Upload access: ${chalk.green("granted")}`); + } else if (user.service_access === false) { + console.log( + ` Upload access: ${chalk.yellow("not granted")} ${chalk.dim( + "(one-time admin approval; see https://nemar.org/support)", + )}`, + ); + } else { + console.log(` Upload access: ${chalk.dim("unknown (backend did not report it)")}`); + } + + console.log(); + console.log(chalk.bold("Where to change each")); + console.log( + ` Email, GitHub, ORCID ${chalk.cyan("https://nemar.org/settings")} ${chalk.dim( + "(sign in, then Settings)", + )}`, + ); + console.log(` Username, name ${chalk.dim("not editable yet (nemarOrg/website#301)")}`); + console.log( + ` Upload access ${chalk.dim("one-time admin approval; see https://nemar.org/support")}`, + ); + console.log(); + console.log( + chalk.dim( + "An ORCID iD, an email address, and a GitHub username each back at most one NEMAR account. If a sign-up or a link is refused because one is taken, change it on the account that already has it rather than making a second one.", + ), + ); +} + +const profileCmd = authCommand + .command("profile") + .description("Show your account identifiers and where to change each") + .action(profileAction); + +addVerboseHelp( + profileCmd, + ` +Description: + Prints the identifiers on your account — username, name, email, GitHub + username, ORCID iD — with the verification state of the email and the + ORCID link, your access tier, and whether upload access has been granted. + + Always fetched from the server, never from the local config cache: this is + the command you run before changing something, so a stale answer is worse + than no answer. + + Each of the ORCID iD, the email address, and the GitHub username backs at + most one NEMAR account. A second sign-up with any of them is refused rather + than creating a duplicate; the fix is to change the identifier on the + account that already holds it. + +Examples: + $ nemar auth profile`, +); diff --git a/test/auth-profile-cli.test.ts b/test/auth-profile-cli.test.ts new file mode 100644 index 000000000..dccb8786a --- /dev/null +++ b/test/auth-profile-cli.test.ts @@ -0,0 +1,228 @@ +/** + * CLI `nemar auth profile` (#1254, epic #1250; ADR 0043), driven through the + * real entry point (`bun run src/index.ts auth profile`). + * + * Identity uniqueness means a duplicate sign-up is REFUSED rather than + * created, so the message a person hits is "that already belongs to an + * account" -- and the only useful next sentence is where to go and change it. + * This command is that sentence, so what it prints is the behaviour under + * test: the identifiers, whether each is verified, and the footer that says + * which are self-service today and which are not. + * + * Harness mirrors test/auth-status-upload-access.test.ts: a real subprocess + * CLI invocation against a real local HTTP server via TEST_API_URL, with an + * isolated NEMAR_CONFIG_DIR and a real on-disk config store. No mocks -- the + * server serves the real `/users/me` envelope shape and the CLI's real + * contract validation runs against it. + */ + +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { spawn } from "bun"; + +const CLI_ENTRY = join(import.meta.dir, "..", "src", "index.ts"); +const REPO_ROOT = join(import.meta.dir, ".."); + +/** The full `/users/me` envelope a current backend returns. */ +function meUser(overrides: Record = {}) { + return { + id: 12, + username: "harlow", + email: "harlow@example.org", + github_username: "harlow-gh", + role: "member", + orcid: "0000-0002-1825-0097", + status: "approved", + email_verified: true, + orcid_verified: true, + given_name: "Ada", + family_name: "Lovelace", + created_at: "2026-05-01T00:00:00Z", + approved_at: null, + dataset_count: 0, + sandbox_completed: true, + sandbox_completed_at: null, + sandbox_dataset_id: null, + service_access: true, + ...overrides, + }; +} + +function startMeServer(user: Record) { + const server = Bun.serve({ + port: 0, + fetch(req) { + const url = new URL(req.url); + if (url.pathname === "/notices") return Response.json({ notices: [] }); + if (url.pathname === "/datasets/facets") return Response.json({}); + return Response.json({ user, token: null }); + }, + }); + return { url: `http://localhost:${server.port}`, stop: () => server.stop(true) }; +} + +let configDir: string; + +function seedAuthenticatedConfig(): void { + writeFileSync( + join(configDir, "config.json"), + JSON.stringify({ + activeAccount: "harlow", + accounts: { harlow: { apiKey: "test-user-key", username: "harlow" } }, + }), + ); +} + +beforeEach(() => { + configDir = mkdtempSync(join(tmpdir(), "nemar-auth-profile-cli-")); +}); + +afterEach(() => { + rmSync(configDir, { recursive: true, force: true }); +}); + +async function runCli(args: string[], testApiUrl: string) { + const env = { + ...process.env, + NEMAR_CONFIG_DIR: configDir, + TEST_API_URL: testApiUrl, + NEMAR_NO_UPDATE_CHECK: "1", + NO_COLOR: "1", + }; + env.FORCE_COLOR = undefined; + env.CLICOLOR_FORCE = undefined; + const proc = spawn({ + cmd: ["bun", "run", CLI_ENTRY, ...args], + cwd: REPO_ROOT, + env, + stdin: "ignore", + stdout: "pipe", + stderr: "pipe", + }); + const stdout = await new Response(proc.stdout).text(); + const stderr = await new Response(proc.stderr).text(); + const exitCode = await proc.exited; + return { stdout, stderr, exitCode }; +} + +describe("nemar auth profile", () => { + test("prints every identifier with its verification state", async () => { + seedAuthenticatedConfig(); + const server = startMeServer(meUser()); + try { + const result = await runCli(["auth", "profile"], server.url); + expect(result.exitCode).toBe(0); + expect(result.stdout).toContain("Username: harlow"); + expect(result.stdout).toContain("Name: Ada Lovelace"); + expect(result.stdout).toContain("Email: harlow@example.org verified"); + expect(result.stdout).toContain("GitHub: @harlow-gh"); + expect(result.stdout).toContain("ORCID: 0000-0002-1825-0097 verified"); + expect(result.stdout).toContain("Tier: approved"); + expect(result.stdout).toContain("Upload access: granted"); + } finally { + server.stop(); + } + }); + + test("an unverified email and an unverified iD say so, separately", async () => { + seedAuthenticatedConfig(); + const server = startMeServer(meUser({ email_verified: false, orcid_verified: false })); + try { + const result = await runCli(["auth", "profile"], server.url); + expect(result.stdout).toContain("Email: harlow@example.org not verified"); + expect(result.stdout).toContain("ORCID: 0000-0002-1825-0097 not verified"); + } finally { + server.stop(); + } + }); + + test("a backend that omits the flags reports unknown, never 'not verified'", async () => { + // The ADR 0040 rule applied to the new fields: telling someone their + // confirmed inbox is unconfirmed sends them to redeem a code they do not + // need, so absence must not collapse to `false`. + seedAuthenticatedConfig(); + const user = meUser(); + // biome-ignore lint/performance/noDelete: absence is the case under test. + delete user.email_verified; + // biome-ignore lint/performance/noDelete: absence is the case under test. + delete user.orcid_verified; + // biome-ignore lint/performance/noDelete: absence is the case under test. + delete user.service_access; + // biome-ignore lint/performance/noDelete: absence is the case under test. + delete user.status; + const server = startMeServer(user); + try { + const result = await runCli(["auth", "profile"], server.url); + expect(result.stdout).toContain("verification state unknown"); + expect(result.stdout).not.toContain("not verified"); + expect(result.stdout).toContain("Upload access: unknown"); + expect(result.stdout).not.toContain("not granted"); + } finally { + server.stop(); + } + }); + + test("missing identifiers render as 'not set', not as the word null", async () => { + // Web/ORCID rows carry username = NULL and github_username = NULL by + // design (#1012); printing the literal `null` is the bug that put it in + // the admin listing. + seedAuthenticatedConfig(); + const server = startMeServer( + meUser({ + username: null, + github_username: null, + orcid: null, + given_name: null, + family_name: null, + }), + ); + try { + const result = await runCli(["auth", "profile"], server.url); + expect(result.stdout).toContain("Username: not set"); + expect(result.stdout).toContain("Name: not set"); + expect(result.stdout).toContain("GitHub: not set"); + expect(result.stdout).toContain("ORCID: not set"); + expect(result.stdout).not.toContain("null"); + } finally { + server.stop(); + } + }); + + test("the footer says where each identifier is changed", async () => { + seedAuthenticatedConfig(); + const server = startMeServer(meUser()); + try { + const result = await runCli(["auth", "profile"], server.url); + expect(result.stdout).toContain("Where to change each"); + expect(result.stdout).toContain("Email, GitHub, ORCID"); + expect(result.stdout).toContain("https://nemar.org/settings"); + // Username and name are NOT self-service yet, and saying so beats + // leaving someone hunting for a field that does not exist. + expect(result.stdout).toContain("Username, name"); + expect(result.stdout).toContain("not editable yet"); + expect(result.stdout).toContain("at most one NEMAR account"); + } finally { + server.stop(); + } + }); + + test("it always fetches: nothing is served from the config cache", async () => { + // The command someone runs BEFORE changing an identifier, so a cached + // answer is the wrong thing to hand them. With no reachable backend it + // must fail rather than print the stored username and email. + seedAuthenticatedConfig(); + const result = await runCli(["auth", "profile"], "http://127.0.0.1:1"); + expect(result.exitCode).toBe(1); + expect(result.stdout).not.toContain("harlow@example.org"); + expect(`${result.stdout}${result.stderr}`).toContain("Could not fetch your profile"); + }); + + test("an unauthenticated config says how to authenticate", async () => { + const result = await runCli(["auth", "profile"], "http://127.0.0.1:1"); + expect(result.exitCode).toBe(0); + expect(result.stdout).toContain("Not authenticated"); + expect(result.stdout).toContain("nemar auth login"); + }); +}); From 4c0f14a5d6f6246027ff5a21958f8a50acf9e61c Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 17:53:47 -0700 Subject: [PATCH 06/18] docs: ADR 0043 one person one account --- .../decisions/0043-one-person-one-account.md | 221 ++++++++++++++++++ .context/decisions/README.md | 1 + 2 files changed, 222 insertions(+) create mode 100644 .context/decisions/0043-one-person-one-account.md diff --git a/.context/decisions/0043-one-person-one-account.md b/.context/decisions/0043-one-person-one-account.md new file mode 100644 index 000000000..e79b0a87d --- /dev/null +++ b/.context/decisions/0043-one-person-one-account.md @@ -0,0 +1,221 @@ +# ADR 0043: One person, one account + +**Status:** accepted +**Date:** 2026-09-05 +**Owner:** Seyed Yahya Shirazi + +> Numbering note: epic #1250 runs phases in parallel, and this one (phase 4, +> #1254) lands alongside phase 3 (#1253), which owns 0042. +> Until phase 3 is rebased in, 0042 does not exist on this branch and the ADR +> index's gapless check fails here by construction; it goes green with the +> rebase. +> The same note is on 0041 for the same reason. + +## Context + +Nothing has ever stopped one person from holding two live NEMAR accounts. + +`users.email` carries an exact-case `UNIQUE` constraint (migration 0026), +so `Ada@Lab.org` and `ada@lab.org` are two accounts. +`users.orcid` carries no constraint at all; +the only ORCID uniqueness in the system is `UNIQUE(provider, provider_subject)` +on `oauth_identities`, which only the ORCID web flow ever writes. +A CLI signup therefore never touched it, +and the ORCID finalize route checked that table and nothing else. + +Production shows exactly what that produces. +Two live rows carry the same iD `0000-0002-1974-1293` with `orcid_verified = 1`: + +| id | email | oauth_identities row | name | +|---|---|---|---| +| 42 | robert.oostenveld@donders.ru.nl | no | none | +| 43 | r.oostenveld@donders.ru.nl | yes | from ORCID | + +The identity row left row 42 — an unlink, or an identity insert whose rollback +did not run — while `users.orcid` and `orcid_verified` stayed behind on it. +The second sign-up then saw an iD that no `oauth_identities` row claimed, +and made a second account for the same person. + +That is not a cosmetic problem. +Two accounts split one person's datasets, DOIs, upload grant and collaborator +access across two owners, +and epic #1250 makes it worse rather than better: +approval is now a per-account decision an admin makes once (ADR 0040), +and a DOI cites the depositing account by name (ADR 0041). +Both of those are answers to "who is this", +and both are wrong when the answer is "half of them". + +## Decision + +**An ORCID iD, an email address (compared case-insensitively), or a GitHub +handle backs at most one LIVE account.** +The database enforces it, the application refuses it with a typed error before +the database has to, and every write normalises the identifier first so the +three rules compare the same strings. + +### Enforcement: flag, then partial-index + +Migration 0077 adds `users.identity_conflict INTEGER NOT NULL DEFAULT 0`, +flags every NON-CANONICAL duplicate already in the catalog, +and only then creates the unique indexes — as PARTIAL indexes that exclude a +flagged row: + +```sql +CREATE UNIQUE INDEX idx_users_orcid_live_unique ON users(orcid) + WHERE orcid IS NOT NULL AND TRIM(orcid) <> '' + AND deleted_at IS NULL AND identity_conflict = 0; + +CREATE UNIQUE INDEX idx_users_email_live_unique ON users(email COLLATE NOCASE) + WHERE deleted_at IS NULL AND identity_conflict = 0; +``` + +Canonical, in both the migration and the report, is: +for an ORCID group, the row that holds the `oauth_identities` row for that iD, +and the lowest id only when none does; +for an email group, the lowest live id. +The ORCID rule is not a detail — in the 42/43 case the lowest id is the ORPHAN, +so "lowest id wins" would have crowned the row that cannot sign in. + +**The deploy can never fail on existing data, and no row is deleted.** +Those are the two properties the whole shape exists to buy. +A plain `CREATE UNIQUE INDEX` fails on rows 42/43 and takes the migration — +and therefore the deploy — with it, +and the only way to make it buildable would be to delete or merge somebody's +real account inside a migration. + +A flagged row keeps everything except its claim on the identifier: +it signs in, owns its datasets, appears in every listing, and reads normally. + +### Remediation: self-service first, manual merge last + +`GET /admin/users/duplicates` (CLI `nemar admin duplicates`) reports the groups, +marks the canonical row, and shows what an operator actually weighs — +account age, dataset count, and whether the row can sign in with its iD. +`POST /admin/users/:id/clear-identity-conflict` +(CLI `nemar admin duplicates --clear `) +clears a flag **only once the collision is actually gone**, +409ing with the still-colliding rows otherwise. + +The fix itself is the person's own, on the account that already exists: +change its email, change its GitHub username, +or unlink and re-link its ORCID iD, in Settings on nemar.org. +Every refusal message says so. +**Merging two accounts stays manual** and is not automated here: +it means moving datasets, DOIs, S3 credentials and a GitHub collaborator set +between owners, and a wrong merge is not recoverable. + +### Application refusals, typed + +The pre-flight checks exist so a person gets a sentence instead of a +constraint error, and the website gets a code instead of a string to grep: +`orcid_in_use`, `orcid_already_linked`, `orcid_linked_other`, `email_in_use`, +`github_in_use`, `identity_conflict_remains` +(`shared/contract/identity.ts`, with the messages, so the CLI, the website and +the JSON body all read the same wording). + +`error` deliberately carries different things on the two families of route, +because they are read by different things and both are already right: +the browser-facing routes put the CODE there (the website reads it there, and +`email_in_use` predates this phase), +while CLI signup puts a short human label there because that is the field the +CLI prints. `code` and `message` are additive on both. + +### Unlink now clears `users.orcid` + +Unlinking used to keep `users.orcid` on the reasoning that it is the +citation-facing value a DOI-discovery pass may have found. +That reasoning does not survive the row it produces: +an account still claiming an iD it can no longer prove, +invisible to every check that looked at `oauth_identities`. +That row is id 42. + +The citation value is RE-DERIVABLE and the identity claim is not: +linking the iD again refills it, and so does the DOI enrichment pass that +discovered it. Losing a re-derivable column beats keeping a false claim. + +### Normalisation at every write + +One module, `backend/src/services/identity.ts`: +email trimmed and lowercased; ORCID reduced to a bare iD with an uppercase +check digit; GitHub handle trimmed with a leading `@` stripped. +Applied at CLI signup, ORCID finalize, the email change, the admin +test-fixture route, and (already) `PATCH /auth/profile`, which now imports the +GitHub rule instead of spelling it out again. + +Migration 0077 also canonicalises the check digit on existing rows, +narrowly (a `GLOB` that matches only a bare iD ending in a lowercase `x`), +so a legacy case variant cannot slip past an index that compares exactly. + +## Consequences + +- **A second sign-up by the same person is now refused, not silently created.** + That is the point, and it is also the new failure mode: someone who has lost + access to an old account cannot route around it by making a new one. The + messages point at Settings on the existing account; when that is not enough, + it is an admin conversation. +- **Two production rows are flagged on deploy** (42 loses the iD to 43), plus + whatever case-variant email pairs the catalog holds. Nothing is emailed and + nothing changes for those users until someone acts on the report. +- **The flag is one-way without an admin.** Only + `clear-identity-conflict` clears it, and only once the collision is gone. + A person cannot un-flag themselves, and should not be able to. +- **A row can be flagged for a collision that no longer exists** (the other + account changed its email). The report says so — `flagged_count` exceeding + what the groups explain is exactly that signal — and clearing is a one-line + admin command. +- **GitHub gets a report but no new index.** `idx_users_github` (0012) has been + a table-wide unique NOCASE index all along, so a live GitHub duplicate cannot + exist. The grouping is carried anyway so the report is complete and so the + three identifiers are described in one place. +- **Email lowercasing is lossy** and deliberately so. RFC 5321 leaves the local + part case-significant; no provider anyone signs up with treats `Ada@` and + `ada@` as two people, and storing both is one of the two ways this catalog + grew duplicates. +- **The exact-case `UNIQUE` from 0026 stays.** Both constraints are enforced; + a case-variant collision now fails at the partial index. SQLite reports + either as `UNIQUE constraint failed: users.email` — the column, not the index + name — so the existing column-scoped catch clauses keep working unchanged. +- **`nemar auth profile` exists because refusals need somewhere to send + people.** It prints each identifier with its verification state and says where + each is changed. Username and name are not self-service yet + (nemarOrg/website#301, and phase 3's `PATCH /auth/profile` work), and the + footer says that rather than leaving someone hunting for a field. + +## Alternatives considered + +- **Add the unique indexes and fix the duplicates by hand first.** The + honest-looking option, and it makes the deploy depend on a manual step being + done in the right order against the right database. If it is not, the + migration fails and takes the deploy with it. The flag makes the index + buildable on any catalog, which is what makes the deploy safe to repeat. +- **Delete or merge the duplicate row inside the migration.** Row 42 is a real + person's account. A migration is the worst possible place to decide that. +- **Enforce ORCID uniqueness on `oauth_identities` alone.** It is already + unique there, and that is exactly the constraint rows 42/43 satisfy. The + claim that matters lives on `users.orcid`, which is what a CLI signup writes + and what DOIs cite. +- **A `users_identities` side table keyed by (kind, value).** Cleaner in the + abstract, and it would need every read path taught about a join for a + property that is three columns on a ~600-row table. ADR 0034's column-budget + reasoning cuts the same way: derive and constrain, do not add structure. +- **Refuse the duplicate silently and sign the person into the existing + account.** A takeover vector: completing an ORCID flow would hand the browser + a session on an account the person may not own. The finalize route already + refuses email collisions for this reason (#832) and this keeps it. +- **Let a person clear their own flag.** The flag means "somebody else holds + this identifier". Self-service clearing is self-service duplicate creation. + +## Receipts + +- Issue #1254, epic #1250 (phase 4). Related: #1249, #1012, #832, #913. +- Migration `backend/src/db/migrations/0077_identity_uniqueness.sql`, + service `backend/src/services/identity.ts`, + routes `backend/src/routes/admin/user-duplicates.ts`, + contract `shared/contract/identity.ts`. +- ADR 0040 (approval is the single writer of upload access) and ADR 0041 (DOIs + cite the uploader by real name) are the two decisions that make a split + identity expensive rather than untidy. +- ADR 0022 (relink intent is never minted on a GET) — the ORCID link surface + this phase adds a second gate to. +- ADR 0034 (derive, do not store) — why this is a flag plus two indexes rather + than a new table. diff --git a/.context/decisions/README.md b/.context/decisions/README.md index 072c5cae6..daa559cba 100644 --- a/.context/decisions/README.md +++ b/.context/decisions/README.md @@ -81,6 +81,7 @@ invisible one — which is why it is checked rather than trusted. - [ADR 0040](0040-approval-is-the-single-writer-of-upload-access.md) - Admin approval is the single writer of upload access; `verified` is the base tier - [ADR 0041](0041-dois-cite-the-uploader-by-real-name-or-not-at-all.md) - DOIs cite the uploader by real name, or not at all - [ADR 0042](0042-upload-access-is-requested-once-by-the-person-who-wants-it.md) - Upload access is requested once, by the person who wants it +- [ADR 0043](0043-one-person-one-account.md) - One person, one account: ORCID, email and GitHub each back at most one live account ## Backfill note (2026-07-31) From f5838d2b50eeb166ddda562dd8574360c351b982 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 17:55:25 -0700 Subject: [PATCH 07/18] test: pin signup pre-checks by GitHub call count --- backend/test/identity-refusals-route.test.ts | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/backend/test/identity-refusals-route.test.ts b/backend/test/identity-refusals-route.test.ts index 81b7c1b4a..887066c85 100644 --- a/backend/test/identity-refusals-route.test.ts +++ b/backend/test/identity-refusals-route.test.ts @@ -47,6 +47,19 @@ let db: Database; let app: Hono<{ Bindings: Bindings; Variables: Variables }>; /** Serves ORCID personal-details, ORCID /oauth/token, and GitHub /users/:login. */ let external: Server; +/** + * GitHub `/users/:login` hits since the last reset. + * + * This is what makes the signup PRE-CHECKS falsifiable at all. Migration + * 0077's partial unique indexes would reject a duplicate email or iD at the + * INSERT anyway, and signup's catch turns that into the same 409 body -- so a + * status-and-body assertion alone passes whether or not the pre-check exists. + * The pre-checks run BEFORE `validateGitHubUsername`, so the observable + * difference is that a refused signup spends no GitHub API call. That is also + * why they are ordered that way: a duplicate must not cost a rate-limited + * request against a shared token. + */ +let githubCalls = 0; let externalBase: string; /** Which iD the local ORCID token endpoint hands back for the next callback. */ let tokenOrcid = OTHER_ORCID; @@ -65,7 +78,10 @@ beforeAll(() => { }); } const gh = url.pathname.match(/^\/users\/(.+)$/); - if (gh) return Response.json({ login: gh[1], id: 4242 }); + if (gh) { + githubCalls += 1; + return Response.json({ login: gh[1], id: 4242 }); + } return new Response("not found", { status: 404 }); }, }); @@ -97,6 +113,7 @@ function env(): Bindings { beforeEach(() => { db = freshDb(); tokenOrcid = OTHER_ORCID; + githubCalls = 0; app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); app.route("/auth", authRoutes); app.route("/auth", authOrcidRoutes); @@ -199,6 +216,9 @@ describe("CLI signup refuses a duplicate identity", () => { expect(body.error).toBe("Email already registered"); expect(body.message).toContain("nemar.org/settings"); expect(db.query<{ n: number }, []>("SELECT COUNT(*) AS n FROM users").get()?.n).toBe(2); + // Refused by the pre-check, not by the index at INSERT time: no GitHub + // call was spent. Without this the assertions above pass either way. + expect(githubCalls).toBe(0); }); test("an iD held only through users.orcid is refused (the 42/43 hole)", async () => { @@ -210,6 +230,7 @@ describe("CLI signup refuses a duplicate identity", () => { const body = (await res.json()) as { error: string; code: string; message: string }; expect(body.code).toBe("orcid_in_use"); expect(body.message).toContain("nemar.org/settings"); + expect(githubCalls).toBe(0); }); test("an iD held only through oauth_identities is refused too", async () => { @@ -222,6 +243,7 @@ describe("CLI signup refuses a duplicate identity", () => { const res = await signup({ orcid: HELD_ORCID }); expect(res.status).toBe(409); expect(((await res.json()) as { code: string }).code).toBe("orcid_in_use"); + expect(githubCalls).toBe(0); }); test("a case-variant GitHub handle is refused", async () => { From df972e656c94f540ad1ee9e47108baf47d6e4068 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 17:56:54 -0700 Subject: [PATCH 08/18] test: pin the new /users/me profile fields --- backend/test/users-me-profile-fields.test.ts | 132 +++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 backend/test/users-me-profile-fields.test.ts diff --git a/backend/test/users-me-profile-fields.test.ts b/backend/test/users-me-profile-fields.test.ts new file mode 100644 index 000000000..7086b8d2d --- /dev/null +++ b/backend/test/users-me-profile-fields.test.ts @@ -0,0 +1,132 @@ +/** + * `GET /users/me` reports the account's identifier state (#1254, ADR 0043). + * + * `nemar auth profile` exists so someone refused a duplicate sign-up has + * somewhere to look, and it can only say "your email is verified, your ORCID + * link is not" if this route sends those facts. The CLI test drives the + * command against a local server, so it pins the RENDERING; this pins the + * SOURCE. Without it, dropping a field here breaks the command with every + * CLI test still green. + * + * Real engine, no mocks: bun:sqlite behind realD1 with every migration + * applied, the real Hono route behind the real `authMiddleware`, reached with + * a real hashed API token. + */ + +import type { Database } from "bun:sqlite"; +import { beforeEach, describe, expect, test } from "bun:test"; +import { Hono } from "hono"; +import { userRoutes } from "../src/routes/users"; +import { hashApiKey } from "../src/services/token"; +import type { Bindings, Variables } from "../src/types/bindings"; +import { freshDb, realD1 } from "./helpers/d1"; + +const API_KEY = "nemar_test_key_for_users_me_profile"; +const ORCID = "0000-0002-1825-0097"; + +let db: Database; +let app: Hono<{ Bindings: Bindings; Variables: Variables }>; + +function env(): Bindings { + return { DB: realD1(db), ENVIRONMENT: "test" } as unknown as Bindings; +} + +interface SeedOpts { + emailVerified?: 0 | 1; + orcidVerified?: 0 | 1; + orcid?: string | null; + givenName?: string | null; + familyName?: string | null; + status?: string; + serviceAccess?: 0 | 1; +} + +async function seed(opts: SeedOpts = {}): Promise { + db.run( + `INSERT INTO users (username, email, password_hash, github_username, status, signup_source, + email_verified, orcid, orcid_verified, given_name, family_name, + role, service_access) + VALUES ('harlow', 'harlow@example.org', 'x', 'harlow-gh', ?, 'cli', ?, ?, ?, ?, ?, 'member', ?)`, + [ + opts.status ?? "approved", + opts.emailVerified ?? 1, + opts.orcid === undefined ? ORCID : opts.orcid, + opts.orcidVerified ?? 1, + opts.givenName === undefined ? "Ada" : opts.givenName, + opts.familyName === undefined ? "Lovelace" : opts.familyName, + opts.serviceAccess ?? 1, + ], + ); + const row = db + .query<{ id: number }, []>("SELECT id FROM users WHERE username = 'harlow'") + .get(); + if (!row) throw new Error("seed failed"); + db.run("INSERT INTO tokens (user_id, api_key_hash, api_key_prefix) VALUES (?, ?, 'nemar_te')", [ + row.id, + await hashApiKey(API_KEY), + ]); + return row.id; +} + +interface MeBody { + user: { + status?: string; + email_verified?: boolean; + orcid_verified?: boolean; + given_name?: string | null; + family_name?: string | null; + orcid?: string | null; + service_access?: boolean; + }; +} + +function me(): Promise { + return app.request("/users/me", { headers: { Authorization: `Bearer ${API_KEY}` } }, env()); +} + +beforeEach(() => { + db = freshDb(); + app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); + app.route("/users", userRoutes); +}); + +describe("GET /users/me profile fields", () => { + test("reports tier, both verification flags, and the name parts", async () => { + await seed(); + const res = await me(); + expect(res.status).toBe(200); + const { user } = (await res.json()) as MeBody; + expect(user.status).toBe("approved"); + expect(user.email_verified).toBe(true); + expect(user.orcid_verified).toBe(true); + expect(user.given_name).toBe("Ada"); + expect(user.family_name).toBe("Lovelace"); + expect(user.orcid).toBe(ORCID); + expect(user.service_access).toBe(true); + }); + + test("the two verification flags are independent", async () => { + // A verified inbox with an UNVERIFIED iD is the common shape: `users.orcid` + // is populated by DOI discovery for accounts that never signed in through + // ORCID (`decideVerifiedFlag`). A single conflated flag would report those + // iDs as proven. + // + // `verified` rather than `pending` because the reverse shape cannot reach + // this route at all: an unverified email is not an active account status + // (ADR 0040), so `authMiddleware` 403s before the handler runs. + await seed({ status: "verified", emailVerified: 1, orcidVerified: 0 }); + const { user } = (await (await me()).json()) as MeBody; + expect(user.email_verified).toBe(true); + expect(user.orcid_verified).toBe(false); + expect(user.status).toBe("verified"); + }); + + test("an account with no name or iD reports nulls, not empty strings", async () => { + await seed({ orcid: null, orcidVerified: 0, givenName: null, familyName: null }); + const { user } = (await (await me()).json()) as MeBody; + expect(user.given_name).toBeNull(); + expect(user.family_name).toBeNull(); + expect(user.orcid).toBeNull(); + expect(user.orcid_verified).toBe(false); + }); +}); From d853a189b632c9ca9ab183d7fd3c6aabebf6d821 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 18:00:46 -0700 Subject: [PATCH 09/18] refactor: one column-scoped UNIQUE-violation check --- backend/src/routes/auth-web.ts | 16 ++++++++++++---- backend/src/routes/auth.ts | 9 +++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/backend/src/routes/auth-web.ts b/backend/src/routes/auth-web.ts index 1e129c078..83eab17b2 100644 --- a/backend/src/routes/auth-web.ts +++ b/backend/src/routes/auth-web.ts @@ -74,7 +74,12 @@ import { } from "../services/email-verification"; import { validateGitHubUsername } from "../services/github"; import { getDatasetsToken } from "../services/github-auth"; -import { emailFieldSchema, findEmailHolder, identityRefusal } from "../services/identity"; +import { + emailFieldSchema, + findEmailHolder, + identityRefusal, + isUniqueViolationOn, +} from "../services/identity"; import { type ProfilePatchInput, githubHandleChanged, @@ -1344,9 +1349,12 @@ authWebRoutes.post( // Column-scoped, matching the profile PATCH and signup precedents: a // UNIQUE hit on anything OTHER than users.email must not be // mislabeled as an address collision — rethrow to the generic 500. - const msg = String(writeErr); - if (msg.includes("UNIQUE constraint failed") && msg.includes("users.email")) { - return c.json({ error: "email_in_use" }, 409); + // Column-scoped through the shared helper, which also matches 0077's + // partial index: SQLite reports `users.email` for both that and the + // 0026 table-level constraint, so one check covers the exact-case and + // the case-insensitive collision alike (ADR 0043). + if (isUniqueViolationOn(writeErr, "email")) { + return c.json({ error: "email_in_use", ...identityRefusal("email_in_use") }, 409); } throw writeErr; } diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index 37f8e3178..313f7aa84 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -28,6 +28,7 @@ import { findEmailHolder, findOrcidHolder, identityRefusal, + isUniqueViolationOn, normalizeEmail, normalizeGithubHandle, normalizeOrcid, @@ -528,10 +529,10 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { // D1 errors may not be standard Error instances, so check multiple ways const msg = error instanceof Error ? error.message : String(error); if (msg.includes("UNIQUE constraint failed")) { - if (msg.includes("users.username")) { + if (isUniqueViolationOn(error, "username")) { return c.json({ error: "Username already taken" }, 409); } - if (msg.includes("users.email")) { + if (isUniqueViolationOn(error, "email")) { return c.json( { error: "Email already registered", ...identityRefusal("email_in_use") }, 409, @@ -540,13 +541,13 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { // 0077's partial index reports the COLUMN, not the index name, so this // catches a concurrent signup that claimed the iD between the check // above and this insert. - if (msg.includes("users.orcid")) { + if (isUniqueViolationOn(error, "orcid")) { return c.json( { error: "ORCID iD already registered", ...identityRefusal("orcid_in_use") }, 409, ); } - if (msg.includes("users.github_username")) { + if (isUniqueViolationOn(error, "github_username")) { return c.json( { error: "GitHub account already linked to another user", From 43589ebad3bf8a26bb9d8205c1497597a15d80fd Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 18:05:19 -0700 Subject: [PATCH 10/18] test: give each seeded web fixture its own iD --- backend/test/web-email-verification.test.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/test/web-email-verification.test.ts b/backend/test/web-email-verification.test.ts index d119ef79b..f8f2a29e5 100644 --- a/backend/test/web-email-verification.test.ts +++ b/backend/test/web-email-verification.test.ts @@ -39,6 +39,8 @@ const ADMIN_EMAIL = "tieradmin@nemar.test"; let db: Database; let app: Hono<{ Bindings: Bindings; Variables: Variables }>; +/** Bumped per seeded row so each gets its own iD; reset with the database. */ +let seededOrcidCount = 0; function env(): Bindings { return { @@ -98,13 +100,25 @@ function seedAdmin(): void { ); } -/** A web/ORCID-shaped row: username NULL, ORCID verified, email unverified. */ +/** + * A web/ORCID-shaped row: username NULL, ORCID verified, email unverified. + * + * Each seeded row gets a DISTINCT iD. It used to reuse ORCID_ID for every + * call, which migration 0077 now refuses outright (an iD backs one live + * account, ADR 0043) -- the fixture was manufacturing exactly the duplicate + * this suite's sibling tests exist to prevent. Keeping ORCID_ID off these rows + * also means the finalize tests below, which sign a pending token FOR + * ORCID_ID, are exercising an unclaimed iD rather than colliding with a + * fixture. + */ function seedWebUser(email: string, status = "pending"): number { + seededOrcidCount += 1; + const orcid = `0000-0002-9999-${String(seededOrcidCount).padStart(4, "0")}`; db.run( `INSERT INTO users (email, status, signup_source, email_verified, orcid, orcid_verified, given_name, family_name, service_access) VALUES (?, ?, 'web', 0, ?, 1, 'Ada', 'Lovelace', 0)`, - [email, status, ORCID_ID], + [email, status, orcid], ); const row = db.query<{ id: number }, [string]>("SELECT id FROM users WHERE email = ?").get(email); if (!row) throw new Error(`seed failed for ${email}`); @@ -196,6 +210,7 @@ function codeRow(email: string) { beforeEach(() => { db = freshDb(); + seededOrcidCount = 0; app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); app.route("/auth", authWebRoutes); app.route("/auth", authOrcidRoutes); From fcf519b44704b4782059133f9d6ded403e84ec4e Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 18:46:18 -0700 Subject: [PATCH 11/18] fix: D1 rejects the 79-char GLOB in 0077 --- .../migrations/0077_identity_uniqueness.sql | 70 ++++- package.json | 1 + scripts/d1-migration-check.ts | 268 ++++++++++++++++++ 3 files changed, 330 insertions(+), 9 deletions(-) create mode 100644 scripts/d1-migration-check.ts diff --git a/backend/src/db/migrations/0077_identity_uniqueness.sql b/backend/src/db/migrations/0077_identity_uniqueness.sql index b11781942..3a651cd7a 100644 --- a/backend/src/db/migrations/0077_identity_uniqueness.sql +++ b/backend/src/db/migrations/0077_identity_uniqueness.sql @@ -34,15 +34,47 @@ -- the collision is actually gone (409 with the colliding rows otherwise), so -- the flag cannot be cleared back into a state the index would refuse. -- +-- NUMBERING: epic #1250 runs phases in parallel. Phase 3 (#1253) owns 0076; +-- this is phase 4 (#1254). Until phase 3 is rebased in, 0076 does not exist on +-- this branch. Nothing here depends on it -- the two touch disjoint columns -- +-- but do not read the gap as a lost file. +-- -- NOT ONE TRANSACTION (same reasoning as 0075's header: wrangler hands the -- file to executeSql as one string whose transactional scope is not --- guaranteed on the remote path). Statements 2-5 are individually idempotent --- and each narrows its own predicate by succeeding, so re-running finishes a --- partial application. Statement 1, the ALTER, is the exception: SQLite has no --- `ADD COLUMN IF NOT EXISTS`, so a re-run after it landed fails on it. That is --- the standing shape for every column-adding migration here (0037, 0062), and --- the recovery is the same: if the ALTER already landed, re-run statements 2 --- onward. Do not hand-repair the flags. +-- guaranteed on the remote path). Statements 2 through the trailing lookup +-- index are individually idempotent and each narrows its own predicate by +-- succeeding, so re-running finishes a partial application. Statement 1, the +-- ALTER, is the exception: SQLite has no `ADD COLUMN IF NOT EXISTS`, so a +-- re-run after it landed fails on it. That is the standing shape for every +-- column-adding migration here (0037, 0062), and the recovery is the same: if +-- the ALTER already landed, re-run statements 2 onward. Do not hand-repair the +-- flags. +-- +-- PROVEN AGAINST REAL D1, not just bun:sqlite. D1's SQLite build enforces +-- limits bun:sqlite does not (see statement 2), so a green `bun test` is NOT +-- evidence this file applies. Run: +-- +-- bun run migrations:d1-check +-- +-- which wipes the local D1 state, replays all 78 migrations through +-- `wrangler d1 execute --local` one file at a time, and then diffs the +-- resulting object and column catalogue against the bun:sqlite one. This file +-- was also applied a second time onto a database already carrying 0001-0075 +-- plus a seeded duplicate (the 42/43 ORCID pair, a case-variant email pair and +-- a lowercase-`x` iD), which is the run that proves the flagging pass and the +-- index creation survive real data rather than an empty table. Results: the +-- orphan flagged and the identity-backed row kept, the higher-id case variant +-- flagged, the check digit canonicalised, and a later duplicate INSERT refused +-- with `UNIQUE constraint failed: users.orcid` / `users.email` -- the COLUMN +-- name, which is what `isUniqueViolationOn` matches on. +-- +-- NOTE `wrangler d1 migrations apply` cannot do this replay, for a +-- PRE-EXISTING reason unrelated to this file: wrangler text-scans a migration +-- for `BEGIN TRANSACTION` / `COMMIT` and refuses one that "contains several +-- transactions" even when those words appear only inside a `--` comment, which +-- they do in 0021, 0031, 0063, 0064, 0071, 0075 and here. The check script +-- strips whole-line comments before handing the SQL over, and its catalogue +-- diff is what proves that strip changed nothing. -- (1) The flag. 0 = this row's identifiers are its own; 1 = at least one of -- them is claimed by another live row and this row is not the canonical @@ -58,13 +90,33 @@ ALTER TABLE users ADD COLUMN identity_conflict INTEGER NOT NULL DEFAULT 0; -- and the index. Every write path now uppercases it (services/identity.ts), -- which closes the future; this closes the past. -- --- The GLOB is deliberately narrow: it matches ONLY a bare iD ending in a +-- The predicate is deliberately narrow: it matches ONLY a bare iD ending in a -- lowercase `x`, so a row holding a full `https://orcid.org/...` URI (or -- anything else unexpected) is left exactly as it is rather than being -- uppercased into mojibake by a blanket UPPER(). +-- +-- SPELLED WITHOUT A LONG GLOB ON PURPOSE. The obvious way to write this is one +-- character-class GLOB per digit +-- (`'[0-9][0-9][0-9][0-9]-...-[0-9][0-9][0-9]x'`), and that is 79 characters. +-- **D1 caps a LIKE/GLOB pattern at 50 characters** and raises +-- `LIKE or GLOB pattern too complex: SQLITE_ERROR` above it. bun:sqlite does +-- NOT enforce that cap, so the long form passes every test in this repo and +-- aborts the real migration right here -- after statement 1 has already added +-- the column. Verified against `wrangler d1 execute --local`: a 50-character +-- pattern is accepted and a 51-character one is not. +-- +-- So the shape is checked with LENGTH/SUBSTR (no pattern at all) and the digits +-- with one 8-character GLOB. `SUBSTR(orcid, 19, 1) = 'x'` is case-SENSITIVE +-- because the column has no COLLATE and SQLite's default is BINARY -- which is +-- the whole point: an already-canonical `X` must not be rewritten. UPDATE users SET orcid = UPPER(orcid) - WHERE orcid GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]x'; + WHERE LENGTH(orcid) = 19 + AND SUBSTR(orcid, 19, 1) = 'x' + AND SUBSTR(orcid, 5, 1) = '-' + AND SUBSTR(orcid, 10, 1) = '-' + AND SUBSTR(orcid, 15, 1) = '-' + AND REPLACE(SUBSTR(orcid, 1, 18), '-', '') NOT GLOB '*[^0-9]*'; -- (3a) Flag non-canonical ORCID holders. -- diff --git a/package.json b/package.json index d465ac0fb..00e64dfdd 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "build": "bun build src/index.ts --outdir dist --target bun --minify && sed '1s|#!/usr/bin/env node|#!/usr/bin/env bun|' dist/index.js > dist/index.js.tmp && mv dist/index.js.tmp dist/index.js", "test": "bun test", "lint": "biome check src/", + "migrations:d1-check": "bun run scripts/d1-migration-check.ts", "lint:fix": "biome check --fix src/", "format": "biome format --write src/", "typecheck": "tsc --noEmit && tsc --noEmit -p backend", diff --git a/scripts/d1-migration-check.ts b/scripts/d1-migration-check.ts new file mode 100644 index 000000000..a6445f23e --- /dev/null +++ b/scripts/d1-migration-check.ts @@ -0,0 +1,268 @@ +#!/usr/bin/env bun +/** + * Replay every D1 migration through REAL D1 and diff the result against + * bun:sqlite (#1254 review; ADR 0043). + * + * WHY THIS EXISTS. Every migration test in this repo runs on bun:sqlite, and + * bun:sqlite is more permissive than the SQLite build D1 ships. Migration 0077 + * was written with one character-class GLOB per digit -- 79 characters -- which + * bun:sqlite executes happily and D1 rejects with + * `LIKE or GLOB pattern too complex: SQLITE_ERROR`. Every test was green and + * the deploy would have aborted mid-file, after the ALTER had already landed. + * + * D1's compile-time limits found so far, both verified with + * `wrangler d1 execute --local`: + * * LIKE/GLOB pattern: 50 characters (51 fails). + * * Compound SELECT terms: a 7-branch `UNION ALL` fails with + * `too many terms in compound SELECT`. + * There are more. The point of this script is not to enumerate them but to + * stop guessing: if D1 will not run it, this fails before a deploy does. + * + * WHAT IT DOES + * 1. Wipes the local D1 state and replays every migration in order through + * `wrangler d1 execute --local --file`, one file at a time, so a failure + * names the file that broke. + * 2. Builds the same schema with bun:sqlite and diffs the two catalogues: + * the object inventory (`sqlite_master` type + name) and every table's + * COLUMNS (`PRAGMA table_info`). + * + * The column comparison is deliberately structural rather than a diff of the + * stored DDL text, because the two SQLite builds legitimately disagree about + * that text and neither is wrong. `ALTER TABLE ... RENAME` regenerates the + * stored `CREATE` statement, and D1's newer SQLite drops the original comments + * and rewrites a renamed table's FK targets (`REFERENCES users_new(id)` -> + * `REFERENCES "users"(id)`) where bun:sqlite's older one leaves both alone. + * Migration 0026 documents that rewrite as the thing it relies on -- so a text + * diff here would fail forever on a difference that is not a defect. + * + * FULL-LINE COMMENTS ARE STRIPPED before the SQL reaches wrangler, and that is + * a workaround for wrangler, not for D1: wrangler scans the file text for + * `BEGIN TRANSACTION` / `COMMIT` and refuses "a file containing several + * transactions" -- including when those words appear only inside a `--` + * comment, which is the case in 0021, 0031, 0063, 0064, 0071, 0075 and 0077. + * Stripping only whole-line comments keeps every statement byte-identical, and + * the schema diff in step 2 is what proves it. + * + * USAGE + * bun run migrations:d1-check + * + * Roughly a minute (one wrangler invocation per migration), so it is a release + * check rather than a per-PR CI job. See AGENTS.md. + */ + +import { Database } from "bun:sqlite"; +import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +const REPO_ROOT = join(import.meta.dir, ".."); +const BACKEND = join(REPO_ROOT, "backend"); +const MIGRATIONS_DIR = join(BACKEND, "src/db/migrations"); +const WRANGLER = join(BACKEND, "node_modules/.bin/wrangler"); +const CONFIG = "wrangler-sccn.toml"; +const DB_NAME = "nemar-db"; +const LOCAL_STATE = join(BACKEND, ".wrangler/state/v3/d1"); + +function migrationFiles(): string[] { + return readdirSync(MIGRATIONS_DIR) + .filter((f) => f.endsWith(".sql")) + .sort(); +} + +/** Drop whole-line `--` comments; every statement is left byte-identical. */ +function stripFullLineComments(sql: string): string { + return sql + .split("\n") + .filter((line) => !/^\s*--/.test(line)) + .join("\n"); +} + +interface MasterRow { + type: string; + name: string; +} + +interface ColumnRow { + name: string; + type: string; + notnull: number; + dflt_value: string | null; + pk: number; +} + +/** + * Objects a migration created, as `type name`. + * + * `sqlite_autoindex_*` are unnamed constraint indexes SQLite invents, and + * `_cf_METADATA` / `d1_migrations` are D1's own bookkeeping tables -- none come + * from a migration file, so none belong in a cross-engine diff. + */ +function objectInventory(rows: MasterRow[]): string[] { + return rows + .filter( + (r) => + !r.name.startsWith("sqlite_autoindex_") && + r.name !== "_cf_METADATA" && + r.name !== "d1_migrations", + ) + .map((r) => `${r.type} ${r.name}`) + .sort(); +} + +/** One table's columns, flattened so a diff names the column that moved. */ +function columnSignature(cols: ColumnRow[]): string { + return cols + .map((c) => `${c.name}:${c.type}:notnull=${c.notnull}:default=${c.dflt_value ?? ""}:pk=${c.pk}`) + .join(" | "); +} + +function bunSqliteCatalogue(): { objects: string[]; columns: Map } { + const db = new Database(":memory:"); + for (const file of migrationFiles()) { + db.exec(readFileSync(join(MIGRATIONS_DIR, file), "utf-8")); + } + const master = db + .query("SELECT type, name FROM sqlite_master") + .all(); + const columns = new Map(); + for (const row of master) { + if (row.type !== "table" || row.name.startsWith("sqlite_")) continue; + const cols = db.query(`PRAGMA table_info("${row.name}")`).all(); + columns.set(row.name, columnSignature(cols)); + } + db.close(); + return { objects: objectInventory(master), columns }; +} + +async function wrangler(args: string[]): Promise<{ ok: boolean; output: string }> { + const proc = Bun.spawn({ + cmd: [WRANGLER, "d1", ...args], + cwd: BACKEND, + stdout: "pipe", + stderr: "pipe", + env: { ...process.env, WRANGLER_SEND_METRICS: "false" }, + }); + const [stdout, stderr] = await Promise.all([ + new Response(proc.stdout).text(), + new Response(proc.stderr).text(), + ]); + const code = await proc.exited; + return { ok: code === 0, output: `${stdout}\n${stderr}` }; +} + +async function main(): Promise { + if (!existsSync(WRANGLER)) { + console.error(`wrangler not found at ${WRANGLER}. Run 'bun install' in backend/ first.`); + process.exit(1); + } + + console.log("Wiping local D1 state..."); + rmSync(LOCAL_STATE, { recursive: true, force: true }); + + const tmp = mkdtempSync(join(tmpdir(), "nemar-d1-check-")); + const files = migrationFiles(); + console.log(`Replaying ${files.length} migrations through real D1...`); + + for (const file of files) { + const stripped = stripFullLineComments(readFileSync(join(MIGRATIONS_DIR, file), "utf-8")); + const path = join(tmp, file); + writeFileSync(path, stripped); + const res = await wrangler([ + "execute", + DB_NAME, + "-c", + CONFIG, + "--local", + "--file", + path, + "-y", + ]); + if (!res.ok) { + console.error(`\nFAILED on ${file}\n`); + console.error(res.output.trim()); + console.error( + "\nThis SQL runs on bun:sqlite and NOT on D1. Rewrite the statement; do not " + + "relax the check. See the header of 0077_identity_uniqueness.sql for the " + + "GLOB-length case.", + ); + rmSync(tmp, { recursive: true, force: true }); + process.exit(1); + } + process.stdout.write("."); + } + rmSync(tmp, { recursive: true, force: true }); + console.log("\nAll migrations applied on real D1."); + + // Cross-engine catalogue diff. A mismatch means the two SQLite builds + // disagreed about what the same files produce. + const bun = bunSqliteCatalogue(); + const tableNames = [...bun.columns.keys()].sort(); + + // One file: the object inventory, then `PRAGMA table_info` per table in a + // fixed order, so the result sets can be zipped back to table names. + const queryPath = join(tmpdir(), "nemar-d1-catalogue.sql"); + writeFileSync( + queryPath, + [ + "SELECT type, name FROM sqlite_master;", + ...tableNames.map((t) => `PRAGMA table_info("${t}");`), + ].join("\n"), + ); + const dump = await wrangler([ + "execute", + DB_NAME, + "-c", + CONFIG, + "--local", + "--file", + queryPath, + "--json", + "-y", + ]); + rmSync(queryPath, { force: true }); + if (!dump.ok) { + console.error("Could not read the resulting D1 catalogue:\n", dump.output.trim()); + process.exit(1); + } + const jsonStart = dump.output.indexOf("["); + const resultSets = JSON.parse(dump.output.slice(jsonStart)) as { results: unknown[] }[]; + if (resultSets.length !== tableNames.length + 1) { + console.error( + `Expected ${tableNames.length + 1} result sets from D1, got ${resultSets.length}.`, + ); + process.exit(1); + } + + let failed = false; + + const d1Objects = objectInventory(resultSets[0].results as MasterRow[]); + for (const o of bun.objects) { + if (!d1Objects.includes(o)) { + console.error(` only in bun:sqlite: ${o}`); + failed = true; + } + } + for (const o of d1Objects) { + if (!bun.objects.includes(o)) { + console.error(` only in D1: ${o}`); + failed = true; + } + } + + tableNames.forEach((table, i) => { + const d1Cols = columnSignature(resultSets[i + 1].results as ColumnRow[]); + const bunCols = bun.columns.get(table) as string; + if (d1Cols !== bunCols) { + console.error(` columns differ: ${table}\n bun: ${bunCols}\n d1 : ${d1Cols}`); + failed = true; + } + }); + + if (failed) { + console.error("\nCatalogue MISMATCH between real D1 and bun:sqlite."); + process.exit(1); + } + console.log(`Catalogue matches bun:sqlite (${bun.objects.length} objects, ${tableNames.length} tables). OK.`); +} + +await main(); From e100e1c2e3035df1923ea338a7e79b3e8d9b36cb Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 18:50:46 -0700 Subject: [PATCH 12/18] fix: find an account whatever case it was stored in --- backend/src/routes/auth.ts | 26 ++-- backend/src/services/identity.ts | 31 +++-- backend/test/identity-refusals-route.test.ts | 139 +++++++++++++++++++ 3 files changed, 174 insertions(+), 22 deletions(-) diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index 313f7aa84..4449bee3d 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -25,11 +25,11 @@ import { import { validateGitHubUsername } from "../services/github"; import { getDatasetsToken } from "../services/github-auth"; import { + emailFieldSchema, findEmailHolder, findOrcidHolder, identityRefusal, isUniqueViolationOn, - normalizeEmail, normalizeGithubHandle, normalizeOrcid, } from "../services/identity"; @@ -192,10 +192,7 @@ const signupSchema = z.object({ // loses a pasted leading "@" instead of failing the format check over it. // `preprocess` rather than `.transform()` because the rules have to run // before `.email()`/`.regex()`, not after them. - email: z.preprocess( - (v) => (typeof v === "string" ? normalizeEmail(v) : v), - z.string().email("Invalid email address"), - ), + email: emailFieldSchema, password: z.string().min(12, "Password must be at least 12 characters").max(128), github_username: z.preprocess( (v) => (typeof v === "string" ? normalizeGithubHandle(v) : v), @@ -873,7 +870,10 @@ authRoutes.post("/login", zValidator("json", loginSchema), async (c) => { * POST /auth/resend-verification - Resend verification email */ const resendSchema = z.object({ - email: z.string().email(), + // Normalised before validation, and looked up NOCASE below (ADR 0043): + // signup stores the address lowercased, so an exact-case lookup would miss + // every LEGACY row whose address was stored exactly as typed. + email: emailFieldSchema, }); authRoutes.post("/resend-verification", zValidator("json", resendSchema), async (c) => { @@ -882,7 +882,9 @@ authRoutes.post("/resend-verification", zValidator("json", resendSchema), async // Find user const user = await db - .prepare("SELECT id, username, status FROM users WHERE email = ? AND deleted_at IS NULL") + .prepare( + "SELECT id, username, status FROM users WHERE email = ? COLLATE NOCASE AND deleted_at IS NULL", + ) .bind(email) .first<{ id: number; username: string; status: string }>(); @@ -937,7 +939,7 @@ authRoutes.post("/resend-verification", zValidator("json", resendSchema), async // ============================================================================ const retrieveKeySchema = z.object({ - email: z.string().email(), + email: emailFieldSchema, password: z.string().min(1, "Password is required"), }); @@ -957,7 +959,7 @@ authRoutes.post("/retrieve-key", zValidator("json", retrieveKeySchema), async (c // Find user by email const user = await db .prepare( - "SELECT id, username, email, password_hash, status FROM users WHERE email = ? AND deleted_at IS NULL", + "SELECT id, username, email, password_hash, status FROM users WHERE email = ? COLLATE NOCASE AND deleted_at IS NULL", ) .bind(email) .first<{ @@ -1063,7 +1065,7 @@ authRoutes.post("/retrieve-key", zValidator("json", retrieveKeySchema), async (c // ============================================================================ const regenRequestSchema = z.object({ - email: z.string().email(), + email: emailFieldSchema, }); /** @@ -1076,7 +1078,9 @@ authRoutes.post("/request-key-regeneration", zValidator("json", regenRequestSche // Find user const user = await db - .prepare("SELECT id, username, email, status FROM users WHERE email = ? AND deleted_at IS NULL") + .prepare( + "SELECT id, username, email, status FROM users WHERE email = ? COLLATE NOCASE AND deleted_at IS NULL", + ) .bind(email) .first<{ id: number; username: string; email: string; status: string }>(); diff --git a/backend/src/services/identity.ts b/backend/src/services/identity.ts index 7cb1da9a5..dd8aca069 100644 --- a/backend/src/services/identity.ts +++ b/backend/src/services/identity.ts @@ -74,24 +74,33 @@ export const emailFieldSchema = z.preprocess( ); /** - * Canonical stored form of an ORCID iD, or `null` when the input holds no - * well-formed iD. + * Canonical stored form of an ORCID iD, or `null` when the input is not one. * - * Accepts a bare iD or a full `https://orcid.org/` URI. The check digit is - * uppercased: `X` is the canonical spelling of a non-numeric checksum, and the - * unique index compares `users.orcid` exactly, so a lowercase `x` would read - * as a different person's iD. Migration 0077 canonicalises the existing rows - * for the same reason. + * Accepts EXACTLY two shapes, both anchored at BOTH ends: a bare iD, or an + * `orcid.org` / `sandbox.orcid.org` URI wrapping one. Anchoring only the tail + * -- which this did until the #1254 review -- accepts + * `garbage0000-0002-1825-0097` and "normalises" it into a valid iD, so a + * fat-fingered paste would silently claim somebody else's identifier and then + * pass the uniqueness checks as that person. + * + * The check digit is uppercased: `X` is the canonical spelling of a + * non-numeric checksum, and the unique index compares `users.orcid` exactly, so + * a lowercase `x` would read as a different person's iD. Migration 0077 + * canonicalises the existing rows for the same reason. * * Deliberately NOT `orcid-auth.ts`'s `normalizeOrcidId`: that one matches only * an uppercase `X` and would reject `...353x` outright rather than fixing it. */ +const BARE_ORCID_RE = /^(\d{4}-\d{4}-\d{4}-\d{3}[\dXx])$/; +const ORCID_URI_RE = /^https?:\/\/(?:sandbox\.)?orcid\.org\/(\d{4}-\d{4}-\d{4}-\d{3}[\dXx])\/?$/i; + export function normalizeOrcid(raw: string | null | undefined): string | null { if (!raw) return null; - const upper = raw.trim().toUpperCase(); - const match = upper.match(/(\d{4}-\d{4}-\d{4}-\d{3}[\dX])$/); - if (!match) return null; - return ORCID_ID_PATTERN.test(match[1]) ? match[1] : null; + const trimmed = raw.trim(); + const id = (BARE_ORCID_RE.exec(trimmed) ?? ORCID_URI_RE.exec(trimmed))?.[1]; + if (!id) return null; + const canonical = id.toUpperCase(); + return ORCID_ID_PATTERN.test(canonical) ? canonical : null; } /** diff --git a/backend/test/identity-refusals-route.test.ts b/backend/test/identity-refusals-route.test.ts index 887066c85..f3ab67924 100644 --- a/backend/test/identity-refusals-route.test.ts +++ b/backend/test/identity-refusals-route.test.ts @@ -29,6 +29,7 @@ import { authRoutes } from "../src/routes/auth"; import { authOrcidRoutes } from "../src/routes/auth-orcid"; import { authWebRoutes } from "../src/routes/auth-web"; import { PENDING_COOKIE_NAME, encodeState, signPending } from "../src/services/orcid-auth"; +import { hashPassword } from "../src/services/password"; import { issueSession } from "../src/services/web-session"; import type { Bindings, Variables } from "../src/types/bindings"; import { freshDb, realD1 } from "./helpers/d1"; @@ -296,6 +297,144 @@ describe("CLI signup normalises what it stores", () => { expect(res.status).toBe(201); expect(userByEmail("newuser@example.org")?.orcid).toBe(OTHER_ORCID); }); + + test("a garbage-prefixed iD is rejected, not silently 'normalised'", async () => { + // `normalizeOrcid` anchors BOTH ends. Anchoring only the tail -- which it + // did until the #1254 review -- turns `garbage0000-...-0097` into a valid + // iD and stores it as if the user had typed one, so a fat-fingered paste + // silently claims somebody else's identifier. + const res = await signup({ orcid: `garbage${OTHER_ORCID}` }); + expect(res.status).toBe(400); + expect(userByEmail("newuser@example.org")).toBeNull(); + }); +}); + +describe("an address is found whatever case it was stored or typed in", () => { + // The regression this pins: signup started lowercasing the stored address, + // while `retrieve-key` / `resend-verification` / `request-key-regeneration` + // still looked it up exact-case. Someone who signed up as + // `John.Smith@gmail.com` and typed it that way got "Invalid email or + // password" -- a dead end with no way to tell it from a wrong password. + const MIXED = "John.Smith@Example.ORG"; + const OTHER_CASE = "JOHN.smith@example.org"; + const PASSWORD = "Correct-Horse-Battery-9"; + + /** + * A LEGACY row: stored with its address exactly as typed, the way every row + * created before #1254 was. + * + * This is what makes the NOCASE lookups load-bearing, and it is why these + * tests do NOT go through signup. Normalising the REQUEST is not enough on + * its own: once the request is lowercased, an exact-case lookup still + * matches every row this phase creates, because those are lowercased too. + * It is the ~600 rows already in the catalog that an exact-case lookup + * cannot find. + */ + async function seedLegacy(): Promise { + db.run( + `INSERT INTO users (username, email, password_hash, github_username, status, + signup_source, email_verified, orcid, orcid_verified, + verification_token) + VALUES ('legacy', ?, ?, 'legacy-gh', 'verified', 'cli', 1, ?, 0, 'seed-token')`, + [MIXED, await hashPassword(PASSWORD), OTHER_ORCID], + ); + } + + function storedToken(): string | null { + return ( + db + .query<{ verification_token: string | null }, [string]>( + "SELECT verification_token FROM users WHERE email = ?", + ) + .get(MIXED)?.verification_token ?? null + ); + } + + /** + * Both notification routes answer vaguely on purpose (no account + * enumeration), so a status code cannot tell found from not-found -- and + * `resend-verification` sends its email OUTSIDE a try/catch, so the dev + * delivery fence can turn a successful lookup into a 500. The honest + * observable is the side effect that happens BEFORE the send: each rotates + * `verification_token`, and only for a row it actually found. + */ + async function post(path: string, email = OTHER_CASE): Promise { + await app + .request( + path, + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email }), + }, + env(), + ) + .catch(() => undefined); + } + + test("signup stores a mixed-case address lowercased", async () => { + const res = await signup({ + email: MIXED, + username: "jsmith", + github_username: "jsmith", + }); + expect(res.status).toBe(201); + expect(userByEmail("john.smith@example.org")).toBeTruthy(); + }); + + test("retrieve-key finds a legacy mixed-case row typed in another case", async () => { + await seedLegacy(); + const res = await app.request( + "/auth/retrieve-key", + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email: OTHER_CASE, password: PASSWORD }), + }, + env(), + ); + // 200 (key minted) or 409 (key already issued) both mean the row was + // found. 401 is the bug: "Invalid email or password" for a correct + // password, with no way for the user to tell which half was wrong. + expect([200, 409]).toContain(res.status); + }); + + test("a wrong password on a found legacy row is still refused", async () => { + // The guard against a lookup so loose that it stops proving anything. + await seedLegacy(); + const res = await app.request( + "/auth/retrieve-key", + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email: OTHER_CASE, password: "not-the-password" }), + }, + env(), + ); + expect(res.status).toBe(401); + }); + + test("resend-verification finds a legacy row typed in another case", async () => { + await seedLegacy(); + db.run("UPDATE users SET status = 'pending' WHERE email = ?", [MIXED]); + await post("/auth/resend-verification"); + expect(storedToken()).not.toBe("seed-token"); + expect(storedToken()).toBeTruthy(); + }); + + test("key regeneration finds a legacy row typed in another case", async () => { + await seedLegacy(); + await post("/auth/request-key-regeneration"); + expect(storedToken()).not.toBe("seed-token"); + }); + + test("an address matching NO account rotates nothing", async () => { + // The guard against a rotation assertion that would pass on any input. + await seedLegacy(); + db.run("UPDATE users SET status = 'pending' WHERE email = ?", [MIXED]); + await post("/auth/resend-verification", "nobody@example.org"); + expect(storedToken()).toBe("seed-token"); + }); }); // -------------------------------------------------------------------------- From cbfbb7cb23ba4ff371d8956582ed063e313578e8 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 18:55:52 -0700 Subject: [PATCH 13/18] fix: map identity races to their typed refusals --- backend/src/routes/admin/user-duplicates.ts | 91 +++++++++++++++---- backend/src/routes/admin/users.ts | 10 +-- backend/src/routes/auth-orcid.ts | 97 +++++++++++++++++---- backend/src/routes/auth-web.ts | 22 ++--- backend/src/routes/auth.ts | 38 ++++---- backend/src/services/identity.ts | 50 +++++++++-- shared/contract/identity.ts | 12 ++- 7 files changed, 235 insertions(+), 85 deletions(-) diff --git a/backend/src/routes/admin/user-duplicates.ts b/backend/src/routes/admin/user-duplicates.ts index 5b74e4992..b8d765451 100644 --- a/backend/src/routes/admin/user-duplicates.ts +++ b/backend/src/routes/admin/user-duplicates.ts @@ -34,7 +34,7 @@ import type { IdentityKind, } from "../../../../shared/contract/identity.js"; import { auditLogStatement } from "../../db/audit-log"; -import { identityRefusal } from "../../services/identity"; +import { identityRefusal, isUniqueViolationOn } from "../../services/identity"; import type { AdminRouter } from "./shared"; /** @@ -122,11 +122,28 @@ function keyFor(row: CandidateRow, kind: IdentityKind): string | null { * route test against a fixture the migration itself flagged. */ function canonicalIdOf(rows: CandidateRow[], kind: IdentityKind): number { + // AN UNFLAGGED ROW ALWAYS WINS, whatever the per-identifier rule says. + // + // The flag is the fact that decides this: a flagged row is invisible to the + // partial unique indexes, so it does NOT hold the identifier no matter how + // low its id is. On the compound shape -- one pair colliding on BOTH iD and + // email, where the ORCID pass flagged the LOWER id because the identity row + // sits on the higher one -- "lowest id" would mark the flagged row canonical + // and send an operator to fix the wrong account. + // + // Migration 0077's email pass encodes the same precedence in SQL + // (`m.identity_conflict = 0` inside its MIN), which is why this has to. + const unflagged = rows.filter((r) => r.identity_conflict === 0); + const pool = unflagged.length > 0 ? unflagged : rows; + if (kind === "orcid") { - const backed = rows.find((r) => r.has_oauth_identity === 1); + const backed = pool.find((r) => r.has_oauth_identity === 1); if (backed) return backed.id; } - return rows.reduce((min, r) => (r.id < min ? r.id : min), rows[0].id); + // Lowest id, over the unflagged rows when there are any and over the whole + // group when a migration flagged every row in it (possible, and then there + // is no right answer -- report the migration's rule rather than nothing). + return pool.reduce((min, r) => (r.id < min ? r.id : min), pool[0].id); } function toAccount(row: CandidateRow, canonicalId: number): DuplicateAccount { @@ -175,9 +192,17 @@ export function registerUserDuplicateRoutes(admin: AdminRouter): void { /** * GET /admin/users/duplicates - live accounts sharing an identifier. * - * Static segment, so Hono's RegExpRouter matches this before - * `GET /users/:username` (static outranks param) -- the same property - * `POST /users/backfill-names` already relies on. + * REGISTRATION ORDER IS LOAD-BEARING. `GET /users/:username` also matches + * `/users/duplicates`, and this route only wins because + * `registerUserDuplicateRoutes` runs BEFORE `registerUsersRoutes` in + * admin/index.ts. Do not reorder them. + * + * There is no "static outranks param" rule to fall back on here: this + * router's path set makes Hono's RegExpRouter throw `UnsupportedPathError`, + * so it falls back to a router that runs every matching handler in + * registration order and takes the first response. Registered second, this + * route would never be reached -- the username lookup would 404 first, which + * is exactly what happened before the order was fixed. */ admin.get("/users/duplicates", async (c) => { try { @@ -257,18 +282,48 @@ export function registerUserDuplicateRoutes(admin: AdminRouter): void { return c.json({ ok: true, id, cleared: false }); } - await c.env.DB.batch([ - c.env.DB.prepare( - "UPDATE users SET identity_conflict = 0, updated_at = datetime('now') WHERE id = ?", - ).bind(id), - auditLogStatement(c.env.DB, { - userId: c.get("user")?.id ?? null, - action: "identity_conflict_cleared", - resourceType: "user", - resourceId: String(id), - details: JSON.stringify({ email: target.email, orcid: target.orcid }), - }), - ]); + // Clearing the flag is what puts the row back INTO the partial unique + // indexes, so this UPDATE is itself a uniqueness-checked write: a + // collision that appeared between the SELECT above and here fails right + // now. That is the same answer the pre-check gives, so it gets the same + // 409 rather than a generic 500 -- an operator who retries then sees the + // colliding rows named instead of an opaque failure. + try { + await c.env.DB.batch([ + c.env.DB.prepare( + "UPDATE users SET identity_conflict = 0, updated_at = datetime('now') WHERE id = ?", + ).bind(id), + auditLogStatement(c.env.DB, { + userId: c.get("user")?.id ?? null, + action: "identity_conflict_cleared", + resourceType: "user", + resourceId: String(id), + details: JSON.stringify({ email: target.email, orcid: target.orcid }), + }), + ]); + } catch (writeErr) { + if (isUniqueViolationOn(writeErr, "orcid") || isUniqueViolationOn(writeErr, "email")) { + // Re-read so the response names who it collided with, rather than + // reporting the empty list the stale pre-check produced. + const fresh = await c.env.DB.prepare(DUPLICATE_CANDIDATES_SQL).all(); + const races = buildDuplicateGroups(fresh.results ?? []) + .filter((g) => g.accounts.some((a) => a.id === id)) + .map((g) => ({ + kind: g.kind, + value: g.value, + user_ids: g.accounts.filter((a) => a.id !== id).map((a) => a.id), + })); + return c.json( + { + error: "identity_conflict_remains", + ...identityRefusal("identity_conflict_remains"), + colliding: races, + }, + 409, + ); + } + throw writeErr; + } return c.json({ ok: true, id, cleared: true }); } catch (err) { console.error("[admin/user-duplicates] clear failed", err); diff --git a/backend/src/routes/admin/users.ts b/backend/src/routes/admin/users.ts index d5060985c..d12d87d65 100644 --- a/backend/src/routes/admin/users.ts +++ b/backend/src/routes/admin/users.ts @@ -37,7 +37,7 @@ import { isNonProductionEnv } from "../../services/environment"; import { removeCollaborator } from "../../services/github"; import { getDatasetsToken } from "../../services/github-auth"; import { revokeUserIamAccess } from "../../services/iam"; -import { normalizeGithubHandle, normalizeOrcid } from "../../services/identity"; +import { emailFieldSchema, normalizeGithubHandle, normalizeOrcid } from "../../services/identity"; import { errorMessage } from "../../services/repo-metadata"; import { type Bindings, type Variables, isDemotion, parseRole } from "../../types/bindings"; import type { AdminRouter } from "./shared"; @@ -1514,11 +1514,9 @@ export function registerUsersRoutes(admin: AdminRouter): void { // --------------------------------------------------------------------------- const seedWebUserSchema = z.object({ - email: z - .string() - .email() - .max(320) - .transform((e) => e.trim().toLowerCase()), + // Shared field: normalises BEFORE validating, so a pasted address with + // surrounding whitespace seeds a row rather than 400ing (ADR 0043). + email: emailFieldSchema, // Optional. Defaults to 'pending' to mirror what the legacy // INSERT-OR-IGNORE produced. The cookie-auth tests (#572) seed a // signed-in tier directly; since ADR 0040 phase 2 the cookie path in diff --git a/backend/src/routes/auth-orcid.ts b/backend/src/routes/auth-orcid.ts index 562b01e2e..3eee93ea5 100644 --- a/backend/src/routes/auth-orcid.ts +++ b/backend/src/routes/auth-orcid.ts @@ -50,6 +50,8 @@ import { findEmailHolder, findOrcidHolder, identityRefusal, + isOrcidIdentityUniqueViolation, + isUniqueViolationOn, normalizeEmail, normalizeOrcid, } from "../services/identity"; @@ -185,8 +187,13 @@ async function linkIdentity( // One D1 batch == one implicit transaction, so the identity insert and the // users reconcile land together. Without it a mid-sequence failure could // leave an identity row with a stale users.orcid_verified (or vice versa). - // A UNIQUE(provider, provider_subject) violation from a concurrent link - // throws here; the caller's try/catch turns it into a clean failure. + // + // A concurrent link that wins the race throws here -- on + // UNIQUE(provider, provider_subject), or since 0077 on `users.orcid`. The + // CALLER is what turns that into a typed refusal (`orcid_in_use`); this + // function only guarantees the batch is atomic. It used to say the caller + // produced "a clean typed failure", which was not true: the callback's + // catch-all mapped it to the generic `orcid_error` redirect. await env.DB.batch([ env.DB.prepare( `INSERT INTO oauth_identities (user_id, provider, provider_subject, provider_email, display_name, last_login_at) @@ -266,6 +273,20 @@ async function refreshUserName(env: Bindings, userId: number, orcid: string): Pr } } +/** + * Whether a write failed because another account claimed the iD in the moment + * between the check and the write. + * + * Both spellings count: `users.orcid` (0077's partial unique index) and + * `oauth_identities.provider_subject` (0050's). They are two constraints over + * the same fact, and which one fires depends only on which statement of the + * batch got there first, so a caller that handled one and not the other would + * return a typed refusal or a generic 500 at random. + */ +function claimedByAnother(err: unknown): boolean { + return isUniqueViolationOn(err, "orcid") || isOrcidIdentityUniqueViolation(err); +} + function clientIp(c: { req: { header: (k: string) => string | undefined } }): string | null { return ( c.req.header("CF-Connecting-IP") || @@ -409,11 +430,21 @@ authOrcidRoutes.get("/orcid/callback", webSessionMiddleware, async (c) => { if (decideSecondOrcidOutcome(state.mode) === "refuse") { return fail("orcid_already_have", state.next); } - await relinkIdentity(c.env, webUser.id, mine.provider_subject, orcid, name); + try { + await relinkIdentity(c.env, webUser.id, mine.provider_subject, orcid, name); + } catch (err) { + if (claimedByAnother(err)) return fail("orcid_in_use", state.next); + throw err; + } afterResponse(c, refreshUserName(c.env, webUser.id, orcid)); return redirect(`${frontend}${state.next}`, [clearState]); } - await linkIdentity(c.env, webUser.id, orcid, name); + try { + await linkIdentity(c.env, webUser.id, orcid, name); + } catch (err) { + if (claimedByAnother(err)) return fail("orcid_in_use", state.next); + throw err; + } afterResponse(c, refreshUserName(c.env, webUser.id, orcid)); return redirect(`${frontend}${state.next}`, [clearState]); } @@ -550,13 +581,32 @@ authOrcidRoutes.post("/orcid/finalize", zValidator("json", finalizeSchema), asyn // upload grant, `service_access`, has exactly one writer and it is admin // approval (phase 1) — never this route, which is why `approved_at` is // NULL rather than a sign-up timestamp. - const insert = await c.env.DB.prepare( - `INSERT INTO users (email, orcid, orcid_verified, status, email_verified, signup_source, - affiliation, city, country, approved_at, service_access) - VALUES (?, ?, 1, 'pending', 0, 'web', ?, ?, ?, NULL, 0)`, - ) - .bind(email, orcid, affiliation || null, city, country) - .run(); + // A concurrent sign-up can claim either identifier between the checks + // above and this INSERT, and 0077's partial unique indexes are what stop + // the second row. Map that to the SAME typed refusal the checks return: + // the loser of a race is in exactly the situation the pre-check describes, + // and a generic 500 "Sign-up failed" tells them nothing and invites a + // retry that will fail identically. + let insert: D1Result; + try { + insert = await c.env.DB.prepare( + `INSERT INTO users (email, orcid, orcid_verified, status, email_verified, signup_source, + affiliation, city, country, approved_at, service_access) + VALUES (?, ?, 1, 'pending', 0, 'web', ?, ?, ?, NULL, 0)`, + ) + .bind(email, orcid, affiliation || null, city, country) + .run(); + } catch (insertErr) { + if (isUniqueViolationOn(insertErr, "email")) { + c.header("Set-Cookie", clearPending); + return c.json({ error: "email_in_use", ...identityRefusal("email_in_use") }, 409); + } + if (isUniqueViolationOn(insertErr, "orcid")) { + c.header("Set-Cookie", clearPending); + return c.json({ error: "orcid_in_use", ...identityRefusal("orcid_in_use") }, 409); + } + throw insertErr; + } const userId = insert.meta?.last_row_id; if (!userId) { console.error("[auth-orcid] finalize: user insert returned no row id"); @@ -576,7 +626,9 @@ authOrcidRoutes.post("/orcid/finalize", zValidator("json", finalizeSchema), asyn .bind(userId, orcid, pending.name) .run(); } catch (identErr) { - console.error("[auth-orcid] finalize: identity insert failed; rolling back user", identErr); + // The users row has already committed, so it MUST be removed either way + // -- an account with an email and no usable ORCID login is a dead end + // the user cannot fix. What differs is what we then tell them. await c.env.DB.prepare("DELETE FROM users WHERE id = ?") .bind(userId) .run() @@ -584,10 +636,25 @@ authOrcidRoutes.post("/orcid/finalize", zValidator("json", finalizeSchema), asyn console.error("[auth-orcid] finalize: failed to roll back orphan user", delErr), ); c.header("Set-Cookie", clearPending); - return c.json( - { error: "orcid_already_linked", ...identityRefusal("orcid_already_linked") }, - 409, + + // ONLY the identity UNIQUE means somebody else claimed the iD. Reporting + // `orcid_already_linked` for any failure -- a D1 outage, a schema error + // -- sends the user to go unlink an iD from an account that does not + // exist, and hides the real fault behind a 409 nobody investigates. + if (isOrcidIdentityUniqueViolation(identErr)) { + console.error( + `[auth-orcid] finalize: iD ${orcid} was claimed concurrently; rolled the user row back`, + ); + return c.json( + { error: "orcid_already_linked", ...identityRefusal("orcid_already_linked") }, + 409, + ); + } + console.error( + "[auth-orcid] finalize: identity insert failed for a NON-uniqueness reason; rolled the user row back", + identErr, ); + return c.json({ error: "Sign-up failed" }, 500); } const { cookieIdRaw, maxAgeSeconds } = await issueSession( diff --git a/backend/src/routes/auth-web.ts b/backend/src/routes/auth-web.ts index 83eab17b2..7a556ee54 100644 --- a/backend/src/routes/auth-web.ts +++ b/backend/src/routes/auth-web.ts @@ -815,10 +815,7 @@ authWebRoutes.patch( .bind(patch.github_username, webUser.id) .first<{ id: number }>(); if (dup) { - return c.json( - { error: "github_in_use", message: "GitHub account already linked to another user" }, - 409, - ); + return c.json({ error: "github_in_use", ...identityRefusal("github_in_use") }, 409); } // #1052: three answers, not two. A 5xx or a transport failure used to @@ -862,10 +859,7 @@ authWebRoutes.patch( .bind(githubUser.login, webUser.id) .first<{ id: number }>(); if (canonicalDup) { - return c.json( - { error: "github_in_use", message: "GitHub account already linked to another user" }, - 409, - ); + return c.json({ error: "github_in_use", ...identityRefusal("github_in_use") }, 409); } } patch.github_username = githubUser.login; @@ -941,12 +935,12 @@ authWebRoutes.patch( // concurrent PATCHes claiming the same free handle both pass the // pre-check, and the loser's UPDATE hits idx_users_github (0012, // COLLATE NOCASE). Same net CLI signup carries in auth.ts. - const msg = String(err); - if (msg.includes("UNIQUE constraint failed") && msg.includes("users.github_username")) { - return c.json( - { error: "github_in_use", message: "GitHub account already linked to another user" }, - 409, - ); + // Column-scoped through the shared helper (ADR 0043), which parses the + // failing columns out of the message rather than substring-matching + // them: `users.github_username` cannot be confused with a neighbour, and + // the same check reads identically at every call site. + if (isUniqueViolationOn(err, "github_username")) { + return c.json({ error: "github_in_use", ...identityRefusal("github_in_use") }, 409); } // The same window for `username`, and the same answer. This one is // reachable ONLY as a race: the pre-check above is COLLATE NOCASE while diff --git a/backend/src/routes/auth.ts b/backend/src/routes/auth.ts index 4449bee3d..c1526a4fb 100644 --- a/backend/src/routes/auth.ts +++ b/backend/src/routes/auth.ts @@ -27,6 +27,7 @@ import { getDatasetsToken } from "../services/github-auth"; import { emailFieldSchema, findEmailHolder, + findGithubHolder, findOrcidHolder, identityRefusal, isUniqueViolationOn, @@ -263,19 +264,22 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { ); } - // NOTE: the username / github_username de-dup checks below deliberately do - // NOT filter `deleted_at IS NULL` — they must see ALL rows, including - // tombstones, to honor the table-wide UNIQUE constraints. Re-signup with a - // deleted user's old email/username/github is enabled by the tombstone - // MASKING (email -> deleted+@deleted.invalid, username/github -> NULL), - // which frees those values, NOT by excluding deleted rows here. See the - // DELETE /admin/users/by-id/:id tombstone + migration 0037. + // NOTE on `deleted_at`. The USERNAME check below deliberately does NOT + // filter `deleted_at IS NULL`: it mirrors a table-wide UNIQUE, so it must + // see tombstones too. Re-signup with a deleted user's old values is + // enabled by the tombstone MASKING (email -> deleted+@deleted.invalid, + // username/github/orcid -> NULL), which frees them, NOT by excluding + // deleted rows here. See DELETE /admin/users/by-id/:id + migration 0037. // - // The email and ORCID checks are the exception and are live-rows-only, - // because the constraints they mirror are the PARTIAL indexes from 0077 - // (`deleted_at IS NULL AND identity_conflict = 0`) rather than table-wide - // ones. Masking still does the freeing; the predicate just matches the - // index the write will actually hit. + // The email, ORCID and GitHub checks go through services/identity.ts and + // are live-rows-only. For email and ORCID that is required: the + // constraints they mirror are 0077's PARTIAL indexes + // (`deleted_at IS NULL AND identity_conflict = 0`), so a live-only + // predicate is what matches the index the write will actually hit. For + // GitHub the predicate makes no difference either way -- the tombstone + // NULLs `github_username`, so no tombstone can hold a handle to collide + // with -- and it uses the same helper for symmetry, so all three + // identifiers are asked the same question in the same place. // Check if username already exists const existingUsername = await db @@ -316,10 +320,7 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { // API call and lets the request 409 even when the GitHub user happens // not to resolve (e.g., user was renamed/deleted on GitHub after // signing up here). - const directDupGithub = await db - .prepare("SELECT id FROM users WHERE github_username = ? COLLATE NOCASE") - .bind(github_username) - .first(); + const directDupGithub = await findGithubHolder(db, github_username); if (directDupGithub) { return c.json( { @@ -364,10 +365,7 @@ authRoutes.post("/signup", zValidator("json", signupSchema), async (c) => { // Re-check with the canonical login when GitHub normalized the case. // Catches the "Octocat" stored vs "octocat" submitted shape. if (githubUser.login.toLowerCase() !== github_username.toLowerCase()) { - const canonicalDup = await db - .prepare("SELECT id FROM users WHERE github_username = ? COLLATE NOCASE") - .bind(githubUser.login) - .first(); + const canonicalDup = await findGithubHolder(db, githubUser.login); if (canonicalDup) { return c.json( { diff --git a/backend/src/services/identity.ts b/backend/src/services/identity.ts index dd8aca069..6af6d1842 100644 --- a/backend/src/services/identity.ts +++ b/backend/src/services/identity.ts @@ -129,23 +129,57 @@ export interface IdentityHolder { username: string | null; } +/** + * The `table.column` names a caught UNIQUE violation actually names, or `[]` + * when the error is not one. + * + * SQLite spells the failure as + * `UNIQUE constraint failed: .[,
.]*`, + * and names the COLUMNS even when the violated index is a named partial one + * (verified against 0077's two indexes on real D1, where the message reads + * `UNIQUE constraint failed: users.orcid: SQLITE_CONSTRAINT (extended: ...)`). + * The capture stops at the next `:` so that D1 suffix is not swallowed. + * + * Parsed into an exact list rather than substring-matched, because substring + * matching is wrong in a way that is easy to miss: `users.email` is a + * substring of `users.email_verified`, so a violation on the latter would be + * reported to a user as "that address is taken". + */ +export function uniqueViolationColumns(err: unknown): string[] { + const msg = err instanceof Error ? err.message : String(err); + const match = /UNIQUE constraint failed:\s*([^\n:]+)/.exec(msg); + if (!match) return []; + return match[1] + .split(",") + .map((c) => c.trim()) + .filter((c) => c.length > 0); +} + /** * Whether a caught D1/SQLite error is a UNIQUE violation on one of the user * identifier columns. * - * SQLite names the COLUMN in the message even when the violated index is a - * named partial one (verified against 0077's two indexes), so - * `UNIQUE constraint failed: users.email` covers both the 0026 table-level - * constraint and `idx_users_email_live_unique`. Column-scoped on purpose: a - * UNIQUE hit on some other column must not be reported to a user as "that - * address is taken". + * Column-scoped on purpose: a UNIQUE hit on some other column must not be + * reported to a user as "that address is taken". */ export function isUniqueViolationOn( err: unknown, column: "email" | "orcid" | "username" | "github_username", ): boolean { - const msg = err instanceof Error ? err.message : String(err); - return msg.includes("UNIQUE constraint failed") && msg.includes(`users.${column}`); + return uniqueViolationColumns(err).includes(`users.${column}`); +} + +/** + * Whether a caught error is the `UNIQUE(provider, provider_subject)` violation + * on `oauth_identities` -- i.e. another account claimed this iD's identity row + * in the moment between a check and an insert. + * + * Separate from {@link isUniqueViolationOn} because it is a different table: + * that one only ever answers about `users`, and widening it would make every + * call site's meaning depend on a string. + */ +export function isOrcidIdentityUniqueViolation(err: unknown): boolean { + return uniqueViolationColumns(err).includes("oauth_identities.provider_subject"); } // --------------------------------------------------------------------------- diff --git a/shared/contract/identity.ts b/shared/contract/identity.ts index 04590a064..8f8f7beb2 100644 --- a/shared/contract/identity.ts +++ b/shared/contract/identity.ts @@ -22,10 +22,14 @@ import { z } from "zod"; * - `orcid_in_use` -- the iD is already on a live account through * `users.orcid`. This is the case nothing checked before #1254, and it is * how production rows 42/43 came to exist. - * - `orcid_already_linked` -- the iD already has an `oauth_identities` row. - * Kept distinct from `orcid_in_use` because it is the pre-existing code the - * ORCID finalize route has always returned and the website already handles - * it; the two mean the same thing to a user and carry the same message. + * - `orcid_already_linked` -- **DEPRECATED ALIAS for `orcid_in_use`.** It means + * the same thing to a user and carries the same message; the only difference + * is which constraint noticed (`oauth_identities` rather than `users.orcid`), + * which is not a distinction anyone can act on. It survives only because it + * is the code the ORCID finalize route has always returned and the website + * switches on it today. Once nemarOrg/website#305 has the site treating + * `orcid_in_use` as the single code, finalize stops emitting this one and it + * can be deleted here. Do not add new emitters. * - `orcid_linked_other` -- (redirect flows only) the finished iD backs a * DIFFERENT account than the session's. Pre-existing. * - `email_in_use` -- the address is on a live account, compared From d21bd7a6e5d92aa6f65f42d4d6dfe67f39a72083 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 18:55:57 -0700 Subject: [PATCH 14/18] docs: correct ADR 0043 and the router comment --- .../decisions/0043-one-person-one-account.md | 24 +++++++++++++++---- AGENTS.md | 16 ++++++++++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.context/decisions/0043-one-person-one-account.md b/.context/decisions/0043-one-person-one-account.md index e79b0a87d..a913253f7 100644 --- a/.context/decisions/0043-one-person-one-account.md +++ b/.context/decisions/0043-one-person-one-account.md @@ -5,11 +5,13 @@ **Owner:** Seyed Yahya Shirazi > Numbering note: epic #1250 runs phases in parallel, and this one (phase 4, -> #1254) lands alongside phase 3 (#1253), which owns 0042. +> #1254) lands alongside phase 3 (#1253), which owns 0042 and migration 0076. > Until phase 3 is rebased in, 0042 does not exist on this branch and the ADR > index's gapless check fails here by construction; it goes green with the > rebase. -> The same note is on 0041 for the same reason. +> ADR 0041 carries an analogous note for the phase-1/phase-2 gap it saw at the +> time; this is the same situation one phase later, not a reference to the same +> gap. ## Context @@ -113,6 +115,11 @@ constraint error, and the website gets a code instead of a string to grep: (`shared/contract/identity.ts`, with the messages, so the CLI, the website and the JSON body all read the same wording). +`orcid_already_linked` is a **deprecated alias** for `orcid_in_use`: the two +differ only in which constraint noticed, which is not a distinction a user can +act on. It survives because the website switches on it today +(nemarOrg/website#305 removes that need). + `error` deliberately carries different things on the two families of route, because they are read by different things and both are already right: the browser-facing routes put the CODE there (the website reads it there, and @@ -129,9 +136,16 @@ an account still claiming an iD it can no longer prove, invisible to every check that looked at `oauth_identities`. That row is id 42. -The citation value is RE-DERIVABLE and the identity claim is not: -linking the iD again refills it, and so does the DOI enrichment pass that -discovered it. Losing a re-derivable column beats keeping a false claim. +**The way back is a manual re-link, and nothing else.** +`users.orcid` is written in exactly three places — ORCID finalize, +`linkIdentity` and `relinkIdentity` — so an account that unlinks has no iD on +file until someone signs in through ORCID again. +That is a real cost and it is the smaller one: +the alternative is a row that permanently claims an identifier it cannot prove, +invisible to every check that looked at `oauth_identities`, +silently blocking the person's own next sign-up. +(An earlier draft of this ADR claimed the DOI-discovery pass would refill the +column. It does not; nothing but those three paths writes it.) ### Normalisation at every write diff --git a/AGENTS.md b/AGENTS.md index 913a55287..0274e3459 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,7 +44,8 @@ Load-bearing ones to know before touching the relevant area: 0032 (facet filters are declared once and report what they exclude), 0033 (the queue stamps which engine converted each dataset; pre-stamp rows are declared current), 0034 (`datasets` stays one table under an enforced column budget; derive, don't store), -0040 (`verified` is the base tier; admin approval is the only writer of upload access). +0040 (`verified` is the base tier; admin approval is the only writer of upload access), +0043 (one person, one account: an ORCID iD, an email or a GitHub handle backs at most one live account). --- @@ -298,6 +299,15 @@ without touching the version. `[skip ci]` is deliberately absent from the strip commit, because GitHub's skip marker would also block the tag-push event that `npm-publish.yml` needs. +**A release that carries a new migration runs `bun run migrations:d1-check` first.** +Every migration test in this repo runs on bun:sqlite, which is more permissive than +the SQLite build D1 ships — migration 0077 shipped a 79-character GLOB that bun:sqlite +executes and D1 rejects (its LIKE/GLOB pattern cap is 50 characters), so every test was +green and the deploy would have aborted mid-file. +The script replays every migration through `wrangler d1 execute --local` and diffs the +resulting object and column catalogue against the bun:sqlite one. +It is not in per-PR CI because it takes about a minute. + **When a manual bump does apply:** cutting a minor or major release (`./scripts/bump-version.sh minor-dev0` on dev, then open the PR), or tagging an explicit pre-release (`-rc*`, `-alpha*`, `-beta*`), @@ -522,12 +532,12 @@ and what is historical. The entries worth knowing by name: | Group | Covers | |---|---| -| `nemar auth` | login, signup, status/whoami, switch, logout, verification, SSH setup, key retrieval and regeneration | +| `nemar auth` | login, signup, status/whoami, profile, switch, logout, verification, SSH setup, key retrieval and regeneration | | `nemar dataset` | validate, upload, download, status (alias: view), list, search, release, update, clone, get, commit, push, drop, ci, manifest | | `nemar dataset publish` | request, status, resend | | `nemar dataset` (access) | request-access, access, invite, collaborators | | `nemar sandbox` | training run, status, reset — required before uploading | -| `nemar admin` | users, approve, revoke, role, notify, s3, repo, ci, doi, publish, revert, make-public, delete-dataset, bulk-delete, reindex, hed-sweep, data-integrity-sweep, recording-stats-sweep, signal-defaults-sweep, zarr-fidelity-sweep, doctor, summary, notice, email-preferences, backfill-names, e2e-test | +| `nemar admin` | users, approve, revoke, role, notify, s3, repo, ci, doi, publish, revert, make-public, delete-dataset, bulk-delete, reindex, hed-sweep, data-integrity-sweep, recording-stats-sweep, signal-defaults-sweep, zarr-fidelity-sweep, doctor, summary, notice, email-preferences, backfill-names, duplicates, e2e-test | | `nemar admin import*` | OpenNeuro import, status, rollback, retry, verify, recover (issue #754, epic #967) | | `nemar admin fleet` | drift, enforce, revalidate — governance across dataset repos (epic #713) | | `nemar admin exemplar` | create, status, remint-dois — the staging exemplar fleet | From 5fe41e2c1469f48bd7d64c9ba6f03e244876b680 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 18:59:11 -0700 Subject: [PATCH 15/18] test: cover the race paths and the flagged row --- .../identity-conflict-still-usable.test.ts | 140 ++++++++++++++++++ backend/test/identity-refusals-route.test.ts | 113 ++++++++++++++ .../identity-unique-violation.unit.test.ts | 109 ++++++++++++++ .../identity-uniqueness-migration.test.ts | 33 +++++ backend/test/user-duplicates-route.test.ts | 90 +++++++++++ test/admin-duplicates-cli.test.ts | 29 ++++ test/admin-route-inventory.unit.test.ts | 19 +++ 7 files changed, 533 insertions(+) create mode 100644 backend/test/identity-conflict-still-usable.test.ts create mode 100644 backend/test/identity-unique-violation.unit.test.ts diff --git a/backend/test/identity-conflict-still-usable.test.ts b/backend/test/identity-conflict-still-usable.test.ts new file mode 100644 index 000000000..b53e7d310 --- /dev/null +++ b/backend/test/identity-conflict-still-usable.test.ts @@ -0,0 +1,140 @@ +/** + * A flagged account still works (#1254, ADR 0043). + * + * Migration 0077 absorbs an existing duplicate by setting + * `users.identity_conflict = 1`, which makes the row invisible to the partial + * unique indexes. That is the WHOLE effect it is allowed to have. A flagged + * row must still sign in, still be readable, still own its datasets -- it has + * lost its claim on an identifier, not its account. + * + * Production row 42 is a real person's account, so this is not a hypothetical: + * if the flag locked people out, the migration would be a silent mass + * revocation dressed as a constraint. + * + * Driven through the REAL entry points -- `POST /auth/login` with a real + * hashed API token, and `GET /users/me` behind the real `authMiddleware` -- + * rather than a SELECT, because a SELECT cannot notice a middleware that + * learned to reject the flag. + */ + +import { Database } from "bun:sqlite"; +import { beforeEach, describe, expect, test } from "bun:test"; +import { readFileSync, readdirSync } from "node:fs"; +import { join } from "node:path"; +import { Hono } from "hono"; +import { authRoutes } from "../src/routes/auth"; +import { userRoutes } from "../src/routes/users"; +import { hashApiKey } from "../src/services/token"; +import type { Bindings, Variables } from "../src/types/bindings"; +import { realD1 } from "./helpers/d1"; + +const MIGRATIONS_DIR = join(import.meta.dir, "../src/db/migrations"); +const TARGET = "0077_identity_uniqueness.sql"; +const SHARED_ORCID = "0000-0002-1974-1293"; +const FLAGGED_KEY = "nemar_test_key_for_the_flagged_row"; + +let db: Database; +let app: Hono<{ Bindings: Bindings; Variables: Variables }>; +let flaggedId: number; +let canonicalId: number; + +function env(): Bindings { + return { DB: realD1(db), ENVIRONMENT: "test" } as unknown as Bindings; +} + +function seed(email: string, identityBacked: boolean): number { + db.run( + `INSERT INTO users (username, email, password_hash, github_username, status, signup_source, + email_verified, orcid, orcid_verified, role, service_access) + VALUES (?, ?, 'x', ?, 'approved', 'web', 1, ?, 1, 'member', 1)`, + [email.split("@")[0], email, `${email.split("@")[0]}-gh`, SHARED_ORCID], + ); + const row = db.query<{ id: number }, [string]>("SELECT id FROM users WHERE email = ?").get(email); + if (!row) throw new Error(`seed failed for ${email}`); + if (identityBacked) { + db.run( + "INSERT INTO oauth_identities (user_id, provider, provider_subject) VALUES (?, 'orcid', ?)", + [row.id, SHARED_ORCID], + ); + } + return row.id; +} + +beforeEach(async () => { + // Every migration EXCEPT 0077, so the duplicate can be seeded pre-flag and + // flagged by the real migration rather than by the test. + db = new Database(":memory:"); + for (const file of readdirSync(MIGRATIONS_DIR) + .filter((f) => f.endsWith(".sql") && f < TARGET) + .sort()) { + db.exec(readFileSync(join(MIGRATIONS_DIR, file), "utf-8")); + } + // The orphan first (lower id, no identity row) -- the production 42 shape. + flaggedId = seed("orphan@example.org", false); + canonicalId = seed("holder@example.org", true); + db.exec(readFileSync(join(MIGRATIONS_DIR, TARGET), "utf-8")); + + db.run("INSERT INTO tokens (user_id, api_key_hash, api_key_prefix) VALUES (?, ?, 'nemar_te')", [ + flaggedId, + await hashApiKey(FLAGGED_KEY), + ]); + + app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); + app.route("/auth", authRoutes); + app.route("/users", userRoutes); +}); + +describe("a row migration 0077 flagged", () => { + test("is actually flagged, and the other row is not (the fixture's premise)", () => { + const flag = (id: number) => + db + .query<{ identity_conflict: number }, [number]>( + "SELECT identity_conflict FROM users WHERE id = ?", + ) + .get(id)?.identity_conflict; + expect(flag(flaggedId)).toBe(1); + expect(flag(canonicalId)).toBe(0); + }); + + test("can still sign in through POST /auth/login", async () => { + const res = await app.request( + "/auth/login", + { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ api_key: FLAGGED_KEY }), + }, + env(), + ); + expect(res.status).toBe(200); + const body = (await res.json()) as { valid: boolean; user: { username: string } }; + expect(body.valid).toBe(true); + expect(body.user.username).toBe("orphan"); + }); + + test("can still read its own account through GET /users/me", async () => { + const res = await app.request( + "/users/me", + { headers: { Authorization: `Bearer ${FLAGGED_KEY}` } }, + env(), + ); + expect(res.status).toBe(200); + const { user } = (await res.json()) as { user: { id: number; orcid: string | null } }; + expect(user.id).toBe(flaggedId); + // It still CARRIES the iD for citation. What it lost is the exclusive + // claim, which lives in the index, not in the column. + expect(user.orcid).toBe(SHARED_ORCID); + }); + + test("keeps its upload access", async () => { + // The flag and the upload grant are unrelated decisions (ADR 0040 owns the + // grant), and a migration about identifiers must not quietly revoke one. + const res = await app.request( + "/users/me", + { headers: { Authorization: `Bearer ${FLAGGED_KEY}` } }, + env(), + ); + const { user } = (await res.json()) as { user: { service_access: boolean } }; + expect(user.service_access).toBe(true); + }); +}); diff --git a/backend/test/identity-refusals-route.test.ts b/backend/test/identity-refusals-route.test.ts index f3ab67924..18aa1e4f7 100644 --- a/backend/test/identity-refusals-route.test.ts +++ b/backend/test/identity-refusals-route.test.ts @@ -525,6 +525,86 @@ describe("ORCID finalize refuses a duplicate identity", () => { }); }); +// -------------------------------------------------------------------------- +// Races: the loser of a concurrent claim +// -------------------------------------------------------------------------- + +/** + * Make the next INSERT on `users` fail with a REAL SQLite UNIQUE error, using + * SQLite's own trigger machinery -- the same fault injection + * web-email-verification.test.ts uses to block writes. + * + * Nothing is mocked: the production statement runs against a database that + * genuinely raises the error, with the exact message text + * (`UNIQUE constraint failed: users.`) that both engines produce -- the + * same text real D1 emits, verified with `wrangler d1 execute --local`. That + * is what makes it a test of the error MAPPING rather than of a string this + * test invented. + * + * A trigger rather than a real second request because the window is between a + * SELECT and an INSERT inside one handler: there is no way to interleave a + * second HTTP request into it deterministically. + */ +function raiseUniqueOnUserInsert(column: string): void { + db.run( + `CREATE TRIGGER race_on_user_insert BEFORE INSERT ON users + BEGIN SELECT RAISE(ABORT, 'UNIQUE constraint failed: users.${column}'); END`, + ); +} + +describe("finalize maps a lost race to its typed refusal", () => { + test("a concurrent email claim is 409 email_in_use, not a generic 500", async () => { + raiseUniqueOnUserInsert("email"); + const res = await finalize(OTHER_ORCID); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string; message: string }; + expect(body.code).toBe("email_in_use"); + expect(body.message).toContain("nemar.org/settings"); + }); + + test("a concurrent iD claim is 409 orcid_in_use", async () => { + raiseUniqueOnUserInsert("orcid"); + const res = await finalize(OTHER_ORCID); + expect(res.status).toBe(409); + expect(((await res.json()) as { code: string }).code).toBe("orcid_in_use"); + }); + + test("a UNIQUE hit on some OTHER column is not dressed up as a collision", async () => { + // `users.email` is a substring of `users.email_verified`, so a substring + // match would report an unrelated constraint failure as "that address is + // taken" and send the user to go change an address that is fine. + raiseUniqueOnUserInsert("email_verified"); + const res = await finalize(OTHER_ORCID); + expect(res.status).toBe(500); + }); + + test("a non-uniqueness identity-insert failure is a 500, and leaves no orphan", async () => { + // The users row commits before the identity insert, so it must be removed + // either way -- an account with an email and no ORCID login is a dead end. + // What must NOT happen is reporting `orcid_already_linked` for a D1 fault, + // which sends the user to unlink an iD from an account that does not exist. + db.run( + `CREATE TRIGGER race_on_identity_insert BEFORE INSERT ON oauth_identities + BEGIN SELECT RAISE(ABORT, 'disk I/O error'); END`, + ); + const res = await finalize(OTHER_ORCID); + expect(res.status).toBe(500); + expect(((await res.json()) as { error: string }).error).toBe("Sign-up failed"); + expect(userByEmail("brandnew@example.org")).toBeNull(); + }); + + test("a concurrent identity claim IS orcid_already_linked, and leaves no orphan", async () => { + db.run( + `CREATE TRIGGER race_on_identity_insert BEFORE INSERT ON oauth_identities + BEGIN SELECT RAISE(ABORT, 'UNIQUE constraint failed: oauth_identities.provider, oauth_identities.provider_subject'); END`, + ); + const res = await finalize(OTHER_ORCID); + expect(res.status).toBe(409); + expect(((await res.json()) as { code: string }).code).toBe("orcid_already_linked"); + expect(userByEmail("brandnew@example.org")).toBeNull(); + }); +}); + // -------------------------------------------------------------------------- // ORCID link / relink, through the callback // -------------------------------------------------------------------------- @@ -576,6 +656,39 @@ describe("ORCID link refuses an iD another account holds", () => { expect(userByEmail("me@example.org")?.orcid).toBe(HELD_ORCID); }); + test("a link that loses a race redirects to orcid_in_use, not orcid_error", async () => { + // Both constraints spell the same fact and either can fire first, so the + // callback has to recognise both. This one is the identity table's. + const mine = seedUser("me@example.org"); + db.run( + `CREATE TRIGGER race_on_identity_insert BEFORE INSERT ON oauth_identities + BEGIN SELECT RAISE(ABORT, 'UNIQUE constraint failed: oauth_identities.provider, oauth_identities.provider_subject'); END`, + ); + const res = await callback(await sessionCookie(mine), "link"); + expect(redirectError(res)).toBe("orcid_in_use"); + }); + + test("a link losing the users.orcid race redirects to orcid_in_use too", async () => { + const mine = seedUser("me@example.org"); + db.run( + `CREATE TRIGGER race_on_user_update BEFORE UPDATE OF orcid ON users + BEGIN SELECT RAISE(ABORT, 'UNIQUE constraint failed: users.orcid'); END`, + ); + const res = await callback(await sessionCookie(mine), "link"); + expect(redirectError(res)).toBe("orcid_in_use"); + }); + + test("an unrelated write failure is still the generic error", async () => { + // The guard against a catch so wide it labels every failure a collision. + const mine = seedUser("me@example.org"); + db.run( + `CREATE TRIGGER race_on_identity_insert BEFORE INSERT ON oauth_identities + BEGIN SELECT RAISE(ABORT, 'disk I/O error'); END`, + ); + const res = await callback(await sessionCookie(mine), "link"); + expect(redirectError(res)).toBe("orcid_error"); + }); + test("linking an unclaimed iD still works", async () => { // The guard against a check that refuses everything. const mine = seedUser("me@example.org"); diff --git a/backend/test/identity-unique-violation.unit.test.ts b/backend/test/identity-unique-violation.unit.test.ts new file mode 100644 index 000000000..10f8bdbda --- /dev/null +++ b/backend/test/identity-unique-violation.unit.test.ts @@ -0,0 +1,109 @@ +/** + * `uniqueViolationColumns` / `isUniqueViolationOn` / `isOrcidIdentityUniqueViolation` + * (#1254 review; ADR 0043). + * + * These decide whether a caught write failure is reported to a user as "that + * identifier is taken" or as a 500, at six call sites. Getting it wrong is not + * cosmetic in either direction: too narrow and a real collision surfaces as an + * opaque error, too broad and a disk fault tells someone to go change an email + * address that is fine. + * + * The error strings here are not invented. Each is the exact text one of the + * two engines produces, captured from a real failure: bun:sqlite writes + * `UNIQUE constraint failed: users.orcid`, and real D1 appends its own suffix + * (`: SQLITE_CONSTRAINT (extended: SQLITE_CONSTRAINT_UNIQUE)`) -- verified with + * `wrangler d1 execute --local` against migration 0077's indexes. A parser + * that handled only the bun form would pass every test in this repo and + * mis-report every collision in production. + * + * Pure functions with a fixed input, so this is a unit test by nature; the + * behaviour AT the call sites is covered by the fault-injected route tests in + * identity-refusals-route.test.ts and user-duplicates-route.test.ts. + */ + +import { describe, expect, test } from "bun:test"; +import { + isOrcidIdentityUniqueViolation, + isUniqueViolationOn, + uniqueViolationColumns, +} from "../src/services/identity"; + +const BUN_FORM = "UNIQUE constraint failed: users.orcid"; +const D1_FORM = + "UNIQUE constraint failed: users.orcid: SQLITE_CONSTRAINT (extended: SQLITE_CONSTRAINT_UNIQUE)"; +const IDENTITY_FORM = + "UNIQUE constraint failed: oauth_identities.provider, oauth_identities.provider_subject"; + +describe("uniqueViolationColumns", () => { + test("parses the bun:sqlite form", () => { + expect(uniqueViolationColumns(new Error(BUN_FORM))).toEqual(["users.orcid"]); + }); + + test("parses the real D1 form, without swallowing its suffix", () => { + expect(uniqueViolationColumns(new Error(D1_FORM))).toEqual(["users.orcid"]); + }); + + test("parses a multi-column constraint", () => { + expect(uniqueViolationColumns(new Error(IDENTITY_FORM))).toEqual([ + "oauth_identities.provider", + "oauth_identities.provider_subject", + ]); + }); + + test("returns nothing for an error that is not a UNIQUE violation", () => { + expect(uniqueViolationColumns(new Error("disk I/O error"))).toEqual([]); + expect(uniqueViolationColumns(new Error("NOT NULL constraint failed: users.email"))).toEqual([]); + }); + + test("survives a non-Error value", () => { + // D1 errors do not always arrive as Error instances, which is why the + // signature takes `unknown`. + expect(uniqueViolationColumns(BUN_FORM)).toEqual(["users.orcid"]); + expect(uniqueViolationColumns({ message: BUN_FORM })).toEqual([]); + expect(uniqueViolationColumns(null)).toEqual([]); + expect(uniqueViolationColumns(undefined)).toEqual([]); + }); +}); + +describe("isUniqueViolationOn", () => { + test("matches its own column, in both engines' spellings", () => { + expect(isUniqueViolationOn(new Error(BUN_FORM), "orcid")).toBe(true); + expect(isUniqueViolationOn(new Error(D1_FORM), "orcid")).toBe(true); + }); + + test("does not match a DIFFERENT column", () => { + expect(isUniqueViolationOn(new Error(BUN_FORM), "email")).toBe(false); + }); + + test("does not confuse users.email with users.email_verified", () => { + // The reason this parses instead of substring-matching: `users.email` is a + // prefix of `users.email_verified`, so `msg.includes("users.email")` -- + // the original implementation -- reports an unrelated constraint failure + // as "that address is taken". + const err = new Error("UNIQUE constraint failed: users.email_verified"); + expect(isUniqueViolationOn(err, "email")).toBe(false); + }); + + test("matches an exact column even when another is listed alongside", () => { + const err = new Error("UNIQUE constraint failed: users.email, users.github_username"); + expect(isUniqueViolationOn(err, "email")).toBe(true); + expect(isUniqueViolationOn(err, "github_username")).toBe(true); + expect(isUniqueViolationOn(err, "orcid")).toBe(false); + }); + + test("is false for a non-UNIQUE failure", () => { + expect(isUniqueViolationOn(new Error("disk I/O error"), "email")).toBe(false); + }); +}); + +describe("isOrcidIdentityUniqueViolation", () => { + test("matches the oauth_identities constraint", () => { + expect(isOrcidIdentityUniqueViolation(new Error(IDENTITY_FORM))).toBe(true); + }); + + test("does not match a users-table collision", () => { + // The two are handled differently at finalize: only the identity one + // means "somebody else already links this iD". + expect(isOrcidIdentityUniqueViolation(new Error(BUN_FORM))).toBe(false); + }); +}); diff --git a/backend/test/identity-uniqueness-migration.test.ts b/backend/test/identity-uniqueness-migration.test.ts index 793f5b4c5..e9bf379b1 100644 --- a/backend/test/identity-uniqueness-migration.test.ts +++ b/backend/test/identity-uniqueness-migration.test.ts @@ -283,6 +283,39 @@ describe("migration 0077: the indexes", () => { expect(row?.status).toBe("verified"); }); + test("re-running statements 2 onward changes nothing and does not error", () => { + // The header promises a partial application can be finished by re-running + // from statement 2. Only the ALTER is non-idempotent (SQLite has no + // `ADD COLUMN IF NOT EXISTS`), so that is what is dropped here; everything + // after it must be safe to replay onto its own output. + const { db } = migratedFixture(); + const snapshot = () => + db + .query<{ id: number; email: string; orcid: string | null; identity_conflict: number }, []>( + "SELECT id, email, orcid, identity_conflict FROM users ORDER BY id", + ) + .all(); + const before = snapshot(); + + const full = readFileSync(join(MIGRATIONS_DIR, TARGET), "utf-8"); + const fromStatementTwo = full.slice(full.indexOf("UPDATE users\n SET orcid = UPPER(orcid)")); + // Guard the slice: if the file is reshaped and this no longer starts at + // statement 2, the test would silently replay nothing and still pass. + expect(fromStatementTwo).toContain("UPPER(orcid)"); + expect(fromStatementTwo).toContain("idx_users_orcid_live_unique"); + expect(fromStatementTwo).not.toContain("ALTER TABLE users ADD COLUMN"); + + expect(() => db.exec(fromStatementTwo)).not.toThrow(); + + expect(snapshot()).toEqual(before); + const indexes = db + .query<{ n: number }, []>( + "SELECT COUNT(*) AS n FROM sqlite_master WHERE type='index' AND name LIKE 'idx_users_%_live_unique'", + ) + .get(); + expect(indexes?.n).toBe(2); + }); + test("multiple rows with no ORCID at all do not collide", () => { const { db } = migratedFixture(); const insert = db.prepare( diff --git a/backend/test/user-duplicates-route.test.ts b/backend/test/user-duplicates-route.test.ts index 71bd59ddd..b77fdcb1e 100644 --- a/backend/test/user-duplicates-route.test.ts +++ b/backend/test/user-duplicates-route.test.ts @@ -277,6 +277,54 @@ describe("GET /admin/users/duplicates", () => { expect(body.flagged_count).toBe(0); }); + test("dataset_count excludes sandbox datasets", async () => { + // A sandbox dataset is training, not work: counting it would inflate the + // number an operator uses to pick which of two accounts survives. + db.run( + `INSERT INTO datasets (dataset_id, name, owner_user_id, is_sandbox) + VALUES ('xx090123', 'Sandbox training', ?, 1)`, + [holderId], + ); + const res = await get("/admin/users/duplicates"); + const body = (await res.json()) as ReportBody; + const survivor = body.groups + .find((g) => g.kind === "orcid") + ?.accounts.find((a) => a.id === holderId); + expect(survivor?.dataset_count).toBe(1); + }); + + test("a compound collision marks the UNFLAGGED row canonical, not the lowest id", async () => { + // The shape where "lowest id" and "the row that actually holds the + // identifier" disagree: one pair colliding on BOTH iD and email, with the + // identity row on the HIGHER id. 0077's ORCID pass flags the lower id, so + // for the EMAIL group the lowest id is a FLAGGED row -- which holds + // nothing, because a flagged row is invisible to the partial indexes. + // Marking it canonical would send an operator to fix the wrong account. + db = dbBeforeTarget(); + const lower = seedUser("Both@Example.org", { orcid: "0000-0005-1111-2222" }); + const higher = seedUser("both@example.org", { + orcid: "0000-0005-1111-2222", + identityBacked: true, + }); + adminId = seedUser("root@nemar.org", { username: "root", role: "admin" }); + db.exec(readFileSync(join(MIGRATIONS_DIR, TARGET), "utf-8")); + await seedAdminToken(adminId); + app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); + app.route("/admin", adminRoutes); + + // Premise: the migration flagged the LOWER id. + expect(flagOf(lower)).toBe(1); + expect(flagOf(higher)).toBe(0); + expect(lower).toBeLessThan(higher); + + const body = (await (await get("/admin/users/duplicates")).json()) as ReportBody; + for (const group of body.groups) { + expect(group.canonical_user_id).toBe(higher); + expect(group.accounts.find((a) => a.canonical)?.identity_conflict).toBe(0); + } + expect(body.groups.map((g) => g.kind).sort()).toEqual(["email", "orcid"]); + }); + test("a non-admin cannot read it", async () => { const memberId = seedUser("member@example.org", { username: "member" }); db.run("UPDATE tokens SET user_id = ? WHERE user_id = ?", [memberId, adminId]); @@ -358,4 +406,46 @@ describe("POST /admin/users/:id/clear-identity-conflict", () => { const res = await post("/admin/users/not-a-number/clear-identity-conflict"); expect(res.status).toBe(400); }); + + test("a non-admin cannot clear a flag", async () => { + const memberId = seedUser("member@example.org", { username: "member" }); + db.run("UPDATE tokens SET user_id = ? WHERE user_id = ?", [memberId, adminId]); + const res = await post(`/admin/users/${orphanId}/clear-identity-conflict`); + expect(res.status).toBe(403); + expect(flagOf(orphanId)).toBe(1); + }); + + test("a collision that appears mid-write is still a 409, not a 500", async () => { + // Clearing the flag is what puts the row back INTO the partial unique + // index, so the UPDATE is itself uniqueness-checked. A collision that + // returned between the SELECT and the UPDATE fails right there, and it + // must read as the same refusal the pre-check gives -- an operator who + // gets a bare 500 has nothing to act on. + // + // Real SQLite error text via a trigger, the same fault injection the + // finalize race tests use. The pre-check is satisfied first by removing + // the other row's claim, so the ONLY thing that can refuse is the write. + db.run("UPDATE users SET deleted_at = datetime('now'), orcid = NULL WHERE id = ?", [holderId]); + db.run( + `CREATE TRIGGER race_on_clear BEFORE UPDATE OF identity_conflict ON users + BEGIN SELECT RAISE(ABORT, 'UNIQUE constraint failed: users.orcid'); END`, + ); + const res = await post(`/admin/users/${orphanId}/clear-identity-conflict`); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string }; + expect(body.error).toBe("identity_conflict_remains"); + expect(body.code).toBe("identity_conflict_remains"); + expect(flagOf(orphanId)).toBe(1); + }); + + test("an unrelated write failure is still a 500", async () => { + // The guard against a catch so wide it labels every failure a collision. + db.run("UPDATE users SET deleted_at = datetime('now'), orcid = NULL WHERE id = ?", [holderId]); + db.run( + `CREATE TRIGGER race_on_clear BEFORE UPDATE OF identity_conflict ON users + BEGIN SELECT RAISE(ABORT, 'disk I/O error'); END`, + ); + const res = await post(`/admin/users/${orphanId}/clear-identity-conflict`); + expect(res.status).toBe(500); + }); }); diff --git a/test/admin-duplicates-cli.test.ts b/test/admin-duplicates-cli.test.ts index 23809cfcb..2ce170ec0 100644 --- a/test/admin-duplicates-cli.test.ts +++ b/test/admin-duplicates-cli.test.ts @@ -236,6 +236,35 @@ describe("nemar admin duplicates", () => { } }); + test("a malformed report fails loudly instead of printing a wrong answer", async () => { + // The command is validated against the shared contract, so a backend that + // drifts is a loud parse failure rather than "No duplicate accounts." -- + // which is the one output that must never be produced by accident, because + // an operator reads it as "nothing to do". + const server = startServer({ report: { groups: "not-an-array", group_count: 1 } }); + try { + const result = await runCli(["admin", "duplicates"], server.url); + expect(result.exitCode).toBe(1); + expect(result.stdout).not.toContain("No duplicate accounts."); + expect(`${result.stdout}${result.stderr}`).toContain("contract"); + } finally { + server.stop(); + } + }); + + test("a report missing flagged_count is rejected, not read as zero", async () => { + // A field silently defaulting to 0 would hide exactly the state the + // `--clear` half exists for: flags with no collision left behind them. + const server = startServer({ report: { groups: [], group_count: 0 } }); + try { + const result = await runCli(["admin", "duplicates"], server.url); + expect(result.exitCode).toBe(1); + expect(result.stdout).not.toContain("No duplicate accounts."); + } finally { + server.stop(); + } + }); + test("a non-numeric --clear id is rejected before any request", async () => { const server = startServer({}); try { diff --git a/test/admin-route-inventory.unit.test.ts b/test/admin-route-inventory.unit.test.ts index 0df47cc03..9a9827054 100644 --- a/test/admin-route-inventory.unit.test.ts +++ b/test/admin-route-inventory.unit.test.ts @@ -139,6 +139,25 @@ describe("admin route inventory", () => { expect(actual).toEqual(EXPECTED_ENTRIES); }); + test("GET /users/duplicates is registered BEFORE GET /users/:username", () => { + // Both patterns match `/users/duplicates`, and this router's path set makes + // Hono's RegExpRouter throw UnsupportedPathError, so the app falls back to + // a router that runs every matching handler in REGISTRATION order and + // takes the first response. Registered second, the duplicate report is + // unreachable -- the username lookup 404s first, which is what happened + // before the order in admin/index.ts was fixed. + // + // Pinned here rather than left to a comment because nothing else fails: + // every other test in the suite passes with the order reversed, and the + // route just quietly stops existing. + const paths = adminRoutes.routes.map((r) => `${r.method} ${r.path}`); + const dup = paths.indexOf("GET /users/duplicates"); + const byUsername = paths.indexOf("GET /users/:username"); + expect(dup).toBeGreaterThanOrEqual(0); + expect(byUsername).toBeGreaterThanOrEqual(0); + expect(dup).toBeLessThan(byUsername); + }); + test("entry total is pinned", () => { expect(adminRoutes.routes.length).toBe(100); }); From e26550b9d4cc5bdd21a9436c2aa65095ac05a886 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 19:01:29 -0700 Subject: [PATCH 16/18] test: cover the profile GitHub collision paths --- backend/test/identity-refusals-route.test.ts | 59 ++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/backend/test/identity-refusals-route.test.ts b/backend/test/identity-refusals-route.test.ts index 18aa1e4f7..615244d12 100644 --- a/backend/test/identity-refusals-route.test.ts +++ b/backend/test/identity-refusals-route.test.ts @@ -747,6 +747,65 @@ describe("ORCID unlink releases the iD", () => { }); }); +// -------------------------------------------------------------------------- +// PATCH /auth/profile +// -------------------------------------------------------------------------- + +describe("profile GitHub collisions", () => { + async function patchProfile(userId: number, handle: string): Promise { + return app.request( + "/auth/profile", + { + method: "PATCH", + headers: { + Origin: ORIGIN, + Cookie: await sessionCookie(userId), + "content-type": "application/json", + }, + body: JSON.stringify({ github_username: handle, city: "San Diego", country: "USA" }), + }, + env(), + ); + } + + test("a taken handle is refused with the shared message", async () => { + seedUser("holder@example.org", { github: "Octocat" }); + const mine = seedUser("me@example.org"); + const res = await patchProfile(mine, "octocat"); + expect(res.status).toBe(409); + const body = (await res.json()) as { error: string; code: string; message: string }; + expect(body.error).toBe("github_in_use"); + expect(body.code).toBe("github_in_use"); + expect(body.message).toContain("nemar.org/settings"); + }); + + test("a handle claimed mid-write is still github_in_use, not a 500", async () => { + // The TOCTOU window past the dedup SELECT: two concurrent PATCHes claiming + // the same free handle both pass the pre-check and the loser's UPDATE hits + // idx_users_github. Real SQLite error text via a trigger, so this tests + // the mapping rather than a string the test invented. + const mine = seedUser("me@example.org"); + db.run( + `CREATE TRIGGER race_on_profile_update BEFORE UPDATE OF github_username ON users + BEGIN SELECT RAISE(ABORT, 'UNIQUE constraint failed: users.github_username'); END`, + ); + const res = await patchProfile(mine, "brand-new-handle"); + expect(res.status).toBe(409); + expect(((await res.json()) as { code: string }).code).toBe("github_in_use"); + }); + + test("an unrelated write failure is still a 500", async () => { + // The guard against a catch so wide it labels every failure a collision. + const mine = seedUser("me@example.org"); + db.run( + `CREATE TRIGGER race_on_profile_update BEFORE UPDATE OF github_username ON users + BEGIN SELECT RAISE(ABORT, 'disk I/O error'); END`, + ); + const res = await patchProfile(mine, "brand-new-handle"); + expect(res.status).toBe(500); + }); +}); + // -------------------------------------------------------------------------- // Email change // -------------------------------------------------------------------------- From 982fe1d00bd60aeef4fe35c38ca623b75b252205 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 19:26:46 -0700 Subject: [PATCH 17/18] fix: sign-in and tombstone respect legacy rows --- backend/src/db/user-tombstone.ts | 10 ++- backend/src/routes/auth-web.ts | 15 +++- backend/test/identity-refusals-route.test.ts | 73 ++++++++++++++++++++ scripts/d1-migration-check.ts | 9 ++- test/user-soft-delete.unit.test.ts | 14 ++-- 5 files changed, 113 insertions(+), 8 deletions(-) diff --git a/backend/src/db/user-tombstone.ts b/backend/src/db/user-tombstone.ts index 571e27a8d..6f63cb793 100644 --- a/backend/src/db/user-tombstone.ts +++ b/backend/src/db/user-tombstone.ts @@ -5,7 +5,7 @@ // The mask is the load-bearing mechanism for two guarantees: // 1. PII erasure — email is replaced with a non-PII placeholder; username, // github, password, orcid, description, AWS creds, verification tokens and -// email prefs are nulled; email_verified is zeroed. +// email prefs are nulled; email_verified and orcid_verified are zeroed. // 2. Re-signup freedom — nulling username/github and rewriting email FREES // those UNIQUE values so the original owner can sign up again later. (The // signup de-dup checks intentionally still see tombstoned rows.) @@ -44,6 +44,14 @@ export const USER_TOMBSTONE_MASK_SQL = `UPDATE users github_username = NULL, password_hash = NULL, orcid = NULL, + -- Cleared WITH users.orcid, not left behind (#1254, ADR 0043). A row + -- with orcid = NULL and orcid_verified = 1 claims to have proven an iD + -- it no longer has, which is a lie in the audit trail and the exact + -- half-state that produced production rows 42/43 in the first place -- + -- there via unlink, observed again here after row 42 was soft-deleted. + -- Every writer of one now writes the other: this statement, the unlink + -- route, and linkIdentity/relinkIdentity. + orcid_verified = 0, description = NULL, email_preferences = NULL, email_verified = 0, diff --git a/backend/src/routes/auth-web.ts b/backend/src/routes/auth-web.ts index 7a556ee54..03afaab63 100644 --- a/backend/src/routes/auth-web.ts +++ b/backend/src/routes/auth-web.ts @@ -227,8 +227,15 @@ authWebRoutes.post("/code/request", zValidator("json", emailSchema), async (c) = // silently fail; the dashboard surfaces an "if your email is on // file, you'll get a code shortly" copy and a CTA pointing typo'd // users at the CLI sign-up (companion issue on nemarOrg/website). + // COLLATE NOCASE (ADR 0043): the schema lowercases the REQUEST, but the + // rows it has to match were stored exactly as typed -- that is most of the + // production catalog. An exact-case lookup sends every one of those people + // the masked "if your email is on file" 200 and then silently sends no + // code, which is indistinguishable from a typo and unreportable. const existing = await db - .prepare("SELECT status, role FROM users WHERE email = ? AND deleted_at IS NULL LIMIT 1") + .prepare( + "SELECT status, role FROM users WHERE email = ? COLLATE NOCASE AND deleted_at IS NULL LIMIT 1", + ) .bind(email) .first<{ status: string; role: string | null }>(); @@ -425,13 +432,17 @@ authWebRoutes.post("/code/verify", zValidator("json", verifySchema), async (c) = return c.json({ error: "Invalid or expired code" }, 401); } + // COLLATE NOCASE for the same reason as /code/request (ADR 0043): the code + // was issued against the normalised address, and the row that owns it may + // be stored mixed-case. Without this a legacy user could receive a code + // and still not be able to redeem it. const userRow = await db .prepare( `SELECT id, email, role, status, email_verified, given_name, family_name, orcid, orcid_verified, github_username, city, country, affiliation, service_access, username, service_access_granted_at, upload_access_requested_at - FROM users WHERE email = ? AND deleted_at IS NULL LIMIT 1`, + FROM users WHERE email = ? COLLATE NOCASE AND deleted_at IS NULL LIMIT 1`, ) .bind(email) .first<{ diff --git a/backend/test/identity-refusals-route.test.ts b/backend/test/identity-refusals-route.test.ts index 615244d12..2697f6538 100644 --- a/backend/test/identity-refusals-route.test.ts +++ b/backend/test/identity-refusals-route.test.ts @@ -747,6 +747,79 @@ describe("ORCID unlink releases the iD", () => { }); }); +// -------------------------------------------------------------------------- +// Passwordless sign-in against a legacy row +// -------------------------------------------------------------------------- + +describe("the email-code sign-in finds a legacy mixed-case row", () => { + // Same regression as the CLI routes, on the flow most of the production + // population actually uses. `emailFieldSchema` lowercases the REQUEST, so an + // exact-case lookup misses every row stored as typed -- and /code/request + // answers those with the masked "if your email is on file" 200 and then + // sends nothing, which is indistinguishable from a typo and unreportable. + // + // `@nemar.test` because that is the suffix the non-production fence admits + // for the dev_code echo (services/auth-code.ts), which is what lets the + // whole request -> verify round trip run here without an inbox. + const LEGACY = "Legacy.User@Nemar.test"; + const LOWERCASE = "legacy.user@nemar.test"; + + function seedLegacyWebRow(): number { + return seedUser(LEGACY, { orcid: OTHER_ORCID }); + } + + async function codeRequest(email: string): Promise { + return app.request( + "/auth/code/request", + { + method: "POST", + headers: { Origin: ORIGIN, "content-type": "application/json" }, + body: JSON.stringify({ email }), + }, + env(), + ); + } + + test("request issues a real code instead of the silent skip", async () => { + seedLegacyWebRow(); + const res = await codeRequest(LOWERCASE); + expect(res.status).toBe(200); + const body = (await res.json()) as { dev_code?: string; dev_skip?: string }; + // `dev_skip: "unregistered"` is the exact symptom: the route decided no + // such account exists and returned the masked 200 having sent nothing. + expect(body.dev_skip).toBeUndefined(); + expect(body.dev_code).toMatch(/^\d{6}$/); + }); + + test("verify then signs the legacy row in", async () => { + const id = seedLegacyWebRow(); + const code = ((await (await codeRequest(LOWERCASE)).json()) as { dev_code?: string }).dev_code; + const res = await app.request( + "/auth/code/verify", + { + method: "POST", + headers: { Origin: ORIGIN, "content-type": "application/json" }, + body: JSON.stringify({ email: LOWERCASE, code, remember: false }), + }, + env(), + ); + expect(res.status).toBe(200); + const body = (await res.json()) as { user: { id: number } }; + expect(body.user.id).toBe(id); + }); + + test("an address matching no account still gets the silent skip", async () => { + // The guard against a lookup so loose it stops distinguishing anything -- + // and against losing the #595 no-enumeration property while fixing this. + seedLegacyWebRow(); + const res = await codeRequest("nobody@nemar.test"); + expect(res.status).toBe(200); + const body = (await res.json()) as { dev_code?: string; dev_skip?: string }; + expect(body.dev_skip).toBe("unregistered"); + expect(body.dev_code).toBeUndefined(); + }); +}); + // -------------------------------------------------------------------------- // PATCH /auth/profile // -------------------------------------------------------------------------- diff --git a/scripts/d1-migration-check.ts b/scripts/d1-migration-check.ts index a6445f23e..6ae305af0 100644 --- a/scripts/d1-migration-check.ts +++ b/scripts/d1-migration-check.ts @@ -69,7 +69,14 @@ function migrationFiles(): string[] { .sort(); } -/** Drop whole-line `--` comments; every statement is left byte-identical. */ +/** + * Drop whole-line `--` comments; every statement is left byte-identical. + * + * INVARIANT: no migration may contain a multi-line string literal whose + * continuation line starts with `--`. Such a line would be stripped as a + * comment and silently corrupt the string VALUE, and the catalogue diff below + * would not catch it -- that compares schema, not the data a migration writes. + */ function stripFullLineComments(sql: string): string { return sql .split("\n") diff --git a/test/user-soft-delete.unit.test.ts b/test/user-soft-delete.unit.test.ts index 88c9f4c88..e9b222290 100644 --- a/test/user-soft-delete.unit.test.ts +++ b/test/user-soft-delete.unit.test.ts @@ -42,6 +42,7 @@ CREATE TABLE users ( aws_secret_access_key_encrypted TEXT, aws_iam_username TEXT, orcid TEXT, + orcid_verified INTEGER NOT NULL DEFAULT 0, role TEXT DEFAULT 'member', email_preferences TEXT DEFAULT NULL, description TEXT, @@ -76,10 +77,10 @@ function seed(db: Database) { // even if the mask SQL typo'd its name). db.run( `INSERT INTO users (id, username, email, password_hash, github_username, status, email_verified, - verification_token, verification_expires_at, role, orcid, description, + verification_token, verification_expires_at, role, orcid, orcid_verified, description, aws_iam_username, aws_access_key_id_encrypted, aws_secret_access_key_encrypted) VALUES (42, 'testuser', 'real@example.com', 'argon2hash', 'ghuser', 'approved', 1, - 'verifytok', '2099-01-01T00:00:00Z', 'member', '0000-0001', 'a bio', + 'verifytok', '2099-01-01T00:00:00Z', 'member', '0000-0002-1974-1293', 1, 'a bio', 'iam-user', 'enc-key-id', 'enc-secret')`, ); db.run("INSERT INTO tokens (user_id, api_key_hash, revoked_at) VALUES (42, 'tokenhash', NULL)"); @@ -121,11 +122,11 @@ describe("user tombstone (soft delete)", () => { expect(cols.some((c) => c.name === "deleted_at")).toBe(true); }); - test("masks PII, zeroes email_verified, stamps deleted_at, revokes creds", () => { + test("masks PII, zeroes both verified flags, stamps deleted_at, revokes creds", () => { tombstone(db, 42); const row = db .query( - "SELECT email, username, github_username, password_hash, orcid, description, email_preferences, email_verified, verification_token, verification_expires_at, aws_iam_username, aws_access_key_id_encrypted, aws_secret_access_key_encrypted, status, deleted_at, revoked_at FROM users WHERE id = 42", + "SELECT email, username, github_username, password_hash, orcid, orcid_verified, description, email_preferences, email_verified, verification_token, verification_expires_at, aws_iam_username, aws_access_key_id_encrypted, aws_secret_access_key_encrypted, status, deleted_at, revoked_at FROM users WHERE id = 42", ) .get() as Record; expect(row.email).toBe("deleted+42@deleted.invalid"); @@ -133,6 +134,11 @@ describe("user tombstone (soft delete)", () => { expect(row.github_username).toBeNull(); expect(row.password_hash).toBeNull(); expect(row.orcid).toBeNull(); + // Cleared WITH users.orcid (#1254, ADR 0043). Leaving it at 1 leaves the + // row asserting it proved an iD it no longer has -- observed on production + // row 42 after it was soft-deleted, and the same half-state that made 42 + // and 43 two accounts for one person in the first place. + expect(row.orcid_verified).toBe(0); expect(row.description).toBeNull(); expect(row.email_preferences).toBeNull(); expect(row.email_verified).toBe(0); From 29fcda55b82bbf48bbbfc682c5c37b1941475448 Mon Sep 17 00:00:00 2001 From: Seyed Yahya Shirazi Date: Sat, 5 Sep 2026 19:51:46 -0700 Subject: [PATCH 18/18] test: adapt Phase 3 fixtures to the iD constraint --- .../decisions/0043-one-person-one-account.md | 12 +++++------ AGENTS.md | 5 +++-- .../migrations/0077_identity_uniqueness.sql | 8 +++---- backend/test/profile-username-route.test.ts | 21 ++++++++++++++++--- test/admin-route-inventory.unit.test.ts | 2 +- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/.context/decisions/0043-one-person-one-account.md b/.context/decisions/0043-one-person-one-account.md index a913253f7..de1825c57 100644 --- a/.context/decisions/0043-one-person-one-account.md +++ b/.context/decisions/0043-one-person-one-account.md @@ -4,14 +4,12 @@ **Date:** 2026-09-05 **Owner:** Seyed Yahya Shirazi -> Numbering note: epic #1250 runs phases in parallel, and this one (phase 4, -> #1254) lands alongside phase 3 (#1253), which owns 0042 and migration 0076. -> Until phase 3 is rebased in, 0042 does not exist on this branch and the ADR -> index's gapless check fails here by construction; it goes green with the -> rebase. +> Numbering note: epic #1250 ran phases in parallel, and this one (phase 4, +> #1254) landed alongside phase 3 (#1253), which owns 0042 and migration 0076. +> Both are now on the epic branch and the index's gapless check is green. > ADR 0041 carries an analogous note for the phase-1/phase-2 gap it saw at the -> time; this is the same situation one phase later, not a reference to the same -> gap. +> time; this was the same situation one phase later, not a reference to the +> same gap. ## Context diff --git a/AGENTS.md b/AGENTS.md index 0274e3459..5e7246f44 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -45,6 +45,7 @@ Load-bearing ones to know before touching the relevant area: 0033 (the queue stamps which engine converted each dataset; pre-stamp rows are declared current), 0034 (`datasets` stays one table under an enforced column budget; derive, don't store), 0040 (`verified` is the base tier; admin approval is the only writer of upload access), +0042 (upload access is requested once, by the person who wants it), 0043 (one person, one account: an ORCID iD, an email or a GitHub handle backs at most one live account). --- @@ -532,12 +533,12 @@ and what is historical. The entries worth knowing by name: | Group | Covers | |---|---| -| `nemar auth` | login, signup, status/whoami, profile, switch, logout, verification, SSH setup, key retrieval and regeneration | +| `nemar auth` | login, signup, status/whoami, profile, request-upload-access, switch, logout, verification, SSH setup, key retrieval and regeneration | | `nemar dataset` | validate, upload, download, status (alias: view), list, search, release, update, clone, get, commit, push, drop, ci, manifest | | `nemar dataset publish` | request, status, resend | | `nemar dataset` (access) | request-access, access, invite, collaborators | | `nemar sandbox` | training run, status, reset — required before uploading | -| `nemar admin` | users, approve, revoke, role, notify, s3, repo, ci, doi, publish, revert, make-public, delete-dataset, bulk-delete, reindex, hed-sweep, data-integrity-sweep, recording-stats-sweep, signal-defaults-sweep, zarr-fidelity-sweep, doctor, summary, notice, email-preferences, backfill-names, duplicates, e2e-test | +| `nemar admin` | users, approve, revoke, role, notify, s3, repo, ci, doi, publish, revert, make-public, delete-dataset, bulk-delete, reindex, hed-sweep, data-integrity-sweep, recording-stats-sweep, signal-defaults-sweep, zarr-fidelity-sweep, doctor, summary, notice, email-preferences, backfill-names, backfill-usernames, duplicates, e2e-test | | `nemar admin import*` | OpenNeuro import, status, rollback, retry, verify, recover (issue #754, epic #967) | | `nemar admin fleet` | drift, enforce, revalidate — governance across dataset repos (epic #713) | | `nemar admin exemplar` | create, status, remint-dois — the staging exemplar fleet | diff --git a/backend/src/db/migrations/0077_identity_uniqueness.sql b/backend/src/db/migrations/0077_identity_uniqueness.sql index 3a651cd7a..f03f4c363 100644 --- a/backend/src/db/migrations/0077_identity_uniqueness.sql +++ b/backend/src/db/migrations/0077_identity_uniqueness.sql @@ -34,10 +34,10 @@ -- the collision is actually gone (409 with the colliding rows otherwise), so -- the flag cannot be cleared back into a state the index would refuse. -- --- NUMBERING: epic #1250 runs phases in parallel. Phase 3 (#1253) owns 0076; --- this is phase 4 (#1254). Until phase 3 is rebased in, 0076 does not exist on --- this branch. Nothing here depends on it -- the two touch disjoint columns -- --- but do not read the gap as a lost file. +-- NUMBERING: epic #1250 ran phases in parallel. Phase 3 (#1253) owns 0076 and +-- this is phase 4 (#1254); both are now on the epic branch. The two touch +-- disjoint columns -- 0076 adds the upload-request stamps, this adds +-- `identity_conflict` -- so their order relative to each other does not matter. -- -- NOT ONE TRANSACTION (same reasoning as 0075's header: wrangler hands the -- file to executeSql as one string whose transactional scope is not diff --git a/backend/test/profile-username-route.test.ts b/backend/test/profile-username-route.test.ts index 7d9458818..e704ead5d 100644 --- a/backend/test/profile-username-route.test.ts +++ b/backend/test/profile-username-route.test.ts @@ -27,7 +27,8 @@ import { freshDb, realD1 } from "./helpers/d1"; const ORIGIN = "https://nemar.org"; const ENCRYPTION_KEY = "profile-username-test-encryption-key-01234"; -const ORCID_ID = "0000-0002-1825-0097"; +/** Distinct-per-row iDs; see seedUser. The last group is a counter. */ +const ORCID_ID_PREFIX = "0000-0002-9999-"; let db: Database; let app: Hono<{ Bindings: Bindings; Variables: Variables }>; @@ -51,14 +52,24 @@ interface SeedOptions { deleted?: boolean; } -/** A web/ORCID-shaped row by default: no username, ORCID verified. */ +/** + * A web/ORCID-shaped row by default: no username, ORCID verified. + * + * Each seeded row gets a DISTINCT iD unless the caller names one. It used to + * default to a single shared `ORCID_ID`, which migration 0077 now refuses + * outright (an iD backs at most one live account, ADR 0043) -- the helper was + * manufacturing exactly the duplicate that migration exists to prevent, and + * every test seeding two rows died in the fixture. Callers that pass an + * explicit `orcid` are unaffected. + */ function seedUser(email: string, options: SeedOptions = {}): number { + seededOrcidCount += 1; const row = { username: null, given_name: "Ada", family_name: "Lovelace", status: "verified", - orcid: ORCID_ID, + orcid: `${ORCID_ID_PREFIX}${String(seededOrcidCount).padStart(4, "0")}`, orcid_verified: 1, deleted: false, ...options, @@ -120,8 +131,12 @@ function usernameOf(id: number): string | null { ); } +/** Bumped per seeded row so each gets its own iD; reset with the database. */ +let seededOrcidCount = 0; + beforeEach(() => { db = freshDb(); + seededOrcidCount = 0; app = new Hono<{ Bindings: Bindings; Variables: Variables }>(); app.route("/auth", authWebRoutes); }); diff --git a/test/admin-route-inventory.unit.test.ts b/test/admin-route-inventory.unit.test.ts index 9a9827054..31f5980ef 100644 --- a/test/admin-route-inventory.unit.test.ts +++ b/test/admin-route-inventory.unit.test.ts @@ -159,7 +159,7 @@ describe("admin route inventory", () => { }); test("entry total is pinned", () => { - expect(adminRoutes.routes.length).toBe(100); + expect(adminRoutes.routes.length).toBe(102); }); // The count pin above can't see a SWAP of the two router-level middleware