Skip to content

Open source CapCut alternative.

TypeScript

fix(site): accept trusted callbacks on email sign-in

David Le00284e12f85d
diff --git a/site/src/lib/email-password-auth.test.ts b/site/src/lib/email-password-auth.test.ts
index 6f9f36c6..5dc6bfcc 100644
--- a/site/src/lib/email-password-auth.test.ts
+++ b/site/src/lib/email-password-auth.test.ts
@@ -13,6 +13,7 @@ const auth = betterAuth({
emailAndPassword: emailPasswordOptions,
disabledPaths: emailPasswordDisabledPaths,
hooks: { before: emailPasswordGuard },
+ trustedOrigins: ["https://*.example.com"],
});
beforeEach(async () => {
@@ -105,3 +106,12 @@ test("external callbacks are rejected for both sign-in and signup", async () =>
expect(db.user).toHaveLength(3);
expect(db.session).toHaveLength(0);
});
+
+test("a callback on a trusted subdomain signs in", async () => {
+ // The back office sends its visitors here with its own address as the
+ // callback; the guard trusts what the auth config trusts.
+ const callbackURL = "https://admin.example.com/analytics";
+ const response = await request("/sign-in/email", { email: "[email protected]", password, callbackURL });
+ expect(response.status).toBe(200);
+ expect((await response.json()).url).toBe(callbackURL);
+});
diff --git a/site/src/lib/email-password-auth.ts b/site/src/lib/email-password-auth.ts
index a72d2c49..cc0dfe69 100644
--- a/site/src/lib/email-password-auth.ts
+++ b/site/src/lib/email-password-auth.ts
@@ -14,13 +14,15 @@ export const emailPasswordDisabledPaths = [
"/verify-email",
];
+// A callback lands on this site or an origin the auth config trusts, such as
+// the back office on its own subdomain.
export const emailPasswordGuard = createAuthMiddleware(async (ctx) => {
if (ctx.path !== "/sign-in/email" && ctx.path !== "/sign-up/email") return;
const callback: unknown = ctx.body?.callbackURL;
if (callback !== undefined && (
typeof callback !== "string" ||
!URL.canParse(callback, ctx.context.baseURL) ||
- new URL(callback, ctx.context.baseURL).origin !== new URL(ctx.context.baseURL).origin
+ !ctx.context.isTrustedOrigin(new URL(callback, ctx.context.baseURL).href)
)) {
throw new APIError("FORBIDDEN", { message: "Invalid callback URL" });
}