fix(mail): request gmail.readonly alone, mailbox address via Gmail profile (Google verification) (#2301)
* fix(mail): request gmail.readonly alone and read the mailbox address from Gmail's profile Google's restricted-scope review (2026-08-31) bounced the Gmail connector on a "scope discrepancy": the authorization URL asked for `openid email` on top of gmail.readonly, while the Cloud Console declares gmail.readonly only, and the review string-matches the two. The extra scopes existed solely to learn the mailbox address from the id_token. Gmail's users.getProfile returns that address under gmail.readonly, so the consent request now carries exactly one scope and the callback reads the address from the profile. Also adds `app_metadata.mfa_exempt === true` to shouldEnforceMfa. Google's reviewers log in with credentials we hand them and treat a second factor as an "authentication blocker"; app_metadata is service-role only, so this is an operator switch for demo accounts, never a user-reachable setting. Tests: scope pinned in google-oauth.test.ts, profile read in gmail-client.test.ts, callback path in oauth-callback.test.ts, flag shape in mfa.test.ts. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01UD3HsDX8hnJEqpt35azxBJ * fix(auth): time-box the reviewer MFA exemption instead of a boolean flag Superagent's P1 on the first shape was fair: a boolean app_metadata.mfa_exempt relied on someone remembering to clear it. The exemption is now app_metadata.mfa_exempt_until, an ISO timestamp honoured only while it lies in the future, so a forgotten flag dies on its own. Anything malformed or non-string enforces MFA. Still service-role only, still meant for the one demo account Google's OAuth reviewers log in with. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01UD3HsDX8hnJEqpt35azxBJ --------- Co-authored-by: Jakob Wennberg <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.1
Jakob Wennberg
parent
0ad83b8d71
commit
971952fe19
File diff suppressed because one or more lines are too long
@@ -30,6 +30,12 @@ vi.mock('../lib/connections', () => ({
|
||||
saveConnection: vi.fn(),
|
||||
}))
|
||||
|
||||
// The mailbox address comes from Gmail's profile endpoint (gmail.readonly),
|
||||
// not from an id_token: the consent request carries that one scope only.
|
||||
vi.mock('../lib/gmail-client', () => ({
|
||||
getMailboxAddress: vi.fn(),
|
||||
}))
|
||||
|
||||
const { mockCreateClient } = vi.hoisted(() => ({ mockCreateClient: vi.fn() }))
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: mockCreateClient,
|
||||
@@ -39,6 +45,7 @@ vi.mock('@/lib/supabase/server', () => ({
|
||||
import { mailExtension } from '../index'
|
||||
import { createOAuthState } from '../lib/crypto'
|
||||
import { exchangeCodeForTokens } from '../lib/google-oauth'
|
||||
import { getMailboxAddress } from '../lib/gmail-client'
|
||||
import { saveConnection } from '../lib/connections'
|
||||
|
||||
const APP_URL = 'https://app.example'
|
||||
@@ -81,9 +88,9 @@ describe('mail GET /oauth/callback: the completing session must be the initiator
|
||||
refreshToken: 'refresh-1',
|
||||
accessToken: 'access-1',
|
||||
expiresAt: '2030-01-01T00:00:00Z',
|
||||
email: '[email protected]',
|
||||
scopes: 'gmail.readonly',
|
||||
scopes: ['https://www.googleapis.com/auth/gmail.readonly'],
|
||||
})
|
||||
;(getMailboxAddress as Mock).mockResolvedValue('[email protected]')
|
||||
;(saveConnection as Mock).mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
@@ -109,6 +116,18 @@ describe('mail GET /oauth/callback: the completing session must be the initiator
|
||||
emailAddress: '[email protected]',
|
||||
}),
|
||||
)
|
||||
// The address is read with the freshly granted token, under the one scope.
|
||||
expect(getMailboxAddress).toHaveBeenCalledWith('access-1')
|
||||
})
|
||||
|
||||
it('saves nothing when Gmail returns no address for the grant', async () => {
|
||||
useSession('user-1')
|
||||
;(getMailboxAddress as Mock).mockResolvedValue(null)
|
||||
|
||||
const res = await callbackRoute().handler(callbackRequest(state))
|
||||
|
||||
expect(res.headers.get('location')).toBe(`${APP_URL}/settings/mail?mail=no_address`)
|
||||
expect(saveConnection).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('refuses a completion by a different signed-in user: no exchange, no save', async () => {
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Extension } from '@/lib/extensions/types'
|
||||
import { registerMailSearchService } from '@/lib/mail-search/service'
|
||||
import { createServiceClientNoCookies } from '@/lib/auth/api-keys'
|
||||
import { GmailSearchService } from './lib/search-service'
|
||||
import { getMailboxAddress } from './lib/gmail-client'
|
||||
import { createOAuthState, verifyOAuthState } from './lib/crypto'
|
||||
import {
|
||||
buildAuthorizationUrl,
|
||||
@@ -103,7 +104,10 @@ export const mailExtension: Extension = {
|
||||
if (!tokens.refreshToken) {
|
||||
return NextResponse.redirect(`${settingsUrl}?mail=no_refresh_token`)
|
||||
}
|
||||
if (!tokens.email) {
|
||||
// The address comes from Gmail's profile endpoint rather than an
|
||||
// id_token, so the consent screen asks for gmail.readonly alone.
|
||||
const email = await getMailboxAddress(tokens.accessToken)
|
||||
if (!email) {
|
||||
// Without the address we cannot tell two grants apart, and the
|
||||
// unique key depends on it.
|
||||
return NextResponse.redirect(`${settingsUrl}?mail=no_address`)
|
||||
@@ -113,7 +117,7 @@ export const mailExtension: Extension = {
|
||||
companyId: verified.companyId,
|
||||
userId: verified.userId,
|
||||
provider: 'gmail',
|
||||
emailAddress: tokens.email,
|
||||
emailAddress: email,
|
||||
refreshToken: tokens.refreshToken,
|
||||
accessToken: tokens.accessToken,
|
||||
expiresAt: tokens.expiresAt,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
* the format and the MIME walk.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { clearMessageCache, getMessageSummary } from '../gmail-client'
|
||||
import { clearMessageCache, getMailboxAddress, getMessageSummary } from '../gmail-client'
|
||||
|
||||
const mockFetch = vi.fn()
|
||||
vi.stubGlobal('fetch', (...args: unknown[]) => mockFetch(...args))
|
||||
@@ -131,3 +131,19 @@ describe('message reads are not repeated', () => {
|
||||
expect(mockFetch).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getMailboxAddress', () => {
|
||||
it('reads the address from the Gmail profile endpoint, which gmail.readonly covers', async () => {
|
||||
respond({ emailAddress: '[email protected]', messagesTotal: 12 })
|
||||
const address = await getMailboxAddress('token')
|
||||
expect(String(mockFetch.mock.calls[0][0])).toBe(
|
||||
'https://gmail.googleapis.com/gmail/v1/users/me/profile',
|
||||
)
|
||||
expect(address).toBe('[email protected]')
|
||||
})
|
||||
|
||||
it('returns null when the profile carries no address', async () => {
|
||||
respond({ messagesTotal: 0 })
|
||||
expect(await getMailboxAddress('token')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* The consent request asks for exactly the scope declared in the Google Cloud
|
||||
* Console, and nothing more.
|
||||
*
|
||||
* Google's restricted-scope review matches the `scope` parameter of the
|
||||
* authorization URL against the console's Data Access list string for string,
|
||||
* and bounced the first submission because the URL also carried
|
||||
* `openid email`. These tests pin the request so a well-meaning "just add
|
||||
* profile" cannot silently reopen that.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { GMAIL_READONLY_SCOPE, buildAuthorizationUrl, exchangeCodeForTokens } from '../google-oauth'
|
||||
|
||||
const env = {
|
||||
clientId: 'client-id',
|
||||
clientSecret: 'client-secret',
|
||||
redirectUri: 'https://app.example.test/api/extensions/ext/mail/oauth/callback',
|
||||
}
|
||||
|
||||
const mockFetch = vi.fn()
|
||||
vi.stubGlobal('fetch', (...args: unknown[]) => mockFetch(...args))
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('buildAuthorizationUrl', () => {
|
||||
it('requests gmail.readonly and no other scope', () => {
|
||||
const url = new URL(buildAuthorizationUrl(env, 'state-token'))
|
||||
expect(url.searchParams.get('scope')).toBe(GMAIL_READONLY_SCOPE)
|
||||
})
|
||||
|
||||
it('asks for an offline grant with explicit consent and no scope inheritance', () => {
|
||||
const url = new URL(buildAuthorizationUrl(env, 'state-token'))
|
||||
expect(url.origin + url.pathname).toBe('https://accounts.google.com/o/oauth2/v2/auth')
|
||||
expect(url.searchParams.get('access_type')).toBe('offline')
|
||||
expect(url.searchParams.get('prompt')).toBe('consent')
|
||||
expect(url.searchParams.get('response_type')).toBe('code')
|
||||
expect(url.searchParams.get('state')).toBe('state-token')
|
||||
expect(url.searchParams.get('redirect_uri')).toBe(env.redirectUri)
|
||||
expect(url.searchParams.has('include_granted_scopes')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('exchangeCodeForTokens', () => {
|
||||
it('returns the tokens and granted scopes without needing an id_token', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () =>
|
||||
Promise.resolve({
|
||||
access_token: 'at',
|
||||
refresh_token: 'rt',
|
||||
expires_in: 3600,
|
||||
scope: GMAIL_READONLY_SCOPE,
|
||||
}),
|
||||
})
|
||||
const tokens = await exchangeCodeForTokens(env, 'auth-code')
|
||||
expect(tokens.accessToken).toBe('at')
|
||||
expect(tokens.refreshToken).toBe('rt')
|
||||
expect(tokens.scopes).toEqual([GMAIL_READONLY_SCOPE])
|
||||
expect(tokens).not.toHaveProperty('email')
|
||||
})
|
||||
|
||||
it('refuses a grant that came back without a refresh token', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
json: () => Promise.resolve({ access_token: 'at', expires_in: 3600 }),
|
||||
})
|
||||
await expect(exchangeCodeForTokens(env, 'auth-code')).rejects.toThrow(/refresh token/)
|
||||
})
|
||||
})
|
||||
@@ -110,6 +110,20 @@ async function gmailFetch<T>(accessToken: string, path: string): Promise<T> {
|
||||
return (await response.json()) as T
|
||||
}
|
||||
|
||||
/**
|
||||
* The address of the mailbox a token belongs to.
|
||||
*
|
||||
* Read from Gmail's own profile endpoint, which gmail.readonly covers, so the
|
||||
* consent flow never has to ask for `openid email` on top. The address is the
|
||||
* unique key of a connection: without it two grants for the same company
|
||||
* could not be told apart.
|
||||
*/
|
||||
export async function getMailboxAddress(accessToken: string): Promise<string | null> {
|
||||
const data = await gmailFetch<{ emailAddress?: string }>(accessToken, '/profile')
|
||||
const address = data.emailAddress?.trim()
|
||||
return address ? address : null
|
||||
}
|
||||
|
||||
export async function searchMessageIds(
|
||||
accessToken: string,
|
||||
query: string,
|
||||
|
||||
@@ -6,6 +6,14 @@
|
||||
* and it structurally cannot send, modify or delete: the promise made in the
|
||||
* consent screen is enforced by the grant, not by our code being careful.
|
||||
*
|
||||
* Exactly one scope, on purpose. Google's restricted-scope review compares the
|
||||
* scopes the authorization URL requests with the ones declared in the Cloud
|
||||
* Console, string for string, and bounced the first submission because the
|
||||
* URL also carried `openid email`. Those only served to learn the mailbox
|
||||
* address, which Gmail's profile endpoint returns under gmail.readonly anyway
|
||||
* (getMailboxAddress in gmail-client.ts). Adding a scope here means adding it
|
||||
* in the console and re-recording the demo video.
|
||||
*
|
||||
* Consequence worth remembering: because we never hold a send scope, the agent
|
||||
* can prepare a forward for the user but can never send one itself.
|
||||
*/
|
||||
@@ -59,7 +67,7 @@ export function buildAuthorizationUrl(env: GoogleOAuthEnv, state: string): strin
|
||||
client_id: env.clientId,
|
||||
redirect_uri: env.redirectUri,
|
||||
response_type: 'code',
|
||||
scope: `openid email ${GMAIL_READONLY_SCOPE}`,
|
||||
scope: GMAIL_READONLY_SCOPE,
|
||||
// Signed, self-expiring CSRF token. The callback refuses anything without
|
||||
// it, so omitting this breaks the flow as well as the protection.
|
||||
state,
|
||||
@@ -79,7 +87,6 @@ export interface GoogleTokens {
|
||||
refreshToken: string | null
|
||||
expiresAt: Date
|
||||
scopes: string[]
|
||||
email: string | null
|
||||
}
|
||||
|
||||
/** Raised when a grant is dead rather than the request being unlucky. */
|
||||
@@ -93,19 +100,6 @@ export class MailTokenRefreshError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
function decodeIdTokenEmail(idToken: string | undefined): string | null {
|
||||
if (!idToken) return null
|
||||
try {
|
||||
const payload = idToken.split('.')[1]
|
||||
const json = JSON.parse(Buffer.from(payload, 'base64url').toString('utf8')) as {
|
||||
email?: string
|
||||
}
|
||||
return json.email ?? null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export async function exchangeCodeForTokens(
|
||||
env: GoogleOAuthEnv,
|
||||
code: string,
|
||||
@@ -127,7 +121,6 @@ export async function exchangeCodeForTokens(
|
||||
refresh_token?: string
|
||||
expires_in?: number
|
||||
scope?: string
|
||||
id_token?: string
|
||||
error?: string
|
||||
error_description?: string
|
||||
}
|
||||
@@ -147,7 +140,6 @@ export async function exchangeCodeForTokens(
|
||||
refreshToken: body.refresh_token,
|
||||
expiresAt: new Date(Date.now() + (body.expires_in ?? 3600) * 1000),
|
||||
scopes: (body.scope ?? '').split(' ').filter(Boolean),
|
||||
email: decodeIdTokenEmail(body.id_token),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { isMfaRequired, shouldEnforceMfa } from '../mfa'
|
||||
import { isMfaExemptionActive, isMfaRequired, shouldEnforceMfa } from '../mfa'
|
||||
|
||||
describe('mfa helpers', () => {
|
||||
afterEach(() => {
|
||||
@@ -38,10 +38,41 @@ describe('mfa helpers', () => {
|
||||
expect(shouldEnforceMfa({ app_metadata: {} })).toBe(true)
|
||||
})
|
||||
|
||||
it('skips MFA while a service-role exemption is still in the future', () => {
|
||||
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
|
||||
vi.stubEnv('NEXT_PUBLIC_REQUIRE_MFA', 'true')
|
||||
const future = new Date(Date.now() + 60 * 60 * 1000).toISOString()
|
||||
expect(shouldEnforceMfa({ app_metadata: { mfa_exempt_until: future } })).toBe(false)
|
||||
})
|
||||
|
||||
it('enforces MFA again once the exemption has expired', () => {
|
||||
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
|
||||
vi.stubEnv('NEXT_PUBLIC_REQUIRE_MFA', 'true')
|
||||
const past = new Date(Date.now() - 60 * 1000).toISOString()
|
||||
expect(shouldEnforceMfa({ app_metadata: { mfa_exempt_until: past } })).toBe(true)
|
||||
})
|
||||
|
||||
it('treats a malformed or non-string exemption as no exemption', () => {
|
||||
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
|
||||
vi.stubEnv('NEXT_PUBLIC_REQUIRE_MFA', 'true')
|
||||
expect(shouldEnforceMfa({ app_metadata: { mfa_exempt_until: 'soon' } })).toBe(true)
|
||||
expect(shouldEnforceMfa({ app_metadata: { mfa_exempt_until: true } })).toBe(true)
|
||||
expect(shouldEnforceMfa({ app_metadata: { mfa_exempt_until: 4102444800000 } })).toBe(true)
|
||||
expect(shouldEnforceMfa({ app_metadata: { mfa_exempt: true } })).toBe(true)
|
||||
})
|
||||
|
||||
it('returns true when app_metadata is undefined', () => {
|
||||
vi.stubEnv('NEXT_PUBLIC_SELF_HOSTED', 'false')
|
||||
vi.stubEnv('NEXT_PUBLIC_REQUIRE_MFA', 'true')
|
||||
expect(shouldEnforceMfa({})).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isMfaExemptionActive', () => {
|
||||
it('compares against the clock it is given', () => {
|
||||
const user = { app_metadata: { mfa_exempt_until: '2026-10-01T00:00:00Z' } }
|
||||
expect(isMfaExemptionActive(user, new Date('2026-09-30T23:59:59Z'))).toBe(true)
|
||||
expect(isMfaExemptionActive(user, new Date('2026-10-01T00:00:00Z'))).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -12,12 +12,37 @@ export function isMfaRequired(): boolean {
|
||||
return flagEnabled(process.env.NEXT_PUBLIC_REQUIRE_MFA)
|
||||
}
|
||||
|
||||
/**
|
||||
* A time-boxed exemption: `app_metadata.mfa_exempt_until` holds an ISO
|
||||
* timestamp, and the gate is skipped only while that instant is in the
|
||||
* future. app_metadata is written only through the service role (never from a
|
||||
* browser session), so this is an operator switch for the one kind of account
|
||||
* that must be usable by someone who cannot enrol an authenticator: Google's
|
||||
* OAuth verification reviewers, who log in with credentials we hand them and
|
||||
* treat any second factor as an "authentication blocker".
|
||||
*
|
||||
* Time-boxed rather than a boolean so a forgotten flag cannot outlive the
|
||||
* review: the exemption dies on its own. Anything malformed enforces MFA.
|
||||
*/
|
||||
export function isMfaExemptionActive(
|
||||
user: { app_metadata?: Record<string, unknown> },
|
||||
now: Date = new Date(),
|
||||
): boolean {
|
||||
const until = user.app_metadata?.mfa_exempt_until
|
||||
if (typeof until !== 'string') return false
|
||||
const expires = Date.parse(until)
|
||||
if (Number.isNaN(expires)) return false
|
||||
return expires > now.getTime()
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if MFA should be enforced for a specific user.
|
||||
* BankID-linked users skip TOTP because BankID is inherently 2FA.
|
||||
* A live, time-boxed exemption (see isMfaExemptionActive) also skips it.
|
||||
*/
|
||||
export function shouldEnforceMfa(user: { app_metadata?: Record<string, unknown> }): boolean {
|
||||
if (!isMfaRequired()) return false
|
||||
if (user.app_metadata?.bankid_linked) return false
|
||||
if (isMfaExemptionActive(user)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user