Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.
TypeScript
fix(auth): a failed sign-in may only tell the user what we wrote
Xialie Zhuangfb645d13056f
diff --git a/server/src/__tests__/auth-public-error.test.ts b/server/src/__tests__/auth-public-error.test.tsnew file mode 100644index 0000000..231fd21--- /dev/null+++ b/server/src/__tests__/auth-public-error.test.ts@@ -0,0 +1,66 @@+/**+ * What a failed sign-in is allowed to tell the user (#102).+ *+ * The callback handler used to put `e.message` straight into the redirect+ * fragment, so whatever went wrong was rendered on the sign-in screen and+ * parked in the browser's address bar and history. That is how a Postgres+ * transaction error became user-visible copy.+ *+ * The rule is allow-by-construction, and these tests exist to keep it that+ * way: a message we WROTE for a person passes through, everything else — in+ * particular anything new — does not.+ *+ * Run: node --import tsx --test server/src/__tests__/auth-public-error.test.ts+ */+import { test } from 'node:test'+import assert from 'node:assert/strict'+import { SignInError, publicSignInError } from '../auth-errors.js'++test('a message written for the user passes through', () => {+ // These are the account-state problems the user can actually act on: go and+ // verify your email, reactivate your account, widen the OAuth scope.+ for (const m of [+ 'google account has no verified email',+ 'github account has no verified email',+ 'gitlab account is not active',+ 'gitlab account has no confirmed email',+ 'gitlab account exposes no email to read_user',+ ]) {+ assert.equal(publicSignInError(new SignInError(m)), m)+ }+})++test('the reported Postgres error never reaches the user', () => {+ const pg = new Error('current transaction is aborted, commands ignored until end of transaction block')+ assert.equal(publicSignInError(pg), 'signin_failed')+})++test('a provider response body is not put in the address bar either', () => {+ // `token exchange` failures interpolate the provider's raw body. Nobody+ // asked for that to be user-visible; it just was, because it was an Error.+ const leak = new Error('github token exchange 401: {"error":"incorrect_client_credentials"}')+ assert.equal(publicSignInError(leak), 'signin_failed')+})++test('an internal error nobody has classified yet is private by default', () => {+ // The point of allow-by-construction. A deny-list would have to be updated+ // every time a new failure mode appears; this does not.+ for (const e of [+ new Error('ECONNREFUSED 127.0.0.1:6379'),+ new TypeError('Cannot read properties of undefined'),+ Object.assign(new Error('duplicate key value violates unique constraint "users_pkey"'), { code: '23505' }),+ 'a bare string someone threw',+ null,+ undefined,+ { message: 'an object shaped like an error' },+ ]) {+ assert.equal(publicSignInError(e), 'signin_failed')+ }+})++test('a subclass of SignInError still passes through', () => {+ // Nothing subclasses it today, but the check is instanceof, not a name+ // comparison — so a future refinement does not silently go private.+ class ProviderSignInError extends SignInError {}+ assert.equal(publicSignInError(new ProviderSignInError('nope')), 'nope')+})diff --git a/server/src/api/router.ts b/server/src/api/router.tsindex 83fff09..bd26341 100644--- a/server/src/api/router.ts+++ b/server/src/api/router.ts@@ -21,7 +21,7 @@ import {import { joinAllHands, onboardStarterAgents, seedMemberDms } from '../onboardCompany.js'import {type Provider, providerEnabled, createState, consumeState, isWebOAuthProvider, WEB_OAUTH_PROVIDERS,- authorizeUrl, handleCallback, errorUrl, returnUrlAllowed,+ authorizeUrl, handleCallback, errorUrl, publicSignInError, returnUrlAllowed,} from '../oauth.js'import { adminRouter } from './admin-router.js'import { isWaitlistEnabled } from '../admin.js'@@ -721,7 +721,13 @@ api.get('/auth/callback/:provider', safe(async (req, res) => {const msg = e instanceof Error ? e.message : String(e)console.warn(`[auth] ${provider} callback failed:`, msg)await audit({ kind: 'login_failed', ip, userAgent: ua, detail: { provider, error: msg } })- res.redirect(errorUrl(claimed?.returnUrl ?? null, msg.slice(0, 120)))+ // The redirect carries only what we WROTE for the user to read. Everything+ // else — a Postgres transaction error, a provider's raw token-endpoint+ // body, a DNS failure — becomes a fixed code. The full text is already on+ // the line above and in the audit row, so nothing is lost for diagnosis;+ // what changes is that it stops being rendered on the sign-in screen and+ // parked in the browser's address bar and history (#102).+ res.redirect(errorUrl(claimed?.returnUrl ?? null, publicSignInError(e).slice(0, 120)))}}))@@ -760,7 +766,9 @@ api.post('/auth/apple/native', safe(async (req, res) => {if (e instanceof SuspendedError) { res.status(403).json({ error: 'suspended', email: e.email, reason: e.reason }); return }const msg = e instanceof Error ? e.message : String(e)console.warn('[auth] apple native sign-in failed:', msg)- res.status(400).json({ error: msg })+ // Same rule as the browser callback: the app shows this string to the+ // user, so it must be one we wrote.+ res.status(400).json({ error: publicSignInError(e) })}}))diff --git a/server/src/auth-errors.ts b/server/src/auth-errors.tsnew file mode 100644index 0000000..d52ff6f--- /dev/null+++ b/server/src/auth-errors.ts@@ -0,0 +1,31 @@+/**+ * Sign-in failure classification. Its own module, with no imports, so the unit+ * tests can pin the rule without booting env.ts / pool.ts / redis.ts — the+ * same reason cli-parse.ts is split out of cli.ts.+ */++/** A sign-in failure whose message was WRITTEN BY US for the user to read —+ * "your GitHub account has no verified email", and its siblings. Everything+ * the user can actually act on is one of these.+ *+ * It exists so the callback handler can tell those apart from the failures it+ * merely caught. That handler used to put `e.message` straight into the+ * redirect fragment, which is how a Postgres transaction error ended up+ * rendered on the sign-in screen (#102) — and, less visibly, how a provider's+ * raw token-endpoint response body could be parked in the address bar too. */+export class SignInError extends Error {}++/** What a failed sign-in is allowed to tell the user.+ *+ * Allow-by-construction: only a SignInError passes through, and everything+ * else collapses to a fixed code. A deny-list would have to be extended every+ * time a new failure mode appeared; this way an internal error introduced+ * tomorrow is private on the day it is written rather than on the day+ * somebody spots it in a screenshot.+ *+ * `signin_failed` is a code rather than a sentence to match the two the same+ * handler already emits (`bad_state`, `missing_code_or_state`); turning those+ * codes into friendly copy is a renderer concern, not this function's. */+export function publicSignInError(e: unknown): string {+ return e instanceof SignInError ? e.message : 'signin_failed'+}