Security

Passing Google's security review to read Gmail for our users

Forty-eight controls, six failures on first review, and one real gap: session cookies that browser scripts could read. I rebuilt sessions in a week.

The problem

Reading Gmail on behalf of users requires Google's restricted-scope verification. That includes an independent lab assessment known as CASA Tier 2. TAC Security was the lab.

The approval was a blocker for onboarding outside agencies. Until verification completes, Google keeps an app in testing status: capped at 100 test users, with refresh tokens that expire after seven days. A pilot agency would have to reconnect every week.

The team feared the review could take as long as six months.

I decided that each mailbox owner connects their own Google account. I rejected asking a tenant's administrator for domain-wide access. It is too large a trust request, and an administrator's consent cannot cover colleagues' mail. An OAuth token reads the mailbox of the person who granted it and nobody else's. Every synced mailbox therefore has its owner's explicit grant.

The approved permissions are reading Gmail and reading Calendar. The platform does not hold permission to send email from a user's account. An early plan included sending. I removed it before the assessment so the application asked only for what the product needed.

I ran the verification as a project of thirteen tasks, all mine.

Two pieces of that preparation are easy to state. Production dependency findings were cleared before the lab scan. And deleting a mailbox now removes its stored files too.

Six failures, one real

The assessment asked for evidence against 48 security controls. On first review the lab passed 42 and failed 6. Five needed more evidence.

One was a real gap. Session cookies were readable by browser scripts, and the lab refused the compensating controls I proposed. I chose to fix the control at its root instead of contesting the finding.

Cookies scripts cannot read

The auth library's cookie defaults set neither HttpOnly nor Secure. Session cookies were written in six places, and they must agree, or one writer reissues a cookie the others hardened. Every writer now passes its options through one function.

One function every cookie writer goes throughIllustrative
// Every place that writes a session cookie goes through this one function,
// so no writer can forget an attribute.
function lockDownCookie<T extends CookieOptions>(options: T) {
  return {
    ...options,
    httpOnly: true, // never readable from page scripts
    secure: isProduction(), // only sent over HTTPS
  };
}

That covers new sessions. A request cannot tell the server whether an incoming cookie was HttpOnly, so auth responses also reissue the existing cookies with the hardened attributes. Sessions from before the deploy are hardened on their next read.

A short-lived token for the browser

Browser code still has to authenticate its API calls. It asks one endpoint, which validates the cookie session on the server and hands back the user and the current access token.

The session endpoint returns an access token, never the refresh tokenIllustrative
// The browser asks the server who it is. The server reads the cookie.
export async function getSession(request: Request) {
  const session = await readSessionFromCookie(request);
  if (!session) return json({ signedIn: false });

  return json({
    signedIn: true,
    user: session.user,
    accessToken: session.accessToken, // short-lived
    expiresAt: session.expiresAt,
    // The long-lived refresh token stays in the cookie. It is never returned.
  });
}
Browser code asks the session endpoint for a session. The endpoint reads HttpOnly cookies, validates them with the identity provider, and returns a short-lived access token held in memory and sent as a bearer to API routes. Sign-out revokes at the provider and always expires the cookies.
The server owns the session. The browser holds only a short-lived access token.

The browser keeps that token in memory only. A small broker serves it until it is about to expire, then asks again. The refresh token never reaches JavaScript.

The browser's only source of a tokenIllustrative
let current: Session | null = null; // held in memory, never in storage

export async function currentToken(): Promise<string | null> {
  // Reuse the token until shortly before it expires...
  if (current && !expiresSoon(current)) return current.accessToken;

  // ...then ask the server for a fresh one.
  current = await fetchSession();
  return current?.accessToken ?? null;
}

Moving the session behind cookies created new edges, and each one needed an answer.

Sign-out that cannot half-fail

Signing out calls the identity provider, and that call can fail. If it does, the browser must still end up signed out. The handler expires every session cookie itself.

Clear the cookies even when the provider failsIllustrative
const result = await identityProvider.signOut(session);

if (!result.ok) {
  // The provider can fail before any cookie is removed.
  log.warn('signOut: provider sign-out failed; clearing cookies locally');
}

// Expire the cookies ourselves either way, even during an outage.
return expireSessionCookies(json({ ok: true }));

If the browser cannot reach the handler at all, the interface reports the error. It does not pretend the sign-out succeeded or navigate away.

What changed

The rejected control was fixed within a week, and the platform passed the assessment.

What I can't tell you from the record: the date of Google's own approval. It was never written down.