Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(security): only allow redirect via target to internal urls (#3512)

Marc Klingenbca09bdefef3
diff --git a/web/src/__e2e__/auth.spec.ts b/web/src/__e2e__/auth.spec.ts
index 980e505907..6c77842d18 100644
--- a/web/src/__e2e__/auth.spec.ts
+++ b/web/src/__e2e__/auth.spec.ts
@@ -97,3 +97,44 @@ test("Unauthenticated user should be redirected to target URL after login", asyn
await expect(page).toHaveURL(promptUrl);
});
+
+test("Unauthenticated user should not be redirected to non-relative URLs after login", async ({
+ page,
+}) => {
+ const nonRelativeUrl = "https://example.com";
+ await page.goto(
+ `/auth/sign-in?targetPath=${encodeURIComponent(nonRelativeUrl)}`,
+ );
+
+ await page.fill('input[name="email"]', "[email protected]");
+ await page.fill('input[type="password"]', "password");
+ await page.click('button[data-testid="submit-email-password-sign-in-form"]');
+
+ // Wait for navigation
+ await page.waitForTimeout(2000);
+
+ // Expect to be redirected to the home page, not the non-relative URL
+ await expect(page).toHaveURL("/");
+
+ // Verify we're logged in
+ await expect(page.getByRole("button", { name: /Demo User/ })).toBeVisible();
+});
+
+test("Unauthenticated user should be redirected to relative URL after login", async ({
+ page,
+}) => {
+ const relativeUrl = "/support";
+ await page.goto(
+ `/auth/sign-in?targetPath=${encodeURIComponent(relativeUrl)}`,
+ );
+
+ await page.fill('input[name="email"]', "[email protected]");
+ await page.fill('input[type="password"]', "password");
+ await page.click('button[data-testid="submit-email-password-sign-in-form"]');
+
+ // Wait for navigation
+ await page.waitForTimeout(2000);
+
+ // Expect to be redirected to the relative URL
+ await expect(page).toHaveURL(relativeUrl);
+});
diff --git a/web/src/components/layouts/layout.tsx b/web/src/components/layouts/layout.tsx
index d3cf909d18..b3f73ee268 100644
--- a/web/src/components/layouts/layout.tsx
+++ b/web/src/components/layouts/layout.tsx
@@ -252,13 +252,20 @@ export default function Layout(props: PropsWithChildren) {
session.status === "authenticated" &&
unauthenticatedPaths.includes(router.pathname)
) {
- const targetPath = router.query.targetPath as string | undefined;
+ const queryTargetPath = router.query.targetPath as string | undefined;
- const sanitizedTargetPath = targetPath
- ? DOMPurify.sanitize(targetPath)
+ const sanitizedTargetPath = queryTargetPath
+ ? DOMPurify.sanitize(queryTargetPath)
: undefined;
- void router.replace(sanitizedTargetPath ?? "/");
+ // only allow relative links
+ const targetPath =
+ sanitizedTargetPath?.startsWith("/") &&
+ !sanitizedTargetPath.startsWith("//")
+ ? sanitizedTargetPath
+ : "/";
+
+ void router.replace(targetPath);
return <Spinner message="Redirecting" />;
}