π OpenHands: AI-Driven Development
feat: protect Agent Canvas behind SaaS auth (#15286)
Rohit Malhotra11d4ecf21fc1
diff --git a/enterprise/server/routes/auth.py b/enterprise/server/routes/auth.pyindex 33af8e9746..763871e2f0 100644--- a/enterprise/server/routes/auth.py+++ b/enterprise/server/routes/auth.py@@ -768,7 +768,9 @@ def _is_cross_app_relative_path(value: str) -> bool:if not value.startswith('/') or value.startswith('//'):return Falseparsed = urlparse(value)- return parsed.path == '/automations' or parsed.path.startswith('/automations/')+ return parsed.path in ('/automations', '/canvas') or parsed.path.startswith(+ ('/automations/', '/canvas/')+ )def _merge_login_wrapper_query(inner_destination: str, outer_query: str) -> str:@@ -791,14 +793,14 @@ def _build_cross_app_redirect_url(redirect_url: str, web_url: str) -> str:OAuth state often points back at the main app's ``/login`` page with thereal destination nested inside ``returnTo`` or the legacy ``redirect`` query- parameter. For paths owned by another frontend, such as ``/automations``,- sending the browser through the main app SPA first is brittle: any old or- already-loaded bundle can client-navigate and show the main app 404 before- ingress sees the route.+ parameter. For paths owned by another frontend, such as ``/automations`` or+ ``/canvas``, sending the browser through the main app SPA first is brittle:+ any old or already-loaded bundle can client-navigate and show the main app+ 404 before ingress sees the route.- Returning a direct ``Location: <web_url>/automations...`` from the backend+ Returning a direct ``Location: <web_url>/<cross-app>...`` from the backendmakes the browser issue a real document request, so ingress routes it to the- automation service.+ owning service."""if not redirect_url:return redirect_urldiff --git a/enterprise/tests/unit/server/routes/test_auth_onboarding.py b/enterprise/tests/unit/server/routes/test_auth_onboarding.pyindex 574490d548..eb7a657716 100644--- a/enterprise/tests/unit/server/routes/test_auth_onboarding.py+++ b/enterprise/tests/unit/server/routes/test_auth_onboarding.py@@ -348,6 +348,23 @@ class TestBuildCrossAppRedirectUrl:assert result == 'https://example.com/automations'+ def test_unwraps_login_return_to_canvas_deep_link(self):+ result = _build_cross_app_redirect_url(+ 'https://example.com/login'+ '?returnTo=%2Fcanvas%2Fconversations%2Fabc'+ '&login_method=github',+ 'https://example.com',+ )++ assert result == (+ 'https://example.com/canvas/conversations/abc?login_method=github'+ )++ def test_direct_canvas_path_becomes_same_origin_absolute_url(self):+ result = _build_cross_app_redirect_url('/canvas', 'https://example.com')++ assert result == 'https://example.com/canvas'+def test_non_automation_login_redirect_is_unchanged(self):result = _build_cross_app_redirect_url('https://example.com/login?redirect=%2Fsettings&login_method=github',diff --git a/frontend/src/routes.ts b/frontend/src/routes.tsindex 6e2f85e2cf..8e01cc0677 100644--- a/frontend/src/routes.ts+++ b/frontend/src/routes.ts@@ -10,6 +10,9 @@ export default [route("onboarding", "routes/onboarding-form.tsx"),route("information-request", "routes/information-request.tsx"),route("automations/*", "routes/cross-app-redirect.tsx"),+ route("canvas/*", "routes/cross-app-redirect.tsx", {+ id: "routes/canvas-cross-app-redirect",+ }),layout("routes/root-layout.tsx", [index("routes/home.tsx"),route("accept-tos", "routes/accept-tos.tsx"),diff --git a/frontend/src/routes/cross-app-redirect.tsx b/frontend/src/routes/cross-app-redirect.tsxindex 0361ed5d2a..008a12e64e 100644--- a/frontend/src/routes/cross-app-redirect.tsx+++ b/frontend/src/routes/cross-app-redirect.tsx@@ -2,7 +2,7 @@ import React from "react";import { useTranslation } from "react-i18next";import { I18nKey } from "#/i18n/declaration";-const RELOAD_SENTINEL_KEY = "openhands:automations-cross-app-reload";+const RELOAD_SENTINEL_KEY = "openhands:cross-app-reload";export default function CrossAppRedirect() {const { t } = useTranslation();diff --git a/frontend/src/utils/cross-app-redirect.test.ts b/frontend/src/utils/cross-app-redirect.test.tsindex 49b728df81..e519f3b9b7 100644--- a/frontend/src/utils/cross-app-redirect.test.ts+++ b/frontend/src/utils/cross-app-redirect.test.ts@@ -8,14 +8,24 @@ describe("isCrossAppPath", () => {expect(isCrossAppPath("/automations/abc?tab=runs")).toBe(true);});+ it("detects Canvas app routes", () => {+ expect(isCrossAppPath("/canvas")).toBe(true);+ expect(isCrossAppPath("/canvas?login_method=github")).toBe(true);+ expect(isCrossAppPath("/canvas/conversations/abc")).toBe(true);+ });+it("does not match similarly-prefixed main app routes", () => {expect(isCrossAppPath("/automation")).toBe(false);expect(isCrossAppPath("/automations-old")).toBe(false);+ expect(isCrossAppPath("/canvases")).toBe(false);expect(isCrossAppPath("/settings?returnTo=/automations")).toBe(false);+ expect(isCrossAppPath("/settings?returnTo=/canvas")).toBe(false);});it("does not treat off-origin targets as cross-app paths", () => {expect(isCrossAppPath("//evil.com/automations")).toBe(false);expect(isCrossAppPath("https://evil.com/automations")).toBe(false);+ expect(isCrossAppPath("//evil.com/canvas")).toBe(false);+ expect(isCrossAppPath("https://evil.com/canvas")).toBe(false);});});diff --git a/frontend/src/utils/cross-app-redirect.ts b/frontend/src/utils/cross-app-redirect.tsindex 37d519a274..52412b9476 100644--- a/frontend/src/utils/cross-app-redirect.ts+++ b/frontend/src/utils/cross-app-redirect.ts@@ -1,5 +1,7 @@import type { NavigateFunction, NavigateOptions } from "react-router";+const CROSS_APP_PATH_PREFIXES = ["/automations", "/canvas"] as const;+export function isCrossAppPath(destination: string): boolean {if (!destination.startsWith("/") || destination.startsWith("//")) {return false;@@ -7,7 +9,9 @@ export function isCrossAppPath(destination: string): boolean {try {const { pathname } = new URL(destination, window.location.origin);- return pathname === "/automations" || pathname.startsWith("/automations/");+ return CROSS_APP_PATH_PREFIXES.some(+ (prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`),+ );} catch {return false;}