Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: preserve missing SSO profile errors

Richie McIlroy53314d6addb2
diff --git a/apps/web/__tests__/unit/sso-auth-route.test.ts b/apps/web/__tests__/unit/sso-auth-route.test.ts
index 82a19cf5e..2d657c763 100644
--- a/apps/web/__tests__/unit/sso-auth-route.test.ts
+++ b/apps/web/__tests__/unit/sso-auth-route.test.ts
@@ -43,6 +43,10 @@ vi.mock("next-auth", () => ({ default: mocks.nextAuth }));
vi.mock("next-auth/jwt", () => ({ getToken: mocks.getToken }));
const RETURN_TO = "/api/mobile/session/request?redirectUri=cap%3A%2F%2Fauth";
+const MISSING_PROFILE_QUERY = {
+ error: "server_error",
+ error_description: "The SAML Response did not contain expected attributes.",
+};
const INTENT = {
organizationId: "caporganization",
workosOrganizationId: "org_verified",
@@ -369,6 +373,98 @@ describe("SSO auth request boundary", () => {
},
);
+ it("explains missing profile attributes without exposing the provider response", async () => {
+ mocks.nextAuth.mockResolvedValueOnce(
+ new Response(null, {
+ status: 302,
+ headers: { location: "/api/auth/error?error=OAuthCallback" },
+ }),
+ );
+ const { request, context } = makeRequest({
+ action: "callback",
+ query: { ...MISSING_PROFILE_QUERY, state: "private-oauth-state" },
+ });
+
+ const response = await GET(request, context);
+ const redirect = await errorUrl(response);
+
+ expect(mocks.nextAuth).toHaveBeenCalledOnce();
+ expect(redirect.origin).toBe(env.WEB_URL);
+ expect(redirect.pathname).toBe("/login");
+ expect(Object.fromEntries(redirect.searchParams)).toEqual({
+ error: "SsoMissingProfileAttributes",
+ next: RETURN_TO,
+ });
+ expect(response.headers.get("set-cookie")).toContain(
+ `${ssoIntentCookie(true).name}=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0; Secure`,
+ );
+ });
+
+ it.each<Record<string, string> | URLSearchParams>([
+ {},
+ { error: "server_error" },
+ { error_description: MISSING_PROFILE_QUERY.error_description },
+ { ...MISSING_PROFILE_QUERY, error: "access_denied" },
+ { error: "server_error", error_description: "Something else failed" },
+ {
+ ...MISSING_PROFILE_QUERY,
+ error_description: `${MISSING_PROFILE_QUERY.error_description} Visit https://attacker.example`,
+ },
+ new URLSearchParams([
+ ...Object.entries(MISSING_PROFILE_QUERY),
+ ["error", "access_denied"],
+ ]),
+ new URLSearchParams([
+ ...Object.entries(MISSING_PROFILE_QUERY),
+ ["error_description", "Something else failed"],
+ ]),
+ ])(
+ "keeps unrecognized or ambiguous provider errors generic: %j",
+ async (query) => {
+ mocks.nextAuth.mockResolvedValueOnce(
+ new Response(null, {
+ status: 302,
+ headers: { location: "/api/auth/error?error=OAuthCallback" },
+ }),
+ );
+ const { request, context } = makeRequest({ action: "callback", query });
+
+ const redirect = await errorUrl(await GET(request, context));
+
+ expect(Object.fromEntries(redirect.searchParams)).toEqual({
+ error: "SsoSignInFailed",
+ next: RETURN_TO,
+ });
+ },
+ );
+
+ it("does not classify a missing-profile error before validating the login intent", async () => {
+ const { request, context } = makeRequest({
+ action: "callback",
+ intent: null,
+ query: MISSING_PROFILE_QUERY,
+ });
+
+ const redirect = await errorUrl(await GET(request, context));
+
+ expect(redirect.searchParams.get("error")).toBe("SsoSessionExpired");
+ expect(mocks.nextAuth).not.toHaveBeenCalled();
+ });
+
+ it("does not replace a successful callback with a query-supplied error", async () => {
+ const { request, context } = makeRequest({
+ action: "callback",
+ query: MISSING_PROFILE_QUERY,
+ });
+
+ const response = await GET(request, context);
+
+ expect(response.headers.get("location")).toBe("/dashboard");
+ expect(response.headers.get("set-cookie")).toContain(
+ "next-auth.session-token=verified-session",
+ );
+ });
+
it("does not append the signed continuation to an external redirect", async () => {
const location = "https://attacker.example/login?error=Callback";
mocks.nextAuth.mockResolvedValueOnce(
diff --git a/apps/web/__tests__/unit/sso-state.test.ts b/apps/web/__tests__/unit/sso-state.test.ts
index a72ea43ab..789cfbc63 100644
--- a/apps/web/__tests__/unit/sso-state.test.ts
+++ b/apps/web/__tests__/unit/sso-state.test.ts
@@ -67,17 +67,21 @@ describe("SSO login intent", () => {
expect(verifySsoLoginIntent(value, secret, now - 31_000)).toBeNull();
});
- it("preserves a same-origin app continuation through SSO error recovery", () => {
- const returnTo = "/api/mobile/session/request?redirectUri=cap%3A%2F%2Fauth";
- const value = createSsoLoginIntent({ ...context, returnTo }, secret, now);
- expect(verifySsoLoginIntent(value, secret, now)?.returnTo).toBe(returnTo);
- const errorUrl = new URL(
- ssoLoginErrorPath("SsoSignInFailed", returnTo),
- "https://cap.test",
- );
- expect(errorUrl.searchParams.get("next")).toBe(returnTo);
- expect(errorUrl.searchParams.get("error")).toBe("SsoSignInFailed");
- });
+ it.each(["SsoSignInFailed", "SsoMissingProfileAttributes"] as const)(
+ "preserves a same-origin app continuation through %s recovery",
+ (error) => {
+ const returnTo =
+ "/api/mobile/session/request?redirectUri=cap%3A%2F%2Fauth";
+ const value = createSsoLoginIntent({ ...context, returnTo }, secret, now);
+ expect(verifySsoLoginIntent(value, secret, now)?.returnTo).toBe(returnTo);
+ const errorUrl = new URL(
+ ssoLoginErrorPath(error, returnTo),
+ "https://cap.test",
+ );
+ expect(errorUrl.searchParams.get("next")).toBe(returnTo);
+ expect(errorUrl.searchParams.get("error")).toBe(error);
+ },
+ );
it.each([
"https://attacker.example/",
diff --git a/apps/web/app/api/auth/[...nextauth]/route.ts b/apps/web/app/api/auth/[...nextauth]/route.ts
index dc47d3e2a..e82123391 100644
--- a/apps/web/app/api/auth/[...nextauth]/route.ts
+++ b/apps/web/app/api/auth/[...nextauth]/route.ts
@@ -108,10 +108,23 @@ async function handler(
(redirectUrl.pathname === "/login" &&
redirectUrl.searchParams.has("error")))
) {
+ const isMissingProfileAttributes =
+ request.method === "GET" &&
+ request.nextUrl.searchParams.getAll("error").length === 1 &&
+ request.nextUrl.searchParams.getAll("error_description").length ===
+ 1 &&
+ request.nextUrl.searchParams.get("error") === "server_error" &&
+ request.nextUrl.searchParams.get("error_description") ===
+ "The SAML Response did not contain expected attributes.";
response.headers.set(
"location",
new URL(
- ssoLoginErrorPath("SsoSignInFailed", intent.returnTo),
+ ssoLoginErrorPath(
+ isMissingProfileAttributes
+ ? "SsoMissingProfileAttributes"
+ : "SsoSignInFailed",
+ intent.returnTo,
+ ),
env.WEB_URL,
).toString(),
);
diff --git a/packages/database/auth/sso-state.ts b/packages/database/auth/sso-state.ts
index de636e255..3aee288b1 100644
--- a/packages/database/auth/sso-state.ts
+++ b/packages/database/auth/sso-state.ts
@@ -37,7 +37,10 @@ function isSafeReturnPath(value: unknown): value is string {
}
export function ssoLoginErrorPath(
- error: "SsoSessionExpired" | "SsoSignInFailed",
+ error:
+ | "SsoSessionExpired"
+ | "SsoSignInFailed"
+ | "SsoMissingProfileAttributes",
returnTo?: string,
) {
const query = new URLSearchParams({ error });