ReviewOS

also looking at this

stacks/commshq

test(auth): pin the browser session contract, and gate the deploy

#14
Merged glennmichael123 wants to merge feat/auth-framework-session into main
4 files +400 -8

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

.github/workflows/deploy.ymlmodified+8-0
Changes to .github/workflows/deploy.yml
@@ -105,6 +105,14 @@ jobs:
105105 DOTENV_PRIVATE_KEY_PRODUCTION: ${{ secrets.DOTENV_PRIVATE_KEY_PRODUCTION }}
106106 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
107107
108 # SMOKE_EMAIL / SMOKE_PASSWORD turn on the sign-in leg: a real POST /login
109 # followed by an authenticated GET /api/v1/workspace. Without them the
110 # script still runs and says it skipped that leg, so this step does not
111 # fail before the secrets exist. The account must belong to a team and
112 # must not have two-factor enabled.
108113 - name: Smoke test production
109114 if: github.event_name == 'push' || github.event.inputs.dry_run == 'false'
110115 run: bun run deployment:smoke
116 env:
117 SMOKE_EMAIL: ${{ secrets.SMOKE_EMAIL }}
118 SMOKE_PASSWORD: ${{ secrets.SMOKE_PASSWORD }}
scripts/smoke-deployment.tsmodified+101-8
Changes to scripts/smoke-deployment.ts
@@ -50,14 +50,107 @@ async function readWithRetry(path: string): Promise<string> {
5050 throw new Error(`${baseUrl}${path} failed after ${attempts} attempts: ${lastFailure}`)
5151}
5252
53const home = await readWithRetry('/')
54if (!home.includes(contract.homeText)) throw new Error(`Homepage does not contain the expected release marker: ${contract.homeText}`)
53/**
54 * The token out of a `Set-Cookie` header, or a failure that says which half of
55 * the contract broke.
56 *
57 * Exported so the parsing can be tested without a network round trip; the live
58 * checks below only run under `import.meta.main`.
59 */
60export function assertSessionCookie(setCookie: string | null | undefined): string {
61 if (!setCookie)
62 throw new Error('POST /login returned no Set-Cookie header, so the sign-in issued no session.')
5563
56const login = await readWithRetry('/login')
57if (!/<(?:form|main)\b/i.test(login)) throw new Error('Login smoke response does not contain an application form or main region.')
64 const match = /(?:^|,\s*)auth-token=([^;,\s]*)/.exec(setCookie)
65 if (!match)
66 throw new Error(`POST /login set no auth-token cookie. Set-Cookie was: ${setCookie.slice(0, 200)}`)
67 if (!match[1])
68 throw new Error('POST /login set an empty auth-token cookie.')
5869
59const robots = await readWithRetry('/robots.txt')
60if (robots.includes('http://localhost')) throw new Error('Deployed robots.txt contains a localhost URL.')
61if (!robots.includes(new URL(baseUrl).hostname)) throw new Error(`Deployed robots.txt does not name ${new URL(baseUrl).hostname}.`)
70 return decodeURIComponent(match[1])
71}
72
73/**
74 * The attributes a deployed session cookie must carry.
75 *
76 * `Secure` is checked because the framework decides it from `config.app.url`,
77 * which falls back to `commshq.localhost` — a loopback host, which drops the
78 * flag. A misconfigured or undecryptable APP_URL would therefore ship a
79 * long-lived session token in the clear, and nothing else would notice.
80 */
81export function assertSessionCookieAttributes(setCookie: string): void {
82 const required = ['HttpOnly', 'Path=/', 'SameSite=Lax', 'Secure']
83 const missing = required.filter(attribute => !new RegExp(`(?:^|;\\s*)${attribute}`, 'i').test(setCookie))
84
85 if (missing.length)
86 throw new Error(`The deployed auth-token cookie is missing ${missing.join(', ')}. Set-Cookie was: ${setCookie.slice(0, 200)}`)
87
88 if (!/(?:^|;\s*)Max-Age=[1-9]\d*/i.test(setCookie))
89 throw new Error(`The deployed auth-token cookie carries no positive Max-Age. Set-Cookie was: ${setCookie.slice(0, 200)}`)
90}
91
92/**
93 * Sign in for real, then spend the session on an authenticated route.
94 *
95 * Static page checks pass with sign-in completely broken, which is how a
96 * schema or session regression reaches production unnoticed. `/api/v1/workspace`
97 * is the only auth-guarded GET in routes/api.ts, so it is the one route that can
98 * be exercised without a side effect.
99 *
100 * Deliberately not retried: POST /login is rate limited to 5/minute
101 * (routes/auth.ts:23), and a retry loop would spend that budget and report a
102 * 429 as a deploy failure.
103 */
104async function assertSignInWorks(): Promise<boolean> {
105 const email = process.env.SMOKE_EMAIL
106 const password = process.env.SMOKE_PASSWORD
107
108 if (!email || !password) {
109 console.log(' sign-in leg skipped: SMOKE_EMAIL and SMOKE_PASSWORD are not set.')
110 return false
111 }
112
113 const signIn = await fetch(`${baseUrl}/login`, {
114 method: 'POST',
115 headers: { 'content-type': 'application/json', 'accept': 'application/json' },
116 body: JSON.stringify({ email, password }),
117 redirect: 'manual',
118 signal: AbortSignal.timeout(15_000),
119 })
120
121 if (signIn.status !== 200)
122 throw new Error(`POST /login returned ${signIn.status}, expected 200. A 422 usually means the smoke credentials are wrong; a 429 means the rate limit was already spent.`)
123
124 const setCookie = signIn.headers.get('set-cookie')
125 if (!setCookie && /twoFactor|two_factor|challenge/i.test(await signIn.clone().text()))
126 throw new Error('POST /login answered with a two-factor challenge instead of a session. The smoke account must not have two-factor enabled.')
62127
63console.log(`Deployment smoke passed for ${baseUrl}: homepage, login, and robots metadata are live.`)
128 const token = assertSessionCookie(setCookie)
129 assertSessionCookieAttributes(setCookie as string)
130
131 const workspace = await fetch(`${baseUrl}/api/v1/workspace`, {
132 headers: { accept: 'application/json', cookie: `auth-token=${encodeURIComponent(token)}` },
133 signal: AbortSignal.timeout(15_000),
134 })
135
136 if (workspace.status !== 200)
137 throw new Error(`GET /api/v1/workspace returned ${workspace.status} for a freshly issued session. A 401 means the cookie was not accepted; a 403 means the smoke account belongs to no team.`)
138
139 return true
140}
141
142if (import.meta.main) {
143 const home = await readWithRetry('/')
144 if (!home.includes(contract.homeText)) throw new Error(`Homepage does not contain the expected release marker: ${contract.homeText}`)
145
146 const login = await readWithRetry('/login')
147 if (!/<(?:form|main)\b/i.test(login)) throw new Error('Login smoke response does not contain an application form or main region.')
148
149 const robots = await readWithRetry('/robots.txt')
150 if (robots.includes('http://localhost')) throw new Error('Deployed robots.txt contains a localhost URL.')
151 if (!robots.includes(new URL(baseUrl).hostname)) throw new Error(`Deployed robots.txt does not name ${new URL(baseUrl).hostname}.`)
152
153 const signedIn = await assertSignInWorks()
154
155 console.log(`Deployment smoke passed for ${baseUrl}: homepage, login, ${signedIn ? 'a real sign-in, ' : ''}and robots metadata are live.`)
156}
tests/Unit/DeploymentSmoke.test.tsadded+66-0
Changes to tests/Unit/DeploymentSmoke.test.ts
@@ -0,0 +1,66 @@
1import { describe, expect, it } from 'bun:test'
2import { assertSessionCookie, assertSessionCookieAttributes } from '../../scripts/smoke-deployment'
3
4/**
5 * The deploy smoke test signs in for real and spends the session on an
6 * authenticated route. These cover the header parsing without a network round
7 * trip, so a broken assertion is caught here rather than by a red deploy.
8 */
9
10const LIVE = 'auth-token=abc123; Path=/; HttpOnly; SameSite=Lax; Max-Age=604800; Secure'
11
12describe('deployment smoke: session cookie parsing', () => {
13 it('reads the token out of a live Set-Cookie header', () => {
14 expect(assertSessionCookie(LIVE)).toBe('abc123')
15 })
16
17 it('url-decodes the value, since the framework encodes it', () => {
18 expect(assertSessionCookie('auth-token=a%2Bb; Path=/; HttpOnly')).toBe('a+b')
19 })
20
21 it('finds the cookie when other cookies precede it', () => {
22 expect(assertSessionCookie('other=1; Path=/, auth-token=xyz; Path=/; HttpOnly')).toBe('xyz')
23 })
24
25 it('names the real failure when sign-in issued no cookie at all', () => {
26 expect(() => assertSessionCookie(null)).toThrow('issued no session')
27 expect(() => assertSessionCookie(undefined)).toThrow('issued no session')
28 })
29
30 it('distinguishes a missing cookie from an empty one', () => {
31 expect(() => assertSessionCookie('session=nope; Path=/')).toThrow('set no auth-token cookie')
32 expect(() => assertSessionCookie('auth-token=; Path=/')).toThrow('empty auth-token cookie')
33 })
34})
35
36describe('deployment smoke: session cookie attributes', () => {
37 it('accepts the attributes a deployed session must carry', () => {
38 expect(() => assertSessionCookieAttributes(LIVE)).not.toThrow()
39 })
40
41 /**
42 * The regression this guards. shouldSecureAuthCookie() reads config.app.url,
43 * which falls back to commshq.localhost - a loopback host, so the flag is
44 * dropped and a 7-to-30 day session token ships in the clear.
45 */
46 it('rejects a production cookie served without Secure', () => {
47 expect(() => assertSessionCookieAttributes('auth-token=abc; Path=/; HttpOnly; SameSite=Lax; Max-Age=604800'))
48 .toThrow('missing Secure')
49 })
50
51 it('rejects a cookie readable by JavaScript', () => {
52 expect(() => assertSessionCookieAttributes('auth-token=abc; Path=/; SameSite=Lax; Max-Age=604800; Secure'))
53 .toThrow('missing HttpOnly')
54 })
55
56 it('reports every missing attribute at once', () => {
57 expect(() => assertSessionCookieAttributes('auth-token=abc; Max-Age=604800'))
58 .toThrow('missing HttpOnly, Path=/, SameSite=Lax, Secure')
59 })
60
61 it('rejects a session cookie with no positive Max-Age', () => {
62 const base = 'auth-token=abc; Path=/; HttpOnly; SameSite=Lax; Secure'
63 expect(() => assertSessionCookieAttributes(base)).toThrow('no positive Max-Age')
64 expect(() => assertSessionCookieAttributes(`${base}; Max-Age=0`)).toThrow('no positive Max-Age')
65 })
66})