diff --git a/.env.example b/.env.example index ccae3672b1e..aa84f569117 100644 --- a/.env.example +++ b/.env.example @@ -298,3 +298,6 @@ GH_APP_SLUG=superset-app # Read-only support account lookup (apps/api /api/support/lookup); unset = endpoint off SUPPORT_LOOKUP_TOKEN= + +# Server-only domain routing opt-in; only literal true enables evaluation. +DOMAIN_MOVE_ENABLED= diff --git a/.env.local.example b/.env.local.example index 62d7e01e031..bfcafa04197 100644 --- a/.env.local.example +++ b/.env.local.example @@ -272,3 +272,6 @@ SANDBOX_GATE_ORIGIN=http://127.0.0.1:8790 # means no sandbox is ever provisioned here to report anything. SENTRY_DSN_SANDBOX= GH_APP_SLUG=superset-app + +# Server-only domain routing opt-in; only literal true enables evaluation. +DOMAIN_MOVE_ENABLED=false diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3b703ec022a..848be76b49e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -142,6 +142,9 @@ jobs: - name: Check out code uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 + - name: Test Paradev deployment guards + run: python3 -B scripts/deploy-paradev-staging/guard_test.py + - name: Setup Bun id: setup-bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 diff --git a/.github/workflows/deploy-paradev-staging.yml b/.github/workflows/deploy-paradev-staging.yml index a846c919562..283ad9afa37 100644 --- a/.github/workflows/deploy-paradev-staging.yml +++ b/.github/workflows/deploy-paradev-staging.yml @@ -2,6 +2,15 @@ name: Deploy Paradev Staging on: workflow_dispatch: + inputs: + mode: + description: Stage aliases or prepare final artifacts without routing changes + type: choice + required: true + default: stage + options: + - stage + - prepare-final permissions: contents: read @@ -25,6 +34,7 @@ jobs: timeout-minutes: 30 environment: production env: + DEPLOY_MODE: ${{ inputs.mode }} VERCEL_PARADEV_API_PROJECT_ID: ${{ vars.VERCEL_PARADEV_API_PROJECT_ID }} VERCEL_PARADEV_WEB_PROJECT_ID: ${{ vars.VERCEL_PARADEV_WEB_PROJECT_ID }} @@ -69,6 +79,11 @@ jobs: if not condition: sys.exit(message) + MODE = os.environ["DEPLOY_MODE"] + require(MODE in ("stage", "prepare-final"), "Unknown deployment mode") + require(sys.argv[1] in ("check", "alias", "report"), "Unknown guard command") + require(sys.argv[1] != "alias" or MODE == "stage", "Final preparation cannot assign aliases") + for kind, project_id, *_ in TARGETS: require(os.environ.get(f"VERCEL_PARADEV_{kind}_PROJECT_ID") == project_id, f"Missing or incorrect {kind} project variable; refusing deployment") @@ -96,6 +111,8 @@ jobs: require(project["id"] == project_id and project["name"] == name and project["accountId"] == os.environ["VERCEL_ORG_ID"] and project["rootDirectory"] == root, f"Unexpected project identity for {kind}") + if MODE == "prepare-final": + return None domain = request(f"/v9/projects/{project_id}/domains/{host}") require(domain["name"] == host and domain.get("verified") is True and not domain.get("redirect") and not domain.get("gitBranch") @@ -109,7 +126,7 @@ jobs: for target in TARGETS: check_target(target) - if sys.argv[1] == "alias": + if sys.argv[1] in ("alias", "report"): deployments = [] for target in TARGETS: kind, project_id, name, host, root = target @@ -122,6 +139,13 @@ jobs: require(deployment["projectId"] == project_id and deployment["readyState"] == "READY" and deployment["target"] == "production", f"Unexpected deployment for {kind}") deployments.append(deployment["id"]) + if sys.argv[1] == "report": + with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as summary: + summary.write(f"{kind}: {deployment['id']} {url}\n\n") + if sys.argv[1] == "report": + with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as summary: + summary.write(f"Commit: {os.environ['GITHUB_SHA']}\n") + sys.exit(0) for target, deployment_id in zip(TARGETS, deployments): alias = check_target(target) host = target[3] @@ -153,13 +177,14 @@ jobs: VERCEL_PROJECT_ID: ${{ vars.VERCEL_PARADEV_API_PROJECT_ID }} DATABASE_URL: ${{ secrets.DATABASE_URL }} DATABASE_URL_UNPOOLED: ${{ secrets.DATABASE_URL_UNPOOLED }} - BROWSER_API_URL: https://api.stage.paradev.com + DOMAIN_MOVE_ENABLED: "false" + BROWSER_API_URL: ${{ inputs.mode == 'prepare-final' && 'https://api.paradev.com' || 'https://api.stage.paradev.com' }} NEXT_PUBLIC_API_URL: https://api.superset.sh - NEXT_PUBLIC_WEB_URL: https://app.stage.paradev.com + NEXT_PUBLIC_WEB_URL: ${{ inputs.mode == 'prepare-final' && 'https://app.paradev.com' || 'https://app.stage.paradev.com' }} NEXT_PUBLIC_MARKETING_URL: https://paradev.com NEXT_PUBLIC_ROOT_DOMAIN: ${{ secrets.NEXT_PUBLIC_ROOT_DOMAIN }} NEXT_PUBLIC_ADMIN_URL: ${{ secrets.NEXT_PUBLIC_ADMIN_URL }} - NEXT_PUBLIC_COOKIE_DOMAIN: stage.paradev.com + NEXT_PUBLIC_COOKIE_DOMAIN: ${{ inputs.mode == 'prepare-final' && 'paradev.com' || 'stage.paradev.com' }} BETTER_AUTH_SECRET: ${{ secrets.BETTER_AUTH_SECRET }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} R2_ACCESS_KEY_ID: ${{ secrets.R2_ACCESS_KEY_ID }} @@ -275,6 +300,7 @@ jobs: --env TEMPORAL_NAMESPACE=$TEMPORAL_NAMESPACE \ --env TEMPORAL_API_KEY=$TEMPORAL_API_KEY \ --env CLOUDFLARE_BROWSER_RENDERING_TOKEN=$CLOUDFLARE_BROWSER_RENDERING_TOKEN \ + --env DOMAIN_MOVE_ENABLED="$DOMAIN_MOVE_ENABLED" \ --env BROWSER_API_URL="$BROWSER_API_URL" \ --env NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL \ --env NEXT_PUBLIC_WEB_URL=$NEXT_PUBLIC_WEB_URL \ @@ -365,9 +391,10 @@ jobs: VERCEL_PROJECT_ID: ${{ vars.VERCEL_PARADEV_WEB_PROJECT_ID }} DATABASE_URL: ${{ secrets.DATABASE_URL }} DATABASE_URL_UNPOOLED: ${{ secrets.DATABASE_URL_UNPOOLED }} - BROWSER_API_URL: https://api.stage.paradev.com - NEXT_PUBLIC_API_URL: https://api.stage.paradev.com - NEXT_PUBLIC_WEB_URL: https://app.stage.paradev.com + DOMAIN_MOVE_ENABLED: "false" + BROWSER_API_URL: ${{ inputs.mode == 'prepare-final' && 'https://api.paradev.com' || 'https://api.stage.paradev.com' }} + NEXT_PUBLIC_API_URL: ${{ inputs.mode == 'prepare-final' && 'https://api.paradev.com' || 'https://api.stage.paradev.com' }} + NEXT_PUBLIC_WEB_URL: ${{ inputs.mode == 'prepare-final' && 'https://app.paradev.com' || 'https://app.stage.paradev.com' }} NEXT_PUBLIC_MARKETING_URL: https://paradev.com NEXT_PUBLIC_ROOT_DOMAIN: ${{ secrets.NEXT_PUBLIC_ROOT_DOMAIN }} NEXT_PUBLIC_ADMIN_URL: ${{ secrets.NEXT_PUBLIC_ADMIN_URL }} @@ -378,7 +405,7 @@ jobs: MEDIA_URL: ${{ secrets.MEDIA_URL }} STATIC_URL: ${{ secrets.STATIC_URL }} BETTER_AUTH_SECRET: ${{ secrets.BETTER_AUTH_SECRET }} - NEXT_PUBLIC_COOKIE_DOMAIN: stage.paradev.com + NEXT_PUBLIC_COOKIE_DOMAIN: ${{ inputs.mode == 'prepare-final' && 'paradev.com' || 'stage.paradev.com' }} NEXT_PUBLIC_POSTHOG_KEY: ${{ secrets.NEXT_PUBLIC_POSTHOG_KEY }} NEXT_PUBLIC_POSTHOG_HOST: ${{ secrets.NEXT_PUBLIC_POSTHOG_HOST }} NEXT_PUBLIC_SENTRY_DSN_WEB: ${{ secrets.NEXT_PUBLIC_SENTRY_DSN_WEB }} @@ -412,6 +439,7 @@ jobs: --env DATABASE_URL=$DATABASE_URL \ --env DATABASE_URL_UNPOOLED=$DATABASE_URL_UNPOOLED \ --env BETTER_AUTH_SECRET=$BETTER_AUTH_SECRET \ + --env DOMAIN_MOVE_ENABLED="$DOMAIN_MOVE_ENABLED" \ --env BROWSER_API_URL="$BROWSER_API_URL" \ --env NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL \ --env NEXT_PUBLIC_WEB_URL=$NEXT_PUBLIC_WEB_URL \ @@ -444,7 +472,15 @@ jobs: --env SERVER_ANTHROPIC_API_KEY=$SERVER_ANTHROPIC_API_KEY > "$RUNNER_TEMP/paradev-web-url" - name: Assign only staging aliases after both deployments succeed + if: inputs.mode == 'stage' env: VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} run: python3 "$RUNNER_TEMP/paradev-staging-guard.py" alias + + - name: Record final artifacts for routing approval + if: inputs.mode == 'prepare-final' + env: + VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }} + VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }} + run: python3 "$RUNNER_TEMP/paradev-staging-guard.py" report diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml index a829b767b1b..b029feb4973 100644 --- a/.github/workflows/deploy-preview.yml +++ b/.github/workflows/deploy-preview.yml @@ -195,6 +195,7 @@ jobs: NEXT_PUBLIC_SENTRY_DSN_API: ${{ secrets.NEXT_PUBLIC_SENTRY_DSN_API }} NEXT_PUBLIC_SENTRY_ENVIRONMENT: ${{ secrets.NEXT_PUBLIC_SENTRY_ENVIRONMENT }} NEXT_PUBLIC_POSTHOG_KEY: ${{ secrets.NEXT_PUBLIC_POSTHOG_KEY }} + DOMAIN_MOVE_ENABLED: ${{ secrets.DOMAIN_MOVE_ENABLED }} NEXT_PUBLIC_POSTHOG_HOST: ${{ secrets.NEXT_PUBLIC_POSTHOG_HOST }} POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }} PLUGIN_CLIENT_METADATA_BASE_URL: ${{ secrets.PLUGIN_CLIENT_METADATA_BASE_URL }} @@ -279,6 +280,7 @@ jobs: --env NEXT_PUBLIC_SENTRY_DSN_API=$NEXT_PUBLIC_SENTRY_DSN_API \ --env NEXT_PUBLIC_SENTRY_ENVIRONMENT=$NEXT_PUBLIC_SENTRY_ENVIRONMENT \ --env NEXT_PUBLIC_POSTHOG_KEY=$NEXT_PUBLIC_POSTHOG_KEY \ + --env DOMAIN_MOVE_ENABLED="$DOMAIN_MOVE_ENABLED" \ --env NEXT_PUBLIC_POSTHOG_HOST=$NEXT_PUBLIC_POSTHOG_HOST \ --env POSTHOG_API_KEY=$POSTHOG_API_KEY \ --env PLUGIN_CLIENT_METADATA_BASE_URL="$PLUGIN_CLIENT_METADATA_BASE_URL" \ @@ -416,6 +418,7 @@ jobs: BETTER_AUTH_SECRET: ${{ secrets.BETTER_AUTH_SECRET }} NEXT_PUBLIC_COOKIE_DOMAIN: ${{ contains(github.event.pull_request.labels.*.name, 'domain-preview') && format('pr-{0}.paradev.com', github.event.pull_request.number) || secrets.NEXT_PUBLIC_COOKIE_DOMAIN }} NEXT_PUBLIC_POSTHOG_KEY: ${{ secrets.NEXT_PUBLIC_POSTHOG_KEY }} + DOMAIN_MOVE_ENABLED: ${{ secrets.DOMAIN_MOVE_ENABLED }} NEXT_PUBLIC_POSTHOG_HOST: ${{ secrets.NEXT_PUBLIC_POSTHOG_HOST }} NEXT_PUBLIC_SENTRY_DSN_WEB: ${{ secrets.NEXT_PUBLIC_SENTRY_DSN_WEB }} NEXT_PUBLIC_SENTRY_ENVIRONMENT: ${{ secrets.NEXT_PUBLIC_SENTRY_ENVIRONMENT }} @@ -455,6 +458,7 @@ jobs: --env STATIC_URL=$STATIC_URL \ --env NEXT_PUBLIC_COOKIE_DOMAIN=$NEXT_PUBLIC_COOKIE_DOMAIN \ --env NEXT_PUBLIC_POSTHOG_KEY=$NEXT_PUBLIC_POSTHOG_KEY \ + --env DOMAIN_MOVE_ENABLED="$DOMAIN_MOVE_ENABLED" \ --env NEXT_PUBLIC_POSTHOG_HOST=$NEXT_PUBLIC_POSTHOG_HOST \ --env NEXT_PUBLIC_SENTRY_DSN_WEB=$NEXT_PUBLIC_SENTRY_DSN_WEB \ --env NEXT_PUBLIC_SENTRY_ENVIRONMENT=$NEXT_PUBLIC_SENTRY_ENVIRONMENT \ diff --git a/apps/api/src/app/api/auth/desktop/connect/desktopConnect.test.ts b/apps/api/src/app/api/auth/desktop/connect/desktopConnect.test.ts new file mode 100644 index 00000000000..6e2141640c2 --- /dev/null +++ b/apps/api/src/app/api/auth/desktop/connect/desktopConnect.test.ts @@ -0,0 +1,115 @@ +import { expect, mock, test } from "bun:test"; +import { desktopConnect } from "./desktopConnect"; + +const base = "https://api.superset.sh/api/auth/desktop/connect"; +const options = () => ({ + webUrl: "https://app.superset.sh", + enabled: "true", + lookup: mock(async () => true), + signInSocial: mock(async (_input: unknown) => + Response.json( + { url: "https://accounts.example/oauth" }, + { headers: { "set-cookie": "old-state=issued; Secure" } }, + ), + ), +}); + +test("validation errors happen before routing or OAuth", async () => { + for (const [query, error] of [ + ["", "Missing provider or state"], + ["?provider=google", "Missing provider or state"], + ["?provider=other&state=x", "Invalid provider"], + ["?provider=github&state=x&protocol=https", "Invalid protocol"], + ]) { + const dependencies = options(); + const response = await desktopConnect( + new Request(base + query), + dependencies, + ); + expect(response.status).toBe(400); + expect(await response.text()).toBe(error); + expect(dependencies.lookup).not.toHaveBeenCalled(); + expect(dependencies.signInSocial).not.toHaveBeenCalled(); + expect(response.headers.get("set-cookie")).toBeNull(); + } +}); + +test("moves both providers and protocol variants before issuing old OAuth state, preserving raw query", async () => { + for (const provider of ["google", "github"]) { + for (const protocol of [ + "superset", + "superset-dev", + "superset-my-worktree2", + "Superset", + ]) { + const query = `?provider=${provider}&state=a%2Fb+c&state=duplicate&protocol=${protocol}&local_callback=http%3A%2F%2F127.0.0.1%3A3456%2Fauth%2Fcallback`; + const dependencies = options(); + const response = await desktopConnect( + new Request(base + query), + dependencies, + ); + expect(response.status).toBe(307); + expect(response.headers.get("location")).toBe( + `https://api.paradev.com/api/auth/desktop/connect${query}`, + ); + expect(response.headers.get("cache-control")).toBe("private, no-store"); + expect(response.headers.get("set-cookie")).not.toContain("old-state"); + expect(dependencies.signInSocial).not.toHaveBeenCalled(); + } + } +}); + +test("gate off retains existing OAuth response and loopback validation without lookup or rollout cookie", async () => { + for (const callback of [ + "http://localhost:3456/auth/callback", + "https://evil.test/auth/callback", + "garbage", + ]) { + const dependencies = { ...options(), enabled: "false" }; + const response = await desktopConnect( + new Request( + `${base}?provider=google&state=test&local_callback=${encodeURIComponent(callback)}`, + ), + dependencies, + ); + expect(response.headers.get("location")).toBe( + "https://accounts.example/oauth", + ); + expect(response.headers.getSetCookie()).toEqual([ + "old-state=issued; Secure", + ]); + expect(dependencies.lookup).not.toHaveBeenCalled(); + const input = dependencies.signInSocial.mock.calls[0]?.[0] as { + body: { callbackURL: string }; + }; + const target = new URL(input.body.callbackURL); + expect(target.searchParams.get("desktop_state")).toBe("test"); + expect(target.searchParams.get("desktop_local_callback")).toBe( + callback.startsWith("http://localhost") ? callback : null, + ); + } +}); + +test("final, stage, preview and non-GET requests retain existing OAuth path", async () => { + for (const origin of [ + "https://api.paradev.com", + "https://api.stage.paradev.com", + "https://api.pr-1.paradev.com", + ]) { + const dependencies = options(); + await desktopConnect( + new Request( + `${origin}/api/auth/desktop/connect?provider=github&state=test`, + ), + dependencies, + ); + expect(dependencies.lookup).not.toHaveBeenCalled(); + expect(dependencies.signInSocial).toHaveBeenCalledTimes(1); + } + const dependencies = options(); + await desktopConnect( + new Request(`${base}?provider=google&state=test`, { method: "HEAD" }), + dependencies, + ); + expect(dependencies.lookup).not.toHaveBeenCalled(); +}); diff --git a/apps/api/src/app/api/auth/desktop/connect/desktopConnect.ts b/apps/api/src/app/api/auth/desktop/connect/desktopConnect.ts new file mode 100644 index 00000000000..0b4328935e8 --- /dev/null +++ b/apps/api/src/app/api/auth/desktop/connect/desktopConnect.ts @@ -0,0 +1,100 @@ +import { withDomainMove } from "@superset/shared/domain-move"; +import { NextResponse } from "next/server"; +import { isDesktopProtocol } from "./desktopProtocol"; + +export async function desktopConnect( + request: Request, + options: { + webUrl: string; + enabled?: string; + lookup: Parameters[0]["lookup"]; + signInSocial: (input: { + body: { + provider: "google" | "github"; + callbackURL: string; + errorCallbackURL: string; + }; + asResponse: true; + }) => Promise; + }, +) { + const url = new URL(request.url); + const provider = url.searchParams.get("provider"); + const state = url.searchParams.get("state"); + const protocol = url.searchParams.get("protocol"); + const localCallback = url.searchParams.get("local_callback"); + + if (!provider || !state) { + return new Response("Missing provider or state", { status: 400 }); + } + + if (provider !== "google" && provider !== "github") { + return new Response("Invalid provider", { status: 400 }); + } + + if (protocol && !isDesktopProtocol(protocol)) { + return new Response("Invalid protocol", { status: 400 }); + } + + const successUrl = new URL(`${options.webUrl}/auth/desktop/success`); + successUrl.searchParams.set("desktop_state", state); + if (protocol) { + successUrl.searchParams.set("desktop_protocol", protocol); + } + if (localCallback) { + try { + const callbackUrl = new URL(localCallback); + const isLoopback = + callbackUrl.protocol === "http:" && + (callbackUrl.hostname === "127.0.0.1" || + callbackUrl.hostname === "localhost"); + if (isLoopback && callbackUrl.pathname === "/auth/callback") { + successUrl.searchParams.set( + "desktop_local_callback", + callbackUrl.toString(), + ); + } + } catch { + // Ignore invalid callback URLs and continue with deep-link flow. + } + } + + return withDomainMove({ + request, + enabled: options.enabled, + surface: "desktop", + lookup: options.lookup, + fallback: async () => { + const result = await options.signInSocial({ + body: { + provider, + callbackURL: successUrl.toString(), + errorCallbackURL: `${options.webUrl}/auth/desktop/success`, + }, + asResponse: true, + }); + + const cookies = result.headers.getSetCookie(); + const body = (await result.json()) as { + url?: string; + redirect?: boolean; + }; + + if (!body.url) { + return new Response( + `Failed to initiate OAuth: ${JSON.stringify(body)}`, + { + status: 500, + }, + ); + } + + const response = NextResponse.redirect(body.url); + for (const cookie of cookies) { + response.headers.append("set-cookie", cookie); + } + + return response; + }, + }); +} diff --git a/apps/api/src/app/api/auth/desktop/connect/route.ts b/apps/api/src/app/api/auth/desktop/connect/route.ts index 9d925f72cfa..bc3c55f9219 100644 --- a/apps/api/src/app/api/auth/desktop/connect/route.ts +++ b/apps/api/src/app/api/auth/desktop/connect/route.ts @@ -1,73 +1,15 @@ import { auth } from "@superset/auth/server"; -import { NextResponse } from "next/server"; - import { env } from "@/env"; -import { isDesktopProtocol } from "./desktopProtocol"; - -export async function GET(request: Request) { - const url = new URL(request.url); - const provider = url.searchParams.get("provider"); - const state = url.searchParams.get("state"); - const protocol = url.searchParams.get("protocol"); - const localCallback = url.searchParams.get("local_callback"); - - if (!provider || !state) { - return new Response("Missing provider or state", { status: 400 }); - } - - if (provider !== "google" && provider !== "github") { - return new Response("Invalid provider", { status: 400 }); - } - - if (protocol && !isDesktopProtocol(protocol)) { - return new Response("Invalid protocol", { status: 400 }); - } - - const successUrl = new URL(`${env.NEXT_PUBLIC_WEB_URL}/auth/desktop/success`); - successUrl.searchParams.set("desktop_state", state); - if (protocol) { - successUrl.searchParams.set("desktop_protocol", protocol); - } - if (localCallback) { - try { - const callbackUrl = new URL(localCallback); - const isLoopback = - callbackUrl.protocol === "http:" && - (callbackUrl.hostname === "127.0.0.1" || - callbackUrl.hostname === "localhost"); - if (isLoopback && callbackUrl.pathname === "/auth/callback") { - successUrl.searchParams.set( - "desktop_local_callback", - callbackUrl.toString(), - ); - } - } catch { - // Ignore invalid callback URLs and continue with deep-link flow. - } - } - - const result = await auth.api.signInSocial({ - body: { - provider, - callbackURL: successUrl.toString(), - errorCallbackURL: `${env.NEXT_PUBLIC_WEB_URL}/auth/desktop/success`, +import { desktopConnect } from "./desktopConnect"; + +export function GET(request: Request) { + return desktopConnect(request, { + webUrl: env.NEXT_PUBLIC_WEB_URL, + enabled: env.DOMAIN_MOVE_ENABLED, + signInSocial: (input) => auth.api.signInSocial(input), + lookup: async (...args) => { + const { posthog } = await import("@/lib/analytics"); + return posthog.getFeatureFlag(...args); }, - asResponse: true, }); - - const cookies = result.headers.getSetCookie(); - const body = (await result.json()) as { url?: string; redirect?: boolean }; - - if (!body.url) { - return new Response(`Failed to initiate OAuth: ${JSON.stringify(body)}`, { - status: 500, - }); - } - - const response = NextResponse.redirect(body.url); - for (const cookie of cookies) { - response.headers.append("set-cookie", cookie); - } - - return response; } diff --git a/apps/api/src/app/api/integrations/sentry/connect/route.ts b/apps/api/src/app/api/integrations/sentry/connect/route.ts index 129eec86d35..ae0034ca7e7 100644 --- a/apps/api/src/app/api/integrations/sentry/connect/route.ts +++ b/apps/api/src/app/api/integrations/sentry/connect/route.ts @@ -1,3 +1,4 @@ +import { withLegacySentryConnect } from "@superset/shared/legacy-integration-connect"; import { env } from "@/env"; import { beginOAuthFlow, STATE_COOKIES } from "@/lib/integrations/oauthFlow"; import { requireOrgMember } from "@/lib/integrations/requireOrgMember"; @@ -13,19 +14,21 @@ import { requireOrgMember } from "@/lib/integrations/requireOrgMember"; * state cookie rather than through Sentry. */ export async function GET(request: Request) { - const member = await requireOrgMember(request); - if (member instanceof Response) return member; + return withLegacySentryConnect(request, async () => { + const member = await requireOrgMember(request); + if (member instanceof Response) return member; - if (!env.SENTRY_APP_SLUG || !env.SENTRY_CLIENT_ID) { - return Response.redirect( - `${env.NEXT_PUBLIC_WEB_URL}/integrations/sentry?error=not_configured`, - ); - } + if (!env.SENTRY_APP_SLUG || !env.SENTRY_CLIENT_ID) { + return Response.redirect( + `${env.NEXT_PUBLIC_WEB_URL}/integrations/sentry?error=not_configured`, + ); + } - return beginOAuthFlow({ - cookie: STATE_COOKIES.sentry, - payload: { organizationId: member.organizationId, userId: member.userId }, - authorizeUrl: () => - `https://sentry.io/sentry-apps/${env.SENTRY_APP_SLUG}/external-install/`, + return beginOAuthFlow({ + cookie: STATE_COOKIES.sentry, + payload: { organizationId: member.organizationId, userId: member.userId }, + authorizeUrl: () => + `https://sentry.io/sentry-apps/${env.SENTRY_APP_SLUG}/external-install/`, + }); }); } diff --git a/apps/api/src/env.ts b/apps/api/src/env.ts index 1eb266108cc..4e869e34c3d 100644 --- a/apps/api/src/env.ts +++ b/apps/api/src/env.ts @@ -8,6 +8,7 @@ export const env = createEnv({ .default("development"), }, server: { + DOMAIN_MOVE_ENABLED: z.string().optional(), BROWSER_API_URL: z.string().url().optional(), DATABASE_URL: z.string(), DATABASE_URL_UNPOOLED: z.string(), diff --git a/apps/web/src/app/auth/integration-connect/route.ts b/apps/web/src/app/auth/integration-connect/route.ts new file mode 100644 index 00000000000..0b8d828656f --- /dev/null +++ b/apps/web/src/app/auth/integration-connect/route.ts @@ -0,0 +1,9 @@ +import { auth } from "@superset/auth/server"; +import { continueLegacyIntegrationConnect } from "@superset/shared/legacy-integration-connect"; + +export function GET(request: Request) { + return continueLegacyIntegrationConnect(request, async () => { + const session = await auth.api.getSession({ headers: request.headers }); + return Boolean(session?.user); + }); +} diff --git a/apps/web/src/proxy-routes.test.ts b/apps/web/src/proxy-routes.test.ts index 1e7ddbd4db3..fa3de54e377 100644 --- a/apps/web/src/proxy-routes.test.ts +++ b/apps/web/src/proxy-routes.test.ts @@ -13,6 +13,7 @@ describe("isPublicRoute", () => { "/sign-in/callback", "/sign-up", "/auth/desktop", + "/auth/integration-connect", "/auth/desktop/success", "/api/auth/desktop", "/api/auth/desktop/callback", @@ -40,6 +41,7 @@ describe("isPublicRoute", () => { "/sign-internal/settings", "/sign-upgrade", "/auth/desktopish", + "/auth/integration-connect-other", "/api/auth/desktopish", "/accept-invitation-list", "/cli/auth/codegen", diff --git a/apps/web/src/proxy-routes.ts b/apps/web/src/proxy-routes.ts index 7fb19ede487..845caa87df2 100644 --- a/apps/web/src/proxy-routes.ts +++ b/apps/web/src/proxy-routes.ts @@ -2,6 +2,7 @@ const authPageRoutes = ["/sign-in", "/sign-up"] as const; const otherPublicRoutes = [ "/auth/desktop", + "/auth/integration-connect", "/api/auth/desktop", "/accept-invitation", "/cli/auth/code", diff --git a/apps/web/src/proxy.ts b/apps/web/src/proxy.ts index cb036bdaae5..c16e2f77fbf 100644 --- a/apps/web/src/proxy.ts +++ b/apps/web/src/proxy.ts @@ -1,5 +1,6 @@ import { auth } from "@superset/auth/server"; import { isStaffEmail } from "@superset/shared/constants"; +import { withDomainMove } from "@superset/shared/domain-move"; import { headers } from "next/headers"; import { type NextRequest, NextResponse } from "next/server"; @@ -15,6 +16,25 @@ export default async function proxy(req: NextRequest) { headers: await headers(), }); + return withDomainMove({ + request: req, + enabled: + session?.user.deletionRequestedAt || isInternalRoute(req.nextUrl.pathname) + ? undefined + : process.env.DOMAIN_MOVE_ENABLED, + surface: "web", + lookup: async (...args) => { + const { posthogServer } = await import("./lib/posthog-server"); + return posthogServer.getFeatureFlag(...args); + }, + fallback: () => legacyProxy(req, session), + }); +} + +async function legacyProxy( + req: NextRequest, + session: Awaited>, +) { const pathname = req.nextUrl.pathname; // API routes must never redirect to /sign-in: clients (CLI, host-service, diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 37131f56be1..7ee772c4661 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -131,3 +131,18 @@ Connector client metadata keeps its stable client ID and accepts callback twins on our own domains. Existing dynamic-client grants look up their original client across those callback twins, scoped to the same issuer and connector; new grants still select registrations for the current callback only. + +## Domain move opt-in + +`DOMAIN_MOVE_ENABLED` is optional in API and web, and only literal `true` enables +the routing flag lookup. Every other value defaults to the existing behavior, +without a rollout cookie or added remote lookup. It is server-only and included +in Turbo's env inputs and API/web preview/manual build/runtime passthrough. +The web proxy reads this optional string directly from `process.env` so its +auth gate does not load the full app environment schema. The API reads its +declared server environment value. +Production passthrough is required in a separately approved activation change. No +secret or local `.env` value is set by the inert preparation change. Stage and +final artifact preparation explicitly pass `false`. See +[the cutover handoff](paradev-staging.md#inert-domain-cutover-preparation) before +any future activation, including its merge/deployment warning. diff --git a/docs/paradev-staging.md b/docs/paradev-staging.md index 6a3ffeee02d..0b184d59304 100644 --- a/docs/paradev-staging.md +++ b/docs/paradev-staging.md @@ -1,9 +1,7 @@ # Paradev staging with production state -`.github/workflows/deploy-paradev-staging.yml` is manual-only. Review and merge it -with its parent changes (PR #9687) before a separately approved dispatch from +`.github/workflows/deploy-paradev-staging.yml` is manual-only and runs from `main`. It does nothing on PRs or pushes, and rejects other refs and repositories. -Do not dispatch it as part of preparing or reviewing this change. The job reads the GitHub `production` environment secrets only inside Actions. Never fetch those secrets locally. It retains the production API/web build @@ -95,3 +93,175 @@ auth cookies are stage-scoped. These deployments use the **production** PostHog key/host: check the intended project, stage event URL/host, and designated test identity. Expect test events in production analytics; this is not the Preview PostHog project. Discard the profile after testing. + +## Inert domain cutover preparation + +Production `DOMAIN_MOVE_ENABLED` build/runtime passthrough is deliberately +absent. Adding it to `deploy-production.yml` is required before rollout; that +watched path can redeploy relay, realtime and agent-worker. Do not weaken those +filters. Building final artifacts does not activate routing or assign domains. + +The manual workflow now accepts `mode`, a fixed choice defaulting to `stage`. +Stage retains the table and ownership checks above. `prepare-final` builds the +same exact new projects with these settings: + +| Setting | API | Web | +| --- | --- | --- | +| `NEXT_PUBLIC_API_URL` | `https://api.superset.sh` | `https://api.paradev.com` | +| `BROWSER_API_URL` | `https://api.paradev.com` | `https://api.paradev.com` | +| `NEXT_PUBLIC_WEB_URL` | `https://app.paradev.com` | `https://app.paradev.com` | +| `NEXT_PUBLIC_COOKIE_DOMAIN` | `paradev.com` | `paradev.com` | +| `DOMAIN_MOVE_ENABLED` | `false` | `false` | + +Both modes use `--prod --skip-domain`, reject cron output, and retain the exact +project ID/name/team/root and pulled project-link checks. Final preparation does +not require final domains on those projects: they may still belong to legacy +projects. It makes no domain/alias requests, transfers, migrations, or worker +changes. After verifying both deployments are READY on their expected projects, +the Actions summary records only their IDs/URLs and the source SHA for approval. +Production secrets stay in the existing Actions environment references. Preparing +these production-state-capable artifacts still requires a separately approved +dispatch; preparing source is not dispatch authorization. + +### Disabled by default + +The optional server setting `DOMAIN_MOVE_ENABLED` must be exactly `true` before +any domain-move flag lookup, PostHog client import by the routing hook, or rollout +cookie creation. Missing, empty, `false`, and other values retain the existing +response. API/web preview jobs pass the optional setting from +`secrets.DOMAIN_MOVE_ENABLED`; production passthrough remains a required separate +activation change. This preparation creates or changes no secret. The +isolated stage/final workflow explicitly keeps the setting `false`. + +Once explicitly enabled, eligible requests evaluate the shared PostHog flag +`web-domain-move`. Only boolean `true` moves the request. Missing/deleted flags, +variants, errors and the 500 ms evaluation deadline retain the old flow. Exposure +events are suppressed. There is no application result cache (maximum age zero) +and no per-user map. The deadline bounds added request latency, including lazy +client loading; it does not cancel the SDK's underlying request. Rollback must +allow already-started evaluations/OAuth flows to finish. + +Eligible requests use a random UUID in the functional first-party cookie +`__Secure-domain-move-id`, scoped to `superset.sh`, path `/`, Secure, HttpOnly, +SameSite=Lax, 30-day lifetime. This is independent of analytics cookies, IP, +provider and OAuth state. Both legacy hosts read the same browser cohort. A +first-time desktop start gets an ID before evaluation, so a 100% boolean rollout +can cover it. After server opt-in, the cookie intentionally persists even when +the percentage excludes the browser or the flag lookup fails. Ineligible routes +never create it; disabling the server opt-in creates no cookies and does not +refresh existing ones. Clearing/blocking cookies or using another browser can +change cohort assignment. Both signed-in and anonymous requests use this same +cookie, never `user.id`; +desktop start adds no session lookup. Percentage rollout is browser-scoped and +stays consistent across both old hosts after login. Staff validation must target +the explicit browser cohort UUID, not a staff user ID or person property. Never +use this flag or cookie for access control. + +### Routing contract and approval handoff + +Eligible old-app browser document GET/HEAD requests receive a fixed-origin 307 +to `https://app.paradev.com`, with the path/query preserved and +`Cache-Control: private, no-store`. New/stage/preview hosts do not move. Host and +forwarded-host headers cannot override the URL origin; conflicting values fail +closed. Verify actual Vercel request-host normalization before activation. + +Keep `/` (desktop billing cancel/default portal return), `/settings/billing`, +invitation, API/tRPC, OAuth, CLI auth, desktop auth/success, `.well-known`, MCP, +assets, telemetry, RSC/prefetch and server actions on the old flow. Sign-in/up +continuations to retained routes, including encoded/absolute/nested root and +billing continuations, also stay there. Invalid or foreign continuations remain +with the existing sanitizer. Pending-deletion and internal-route enforcement +remain on the existing path. Root retention deliberately leaves ordinary root +visits on the old app too. + +Only exact GET `https://api.superset.sh/api/auth/desktop/connect` can move to +`https://api.paradev.com/api/auth/desktop/connect`. Required provider/state and +protocol validation still runs first. Preserve the original query bytes, +including duplicates, provider, state, protocol and Linux loopback callback. +No old-host OAuth call or state/session cookie is issued on the move branch; +only the functional cohort cookie may be set. Invalid local callbacks retain the +existing deep-link fallback. Old callbacks and desktop success remain available +for in-flight sessions; API issuer/audience and released client defaults stay +unchanged. No provider fallback buttons are added. + +Before requesting activation support, collect the reviewed SHA, prepared final +IDs/URLs, existing stage/final alias owners and rollback deployment IDs. Read +ownership separately; preparation must not auto-transfer it. Confirm final +Google/GitHub registrations, database compatibility, and the designated test +account. Do not remove old callbacks. Request explicit support for final alias +assignment/transfer, removal of any new-to-old platform redirect, and server +opt-in/flag rollout separately. A new-to-old redirect would loop once the +old-to-new gate is active; the source helper cannot prevent an external reverse +redirect. Verify final hosts independently before enabling the old-host gate. + +After explicit authorization, test designated-account browser sign-in, released +desktop Google/GitHub starts and Linux loopback, query/state handoff, cookies, +RSC navigation, CLI/MCP consent and AASA/public links. Confirm stage still points +to stage-configured deployments. Do not perform billing/vendor operations as +part of these checks. Live verification and provider readiness are not proven +by local tests. + +For rollback, turn the shared flag off first; the next completed evaluation +fails closed without an application cache delay. The server opt-in can also be +disabled on both legacy deployments. Keep old and new callbacks/deployments so +in-flight OAuth completes. Do not restore any new-to-old redirect until the +old-to-new move is disabled. Restoring recorded final alias owners requires its +own routing approval; never send final hosts to stage-configured builds. No DB +rollback is involved. + +### Verified staging evidence — 2026-11-12 + +Coordinator-observed evidence, from before this preparation commit: + +- Production-state staging [workflow run 43332717765](https://github.com/ParadevAI/Paradev/actions/runs/43332717765) + succeeded at `6dcb20252acb34dd8deb99c35a5f4ad646b6604f`. +- Real Google and GitHub stage sign-in succeeded. A real desktop Google loopback + temporary session was cryptographically validated against the legacy + issuer/audience/JWKS; only that test session was revoked. +- Chrome's marketing-to-stage-sign-in journey joined one identified + [PostHog person](https://us.posthog.com/project/264803/persons/8a200ca9-4273-540e-aa06-6418b8511a64). +- The primary Slack and Linear production consoles already contained the final + callbacks. Sentry, Teams and Notion settings remain unverified, as do actual + integration consent/writes and final-host routing/cutover. +- At observation, `https://app.paradev.com/sign-in` still returned 307 to + `https://app.superset.sh/sign-in`; old and stage sign-in returned 200. Both + observed OAuth metadata responses retained issuer `https://api.superset.sh`, + and MCP resource discovery retained `https://api.superset.sh/mcp` with 200 + responses. + +This evidence does not validate this commit on live final hosts or authorize +removing the reverse redirect, assigning final aliases, or enabling rollout. + +### Sentry legacy connect fallback + +Sentry's public integration has one fixed callback, +`https://api.superset.sh/api/integrations/sentry/callback`. Until a separately +reviewed provider change, final and stage API Sentry connect requests must reach +that legacy host before OAuth state is created. This preparation changes no +provider registration, environment setting, or live routing. + +An explicit Sentry connect request on `https://api.paradev.com` or +`https://api.stage.paradev.com` now follows this sequence: + +1. Validate the explicit `organizationId` UUID, then return a no-store 307 to + `https://app.superset.sh/auth/integration-connect?provider=sentry&organizationId=...`. + No new-host membership lookup, OAuth state, or state cookie is created. +2. The legacy-web continuation accepts only Sentry and one valid organization + UUID. It reads the existing legacy auth session. Without a session, it sends + the browser to the existing legacy sign-in page with a **relative** continuation + containing the same provider and organization ID. +3. After sign-in, redirect to the fixed old API Sentry connect route with the + explicit organization ID. `requireOrgMember` remains the authorization check + there before the original OAuth flow runs. Active organization selection is + never used to choose the target, and no session/token is forwarded. + +`/auth/integration-connect`, `/integrations/sentry`, and sign-in/up continuations +into either route remain on legacy even at 100% domain rollout. Old, local, and +preview Sentry starts retain their original behavior. Other providers are +unchanged. This is a handoff for an explicit user connect action, not an automatic +installation or authorization grant. The user may need to sign in on legacy. + +Local tests cover the redirect/sign-in/resume sequence and routing exclusions; +no real Sentry consent, installation, callback or connection write was performed. +Final-host/provider integration verification still needs separately authorized +access and a designated test organization. diff --git a/packages/shared/package.json b/packages/shared/package.json index 79bfbf69879..02234501332 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -4,6 +4,8 @@ "private": true, "type": "module", "exports": { + "./legacy-integration-connect": "./src/legacy-integration-connect/index.ts", + "./domain-move": "./src/domain-move/index.ts", "./browser-api-url": "./src/browser-api-url.ts", "./agent-routines": { "types": "./src/agent-routines.ts", diff --git a/packages/shared/src/constants.ts b/packages/shared/src/constants.ts index f6c2bc96bae..aced6bdf40b 100644 --- a/packages/shared/src/constants.ts +++ b/packages/shared/src/constants.ts @@ -146,6 +146,7 @@ export const V2_NEW_USER_V1_EXPERIMENT_START = "2026-06-08T06:59:00.000Z"; export const V2_NEW_USER_V2_DEFAULT_START = "2026-07-09T17:00:00.000Z"; export const FEATURE_FLAGS = { + WEB_DOMAIN_MOVE: "web-domain-move", /** * Shows the `.paradev/config.toml` editor in project settings in place of * the config.json form. host-service reads a config.toml wherever one diff --git a/packages/shared/src/domain-move/domain-move.test.ts b/packages/shared/src/domain-move/domain-move.test.ts new file mode 100644 index 00000000000..855bb6c88f3 --- /dev/null +++ b/packages/shared/src/domain-move/domain-move.test.ts @@ -0,0 +1,371 @@ +import { describe, expect, mock, test } from "bun:test"; +import { domainMoveTarget, withDomainMove } from "./domain-move"; + +const web = (path = "/dashboard", init: RequestInit = {}) => + new Request(`https://app.superset.sh${path}`, { + headers: { accept: "text/html" }, + ...init, + }); +const legacy = () => + Promise.resolve( + new Response("legacy", { headers: { "x-legacy": "unchanged" } }), + ); +const cookie = "__Secure-domain-move-id=12345678-1234-4123-8123-123456789abc"; + +describe("domain move policy", () => { + test("only exact old origins are sources, independent of forwarded headers", () => { + for (const origin of [ + "https://app.paradev.com", + "https://app.stage.paradev.com", + "https://app.pr-1.paradev.com", + "http://localhost:3000", + "https://app.superset.sh.evil.test", + "https://app.superset.sh:444", + "http://app.superset.sh", + ]) { + expect( + domainMoveTarget( + new Request(`${origin}/`, { + headers: { + accept: "text/html", + "x-forwarded-host": "app.superset.sh", + }, + }), + "web", + ), + ).toBeNull(); + } + expect( + domainMoveTarget( + web("/dashboard", { + headers: { accept: "text/html", "x-forwarded-host": "evil.test" }, + }), + "web", + ), + ).toBeNull(); + expect( + domainMoveTarget( + web("/dashboard", { + headers: { accept: "text/html", host: "evil.test" }, + }), + "web", + ), + ).toBeNull(); + }); + + test("retains protocol, auth, billing, invitation and asset routes with boundaries", () => { + for (const path of [ + "/api", + "/trpc", + "/oauth", + "/cli/auth", + "/auth/desktop", + "/auth/integration-connect", + "/integrations/sentry", + "/.well-known", + "/mcp", + "/accept-invitation", + "/settings/billing", + "/_next", + "/ingest", + "/monitoring", + ]) { + for (const suffix of ["", "/child", "?success=true", "?cancel=true"]) + expect(domainMoveTarget(web(path + suffix), "web")).toBeNull(); + } + for (const path of [ + "/", + "/?success=true", + "/image.png", + "/robots.txt", + "/%61pi/auth", + "/settings%2Fbilling", + "/broken%zz", + ]) + expect(domainMoveTarget(web(path), "web")).toBeNull(); + for (const path of ["/api-keys", "/page/123", "/workspaces/123"]) + expect(domainMoveTarget(web(path), "web")?.pathname).toBe(path); + }); + + test("retains encoded, absolute and nested protected sign-in/up continuations", () => { + for (const page of ["/sign-in", "/sign-up"]) { + for (const path of [ + "/", + "/oauth/consent?client_id=123", + "/cli/auth/code", + "/auth/desktop/success", + "/auth/integration-connect?provider=sentry&organizationId=803f13a4-49b9-4664-826f-4090c750b0bc", + "/integrations/sentry", + "/settings/billing?success=true", + "/settings/billing", + "/accept-invitation/id", + ]) { + for (const redirect of [ + path, + `https://app.superset.sh${path}`, + encodeURIComponent(path), + `/sign-in?redirect=${encodeURIComponent(path)}`, + ]) { + expect( + domainMoveTarget( + web(`${page}?redirect=${encodeURIComponent(redirect)}`), + "web", + ), + ).toBeNull(); + } + } + for (const redirect of [ + "https://evil.test", + "//evil.test", + "/\t/evil.test", + "/\\evil.test", + "/%252F%252Fevil.test", + ]) + expect( + domainMoveTarget( + web(`${page}?redirect=${encodeURIComponent(redirect)}`), + "web", + ), + ).toBeNull(); + } + expect( + domainMoveTarget(web("/sign-in?redirect=%2Fapi-keys"), "web")?.hostname, + ).toBe("app.paradev.com"); + }); + + test("only browser document GET/HEAD moves; RSC, prefetch and actions remain", () => { + for (const method of ["POST", "PUT", "PATCH", "DELETE", "OPTIONS"]) + expect( + domainMoveTarget( + web("/dashboard", { method, headers: { accept: "text/html" } }), + "web", + ), + ).toBeNull(); + for (const headers of [ + { rsc: "1" }, + { "next-action": "id" }, + { "next-router-prefetch": "1" }, + { "next-router-state-tree": "[]" }, + { "next-router-segment-prefetch": "/" }, + { purpose: "prefetch" }, + { "sec-purpose": "prefetch;prerender" }, + { "sec-fetch-dest": "empty" }, + { "sec-fetch-mode": "cors" }, + { accept: "application/json" }, + { accept: "text/html;q=0" }, + ] as Record[]) + expect( + domainMoveTarget( + web("/dashboard", { headers: { accept: "text/html", ...headers } }), + "web", + ), + ).toBeNull(); + expect(domainMoveTarget(web("/dashboard?_rsc=abc"), "web")).toBeNull(); + expect( + domainMoveTarget(web("/dashboard", { headers: {} }), "web"), + ).toBeNull(); + for (const method of ["GET", "HEAD"]) + expect( + domainMoveTarget( + web("/foo?a=%2f&b=1+2&a=3", { + method, + headers: { accept: "text/html" }, + }), + "web", + )?.href, + ).toBe("https://app.paradev.com/foo?a=%2f&b=1+2&a=3"); + expect(domainMoveTarget(web("//evil.test/path"), "web")).toBeNull(); + }); + + test("desktop source is exact GET and cannot recurse at destination", () => { + for (const url of [ + "https://api.paradev.com/api/auth/desktop/connect", + "https://api.stage.paradev.com/api/auth/desktop/connect", + "https://api.superset.sh/api/auth/desktop/connect/", + "https://api.superset.sh/api/auth/callback/google", + ]) + expect(domainMoveTarget(new Request(url), "desktop")).toBeNull(); + expect( + domainMoveTarget( + new Request("https://api.superset.sh/api/auth/desktop/connect", { + method: "HEAD", + }), + "desktop", + ), + ).toBeNull(); + }); +}); + +describe("domain move evaluation", () => { + test("OAuth-provider sign-in and sign-up stay on legacy without flag lookup at full rollout", async () => { + const lookup = mock(async () => true); + for (const page of ["/sign-in", "/sign-up"]) { + for (const query of [ + "client_id=cli-client&sig=signed-request", + "client_id=cli-client", + "sig=signed-request", + "client_id=", + "sig=", + "%63lient_id=cli-client&redirect=%2Fdashboard", + ]) { + for (const path of [ + `${page}?${query}`, + `/sign-in?redirect=${encodeURIComponent(`${page}?${query}`)}`, + ]) { + const original = new Response("legacy OAuth login"); + const response = await withDomainMove({ + request: web(path), + surface: "web", + enabled: "true", + lookup, + fallback: async () => original, + }); + expect(response).toBe(original); + expect(response.headers.get("set-cookie")).toBeNull(); + } + } + expect(domainMoveTarget(web(page), "web")?.hostname).toBe( + "app.paradev.com", + ); + } + expect(lookup).not.toHaveBeenCalled(); + }); + + test("off is byte-equivalent, with no lookup or cookie even if flag would be true", async () => { + const lookup = mock(async () => true); + for (const enabled of [undefined, "", "false", "TRUE", "1"]) { + const original = new Response("legacy", { status: 201 }); + const response = await withDomainMove({ + request: web(), + surface: "web", + enabled, + lookup, + fallback: async () => original, + }); + expect(response).toBe(original); + expect(response.headers.get("set-cookie")).toBeNull(); + } + expect(lookup).not.toHaveBeenCalled(); + }); + + test("excluded requests do not evaluate or create cookies when enabled", async () => { + const lookup = mock(async () => true); + const response = await withDomainMove({ + request: web("/settings/billing"), + surface: "web", + enabled: "true", + lookup, + fallback: legacy, + }); + expect(lookup).not.toHaveBeenCalled(); + expect(response.headers.get("set-cookie")).toBeNull(); + }); + + test("false, absent, variants, failure and deadline retain legacy with functional cohort cookie", async () => { + for (const value of [false, undefined, null, "true", "variant", 1]) { + const response = await withDomainMove({ + request: web(), + surface: "web", + enabled: "true", + lookup: async () => value, + fallback: legacy, + }); + expect(await response.text()).toBe("legacy"); + expect(response.headers.get("set-cookie")).toContain( + "Domain=superset.sh", + ); + expect(response.headers.get("cache-control")).toBe("private, no-store"); + } + const failed = await withDomainMove({ + request: web(), + surface: "web", + enabled: "true", + lookup: async () => { + throw Error("offline"); + }, + fallback: legacy, + }); + expect(await failed.text()).toBe("legacy"); + const schedule = ((callback: () => void) => { + queueMicrotask(callback); + return 0; + }) as unknown as typeof setTimeout; + const timedOut = await withDomainMove({ + request: web(), + surface: "web", + enabled: "true", + lookup: () => new Promise(() => {}), + fallback: legacy, + schedule, + }); + expect(await timedOut.text()).toBe("legacy"); + }); + + test("first-time browser gets a stable shared cohort even when excluded by percentage", async () => { + const ids: string[] = []; + const lookup = mock( + async ( + key: string, + id: string, + options: { sendFeatureFlagEvents: false }, + ) => { + expect(key).toBe("web-domain-move"); + expect(options).toEqual({ sendFeatureFlagEvents: false }); + ids.push(id); + return false; + }, + ); + const first = await withDomainMove({ + request: web(), + surface: "web", + enabled: "true", + lookup, + fallback: legacy, + }); + const stored = first.headers.get("set-cookie")?.split(";")[0] ?? ""; + const second = await withDomainMove({ + request: new Request("https://api.superset.sh/api/auth/desktop/connect", { + headers: { cookie: stored }, + }), + surface: "desktop", + enabled: "true", + lookup, + fallback: legacy, + }); + expect(ids[0]).toBe(ids[1]); + expect(ids[0]).toMatch(/^[0-9a-f-]{36}$/); + expect(second.headers.get("set-cookie")).toBeNull(); + }); + + test("true redirects with no-store using the browser cohort and destination stays put", async () => { + const lookup = mock(async (_key: string, id: string) => { + expect(id).toBe("12345678-1234-4123-8123-123456789abc"); + return true; + }); + const fallback = mock(legacy); + const response = await withDomainMove({ + request: web("/api-keys?a=%2f&a=two+words", { + headers: { accept: "text/html", cookie }, + }), + surface: "web", + enabled: "true", + lookup, + fallback, + }); + expect(response.status).toBe(307); + expect(response.headers.get("location")).toBe( + "https://app.paradev.com/api-keys?a=%2f&a=two+words", + ); + expect(response.headers.get("cache-control")).toBe("private, no-store"); + expect(response.headers.get("set-cookie")).toBeNull(); + expect(fallback).not.toHaveBeenCalled(); + expect( + domainMoveTarget( + new Request(response.headers.get("location") ?? "", { + headers: { accept: "text/html" }, + }), + "web", + ), + ).toBeNull(); + }); +}); diff --git a/packages/shared/src/domain-move/domain-move.ts b/packages/shared/src/domain-move/domain-move.ts new file mode 100644 index 00000000000..d8937d9ea25 --- /dev/null +++ b/packages/shared/src/domain-move/domain-move.ts @@ -0,0 +1,214 @@ +import { FEATURE_FLAGS } from "../constants"; + +const COOKIE = "__Secure-domain-move-id"; +const LEGACY_WEB = "https://app.superset.sh"; +const LEGACY_API = "https://api.superset.sh"; +const retained = [ + "/", + "/api", + "/trpc", + "/oauth", + "/cli/auth", + "/auth/desktop", + "/auth/integration-connect", + "/integrations/sentry", + "/.well-known", + "/mcp", + "/accept-invitation", + "/settings/billing", + "/_next", + "/ingest", + "/monitoring", +]; +const matches = (path: string, route: string) => + path === route || path.startsWith(`${route}/`); + +function retainedPath(path: string): boolean { + return ( + retained.some((route) => matches(path, route)) || /\.[^/]+$/.test(path) + ); +} + +function ambiguous(value: string): boolean { + return [...value].some( + (character) => + character.charCodeAt(0) <= 32 || character === "\\" || character === "%", + ); +} + +function safePage(url: URL, depth = 0): boolean { + if (depth > 4) return false; + let path: string; + try { + path = decodeURIComponent(url.pathname); + } catch { + return false; + } + if (ambiguous(path) || retainedPath(path)) return false; + if (!["/sign-in", "/sign-up"].some((route) => matches(path, route))) + return true; + if (["client_id", "sig"].some((key) => url.searchParams.has(key))) + return false; + for (const value of url.searchParams.getAll("redirect")) { + let continuation = value; + try { + for (let i = 0; i < 4 && continuation.includes("%"); i++) { + continuation = decodeURIComponent(continuation); + } + if (ambiguous(continuation) || continuation.startsWith("//")) + return false; + const target = new URL(continuation, LEGACY_WEB); + if (target.origin !== LEGACY_WEB || !safePage(target, depth + 1)) + return false; + } catch { + return false; + } + } + return true; +} + +export function domainMoveTarget( + request: Request, + surface: "web" | "desktop", +): URL | null { + const url = new URL(request.url); + const origin = surface === "web" ? LEGACY_WEB : LEGACY_API; + if (url.origin !== origin || url.username || url.password) return null; + // A forwarded host is never a source of authority; a conflicting one fails closed. + const host = new URL(origin).host; + if ( + ["host", "x-forwarded-host"].some((name) => { + const value = request.headers.get(name); + return value !== null && value !== host; + }) + ) + return null; + if (surface === "desktop") { + if ( + request.method !== "GET" || + url.pathname !== "/api/auth/desktop/connect" + ) + return null; + } else { + if (!["GET", "HEAD"].includes(request.method) || !safePage(url)) + return null; + if ( + [ + "next-action", + "rsc", + "next-router-prefetch", + "next-router-state-tree", + "next-router-segment-prefetch", + ].some((name) => request.headers.has(name)) + ) + return null; + if (url.searchParams.has("_rsc")) return null; + if ( + ["purpose", "sec-purpose"].some((name) => + /prefetch/i.test(request.headers.get(name) ?? ""), + ) + ) + return null; + const mode = request.headers.get("sec-fetch-mode"); + const dest = request.headers.get("sec-fetch-dest"); + if ((mode && mode !== "navigate") || (dest && dest !== "document")) + return null; + if ( + !(request.headers.get("accept") ?? "") + .split(",") + .some( + (type) => + /^\s*text\/html(?:\s*;|\s*$)/i.test(type) && + !/;\s*q=0(?:\.0*)?(?:;|\s*$)/i.test(type), + ) + ) + return null; + } + const target = new URL( + surface === "web" ? "https://app.paradev.com" : "https://api.paradev.com", + ); + target.pathname = url.pathname; + target.search = url.search; + return target; +} + +type FlagLookup = ( + key: string, + id: string, + options: { sendFeatureFlagEvents: false }, +) => Promise; + +type Move = { location?: string; cookie?: string }; + +async function evaluate( + request: Request, + target: URL, + lookup: FlagLookup, + schedule: typeof setTimeout = setTimeout, +): Promise { + const stored = (request.headers.get("cookie") ?? "") + .slice(0, 16384) + .split(";") + .map((part) => part.trim()) + .find((part) => part.startsWith(`${COOKIE}=`)) + ?.slice(COOKIE.length + 1); + const valid = + stored && + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test( + stored, + ); + const id = valid ? stored : crypto.randomUUID(); + const cookie = !valid + ? `${COOKIE}=${id}; Domain=superset.sh; Path=/; Max-Age=2592000; HttpOnly; Secure; SameSite=Lax` + : undefined; + let timer: ReturnType | undefined; + try { + const flag = await Promise.race([ + Promise.resolve().then(() => + lookup(FEATURE_FLAGS.WEB_DOMAIN_MOVE, id, { + sendFeatureFlagEvents: false, + }), + ), + new Promise((resolve) => { + timer = schedule(() => resolve(undefined), 500); + }), + ]); + return { cookie, location: flag === true ? target.href : undefined }; + } catch { + return { cookie }; + } finally { + clearTimeout(timer); + } +} + +function applyDomainMove(response: Response, move: Move): Response { + if (move.cookie) response.headers.append("set-cookie", move.cookie); + if (move.cookie || move.location) + response.headers.set("cache-control", "private, no-store"); + return response; +} + +export async function withDomainMove({ + request, + enabled, + surface, + lookup, + fallback, + schedule, +}: { + request: Request; + enabled?: string; + surface: "web" | "desktop"; + lookup: FlagLookup; + fallback: () => Promise; + schedule?: typeof setTimeout; +}): Promise { + if (enabled !== "true") return fallback(); + const target = domainMoveTarget(request, surface); + if (!target) return fallback(); + const move = await evaluate(request, target, lookup, schedule); + const response = move.location + ? new Response(null, { status: 307, headers: { location: move.location } }) + : await fallback(); + return applyDomainMove(response, move); +} diff --git a/packages/shared/src/domain-move/index.ts b/packages/shared/src/domain-move/index.ts new file mode 100644 index 00000000000..17c6c243894 --- /dev/null +++ b/packages/shared/src/domain-move/index.ts @@ -0,0 +1,4 @@ +export { + domainMoveTarget, + withDomainMove, +} from "./domain-move"; diff --git a/packages/shared/src/legacy-integration-connect/index.ts b/packages/shared/src/legacy-integration-connect/index.ts new file mode 100644 index 00000000000..6d374db4ec0 --- /dev/null +++ b/packages/shared/src/legacy-integration-connect/index.ts @@ -0,0 +1,4 @@ +export { + continueLegacyIntegrationConnect, + withLegacySentryConnect, +} from "./legacy-integration-connect"; diff --git a/packages/shared/src/legacy-integration-connect/legacy-integration-connect.test.ts b/packages/shared/src/legacy-integration-connect/legacy-integration-connect.test.ts new file mode 100644 index 00000000000..84eb4f94d28 --- /dev/null +++ b/packages/shared/src/legacy-integration-connect/legacy-integration-connect.test.ts @@ -0,0 +1,272 @@ +import { expect, mock, test } from "bun:test"; +import { withDomainMove } from "../domain-move"; +import { + continueLegacyIntegrationConnect, + withLegacySentryConnect, +} from "./legacy-integration-connect"; + +const orgId = "803f13a4-49b9-4664-826f-4090c750b0bc"; +const connectPath = `/api/integrations/sentry/connect?organizationId=${orgId}`; +const continuationPath = `/auth/integration-connect?provider=sentry&organizationId=${orgId}`; +const request = (url: string, init: RequestInit = {}) => + new Request(url, { + headers: { accept: "text/html" }, + ...init, + }); +const location = (response: Response) => { + expect(response.status).toBe(307); + expect(response.headers.get("cache-control")).toBe("private, no-store"); + expect(response.headers.get("set-cookie")).toBeNull(); + return response.headers.get("location") ?? ""; +}; + +test("final and stage hand off before OAuth, sign in on legacy, then authorize the explicit org with rollout at 100%", async () => { + for (const origin of [ + "https://api.paradev.com", + "https://api.stage.paradev.com", + ]) { + const beginOAuth = mock( + async () => + new Response(null, { + status: 302, + headers: { + location: "https://sentry.io/install", + "set-cookie": "sentry_oauth_state=legacy-only", + }, + }), + ); + const moved = await withLegacySentryConnect( + request(`${origin}${connectPath}`), + beginOAuth, + ); + expect(beginOAuth).not.toHaveBeenCalled(); + const continuation = location(moved); + expect(continuation).toBe(`https://app.superset.sh${continuationPath}`); + + const flag = mock(async () => true); + const legacyWeb = (url: string, signedIn: boolean) => + withDomainMove({ + request: request(url), + enabled: "true", + surface: "web", + lookup: flag, + fallback: () => + continueLegacyIntegrationConnect(request(url), async () => signedIn), + }); + const signIn = new URL(location(await legacyWeb(continuation, false))); + expect(signIn.origin + signIn.pathname).toBe( + "https://app.superset.sh/sign-in", + ); + expect(signIn.searchParams.get("redirect")).toBe(continuationPath); + const signInPage = new Response("existing sign-in"); + expect( + await withDomainMove({ + request: request(signIn.href), + enabled: "true", + surface: "web", + lookup: flag, + fallback: async () => signInPage, + }), + ).toBe(signInPage); + + const resumed = new URL( + signIn.searchParams.get("redirect") ?? "", + signIn.origin, + ); + const oldApi = location(await legacyWeb(resumed.href, true)); + expect(oldApi).toBe(`https://api.superset.sh${connectPath}`); + const memberCheck = mock(async () => { + expect(new URL(oldApi).searchParams.get("organizationId")).toBe(orgId); + return beginOAuth(); + }); + const authorized = await withLegacySentryConnect( + request(oldApi), + memberCheck, + ); + expect(memberCheck).toHaveBeenCalledTimes(1); + expect(beginOAuth).toHaveBeenCalledTimes(1); + expect(authorized.headers.get("set-cookie")).toBe( + "sentry_oauth_state=legacy-only", + ); + + const resultPage = new Response("existing integration result"); + expect( + await withDomainMove({ + request: request( + "https://app.superset.sh/integrations/sentry?error=not_configured", + ), + enabled: "true", + surface: "web", + lookup: flag, + fallback: async () => resultPage, + }), + ).toBe(resultPage); + expect(flag).not.toHaveBeenCalled(); + } +}); + +test("handoff only carries the explicit org and provider, never credentials, state, or redirect input", async () => { + const credentials = + "&token=do-not-forward&state=do-not-forward&redirect=https://evil.test"; + const originalFlow = mock(async () => new Response("should not start")); + const response = await withLegacySentryConnect( + request(`https://api.paradev.com${connectPath}${credentials}`, { + headers: { + authorization: "Bearer do-not-forward", + cookie: "session=do-not-forward", + }, + }), + originalFlow, + ); + expect(location(response)).toBe(`https://app.superset.sh${continuationPath}`); + const isSignedIn = mock(async () => true); + const target = await continueLegacyIntegrationConnect( + request(`https://app.superset.sh${continuationPath}${credentials}`), + isSignedIn, + ); + expect(location(target)).toBe(`https://api.superset.sh${connectPath}`); + expect(isSignedIn).toHaveBeenCalledTimes(1); + expect(originalFlow).not.toHaveBeenCalled(); +}); + +test("invalid or ambiguous organization input fails before session reads or OAuth", async () => { + for (const query of [ + "", + "organizationId=", + "organizationId=not-a-uuid", + `organizationId=${orgId}&organizationId=${orgId}`, + "organizationId=https%3A%2F%2Fevil.test", + `organizationId=${orgId}%0A`, + ]) { + const startOAuth = mock(async () => new Response("OAuth")); + const isSignedIn = mock(async () => true); + const api = await withLegacySentryConnect( + request( + `https://api.paradev.com/api/integrations/sentry/connect?${query}`, + ), + startOAuth, + ); + const web = await continueLegacyIntegrationConnect( + request( + `https://app.superset.sh/auth/integration-connect?provider=sentry&${query}`, + ), + isSignedIn, + ); + for (const response of [api, web]) { + expect(response.status).toBe(400); + expect(response.headers.get("location")).toBeNull(); + expect(response.headers.get("set-cookie")).toBeNull(); + } + expect(startOAuth).not.toHaveBeenCalled(); + expect(isSignedIn).not.toHaveBeenCalled(); + } +}); + +test("continuation accepts only one allowlisted Sentry provider", async () => { + for (const query of [ + "", + "provider=", + "provider=slack", + "provider=google", + "provider=Sentry", + "provider=https%3A%2F%2Fevil.test", + "provider=sentry&provider=slack", + ]) { + const session = mock(async () => true); + const response = await continueLegacyIntegrationConnect( + request( + `https://app.superset.sh/auth/integration-connect?organizationId=${orgId}&${query}`, + ), + session, + ); + expect(response.status).toBe(400); + expect(session).not.toHaveBeenCalled(); + } +}); + +test("continuation rejects foreign hosts and spoofed forwarded hosts before reading a session", async () => { + const session = mock(async () => true); + for (const origin of [ + "https://app.paradev.com", + "https://app.stage.paradev.com", + "https://app.pr-1.paradev.com", + "http://localhost:3000", + "https://app.superset.sh:444", + "http://app.superset.sh", + "https://app.superset.sh.evil.test", + ]) { + const response = await continueLegacyIntegrationConnect( + request(`${origin}${continuationPath}`, { + headers: { "x-forwarded-host": "app.superset.sh" }, + }), + session, + ); + expect(response.status).toBe(404); + } + for (const header of ["host", "x-forwarded-host"]) { + const response = await continueLegacyIntegrationConnect( + request(`https://app.superset.sh${continuationPath}`, { + headers: { [header]: "evil.test" }, + }), + session, + ); + expect(response.status).toBe(404); + const oauth = mock(async () => new Response("OAuth")); + const moved = await withLegacySentryConnect( + request(`https://api.paradev.com${connectPath}`, { + headers: { [header]: "evil.test" }, + }), + oauth, + ); + expect(moved.status).toBe(400); + expect(oauth).not.toHaveBeenCalled(); + } + expect(session).not.toHaveBeenCalled(); +}); + +test("legacy, local, preview, foreign sources and unrelated provider starts keep the exact original response", async () => { + for (const url of [ + `https://api.superset.sh${connectPath}`, + `http://localhost:3001${connectPath}`, + `https://api.pr-1.paradev.com${connectPath}`, + `https://api.paradev.com.evil.test${connectPath}`, + `https://api.paradev.com:444${connectPath}`, + `http://api.paradev.com${connectPath}`, + `https://api.paradev.com/api/integrations/slack/connect?organizationId=${orgId}`, + ]) { + const original = new Response("legacy", { status: 403 }); + const fallback = mock(async () => original); + expect( + await withLegacySentryConnect( + request(url, { headers: { "x-forwarded-host": "api.paradev.com" } }), + fallback, + ), + ).toBe(original); + expect(fallback).toHaveBeenCalledTimes(1); + } +}); + +test("new routes do not start OAuth or read sessions for non-GET requests", async () => { + for (const method of ["HEAD", "POST", "OPTIONS"]) { + const oauth = mock(async () => new Response("OAuth")); + const session = mock(async () => true); + expect( + ( + await withLegacySentryConnect( + request(`https://api.paradev.com${connectPath}`, { method }), + oauth, + ) + ).status, + ).toBe(405); + expect( + ( + await continueLegacyIntegrationConnect( + request(`https://app.superset.sh${continuationPath}`, { method }), + session, + ) + ).status, + ).toBe(405); + expect(oauth).not.toHaveBeenCalled(); + expect(session).not.toHaveBeenCalled(); + } +}); diff --git a/packages/shared/src/legacy-integration-connect/legacy-integration-connect.ts b/packages/shared/src/legacy-integration-connect/legacy-integration-connect.ts new file mode 100644 index 00000000000..99c3dbfd5e2 --- /dev/null +++ b/packages/shared/src/legacy-integration-connect/legacy-integration-connect.ts @@ -0,0 +1,95 @@ +import { z } from "zod"; + +const LEGACY_WEB = "https://app.superset.sh"; +const LEGACY_API = "https://api.superset.sh"; +const CONTINUATION_PATH = "/auth/integration-connect"; +const SENTRY_CONNECT_PATH = "/api/integrations/sentry/connect"; +const SENTRY_FALLBACK_ORIGINS = new Set([ + "https://api.paradev.com", + "https://api.stage.paradev.com", +]); + +function exactHost(request: Request, url: URL): boolean { + return ( + !url.username && + !url.password && + ["host", "x-forwarded-host"].every((name) => { + const value = request.headers.get(name); + return value === null || value === url.host; + }) + ); +} + +function organizationId(url: URL): string | null { + const values = url.searchParams.getAll("organizationId"); + if (values.length !== 1) return null; + const parsed = z.uuid().safeParse(values[0]); + return parsed.success ? parsed.data : null; +} + +function redirect(url: URL): Response { + return new Response(null, { + status: 307, + headers: { location: url.href, "cache-control": "private, no-store" }, + }); +} + +function invalid(status: number): Response { + return new Response(null, { + status, + headers: { "cache-control": "private, no-store" }, + }); +} + +export async function withLegacySentryConnect( + request: Request, + fallback: () => Promise, +): Promise { + const url = new URL(request.url); + if ( + !SENTRY_FALLBACK_ORIGINS.has(url.origin) || + url.pathname !== SENTRY_CONNECT_PATH + ) { + return fallback(); + } + if (!exactHost(request, url)) return invalid(400); + if (request.method !== "GET") return invalid(405); + const orgId = organizationId(url); + if (!orgId) return invalid(400); + const target = new URL(CONTINUATION_PATH, LEGACY_WEB); + target.searchParams.set("provider", "sentry"); + target.searchParams.set("organizationId", orgId); + return redirect(target); +} + +export async function continueLegacyIntegrationConnect( + request: Request, + isSignedIn: () => Promise, +): Promise { + const url = new URL(request.url); + if ( + url.origin !== LEGACY_WEB || + url.pathname !== CONTINUATION_PATH || + !exactHost(request, url) + ) + return invalid(404); + if (request.method !== "GET") return invalid(405); + const providers = url.searchParams.getAll("provider"); + const orgId = organizationId(url); + if (providers.length !== 1 || providers[0] !== "sentry" || !orgId) + return invalid(400); + if (!(await isSignedIn())) { + const continuation = new URL(CONTINUATION_PATH, LEGACY_WEB); + continuation.searchParams.set("provider", "sentry"); + continuation.searchParams.set("organizationId", orgId); + const signIn = new URL("/sign-in", LEGACY_WEB); + signIn.searchParams.set( + "redirect", + continuation.pathname + continuation.search, + ); + return redirect(signIn); + } + const target = new URL(SENTRY_CONNECT_PATH, LEGACY_API); + target.searchParams.set("organizationId", orgId); + return redirect(target); +} diff --git a/scripts/deploy-paradev-staging/guard_test.py b/scripts/deploy-paradev-staging/guard_test.py new file mode 100644 index 00000000000..26efd41ddc3 --- /dev/null +++ b/scripts/deploy-paradev-staging/guard_test.py @@ -0,0 +1,102 @@ +import io +import json +import os +from pathlib import Path +import tempfile +import textwrap +import unittest +from unittest.mock import patch +from urllib.parse import urlparse + +WORKFLOW = Path(__file__).resolve().parents[2] / ".github/workflows/deploy-paradev-staging.yml" +SOURCE = WORKFLOW.read_text() +GUARD = textwrap.dedent(SOURCE.split("<<'PYTHON'\n", 1)[1].split(" PYTHON", 1)[0]) +TARGETS = ( + ("API", "prj_MmAnFWXbST3HSeLr313KNqZ00Pve", "api-paradev", "api.stage.paradev.com", "apps/api"), + ("WEB", "prj_7XZEFgKHMki81K1bTCl0JCG4lToQ", "web-paradev", "app.stage.paradev.com", "apps/web"), +) + + +class GuardTest(unittest.TestCase): + def run_guard(self, mode, command, mismatch=False, variable_mismatch=False): + calls = [] + aliases = {} + + def request(req, timeout): + path = urlparse(req.full_url).path + calls.append((req.get_method(), path)) + for kind, project, name, host, root in TARGETS: + if path == f"/v9/projects/{project}": + return io.BytesIO(json.dumps(dict(id=project, name="wrong" if mismatch else name, accountId="team", rootDirectory=root)).encode()) + if path == f"/v9/projects/{project}/domains/{host}": + self.assertEqual(mode, "stage", "Final preparation must not require domain ownership") + return io.BytesIO(json.dumps(dict(name=host, verified=True)).encode()) + if path == f"/v13/deployments/{kind.lower()}.vercel.app": + return io.BytesIO(json.dumps(dict(id=f"dpl_{kind}", projectId=project, readyState="READY", target="production")).encode()) + if path == f"/v4/aliases/{host}": + return io.BytesIO(json.dumps(dict(projectId=project, deploymentId=aliases.get(host, "old"))).encode()) + if path == f"/v2/deployments/dpl_{kind}/aliases": + aliases[host] = f"dpl_{kind}" + return io.BytesIO(b"{}") + self.fail(f"Unexpected request {path}") + + with tempfile.TemporaryDirectory() as directory: + summary = Path(directory, "summary") + for kind, *_ in TARGETS: + Path(directory, f"paradev-{kind.lower()}-url").write_text(f"https://{kind.lower()}.vercel.app") + env = dict(DEPLOY_MODE=mode, VERCEL_TOKEN="fake", VERCEL_ORG_ID="team", RUNNER_TEMP=directory, GITHUB_SHA="reviewed-sha", GITHUB_STEP_SUMMARY=str(summary)) + env.update({f"VERCEL_PARADEV_{kind}_PROJECT_ID": "wrong" if variable_mismatch else project for kind, project, *_ in TARGETS}) + with patch.dict(os.environ, env, clear=True), patch("sys.argv", ["guard", command]), patch("urllib.request.urlopen", request): + try: + exec(compile(GUARD, "guard", "exec"), {}) + except SystemExit as error: + if error.code not in (0, None): + return calls, str(error), "" + return calls, None, summary.read_text() if summary.exists() else "" + + def test_final_report_has_only_ids_urls_sha_and_no_domain_requests_or_writes(self): + calls, error, summary = self.run_guard("prepare-final", "report") + self.assertIsNone(error) + self.assertTrue(all(method == "GET" and "/domains/" not in path and "/aliases" not in path for method, path in calls)) + self.assertEqual(summary, "API: dpl_API https://api.vercel.app\n\nWEB: dpl_WEB https://web.vercel.app\n\nCommit: reviewed-sha\n") + + def test_final_cannot_assign_alias_even_with_wrong_command(self): + calls, error, _ = self.run_guard("prepare-final", "alias") + self.assertEqual(calls, []) + self.assertIn("cannot assign aliases", error) + + def test_stage_still_checks_ownership_and_assigns_both_stage_aliases(self): + calls, error, _ = self.run_guard("stage", "alias") + self.assertIsNone(error) + self.assertEqual(sum(method == "POST" for method, _ in calls), 2) + for _, project, _, host, _ in TARGETS: + self.assertIn(("GET", f"/v9/projects/{project}/domains/{host}"), calls) + + def test_project_mismatch_stops_preparation_before_deployment(self): + for mode in ("stage", "prepare-final"): + for variable_mismatch in (False, True): + calls, error, _ = self.run_guard(mode, "check", mismatch=True, variable_mismatch=variable_mismatch) + self.assertIsNotNone(error) + self.assertFalse(any("deployments" in path or method != "GET" for method, path in calls)) + + def test_fixed_modes_static_env_and_skip_domain(self): + self.assertIn("default: stage\n options:\n - stage\n - prepare-final", SOURCE) + self.assertEqual(SOURCE.count("vercel deploy --prod --skip-domain"), 2) + self.assertEqual(SOURCE.count("DOMAIN_MOVE_ENABLED: \"false\""), 2) + for var, final, stage, count in ( + ("BROWSER_API_URL", "https://api.paradev.com", "https://api.stage.paradev.com", 2), + ("NEXT_PUBLIC_API_URL", "https://api.paradev.com", "https://api.stage.paradev.com", 1), + ("NEXT_PUBLIC_WEB_URL", "https://app.paradev.com", "https://app.stage.paradev.com", 2), + ("NEXT_PUBLIC_COOKIE_DOMAIN", "paradev.com", "stage.paradev.com", 2), + ): + self.assertEqual(SOURCE.count(f"{var}: ${{{{ inputs.mode == 'prepare-final' && '{final}' || '{stage}' }}}}"), count) + self.assertIn("NEXT_PUBLIC_API_URL: https://api.superset.sh", SOURCE) + self.assertIn("if: inputs.mode == 'stage'", SOURCE) + self.assertIn("if: inputs.mode == 'prepare-final'", SOURCE) + self.assertNotIn("db:migrate", SOURCE) + self.assertNotIn("wrangler deploy", SOURCE) + self.assertNotIn("vercel alias", SOURCE) + + +if __name__ == "__main__": + unittest.main() diff --git a/turbo.jsonc b/turbo.jsonc index 74a2a0299c8..cde324d6b90 100644 --- a/turbo.jsonc +++ b/turbo.jsonc @@ -8,6 +8,7 @@ "MEDIA_URL", "NEXT_PUBLIC_API_URL", "BROWSER_API_URL", + "DOMAIN_MOVE_ENABLED", "NEXT_PUBLIC_WEB_URL", "NEXT_PUBLIC_MARKETING_URL", "NEXT_PUBLIC_DOCS_URL",