Skip to content
37 changes: 33 additions & 4 deletions api/auth/callback.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import crypto from 'node:crypto'
import { waitUntil } from '@vercel/functions'
import { getSession, getUserOctokit, getAppOctokit, configureSessionPersistence } from '../../lib/auth.mjs'
import { github, appUrl, sessionLeaseMs } from '../../lib/config.mjs'
Expand Down Expand Up @@ -61,16 +62,28 @@ export default async function handler(req, res) {
const attempts = Array.isArray(session.oauthAttempts) ? session.oauthAttempts : []
const attempt = attempts.find((entry) => entry && entry.state === state)
let stateOk = false
if (code && state && (attempt || state === session.oauthState)) {
let oauthAgeSeconds = null
// Repeated query parameters arrive as arrays. Never coerce a list of
// GitHub codes into a comma-joined string during the token exchange.
// Do not log either the code or its state.
const singleCode = typeof code === 'string' && code.length > 0 && code.length <= 512
const singleState = typeof state === 'string' && state.length > 0
if (singleCode && singleState && (attempt || state === session.oauthState)) {
try {
const parsed = JSON.parse(Buffer.from(String(state), 'base64url').toString('utf8'))
const parsed = JSON.parse(Buffer.from(state, 'base64url').toString('utf8'))
stateOk = Number.isFinite(parsed.at)
&& Date.now() - parsed.at >= 0
&& Date.now() - parsed.at < 15 * 60 * 1000
if (stateOk) oauthAgeSeconds = Math.floor((Date.now() - parsed.at) / 1000)
} catch {
stateOk = false
}
}
if (!singleCode || !singleState) {
console.warn('github oauth callback query cardinality invalid: code_count=%d state_count=%d',
Array.isArray(code) ? code.length : code === undefined ? 0 : 1,
Array.isArray(state) ? state.length : state === undefined ? 0 : 1)
}
if (!stateOk) {
securityEvent('auth_callback_failed', { req, reasonCode: 'state_mismatch' })
res.status(400).json({ error: 'Invalid OAuth state' })
Expand Down Expand Up @@ -102,6 +115,14 @@ export default async function handler(req, res) {
}

const redirectUri = `${appUrl}/api/auth/callback`
// GitHub state includes 128 bits of CSPRNG entropy plus a timestamp.
// A bounded SHA-256 fingerprint is therefore safe to correlate within
// short-lived server logs without retaining state, code, cookie or tokens.
// Unlike securityEvent.correlate(), it works without an optional hash key.
// Do not use this on low-entropy user identifiers or access credentials.
const stateFingerprint = typeof state === 'string' && state.length <= 1024
? crypto.createHash('sha256').update(state).digest('hex').slice(0, 16)
: 'unavailable'
// GitHub's documented web-application flow sends URL-encoded form fields.
// Keep redirect_uri identical to the authorization request, and never put
// the short-lived OAuth code or client secret in a URL or diagnostic log.
Expand All @@ -125,17 +146,25 @@ export default async function handler(req, res) {
// in the browser cookie, preventing a refresh from retrying the code.
await session.save()
console.error(
'github token exchange failed: http=%d error=%s desc=%s flow=%s',
'github token exchange failed: http=%d error=%s desc=%s flow=%s oauth_age_sec=%d state_fp=%s',
tokenRes.status,
tokenData?.error,
tokenData?.error_description,
session.desktopFlow ? 'desktop' : 'web'
session.desktopFlow ? 'desktop' : 'web',
oauthAgeSeconds,
stateFingerprint
)
securityEvent('auth_callback_failed', { req, reasonCode: 'token_exchange' })
res.status(400).json({ error: 'GitHub token exchange failed' })
return
}

if (session.desktopFlow) {
console.info(
'github oauth token exchange accepted: flow=desktop oauth_age_sec=%d state_fp=%s',
oauthAgeSeconds, stateFingerprint
)
}
const userOctokit = getUserOctokit(tokenData.access_token)
const { data: user } = await userOctokit.rest.users.getAuthenticated()

Expand Down
125 changes: 125 additions & 0 deletions test/auth-login.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,131 @@ test('GitHub code exchange uses documented form encoding with matching callback
assert.equal(resCb.getHeader('location'), '/')
})

test('a GitHub bad_verification_code failure records only safe OAuth age and consumes state', async (t) => {
const desktop = mockRes()
const flow = '9f5a9d2c-2d46-42ff-8f91-e4c93814e055'
await loginHandler(mockReq({
query: { desktop_flow: flow, desktop_challenge: 'a'.repeat(64) },
}), desktop)
const state = authorizeUrl(desktop).searchParams.get('state')
const originalFetch = globalThis.fetch
globalThis.fetch = async (url) => {
assert.equal(String(url), 'https://github.com/login/oauth/access_token')
return new Response(JSON.stringify({
error: 'bad_verification_code',
error_description: 'The code passed is incorrect or expired.',
}), { headers: { 'Content-Type': 'application/json' } })
}
t.after(() => { globalThis.fetch = originalFetch })
const originalError = console.error
const errors = []
console.error = (...parts) => errors.push(parts)
t.after(() => { console.error = originalError })

const callback = mockRes()
await callbackHandler(mockReq({
headers: { cookie: cookieHeader(desktop) },
query: { code: 'one-time-placeholder', state },
}), callback)

assert.equal(callback.statusCode, 400)
assert.equal(callback.body?.error, 'GitHub token exchange failed')
const detail = errors.find((parts) => String(parts[0]).includes('github token exchange failed:'))
assert.ok(detail, 'diagnostic must emit a bounded, credential-free error')
assert.equal(detail[4], 'desktop')
assert.equal(typeof detail[5], 'number')
assert.ok(detail[5] >= 0 && detail[5] < 900)
assert.match(detail[6], /^(?:[a-f0-9]{16}|unavailable)$/)
assert.ok(!JSON.stringify(errors).includes('one-time-placeholder'), 'OAuth code must never be logged')
assert.ok(!JSON.stringify(errors).includes(state), 'OAuth state must never be logged')
const finalCookie = cookieHeader(callback)
const sealed = await sessionFromCookie(finalCookie)
assert.equal(sealed.oauthAttempts?.length, 0, 'failed exchanges consume their pending attempt')
})

test('duplicate desktop callbacks share a one-way fingerprint without logging OAuth secrets', async (t) => {
const flow = '9f5a9d2c-2d46-42ff-8f91-e4c93814e055'
const login = mockRes()
await loginHandler(mockReq({
query: { desktop_flow: flow, desktop_challenge: 'a'.repeat(64) },
}), login)
const state = authorizeUrl(login).searchParams.get('state')
const cookie = cookieHeader(login)
const originalFetch = globalThis.fetch
let exchanges = 0
globalThis.fetch = async (url) => {
const u = String(url)
if (u.includes('login/oauth/access_token')) {
exchanges++
return new Response(JSON.stringify(exchanges === 1
? { access_token: 'fake-test-token' }
: { error: 'bad_verification_code', error_description: 'The code passed is incorrect or expired.' }),
{ headers: { 'Content-Type': 'application/json' } })
}
if (u.includes('api.github.com/user')) {
return new Response(JSON.stringify({ id: 4242, login: 'account-b', node_id: 'n42', avatar_url: '', name: 'B' }),
{ headers: { 'Content-Type': 'application/json' } })
}
if (u.includes('api.github.com/user/installations')) {
return new Response(JSON.stringify({ total_count: 0, installations: [] }),
{ headers: { 'Content-Type': 'application/json' } })
}
return new Response(JSON.stringify({}), { status: 404 })
}
t.after(() => { globalThis.fetch = originalFetch })
const priorInfo = console.info
const priorError = console.error
const infos = []
const errors = []
console.info = (...parts) => infos.push(parts)
console.error = (...parts) => errors.push(parts)
t.after(() => { console.info = priorInfo; console.error = priorError })

// Both requests present the same original cookie. This models concurrent
// browser callback requests, not a replay from the post-success cookie.
const first = mockRes()
await callbackHandler(mockReq({
headers: { cookie }, query: { code: 'fake-one-use-code', state },
}), first)
const second = mockRes()
await callbackHandler(mockReq({
headers: { cookie }, query: { code: 'fake-one-use-code', state },
}), second)
assert.equal(exchanges, 2)
assert.equal(first.statusCode, 302)
assert.equal(second.statusCode, 400)
const successLog = infos.find((parts) => String(parts[0]).includes('github oauth token exchange accepted:'))
const failureLog = errors.find((parts) => String(parts[0]).includes('github token exchange failed:'))
assert.ok(successLog)
assert.ok(failureLog)
assert.match(successLog[2], /^[a-f0-9]{16}$/)
assert.equal(failureLog[6], successLog[2], 'both callback attempts correlate without exposing raw state')
const logged = JSON.stringify({ infos, errors })
assert.ok(!logged.includes('fake-one-use-code'))
assert.ok(!logged.includes(state))
})

test('repeated OAuth code query parameters are rejected before any GitHub exchange', async (t) => {
const login = mockRes()
await loginHandler(mockReq(), login)
const state = authorizeUrl(login).searchParams.get('state')
const originalFetch = globalThis.fetch
let exchanged = 0
globalThis.fetch = async () => {
exchanged++
return new Response('{}')
}
t.after(() => { globalThis.fetch = originalFetch })
const res = mockRes()
await callbackHandler(mockReq({
headers: { cookie: cookieHeader(login) },
url: '/api/auth/callback?code=first&code=second&state=' + encodeURIComponent(state),
}), res)
assert.equal(res.statusCode, 400)
assert.equal(res.body?.error, 'Invalid OAuth state')
assert.equal(exchanged, 0, 'repeated authorization codes must not reach the token endpoint')
})

test('web login started during desktop authorization never inherits desktop handoff', async (t) => {
const flow = '9f5a9d2c-2d46-42ff-8f91-e4c93814e055'
const challenge = 'a'.repeat(64)
Expand Down
Loading