Skip to content

fix(auth): match XID ACL principals case-insensitively - #1446

Merged
VLukiantsev merged 2 commits into
mainfrom
fix/acl-case-insensitive-principal-match
Oct 28, 2026
Merged

VLukiantsev merged 2 commits into
mainfrom
fix/acl-case-insensitive-principal-match

Conversation

@VLukiantsev

@VLukiantsev VLukiantsev commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Canonicalize the authenticated email once in AuthenticateUserFromContext and match XID ACL user, service account and domain principals case-insensitively, so ACL entries such as First.Last@Example.com authorize first.last@example.com. Stack 1/3 for b/575771582.

Motivation & Context

Web login lowercases emails, but the Google/IAP ID token, JWKS and custom session token paths did not normalize, and isPrincipalInList compared principals byte-for-byte. Mixed-case ACL entries (new or legacy rows) therefore returned PermissionDenied. Comparison normalizes both sides, so no data migration is required for authorization correctness.

Security review hardening: the email must be a plain ASCII address (mail.ParseAddress with no display name, exact match, conservative charset) and is lowercased for ASCII letters only, preventing Unicode case-folding aliasing (e.g. U+212A KELVIN SIGN → k) and display-name / quoted-local-part tricks. Non-conforming identities fail closed with Unauthenticated.

Key Changes

  • internal/auth/principal.go: NormalizePrincipal (trim + ASCII lowercase), canonicalEmail, principalsMatch, emailDomain (LastIndex; empty principal never matches).
  • internal/auth/authentication.go: single canonicalization point for all identity paths; JWKS user ID falls back to the canonical email.
  • internal/auth/authorization.go: case-insensitive user/SA/domain match; group cache keyed on canonical email.
  • internal/auth/test_utils.go: MockAuth matching is case-insensitive, consistent with Authorizer.
  • docs/authentication.md: documents case-insensitive matching and email requirements.

Assumptions

  • External JWKS identities whose email/Subject is not a plain ASCII address are rejected — required to close the aliasing hole — impact if wrong: such identities lose access and would need an allowlisted format.
  • Creator / dashboard Owner values stored from the authenticated identity are lowercase going forward — Google identities are almost always lowercase already — impact if wrong: exact-match creator filters may split for legacy mixed-case rows; backfill deferred.
  • Session-token userID left as the raw claim — session emails are already lowercased at login.

Verification & Testing

  • Commands Executed: ./.githooks/pre-commit (gofmt, golangci-lint, buildifier, addlicense, tools/run_affected_tests.py — 11 affected Bazel targets).
  • Coverage & Test Status: All passing. New table-driven cases: mixed-case User/SA/Domain, whitespace, U+212A, Eve <a@corp.com>, "x@evil.com"@corp.com, JWKS subject fallback, all three identity paths; XID CheckUserAuthorization and ESS CreateExperiment mixed-case owner cases.

Related Issues / Bugs

Fixes bug b/575771582 (part 1/3)

TAG=agy
CONV=99d9ba56-87b3-434a-8f7d-5c0546bef484

@VLukiantsev
VLukiantsev added this pull request to stack #1449 October 27, 2026 15:36

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces case-insensitive matching for ASCII letters in ACL principals (users, service accounts, groups, and domains) and canonicalizes authenticated emails to lowercase. It also adds validation to reject non-ASCII or malformed email addresses. Feedback on the changes suggests normalizing userEmail and groupEmail at the beginning of isUserInGroup to ensure consistent cache keys and robust API queries.

Comment thread internal/auth/authorization.go Resolved
@VLukiantsev
VLukiantsev marked this pull request as ready for review October 27, 2026 16:31
@github-actions
github-actions Bot requested a review from andrzejfudro October 27, 2026 16:31
Email addresses issued by Google identity providers are case-insensitive,
but XID ACL checks compared user, service account and domain principals
byte-for-byte, so an ACL entry such as "First.Last@Example.com" denied the
same user authenticating as "first.last@example.com".

- Canonicalize the authenticated email once in AuthenticateUserFromContext
  for all identity paths (session token, JWKS, Google/IAP ID token). The
  email must be a plain ASCII address (no display name, comment, quoting or
  surrounding whitespace) in a conservative charset; it is then lowercased
  for ASCII letters only, so Unicode case folding (e.g. U+212A KELVIN SIGN)
  cannot alias principals. Non-conforming identities fail closed with
  Unauthenticated.
- When a JWKS token has no subject, the user ID falls back to the canonical
  email.
- Add auth.NormalizePrincipal (trim + ASCII lowercase) for stored-side ACL
  values, and compare user, service account and domain principals in that
  form. Extract the domain with LastIndex and never let an empty principal
  match.
- Key the group membership cache on canonical emails.
- Make MockAuth ACL matching case-insensitive, consistent with Authorizer.
- Document case-insensitive matching and the email requirements in
  docs/authentication.md.

Creator and dashboard Owner values stored from the authenticated email are
lowercase from now on. Existing rows with mixed-case values may no longer
match exact creator filters; a backfill is deferred (b/575771582).

Bug: b/575771582
TAG=agy
Normalize user and group emails at the start of isUserInGroup so a
mixed-case cache miss queries the membership API with canonical
principals, consistent with the cache key.

Addresses review comment on #1446.

Bug: b/575771582
TAG=agy
CONV=99d9ba56-87b3-434a-8f7d-5c0546bef484
@VLukiantsev
VLukiantsev force-pushed the fix/acl-case-insensitive-principal-match branch from 1988006 to d0c0279 Compare October 27, 2026 17:07
@VLukiantsev
VLukiantsev added this pull request to the merge queue Oct 28, 2026
Merged via the queue into main with commit dff198e Oct 28, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants