Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🔒️ Refuse multi-tenant Microsoft sign-ins that carry no verified email claim (#3266)

Luke Vella2cd99aecc8e8
diff --git a/apps/docs/self-hosting/single-sign-on.mdx b/apps/docs/self-hosting/single-sign-on.mdx
index 79085661f..02b3edc2f 100644
--- a/apps/docs/self-hosting/single-sign-on.mdx
+++ b/apps/docs/self-hosting/single-sign-on.mdx
@@ -45,7 +45,7 @@ After creating the application, set the redirect URI to:
https://<YOUR_DOMAIN>/api/auth/callback/microsoft-entra-id
```
-On a multi-tenant endpoint (`MICROSOFT_TENANT_ID` set to `common`, `organizations` or `consumers`) any tenant can assert any email address, so Rallly needs Microsoft to confirm the address before it creates an account from it. Microsoft does not include that information unless the app registration asks for it. While the claims are missing, Rallly still creates the account and logs a warning at sign-in; a later release will refuse these sign-ins. Add the `verified_primary_email` and `verified_secondary_email` optional claims to the ID token: in the app registration open **Manifest** and add these two entries to the `optionalClaims.idToken` array, keeping any claims already there:
+On a multi-tenant endpoint (`MICROSOFT_TENANT_ID` set to `common`, `organizations` or `consumers`) any tenant can assert any email address, so Rallly needs Microsoft to confirm the address before it creates an account from it. Microsoft does not include that information unless the app registration asks for it, so a multi-tenant registration must emit the verified email claims: without them every new Microsoft sign-in fails with "Your email address is not verified". Add the `verified_primary_email` and `verified_secondary_email` optional claims to the ID token: in the app registration open **Manifest** and add these two entries to the `optionalClaims.idToken` array, keeping any claims already there:
```json
{ "name": "verified_primary_email", "source": null, "essential": false, "additionalProperties": [] },
diff --git a/apps/web/src/lib/auth-plugins/microsoft-email-claim.test.ts b/apps/web/src/lib/auth-plugins/microsoft-email-claim.test.ts
deleted file mode 100644
index 595d4e4c5..000000000
--- a/apps/web/src/lib/auth-plugins/microsoft-email-claim.test.ts
+++ /dev/null
@@ -1,77 +0,0 @@
-import { describe, expect, it } from "vitest";
-
-import {
- readMicrosoftEmailClaim,
- rememberMicrosoftEmailClaim,
- takeMicrosoftEmailClaim,
-} from "./microsoft-email-claim";
-
-describe("readMicrosoftEmailClaim", () => {
- it("is absent when the registration emits no verification claim", () => {
- expect(readMicrosoftEmailClaim({ email: "[email protected]" })).toBe("absent");
- });
-
- it("is verified when the address is in the primary list", () => {
- expect(
- readMicrosoftEmailClaim({
- email: "[email protected]",
- verified_primary_email: ["[email protected]"],
- }),
- ).toBe("verified");
- });
-
- it("matches exactly, as better-auth does when it sets emailVerified", () => {
- expect(
- readMicrosoftEmailClaim({
- email: "[email protected]",
- verified_primary_email: ["[email protected]"],
- }),
- ).toBe("unverified");
- });
-
- it("is verified when the address is only in the secondary list", () => {
- expect(
- readMicrosoftEmailClaim({
- email: "[email protected]",
- verified_primary_email: [],
- verified_secondary_email: ["[email protected]"],
- }),
- ).toBe("verified");
- });
-
- it("is unverified when the claim is present but does not carry the address", () => {
- expect(
- readMicrosoftEmailClaim({
- email: "[email protected]",
- verified_primary_email: ["[email protected]"],
- }),
- ).toBe("unverified");
- });
-
- it("is unverified when the claim is present and empty", () => {
- expect(
- readMicrosoftEmailClaim({
- email: "[email protected]",
- verified_primary_email: [],
- }),
- ).toBe("unverified");
- });
-
- it("prefers an explicit email_verified claim", () => {
- expect(
- readMicrosoftEmailClaim({
- email: "[email protected]",
- email_verified: false,
- verified_primary_email: ["[email protected]"],
- }),
- ).toBe("unverified");
- });
-});
-
-describe("claim registry", () => {
- it("hands the reading over once, case-insensitively", () => {
- rememberMicrosoftEmailClaim({ email: "[email protected]", claim: "absent" });
- expect(takeMicrosoftEmailClaim("[email protected]")).toBe("absent");
- expect(takeMicrosoftEmailClaim("[email protected]")).toBeUndefined();
- });
-});
diff --git a/apps/web/src/lib/auth-plugins/microsoft-email-claim.ts b/apps/web/src/lib/auth-plugins/microsoft-email-claim.ts
deleted file mode 100644
index 85e155bb6..000000000
--- a/apps/web/src/lib/auth-plugins/microsoft-email-claim.ts
+++ /dev/null
@@ -1,63 +0,0 @@
-// Microsoft only vouches for an address when the app registration requests
-// the verified email optional claims. Their absence means the registration is
-// unconfigured, which is a different situation from Microsoft declining to
-// vouch, and the two need different handling downstream.
-//
-// The claim is only visible where the ID token is decoded (the provider's
-// profile mapping), while the decision is made in the user create hook, which
-// sees the mapped user only. The registry carries the reading between the two
-// within one sign-in; entries are consumed on read and expire quickly.
-
-export type MicrosoftEmailClaim = "verified" | "unverified" | "absent";
-
-export function readMicrosoftEmailClaim(profile: {
- email?: string | undefined;
- email_verified?: boolean | undefined;
- verified_primary_email?: string[] | undefined;
- verified_secondary_email?: string[] | undefined;
-}): MicrosoftEmailClaim {
- if (typeof profile.email_verified === "boolean") {
- return profile.email_verified ? "verified" : "unverified";
- }
- const primary = profile.verified_primary_email;
- const secondary = profile.verified_secondary_email;
- if (!Array.isArray(primary) && !Array.isArray(secondary)) {
- return "absent";
- }
- // Exact match, the same comparison better-auth makes when it derives
- // emailVerified from these claims, so the two readings never disagree.
- const email = profile.email;
- const verified =
- !!email && [...(primary ?? []), ...(secondary ?? [])].includes(email);
- return verified ? "verified" : "unverified";
-}
-
-const TTL_MS = 60_000;
-
-const pending = new Map<string, { claim: MicrosoftEmailClaim; at: number }>();
-
-export function rememberMicrosoftEmailClaim({
- email,
- claim,
-}: {
- email: string;
- claim: MicrosoftEmailClaim;
-}) {
- const now = Date.now();
- for (const [key, entry] of pending) {
- if (now - entry.at > TTL_MS) {
- pending.delete(key);
- }
- }
- pending.set(email.toLowerCase(), { claim, at: now });
-}
-
-export function takeMicrosoftEmailClaim(email: string) {
- const key = email.toLowerCase();
- const entry = pending.get(key);
- pending.delete(key);
- if (!entry || Date.now() - entry.at > TTL_MS) {
- return undefined;
- }
- return entry.claim;
-}
diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts
index 46e618977..1c47187f7 100644
--- a/apps/web/src/lib/auth.ts
+++ b/apps/web/src/lib/auth.ts
@@ -44,11 +44,6 @@ import {
} from "@/lib/acquisition";
import { SESSION_TTL_SECONDS } from "@/lib/auth-config";
import { hostOnlyCookieCleanup } from "@/lib/auth-plugins/host-only-cookie-cleanup";
-import {
- readMicrosoftEmailClaim,
- rememberMicrosoftEmailClaim,
- takeMicrosoftEmailClaim,
-} from "@/lib/auth-plugins/microsoft-email-claim";
import { redis } from "@/lib/kv";
import {
LOCALE_COOKIE_NAME,
@@ -136,17 +131,6 @@ const isMultiTenantMicrosoft = [
"consumers",
].includes(env.MICROSOFT_TENANT_ID);
-let microsoftClaimWarned = false;
-function warnMicrosoftClaimMissing() {
- if (microsoftClaimWarned) {
- return;
- }
- microsoftClaimWarned = true;
- logger.warn(
- "Microsoft sign-in is creating accounts from unverified email addresses because the app registration does not emit the verified_primary_email claim. Add it before the next release, which will refuse these sign-ins: https://support.rallly.co/self-hosting/single-sign-on#microsoft",
- );
-}
-
export const authLib = betterAuth({
appName: env.APP_NAME,
secret: env.SECRET_PASSWORD,
@@ -263,15 +247,6 @@ export const authLib = betterAuth({
clientId: env.MICROSOFT_CLIENT_ID,
clientSecret: env.MICROSOFT_CLIENT_SECRET,
redirectURI: absoluteUrl("/api/auth/callback/microsoft-entra-id"),
- mapProfileToUser: (profile) => {
- if (profile.email) {
- rememberMicrosoftEmailClaim({
- email: profile.email,
- claim: readMicrosoftEmailClaim(profile),
- });
- }
- return {};
- },
}
: undefined,
},
@@ -522,11 +497,10 @@ export const authLib = betterAuth({
// address on a user, so an unverified address must not become an
// account: the mailbox owner's later OTP login would land in it with
// the Microsoft link intact. Microsoft only vouches when the app
- // registration requests the verified email claims, so their absence
- // means an unconfigured registration rather than a refusal. That
- // case is allowed with a warning for now and becomes a rejection in
- // a later release. Single-tenant Microsoft is the instance's own
- // directory; OIDC likewise; Google always asserts email_verified.
+ // registration requests the verified email claims, so an unverified
+ // user here means the registration is unconfigured or Microsoft
+ // declined. Single-tenant Microsoft is the instance's own directory;
+ // OIDC likewise; Google always asserts email_verified.
before: async (user, ctx) => {
if (user.isAnonymous) {
return;
@@ -545,17 +519,11 @@ export const authLib = betterAuth({
// The redirect callback names the provider in the route; the
// ID-token variant of /sign-in/social names it in the body.
const provider = ctx?.params?.id ?? ctx?.body?.provider;
- if (user.emailVerified || provider !== "microsoft") {
- return;
- }
- // better-auth derives emailVerified from the same claims, so an
- // unverified user here means the claim was absent, declined to
- // vouch, or never recorded for this sign-in (fail closed).
- const claim = takeMicrosoftEmailClaim(user.email);
- if (claim === "absent") {
- if (isMultiTenantMicrosoft) {
- warnMicrosoftClaimMissing();
- }
+ if (
+ user.emailVerified ||
+ provider !== "microsoft" ||
+ !isMultiTenantMicrosoft
+ ) {
return;
}
throw new APIError("FORBIDDEN", {