Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
fix: correct alias verification check
Scott Wilson1359f66e697d
diff --git a/backend/e2e-test/routes/v3/auth-signup-oauth.spec.ts b/backend/e2e-test/routes/v3/auth-signup-oauth.spec.tsindex 0cebd8b1e2..c30088430c 100644--- a/backend/e2e-test/routes/v3/auth-signup-oauth.spec.ts+++ b/backend/e2e-test/routes/v3/auth-signup-oauth.spec.ts@@ -176,6 +176,81 @@ describe("Auth OAuth Signup V3 (provider-attested email verification)", () => {.first();expect(attackerAlias?.isEmailVerified).toBe(false);});++ test("an unverified alias cannot be flipped to verified to take over the attached account", async () => {+ // Two-step stale-alias takeover (the OAuth analogue of the SAML stale-alias guard):+ // 1) an attacker provider identity asserts the victim's email UNVERIFIED, backfilling an+ // unverified alias onto the victim via the email fallback (no session is issued);+ // 2) the attacker switches their provider email to one they control and re-logs in VERIFIED.+ // The asserted email no longer matches the victim account, so the alias is stale and must not+ // be promoted: no session, and the victim's email/username are left untouched.+ const victimEmail = `oauth-victim-${crypto.randomUUID()}@localhost.local`;+ const attackerEmail = `oauth-attacker-${crypto.randomUUID()}@localhost.local`;+ const attackerExternalId = `github-attacker-${crypto.randomUUID()}`;++ const [victim] = await getDb()(TableName.Users)+ .insert({+ username: victimEmail,+ email: victimEmail,+ isAccepted: true,+ isEmailVerified: true,+ isGhost: false,+ authMethods: [AuthMethod.EMAIL]+ })+ .returning("*");+ createdUserIds.push(victim.id);++ // Step 1: seed an unverified alias on the victim by asserting their email unverified.+ const seed = await oauthLogin({+ email: victimEmail,+ isEmailVerifiedByProvider: false,+ authMethod: AuthMethod.GITHUB,+ providerUserId: attackerExternalId+ });+ expect(seed.result).toBe(ProviderAuthResult.SIGNUP_REQUIRED);+ expect(seed.user.id).toBe(victim.id);++ const seededAlias = await getDb()(TableName.UserAliases)+ .where({ userId: victim.id, externalId: attackerExternalId })+ .first();+ expect(seededAlias?.isEmailVerified).toBe(false);++ smtp().clear();++ // Step 2: same provider identity, now asserting an attacker-controlled VERIFIED email.+ const takeover = await oauthLogin({+ email: attackerEmail,+ isEmailVerifiedByProvider: true,+ authMethod: AuthMethod.GITHUB,+ providerUserId: attackerExternalId+ });++ // No session: the stale alias is not promoted.+ expect(takeover.result).toBe(ProviderAuthResult.SIGNUP_REQUIRED);+ expect(takeover.didCompleteSignup).toBe(false);++ // The victim account is untouched: the attacker could neither overwrite the email/username nor+ // scribble on the profile (names), link their auth method, or flip a verified flag.+ const victimAfter = await findUser(victim.id);+ expect(victimAfter?.username).toBe(victimEmail);+ expect(victimAfter?.email).toBe(victimEmail);+ expect(victimAfter?.authMethods).not.toContain(AuthMethod.GITHUB);+ expect(victimAfter?.authMethods).toEqual([AuthMethod.EMAIL]);+ expect(victimAfter?.firstName ?? null).toBeNull();+ expect(victimAfter?.lastName ?? null).toBeNull();+ expect(victimAfter?.isGitHubVerified ?? false).toBe(false);++ // The alias stays unverified and never adopts the attacker's email.+ const aliasAfter = await getDb()(TableName.UserAliases)+ .where({ userId: victim.id, externalId: attackerExternalId })+ .first();+ expect(aliasAfter?.isEmailVerified).toBe(false);+ expect(aliasAfter?.emails ?? []).not.toContain(attackerEmail);++ // The verification code went to the victim's real inbox, never to the attacker's address.+ expect(findEmailTo(attackerEmail)).toBeUndefined();+ expect(findEmailTo(victimEmail)).toBeDefined();+ });});describe("provider verified the email -> a session is issued directly, no completion step", () => {diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.tsindex dc03472886..1c873d43b3 100644--- a/backend/src/services/auth/auth-login-service.ts+++ b/backend/src/services/auth/auth-login-service.ts@@ -50,7 +50,7 @@ import { LoginMethod } from "../super-admin/super-admin-types";import { TTotpServiceFactory } from "../totp/totp-service";import { TUserDALFactory } from "../user/user-dal";import { TUserAliasDALFactory } from "../user-alias/user-alias-dal";-import { ensureSsoAccountVerified } from "../user-alias/user-alias-fns";+import { ensureSsoAccountVerified, isStaleSsoAlias } from "../user-alias/user-alias-fns";import { UserAliasType } from "../user-alias/user-alias-types";import { enforceUserLockStatus, getRequiredMfaMethod, verifyCaptcha } from "./auth-fns";import {@@ -1017,8 +1017,22 @@ export const authLoginServiceFactory = ({return newUser;});} else {+ // Whether this login is trusted to mutate the matched account (link an auth method, overwrite+ // profile names, change the email / verified flags). The account was resolved either by a+ // stable provider alias (isNewAlias === false) or by an unverified email fallback (true):+ // - alias match: trusted unless the alias is stale, i.e. still-unverified and the asserted+ // email matches none of the account's known emails. A legacy/backfilled alias can point at+ // another user's account, and a provider-verified email only proves the caller owns *that*+ // email, not the aliased account, so a stale alias must never mutate it.+ // - email-fallback match: the asserted email IS the account's username, so trust it only when+ // the provider verified that email; an unverified assertion doesn't prove control of it.+ const isTrustedForAccount =+ existingAlias && !isNewAlias+ ? !isStaleSsoAlias({ user, userAlias: existingAlias, assertedEmail: sanitizedEmail })+ : isEmailVerifiedByProvider;+const isLinkingRequired = !user?.authMethods?.includes(authMethod);- if (isLinkingRequired) {+ if (isLinkingRequired && isTrustedForAccount) {// we update the names here because upon org invitation, the names are set to be NULL// if user is signing up with SSO after invitation, their names should be set based on their SSO profileuser = await userDAL.updateById(user.id, {@@ -1032,7 +1046,12 @@ export const authLoginServiceFactory = ({});}- if (existingAlias && user.email !== sanitizedEmail) {+ // A provider-driven email change renames the account and marks the alias verified, so only do+ // it for a trusted login (see isTrustedForAccount above). For a stale alias this is skipped,+ // letting the stale-alias guard below decline promotion and fall through to the email-+ // verification flow (no session). With existingAlias set, isTrustedForAccount reduces to "the+ // alias is not stale".+ if (existingAlias && user.email !== sanitizedEmail && isTrustedForAccount) {const conflictingUser = await userDAL.findOne({ username: sanitizedEmail });if (conflictingUser && conflictingUser.id !== user.id) {throw new BadRequestError({