Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: restart IdP-initiated SSO through verified login

Richie McIlroyd327051c175a
diff --git a/apps/web/__tests__/unit/sso-auth-route.test.ts b/apps/web/__tests__/unit/sso-auth-route.test.ts
index 2d657c763..b1744faf3 100644
--- a/apps/web/__tests__/unit/sso-auth-route.test.ts
+++ b/apps/web/__tests__/unit/sso-auth-route.test.ts
@@ -18,6 +18,8 @@ const mocks = vi.hoisted(() => ({
providers: [],
})),
decode: vi.fn(),
+ getProfileAndToken: vi.fn(),
+ isRateLimited: vi.fn(),
getToken: vi.fn<() => Promise<JWT | null>>(),
nextAuth:
vi.fn<
@@ -32,6 +34,7 @@ const mocks = vi.hoisted(() => ({
const env = vi.hoisted(() => ({
WEB_URL: "https://cap.example",
NEXTAUTH_SECRET: "test-sso-route-secret-with-sufficient-entropy",
+ WORKOS_CLIENT_ID: "client_fixture",
}));
vi.mock("@cap/env", () => ({ serverEnv: () => env }));
@@ -41,6 +44,10 @@ vi.mock("@cap/database/auth/auth-options", () => ({
}));
vi.mock("next-auth", () => ({ default: mocks.nextAuth }));
vi.mock("next-auth/jwt", () => ({ getToken: mocks.getToken }));
+vi.mock("@cap/database/auth/sso", () => ({
+ getWorkOS: () => ({ sso: { getProfileAndToken: mocks.getProfileAndToken } }),
+}));
+vi.mock("@/lib/rate-limit", () => ({ isRateLimited: mocks.isRateLimited }));
const RETURN_TO = "/api/mobile/session/request?redirectUri=cap%3A%2F%2Fauth";
const MISSING_PROFILE_QUERY = {
@@ -123,6 +130,11 @@ async function errorUrl(response: Response) {
beforeEach(() => {
vi.clearAllMocks();
+ mocks.getProfileAndToken.mockReset().mockResolvedValue({
+ accessToken: "private-access-token",
+ profile: { connectionId: "conn_verified", email: "[email protected]" },
+ });
+ mocks.isRateLimited.mockReset().mockResolvedValue(false);
mocks.getToken.mockReset().mockResolvedValue(null);
mocks.nextAuth.mockReset().mockImplementation(async () => {
return new Response(null, {
@@ -136,6 +148,123 @@ beforeEach(() => {
});
describe("SSO auth request boundary", () => {
+ it.each([null, "tampered.invalid-signature"])(
+ "restarts an IdP-initiated callback without authenticating its profile: %s",
+ async (intent) => {
+ const { request, context } = makeRequest({
+ action: "callback",
+ intent,
+ query: { code: "idp-code" },
+ });
+
+ const response = await GET(request, context);
+
+ expect(response.status).toBe(303);
+ expect(response.headers.get("location")).toBe(
+ `${env.WEB_URL}/login?connection_id=conn_verified`,
+ );
+ expect(response.headers.get("cache-control")).toBe("no-store");
+ expect(response.headers.get("referrer-policy")).toBe("no-referrer");
+ expect(response.headers.get("set-cookie")).toBe(
+ `${ssoIntentCookie(true).name}=; Path=/; Max-Age=0; Secure; HttpOnly; SameSite=lax`,
+ );
+ expect(mocks.getProfileAndToken).toHaveBeenCalledWith({
+ clientId: env.WORKOS_CLIENT_ID,
+ code: "idp-code",
+ });
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ expect(mocks.authOptions).not.toHaveBeenCalled();
+ },
+ );
+
+ it("does not reuse an existing browser session or signed intent for an unsolicited callback", async () => {
+ mocks.getToken.mockResolvedValue({ id: "existing-user" });
+ const { request, context } = makeRequest({
+ action: "callback",
+ query: { code: "idp-code" },
+ });
+
+ const response = await GET(request, context);
+
+ expect(response.headers.get("location")).toBe(
+ `${env.WEB_URL}/login?connection_id=conn_verified`,
+ );
+ expect(response.headers.get("set-cookie")).not.toContain(
+ "next-auth.session-token",
+ );
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ });
+
+ it.each<Record<string, string> | URLSearchParams>([
+ { code: "" },
+ { code: "x".repeat(2049) },
+ { code: "idp-code", state: "" },
+ { code: "idp-code", state: "invalid-state" },
+ { code: "idp-code", connection_id: "conn_attacker" },
+ { code: "idp-code", error: "access_denied" },
+ new URLSearchParams([
+ ["code", "first"],
+ ["code", "second"],
+ ]),
+ ])(
+ "does not redeem a malformed or stateful IdP callback: %j",
+ async (query) => {
+ const { request, context } = makeRequest({
+ action: "callback",
+ intent: null,
+ query,
+ });
+
+ const redirect = await errorUrl(await GET(request, context));
+
+ expect(redirect.searchParams.get("error")).toBe("SsoSessionExpired");
+ expect(mocks.getProfileAndToken).not.toHaveBeenCalled();
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ },
+ );
+
+ it("does not restart from a POST callback", async () => {
+ const { request, context } = makeRequest({
+ action: "callback",
+ method: "POST",
+ intent: null,
+ query: { code: "idp-code" },
+ });
+
+ await POST(request, context);
+
+ expect(mocks.getProfileAndToken).not.toHaveBeenCalled();
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ });
+
+ it.each(["rate-limited", "expired-code", "invalid-connection"])(
+ "fails closed when the IdP callback is %s",
+ async (reason) => {
+ if (reason === "rate-limited")
+ mocks.isRateLimited.mockResolvedValue(true);
+ if (reason === "expired-code")
+ mocks.getProfileAndToken.mockRejectedValue(
+ new Error("private details"),
+ );
+ if (reason === "invalid-connection")
+ mocks.getProfileAndToken.mockResolvedValue({
+ profile: { connectionId: "https://attacker.example" },
+ });
+ const { request, context } = makeRequest({
+ action: "callback",
+ intent: null,
+ query: { code: "idp-code" },
+ });
+
+ const redirect = await errorUrl(await GET(request, context));
+
+ expect(redirect.searchParams.get("error")).toBe("SsoSessionExpired");
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ if (reason === "rate-limited")
+ expect(mocks.getProfileAndToken).not.toHaveBeenCalled();
+ },
+ );
+
it.each(["missing", "tampered", "expired"])(
"returns a JSON sign-in error for a %s intent without calling NextAuth",
async (reason) => {
diff --git a/apps/web/app/api/auth/[...nextauth]/route.ts b/apps/web/app/api/auth/[...nextauth]/route.ts
index e82123391..2d5c0d65a 100644
--- a/apps/web/app/api/auth/[...nextauth]/route.ts
+++ b/apps/web/app/api/auth/[...nextauth]/route.ts
@@ -2,6 +2,7 @@ import {
authOptions,
decodeSessionToken,
} from "@cap/database/auth/auth-options";
+import { getWorkOS } from "@cap/database/auth/sso";
import {
ssoIntentCookie,
ssoLoginErrorPath,
@@ -12,9 +13,37 @@ import { type NextRequest, NextResponse } from "next/server";
import NextAuth from "next-auth";
import { getToken } from "next-auth/jwt";
import { getSafeNextPath } from "@/app/(org)/safe-next";
+import { isRateLimited } from "@/lib/rate-limit";
export const dynamic = "force-dynamic";
+async function idpSignInConnection(request: NextRequest) {
+ const query = request.nextUrl.searchParams;
+ const code = query.get("code");
+ if (
+ request.method !== "GET" ||
+ query.size !== 1 ||
+ !code ||
+ code.length > 2048 ||
+ (await isRateLimited("rl_auth_sso_start", { headers: request.headers }))
+ ) {
+ return null;
+ }
+ try {
+ const clientId = serverEnv().WORKOS_CLIENT_ID;
+ if (!clientId) return null;
+ const { profile } = await getWorkOS().sso.getProfileAndToken({
+ clientId,
+ code,
+ });
+ return /^conn_[a-zA-Z0-9]{1,100}$/.test(profile.connectionId)
+ ? profile.connectionId
+ : null;
+ } catch {
+ return null;
+ }
+}
+
async function readSignInBody(request: NextRequest) {
if (request.method !== "POST") return null;
try {
@@ -52,6 +81,22 @@ async function handler(
}
const env = serverEnv();
const cookie = ssoIntentCookie(new URL(env.WEB_URL).protocol === "https:");
+ if (
+ nextauth[0] === "callback" &&
+ !request.nextUrl.searchParams.has("state")
+ ) {
+ const connectionId = await idpSignInConnection(request);
+ if (connectionId) {
+ // An unsolicited code only selects the IdP; a fresh state/PKCE flow must authenticate the browser.
+ const signInUrl = new URL("/login", env.WEB_URL);
+ signInUrl.searchParams.set("connection_id", connectionId);
+ const response = NextResponse.redirect(signInUrl, 303);
+ response.headers.set("cache-control", "no-store");
+ response.headers.set("referrer-policy", "no-referrer");
+ response.cookies.set(cookie.name, "", { ...cookie.options, maxAge: 0 });
+ return response;
+ }
+ }
const intent = verifySsoLoginIntent(
request.cookies.get(cookie.name)?.value,
env.NEXTAUTH_SECRET,