Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

security: confirm if the email is verified in SSO

daniel31x1328139f55f6ac
diff --git a/apps/web/lib/api/ssoEmailVerified.test.ts b/apps/web/lib/api/ssoEmailVerified.test.ts
new file mode 100644
index 00000000..fa03382d
--- /dev/null
+++ b/apps/web/lib/api/ssoEmailVerified.test.ts
@@ -0,0 +1,37 @@
+import { describe, expect, it } from "vitest";
+import { ssoEmailVerified } from "./ssoEmailVerified";
+
+describe("ssoEmailVerified", () => {
+ it("accepts a verified boolean claim", () => {
+ expect(ssoEmailVerified({ email_verified: true })).toBe(true);
+ });
+
+ it("accepts the string 'true' (Cognito / userinfo style)", () => {
+ expect(ssoEmailVerified({ email_verified: "true" })).toBe(true);
+ });
+
+ it("rejects an explicitly unverified claim (the core attack vector)", () => {
+ expect(ssoEmailVerified({ email_verified: false })).toBe(false);
+ });
+
+ it("rejects the string 'false'", () => {
+ expect(ssoEmailVerified({ email_verified: "false" })).toBe(false);
+ });
+
+ it("rejects an absent claim (fail closed)", () => {
+ expect(ssoEmailVerified({ sub: "123", email: "[email protected]" })).toBe(false);
+ });
+
+ it("rejects a differently-named flag, e.g. Discord's `verified`", () => {
+ expect(ssoEmailVerified({ verified: true })).toBe(false);
+ });
+
+ it("rejects a non-boolean truthy value such as 1", () => {
+ expect(ssoEmailVerified({ email_verified: 1 })).toBe(false);
+ });
+
+ it("handles undefined / null profile", () => {
+ expect(ssoEmailVerified(undefined)).toBe(false);
+ expect(ssoEmailVerified(null)).toBe(false);
+ });
+});
diff --git a/apps/web/lib/api/ssoEmailVerified.ts b/apps/web/lib/api/ssoEmailVerified.ts
new file mode 100644
index 00000000..e7054e15
--- /dev/null
+++ b/apps/web/lib/api/ssoEmailVerified.ts
@@ -0,0 +1,22 @@
+/**
+ * Returns true only when the identity provider positively asserts that the
+ * user's email address has been verified — the OIDC `email_verified` standard
+ * claim (OpenID Connect Core §5.1).
+ *
+ * Used to gate auto-linking an SSO identity to an EXISTING local account. The
+ * email claim alone is not proof of ownership, so without this check anyone
+ * able to register a victim's email at a configured SSO provider could take
+ * over the victim's account.
+ *
+ * Fail-closed by design: an absent claim, a differently-named flag (e.g.
+ * Discord's `verified`), or any non-`true` value all return false. A
+ * missing/renamed claim can therefore only ever refuse a legitimate link — it
+ * can never let an unverified identity through. Some IdPs / userinfo endpoints
+ * (e.g. AWS Cognito) send the claim as the string "true", which is accepted.
+ */
+export function ssoEmailVerified(profile: unknown): boolean {
+ const claim = (profile as { email_verified?: unknown } | null | undefined)
+ ?.email_verified;
+
+ return claim === true || claim === "true";
+}
diff --git a/apps/web/pages/api/v1/auth/[...nextauth].ts b/apps/web/pages/api/v1/auth/[...nextauth].ts
index ea597fe5..5c4827e9 100644
--- a/apps/web/pages/api/v1/auth/[...nextauth].ts
+++ b/apps/web/pages/api/v1/auth/[...nextauth].ts
@@ -4,6 +4,7 @@ import sendVerificationRequest from "@/lib/api/sendVerificationRequest";
import updateSeats from "@/lib/api/billing/updateSeats";
import verifySubscription from "@/lib/api/billing/verifySubscription";
import { getAppleClientId, getAppleClientSecret } from "@/lib/api/apple";
+import { ssoEmailVerified } from "@/lib/api/ssoEmailVerified";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { User } from "@linkwarden/prisma/client";
import bcrypt from "bcrypt";
@@ -1339,7 +1340,7 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
},
},
callbacks: {
- async signIn({ user, account, email }) {
+ async signIn({ user, account, email, profile }) {
if (!(user as User).emailVerified && !email?.verificationRequest) {
const parentSubscriptionId = (user as User).parentSubscriptionId;
@@ -1405,6 +1406,10 @@ export default async function auth(req: NextApiRequest, res: NextApiResponse) {
});
if (findUser && findUser.accounts.length === 0) {
+ if (!ssoEmailVerified(profile)) {
+ return false;
+ }
+
await prisma.account.create({
data: {
userId: findUser.id,