Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Count a single sign-on address as verified only when the provider says so
Claude3d13a9ab1da8
diff --git a/docs/self-hosting.md b/docs/self-hosting.mdindex 8429c9c..1b22943 100644--- a/docs/self-hosting.md+++ b/docs/self-hosting.md@@ -215,7 +215,7 @@ People can sign in through your organization's identity provider instead of withHow it behaves:- **New people** get an account the first time they sign in, under the same sign-up rule as a password sign-up (Settings, Organization, Who can sign up). So the email domain limit holds for single sign-on too. With Google this matters: any Google account can use the button, and the rule decides who gets an account. To let only your directory in, set the rule to your domains, or to nobody and add people some other way first.-- **People who already have an account** with the same email address are signed in to it, if their address here is confirmed and the provider does not say theirs is unverified. Microsoft Entra ID does not say either way, and its addresses are trusted.+- **People who already have an account** with the same email address are signed in to it, if their address here is confirmed and the provider says theirs is verified (`email_verified` is `true`). An address the provider does not vouch for is never tied to an existing account, and a new account made with it has to confirm the address by email first. Microsoft Entra ID does not send `email_verified`: add the optional `xms_edov` claim to the app's ID token (App registrations, Token configuration, Add optional claim, ID, `xms_edov`), which says your tenant owns the address's domain.- **Two-step sign-in** is not asked for after single sign-on: the provider's sign-in, with its own MFA, is the check. When an administrator requires two-step sign-in, it applies to people who have a password; someone who only uses single sign-on is not held up.- **Turning passwords off.** With single sign-on set up, `PASSWORD_SIGN_IN=off` stops password sign-in, password sign-up and password reset links. Put one or two administrator addresses in `PASSWORD_SIGN_IN_ALLOWED_EMAILS` (comma-separated) so there is a way in if the provider is down; their sign-in page shows "Sign in with a password instead". Without single sign-on set up, `PASSWORD_SIGN_IN=off` does nothing, so nobody is locked out by a half-finished setup.- **The site audit trail** records single sign-on sign-ins, and refused ones with the reason (for example, an address the sign-up rule turned away).diff --git a/src/lib/auth/server.ts b/src/lib/auth/server.tsindex 3ef4555..9148097 100644--- a/src/lib/auth/server.ts+++ b/src/lib/auth/server.ts@@ -42,6 +42,7 @@ import { escapeHtml } from "../utils/html.ts";import { isRunningSetup } from "./setup-context.ts";import { signInEvents } from "./sign-in-events.ts";import {+ isSsoEmailVerified,mayUsePassword,readPasswordSignInPolicy,readSsoConfig,@@ -309,13 +310,12 @@ export function createAppAuth({// A provider whose discovery cannot vouch for its ID tokens// is left out rather than trusted unchecked.requireIdTokenVerification: true,- // Microsoft Entra ID leaves email_verified out of its- // tokens. The organization's own directory is trusted for- // its addresses unless it says one is not verified.+ // An address counts as verified only when the provider+ // says so: a sign-in with a verified address is tied to the+ // existing account with that address, so a missing claim+ // must not count. See isSsoEmailVerified in sso.ts.mapProfileToUser: (profile) => ({- emailVerified:- profile.email_verified !== false &&- profile.email_verified !== "false",+ emailVerified: isSsoEmailVerified(profile),}),name: sso.name,pkce: true,diff --git a/src/lib/auth/sso.dbtest.ts b/src/lib/auth/sso.dbtest.tsindex 6636a64..ebf5500 100644--- a/src/lib/auth/sso.dbtest.ts+++ b/src/lib/auth/sso.dbtest.ts@@ -36,9 +36,10 @@ registerHooks({type Profile = {email?: string;- email_verified?: boolean;+ email_verified?: unknown;name: string;sub: string;+ xms_edov?: unknown;};let database: pg.Pool;@@ -328,6 +329,7 @@ test("a first sign-in through the identity provider makes a verified account andconst auth = await makeAuth();const { cookies, location } = await signInWithSso(auth, {email: "[email protected]",+ email_verified: true,name: "Pat Person",sub: "okta-pat",});@@ -337,8 +339,6 @@ test("a first sign-in through the identity provider makes a verified account andconst user = await sessionUser(auth, cookies);assert.equal(user?.email, "[email protected]");- // Entra ID leaves email_verified out; the organization's directory is- // trusted for its own addresses.assert.equal(user?.emailVerified, true);const events = await listSystemAuditEvents({ limit: 100 }, database);@@ -412,6 +412,7 @@ test("the sign-up rule holds for single sign-on too", async () => {const allowed = await signInWithSso(auth, {email: "[email protected]",+ email_verified: true,name: "Sam",sub: "google-sam",});@@ -439,6 +440,68 @@ test("an address the provider says is unverified is not tied to an existing accoassert.equal(await sessionUser(auth, cookies), null);});+test("an address the provider does not vouch for never reaches an existing administrator", async () => {+ const auth = await makeAuth();+ const adminId = randomUUID();++ await database.query(+ `insert into "user" (id, name, email, "emailVerified", role) values ($1, 'Pat', '[email protected]', true, 'admin')`,+ [adminId],+ );++ // Left out (as Entra ID does), null, or anything that is not a plain yes.+ const claims: Array<Partial<Profile>> = [+ {},+ { email_verified: null },+ { email_verified: 1 },+ { email_verified: "yes" },+ { email_verified: "False" },+ { xms_edov: false },+ ];++ for (const [index, claim] of claims.entries()) {+ const { cookies } = await signInWithSso(auth, {+ email: "[email protected]",+ name: "Pat",+ sub: `intruder-${String(index)}`,+ ...claim,+ });++ assert.notEqual(+ (await sessionUser(auth, cookies))?.id,+ adminId,+ JSON.stringify(claim),+ );+ }++ const { rows } = await database.query<{ count: string }>(+ `select count(*)::text as count from account where "userId" = $1 and "providerId" = 'sso'`,+ [adminId],+ );++ assert.equal(rows[0]?.count, "0");+});++test("an Entra ID sign-in whose tenant owns the address's domain reaches the existing account", async () => {+ const auth = await makeAuth();+ const id = randomUUID();++ await database.query(+ `insert into "user" (id, name, email, "emailVerified", role) values ($1, 'Pat', '[email protected]', true, 'user')`,+ [id],+ );++ const { cookies, location } = await signInWithSso(auth, {+ email: "[email protected]",+ name: "Pat",+ sub: "entra-pat",+ xms_edov: true,+ });++ assert.equal(location.pathname, "/dashboard");+ assert.equal((await sessionUser(auth, cookies))?.id, id);+});+test("with password sign-in off, only the listed addresses may use a password", async () => {const auth = await makeAuth({PASSWORD_SIGN_IN: "off",diff --git a/src/lib/auth/sso.ts b/src/lib/auth/sso.tsindex cd33155..324d670 100644--- a/src/lib/auth/sso.ts+++ b/src/lib/auth/sso.ts@@ -15,6 +15,28 @@ import { readEnv, type EnvSource } from "../utils/env.ts";// The addresses in PASSWORD_SIGN_IN_ALLOWED_EMAILS keep their password, as a// way in for an administrator when the provider is down.+/**+ * Whether the identity provider vouches for a profile's email address. Only+ * an explicit yes counts: the address decides which existing account a+ * sign-in is tied to, so a claim that is missing, or anything other than+ * true, leaves it unverified.+ *+ * Microsoft Entra ID never sends email_verified, and an Entra user can set an+ * address in a domain the tenant does not own. Entra's optional xms_edov+ * claim ("email domain owner verified") says the tenant owns the address's+ * domain; add it to the app's ID token under Token configuration to let+ * Entra users sign in to existing accounts.+ */+export function isSsoEmailVerified(profile: Record<string, unknown>): boolean {+ return (+ profile.email_verified === true ||+ profile.email_verified === "true" ||+ profile.xms_edov === true ||+ profile.xms_edov === "true" ||+ profile.xms_edov === 1+ );+}+/** The provider id in Better Auth: its routes end in /oauth2/callback/sso. */export const SSO_PROVIDER_ID = "sso";