From 0778db8ddfca72fe5149e2549c57b8e82298969b Mon Sep 17 00:00:00 2001 From: "B." Date: Mon, 19 Oct 2026 12:51:07 -0700 Subject: [PATCH 01/13] feat(auth): add live bearer validation helper --- packages/cli/src/lib/auth-health.ts | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 packages/cli/src/lib/auth-health.ts diff --git a/packages/cli/src/lib/auth-health.ts b/packages/cli/src/lib/auth-health.ts new file mode 100644 index 0000000..2e0ab69 --- /dev/null +++ b/packages/cli/src/lib/auth-health.ts @@ -0,0 +1,41 @@ +export type AccessTokenCheck = { + state: 'valid' | 'invalid' | 'unknown' | 'missing' + status?: number + error?: string +} + +export async function checkAccessToken( + baseURL: string, + token: string | undefined, + options: { + timeoutMs?: number + fetcher?: typeof fetch + } = {}, +): Promise { + if (!token) return { state: 'missing' } + + const fetcher = options.fetcher ?? fetch + const controller = new AbortController() + const timeout = setTimeout(() => controller.abort(), options.timeoutMs ?? 2_000) + + try { + const response = await fetcher(new URL('/v1/app/setup', baseURL), { + headers: { authorization: `Bearer ${token}` }, + signal: controller.signal, + }) + + if (response.status === 401 || response.status === 403) { + return { state: 'invalid', status: response.status } + } + if (response.ok) return { state: 'valid', status: response.status } + + return { state: 'unknown', status: response.status } + } catch (error) { + return { + state: 'unknown', + error: error instanceof Error ? error.message : String(error), + } + } finally { + clearTimeout(timeout) + } +} From 632d353670c0a37596f5127b848f9da111844405 Mon Sep 17 00:00:00 2001 From: "B." Date: Mon, 19 Oct 2026 12:51:17 -0700 Subject: [PATCH 02/13] feat(auth): add live check to auth status --- packages/cli/src/commands/auth/status.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands/auth/status.ts b/packages/cli/src/commands/auth/status.ts index 0d0f94e..6c378b2 100644 --- a/packages/cli/src/commands/auth/status.ts +++ b/packages/cli/src/commands/auth/status.ts @@ -1,14 +1,20 @@ +import { Flags } from '@oclif/core' import { BeeperCommand } from '../../lib/command.js' +import { checkAccessToken } from '../../lib/auth-health.js' import { resolveTarget } from '../../lib/targets.js' import { printData } from '../../lib/output.js' export default class AuthStatus extends BeeperCommand { static override summary = 'Show stored auth for the selected target' + static override flags = { + check: Flags.boolean({ default: false, description: 'Validate the selected bearer token against the live target' }), + } async run(): Promise { const { flags } = await this.parse(AuthStatus) const target = await resolveTarget({ target: flags.target, baseURL: flags['base-url'] }) - const authenticated = Boolean(process.env.BEEPER_ACCESS_TOKEN || target.auth?.accessToken) + const token = process.env.BEEPER_ACCESS_TOKEN || target.auth?.accessToken + const authenticated = Boolean(token) const data = { authenticated, target: target.id, @@ -17,6 +23,7 @@ export default class AuthStatus extends BeeperCommand { clientID: target.auth?.clientID, expiresAt: target.auth?.expiresAt, scope: target.auth?.scope, + ...(flags.check ? { check: await checkAccessToken(target.baseURL, token) } : {}), } await printData(data, flags.json ? 'json' : 'human') } From fa5049da7edfe7bb20691d0e4f20c8ce7e410062 Mon Sep 17 00:00:00 2001 From: "B." Date: Mon, 19 Oct 2026 12:51:32 -0700 Subject: [PATCH 03/13] test(auth): cover live bearer validation --- packages/cli/test/auth-health.test.ts | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 packages/cli/test/auth-health.test.ts diff --git a/packages/cli/test/auth-health.test.ts b/packages/cli/test/auth-health.test.ts new file mode 100644 index 0000000..b40ab82 --- /dev/null +++ b/packages/cli/test/auth-health.test.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from 'bun:test' +import { checkAccessToken } from '../src/lib/auth-health.js' + +describe('checkAccessToken', () => { + it('reports missing without making a request', async () => { + let calls = 0 + const result = await checkAccessToken('http://127.0.0.1:23373', undefined, { + fetcher: async () => { + calls += 1 + return new Response(null, { status: 200 }) + }, + }) + + expect(result).toEqual({ state: 'missing' }) + expect(calls).toBe(0) + }) + + it('reports valid when the target accepts the bearer', async () => { + let authHeader: string | null = null + const result = await checkAccessToken('http://127.0.0.1:23373', 'secret-token', { + fetcher: async (_input, init) => { + authHeader = new Headers(init?.headers).get('authorization') + return new Response('{}', { status: 200 }) + }, + }) + + expect(result).toEqual({ state: 'valid', status: 200 }) + expect(authHeader).toBe('Bearer secret-token') + }) + + it('reports invalid on authentication rejection without exposing the token', async () => { + const result = await checkAccessToken('http://127.0.0.1:23373', 'secret-token', { + fetcher: async () => new Response('Invalid token', { status: 401 }), + }) + + expect(result).toEqual({ state: 'invalid', status: 401 }) + expect(JSON.stringify(result)).not.toContain('secret-token') + }) + + it('reports unknown for non-auth failures', async () => { + const statusResult = await checkAccessToken('http://127.0.0.1:23373', 'token', { + fetcher: async () => new Response('server error', { status: 500 }), + }) + expect(statusResult).toEqual({ state: 'unknown', status: 500 }) + + const networkResult = await checkAccessToken('http://127.0.0.1:23373', 'token', { + fetcher: async () => { + throw new Error('connection refused') + }, + }) + expect(networkResult).toEqual({ state: 'unknown', error: 'connection refused' }) + }) +}) From e46327b1a3dc4e731281adbfda5a7c0624db6624 Mon Sep 17 00:00:00 2001 From: "B." Date: Mon, 19 Oct 2026 12:51:57 -0700 Subject: [PATCH 04/13] docs(auth): document live token validation --- packages/cli/src/lib/manifest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/lib/manifest.ts b/packages/cli/src/lib/manifest.ts index 5b7dfe6..484b07b 100644 --- a/packages/cli/src/lib/manifest.ts +++ b/packages/cli/src/lib/manifest.ts @@ -118,7 +118,7 @@ export const commandManifest: ManifestCommand[] = [ { command: 'auth status', description: 'Show stored auth for the selected target', - examples: ['beeper auth status', 'beeper auth status --json'], + examples: ['beeper auth status', 'beeper auth status --check', 'beeper auth status --check --json'], }, { command: 'auth logout', From 628036473dd48a91328207aad91647827a0af37d Mon Sep 17 00:00:00 2001 From: "B." Date: Mon, 19 Oct 2026 12:52:02 -0700 Subject: [PATCH 05/13] docs(auth): document live token validation --- packages/cli/docs/auth.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cli/docs/auth.md b/packages/cli/docs/auth.md index 7ca3a99..85a76b4 100644 --- a/packages/cli/docs/auth.md +++ b/packages/cli/docs/auth.md @@ -10,7 +10,7 @@ target file under `~/.beeper/targets/`; `BEEPER_ACCESS_TOKEN` overrides it. ## Commands ```sh -beeper auth status +beeper auth status [--check] beeper auth logout beeper auth email start --email # headless sign-in, step 1 beeper auth email response --setup-request-id --code # step 2 @@ -30,7 +30,7 @@ beeper verify cancel ## Notes -- `auth status` reports the token source (env vs. target file) and metadata; it does not call the network. +- `auth status` reports the token source (env vs. target file) and metadata without a network call by default. Add `--check` to ask the selected target whether that bearer is still accepted; the result is `valid`, `invalid`, `unknown`, or `missing`, and the token itself is never printed. - `auth logout` revokes the token at the Desktop OAuth endpoint and clears the local copy. - `auth email start` + `auth email response` sign in with an emailed code, no browser and no prompts. `response` also takes `--username --yes` when the email has no Beeper account yet. Walkthrough: [Headless server setup](setup.md#headless-server-setup). - `verify` (no subcommand) walks the most common SAS/emoji verification flow interactively. @@ -41,7 +41,7 @@ beeper verify cancel ## Examples ```sh -beeper auth status --json +beeper auth status --check --json beeper auth email start --email you@example.com -t server --json beeper auth email response --setup-request-id --code 123456 -t server --json beeper verify From 7a056089eaedb6a8ec358e5eb48a2c4b1897067a Mon Sep 17 00:00:00 2001 From: "B." Date: Mon, 19 Oct 2026 12:52:06 -0700 Subject: [PATCH 06/13] docs(auth): document live token validation --- packages/cli/README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/cli/README.md b/packages/cli/README.md index 9545291..7a0c2d7 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -895,11 +895,18 @@ Show stored auth for the selected target beeper auth status ``` +Flags: + +| Flag | Type | Description | +| --- | --- | --- | +| `--check` | boolean | Validate the selected bearer token against the live target | + Examples: ```sh beeper auth status -beeper auth status --json +beeper auth status --check +beeper auth status --check --json ``` Global flags: `--base-url`, `--target`, `--debug`, `--events`, `--full`, `--json`, `--quiet`, `--read-only`, `--timeout`, `--yes`. From 1d089ddd8a3c8529d0a349615cd488152daff8d6 Mon Sep 17 00:00:00 2001 From: "B." Date: Mon, 19 Oct 2026 12:58:39 -0700 Subject: [PATCH 07/13] fix(auth): render live token check in human output --- packages/cli/src/lib/ink/components.tsx | 30 ++++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/lib/ink/components.tsx b/packages/cli/src/lib/ink/components.tsx index de288d1..954dc20 100644 --- a/packages/cli/src/lib/ink/components.tsx +++ b/packages/cli/src/lib/ink/components.tsx @@ -636,31 +636,45 @@ export const DoctorCard: React.FC<{ checks: Array<{ ok: boolean; name: string; d } export const AuthStatusCard: React.FC<{ auth: RecordValue }> = ({ auth }) => { - const ok = Boolean(auth.authenticated) + const check = auth.check && typeof auth.check === 'object' ? auth.check as RecordValue : undefined + const checkState = check ? stringValue(check.state) : undefined + const stored = Boolean(auth.authenticated) + const ok = checkState ? checkState === 'valid' : stored const expires = auth.expiresAt ? formatTime(auth.expiresAt) ?? String(auth.expiresAt) : undefined + const status = checkState === 'valid' + ? { color: theme.mine, glyph: glyphs.check, label: 'token valid' } + : checkState === 'invalid' + ? { color: theme.danger, glyph: glyphs.cross, label: 'token invalid' } + : checkState === 'unknown' + ? { color: theme.warn, glyph: glyphs.ring, label: 'check unknown' } + : checkState === 'missing' + ? { color: theme.warn, glyph: glyphs.ring, label: 'no token' } + : stored + ? { color: theme.mine, glyph: glyphs.check, label: 'signed in' } + : { color: theme.warn, glyph: glyphs.ring, label: 'signed out' } + return ( - + Authentication - {ok ? ( - {glyphs.check} signed in - ) : ( - {glyphs.ring} signed out - )} + {status.glyph} {status.label} {String(auth.baseURL)} } /> + {checkState ? : null} + {typeof check?.status === 'number' ? : null} + {check?.error ? : null} {auth.clientID ? : null} {auth.scope ? : null} {expires ? : null} {!ok && ( )} From 78d2fbd6f7b108e47e7b9966990fd199bc3d8919 Mon Sep 17 00:00:00 2001 From: "B." Date: Tue, 20 Oct 2026 00:44:17 -0700 Subject: [PATCH 08/13] fix(auth): redact bearer-bearing errors and honor status timeout --- packages/cli/src/lib/auth-health.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/lib/auth-health.ts b/packages/cli/src/lib/auth-health.ts index 2e0ab69..2c270d8 100644 --- a/packages/cli/src/lib/auth-health.ts +++ b/packages/cli/src/lib/auth-health.ts @@ -4,6 +4,20 @@ export type AccessTokenCheck = { error?: string } +/** Translate the CLI's documented --timeout syntax into a safe timer duration. */ +export function parseTimeoutMs(value: string | undefined): number { + if (value === undefined) return 2_000 + const match = value.trim().match(/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/i) + if (!match) throw new Error('Invalid --timeout. Use a positive duration such as 500ms, 30s, 2m, or 1h.') + const unit = (match[2] ?? 'ms').toLowerCase() + const multiplier = { ms: 1, s: 1_000, m: 60_000, h: 3_600_000 }[unit as 'ms' | 's' | 'm' | 'h'] + const milliseconds = Math.round(Number(match[1]) * multiplier) + if (!Number.isSafeInteger(milliseconds) || milliseconds < 1 || milliseconds > 2_147_483_647) { + throw new Error('Invalid --timeout. Duration must be between 1ms and 2147483647ms.') + } + return milliseconds +} + export async function checkAccessToken( baseURL: string, token: string | undefined, @@ -30,11 +44,10 @@ export async function checkAccessToken( if (response.ok) return { state: 'valid', status: response.status } return { state: 'unknown', status: response.status } - } catch (error) { - return { - state: 'unknown', - error: error instanceof Error ? error.message : String(error), - } + } catch { + // Fetch implementations can echo the Authorization header (including the bearer) in errors. + // Never return a raw exception from a request that included a credential. + return { state: 'unknown', error: 'Token check failed; verify target connectivity and token configuration.' } } finally { clearTimeout(timeout) } From 360830b8402bcd2690c45dee7bc07c5e92d3f2e4 Mon Sep 17 00:00:00 2001 From: "B." Date: Tue, 20 Oct 2026 00:44:19 -0700 Subject: [PATCH 09/13] fix(auth): redact bearer-bearing errors and honor status timeout --- packages/cli/src/commands/auth/status.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/auth/status.ts b/packages/cli/src/commands/auth/status.ts index 6c378b2..e02c704 100644 --- a/packages/cli/src/commands/auth/status.ts +++ b/packages/cli/src/commands/auth/status.ts @@ -1,6 +1,6 @@ import { Flags } from '@oclif/core' import { BeeperCommand } from '../../lib/command.js' -import { checkAccessToken } from '../../lib/auth-health.js' +import { checkAccessToken, parseTimeoutMs } from '../../lib/auth-health.js' import { resolveTarget } from '../../lib/targets.js' import { printData } from '../../lib/output.js' @@ -23,7 +23,7 @@ export default class AuthStatus extends BeeperCommand { clientID: target.auth?.clientID, expiresAt: target.auth?.expiresAt, scope: target.auth?.scope, - ...(flags.check ? { check: await checkAccessToken(target.baseURL, token) } : {}), + ...(flags.check ? { check: await checkAccessToken(target.baseURL, token, { timeoutMs: parseTimeoutMs(flags.timeout) }) } : {}), } await printData(data, flags.json ? 'json' : 'human') } From f43e635370a578a9b1612d1eb7f047a65132699f Mon Sep 17 00:00:00 2001 From: "B." Date: Tue, 20 Oct 2026 00:44:21 -0700 Subject: [PATCH 10/13] test(auth): cover secret-safe failures and timeout propagation --- packages/cli/test/auth-health.test.ts | 43 +++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/packages/cli/test/auth-health.test.ts b/packages/cli/test/auth-health.test.ts index b40ab82..65d8c48 100644 --- a/packages/cli/test/auth-health.test.ts +++ b/packages/cli/test/auth-health.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'bun:test' -import { checkAccessToken } from '../src/lib/auth-health.js' +import { checkAccessToken, parseTimeoutMs } from '../src/lib/auth-health.js' describe('checkAccessToken', () => { it('reports missing without making a request', async () => { @@ -48,6 +48,45 @@ describe('checkAccessToken', () => { throw new Error('connection refused') }, }) - expect(networkResult).toEqual({ state: 'unknown', error: 'connection refused' }) + expect(networkResult).toEqual({ state: 'unknown', error: 'Token check failed; verify target connectivity and token configuration.' }) + }) +}) + + +describe('auth status credential/timeout regression', () => { + it('never returns malformed bearer data from fetch errors', async () => { + const token = 'line-one\\nline-two-secret' + const result = await checkAccessToken('http://127.0.0.1:23373', token, { + fetcher: async () => { + throw new TypeError(`Header authorization has invalid value: 'Bearer ${token}'`) + }, + }) + expect(result.state).toBe('unknown') + expect(JSON.stringify(result)).not.toContain(token) + expect(JSON.stringify(result)).not.toContain('line-two-secret') + expect(JSON.stringify(result)).not.toContain('Bearer') + }) + + it('parses the global timeout flag, including documented minute/hour units', () => { + expect(parseTimeoutMs(undefined)).toBe(2_000) + expect(parseTimeoutMs('500ms')).toBe(500) + expect(parseTimeoutMs('30s')).toBe(30_000) + expect(parseTimeoutMs('2m')).toBe(120_000) + expect(parseTimeoutMs('1h')).toBe(3_600_000) + expect(() => parseTimeoutMs('0ms')).toThrow() + expect(() => parseTimeoutMs('abc')).toThrow() + expect(() => parseTimeoutMs('999999999h')).toThrow() + }) + + it('honors the supplied small timeout rather than the default two seconds', async () => { + const result = await checkAccessToken('http://127.0.0.1:23373', 'secret-token', { + timeoutMs: 5, + fetcher: async (_url, init) => new Promise((_resolve, reject) => { + if (init?.signal?.aborted) return reject(new Error('aborted')) + init?.signal?.addEventListener('abort', () => reject(new Error('aborted')), { once: true }) + }), + }) + expect(result.state).toBe('unknown') + expect(JSON.stringify(result)).not.toContain('secret-token') }) }) From a8246861719b1d04ebf999e78695bf754e8e7709 Mon Sep 17 00:00:00 2001 From: "B." Date: Tue, 20 Oct 2026 00:45:10 -0700 Subject: [PATCH 11/13] test(auth): exercise actual malformed bearer newline without leaking secrets --- packages/cli/test/auth-health.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/cli/test/auth-health.test.ts b/packages/cli/test/auth-health.test.ts index 65d8c48..02370f6 100644 --- a/packages/cli/test/auth-health.test.ts +++ b/packages/cli/test/auth-health.test.ts @@ -52,10 +52,9 @@ describe('checkAccessToken', () => { }) }) - describe('auth status credential/timeout regression', () => { it('never returns malformed bearer data from fetch errors', async () => { - const token = 'line-one\\nline-two-secret' + const token = 'line-one\nline-two-secret' const result = await checkAccessToken('http://127.0.0.1:23373', token, { fetcher: async () => { throw new TypeError(`Header authorization has invalid value: 'Bearer ${token}'`) From 11bd692e6916ccf6d726f8a3c690aee3462cacc2 Mon Sep 17 00:00:00 2001 From: "B." Date: Tue, 20 Oct 2026 00:58:15 -0700 Subject: [PATCH 12/13] fix(auth): validate bearer against protected accounts endpoint --- packages/cli/src/lib/auth-health.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/lib/auth-health.ts b/packages/cli/src/lib/auth-health.ts index 2c270d8..1867a3f 100644 --- a/packages/cli/src/lib/auth-health.ts +++ b/packages/cli/src/lib/auth-health.ts @@ -33,7 +33,9 @@ export async function checkAccessToken( const timeout = setTimeout(() => controller.abort(), options.timeoutMs ?? 2_000) try { - const response = await fetcher(new URL('/v1/app/setup', baseURL), { + // /v1/app/setup is public and may return 200 during needs-login; accounts is bearer-protected. + // We only inspect the status code, never log or retain the response body. + const response = await fetcher(new URL('/v1/accounts', baseURL), { headers: { authorization: `Bearer ${token}` }, signal: controller.signal, }) From d4198735c5dcb6ca48f330874640c6072b574a67 Mon Sep 17 00:00:00 2001 From: "B." Date: Tue, 20 Oct 2026 00:58:31 -0700 Subject: [PATCH 13/13] test(auth): reject stale bearers despite public setup 200 response --- packages/cli/test/auth-health.test.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/cli/test/auth-health.test.ts b/packages/cli/test/auth-health.test.ts index 02370f6..f5a5375 100644 --- a/packages/cli/test/auth-health.test.ts +++ b/packages/cli/test/auth-health.test.ts @@ -2,6 +2,20 @@ import { describe, expect, it } from 'bun:test' import { checkAccessToken, parseTimeoutMs } from '../src/lib/auth-health.js' describe('checkAccessToken', () => { + + it('checks a bearer-protected endpoint, not the public setup endpoint', async () => { + let requestedPath = '' + const result = await checkAccessToken('http://127.0.0.1:23373', 'stale-bearer', { + fetcher: async input => { + requestedPath = new URL(String(input)).pathname + return new Response('{}', { status: requestedPath === '/v1/app/setup' ? 200 : 401 }) + }, + }) + expect(requestedPath).toBe('/v1/accounts') + expect(result).toEqual({ state: 'invalid', status: 401 }) + expect(JSON.stringify(result)).not.toContain('stale-bearer') + }) + it('reports missing without making a request', async () => { let calls = 0 const result = await checkAccessToken('http://127.0.0.1:23373', undefined, {