The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Easy support for app.dub.co previews (#4349)
Steven Teyf73912b93bcb
diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yamlindex e3fd9add56..1ff532eed2 100644--- a/.github/workflows/e2e.yaml+++ b/.github/workflows/e2e.yaml@@ -8,6 +8,9 @@ jobs:timeout-minutes: 30if: github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success'runs-on: ubuntu-latest+ permissions:+ contents: read+ id-token: writesteps:- name: Check out codeuses: actions/checkout@v2diff --git a/apps/web/.env.example b/apps/web/.env.exampleindex b43adfd90b..0cdb31e258 100644--- a/apps/web/.env.example+++ b/apps/web/.env.example@@ -5,7 +5,8 @@# Generate secrets with: node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"NEXTAUTH_SECRET=NEXTAUTH_URL=http://localhost:8888 # (only needed for localhost)-# Secret for Vercel cron jobs (https://vercel.com/docs/cron-jobs/manage-cron-jobs#securing-cron-jobs)++# Secret for Vercel cron jobs + sync-embeddingsCRON_SECRET=# Encryption key (AES-256-GCM) for encrypting sensitive data in the databaseENCRYPTION_KEY=diff --git a/apps/web/lib/api/workspaces/is-saml-enforced-for-email-domain.ts b/apps/web/lib/api/workspaces/is-saml-enforced-for-email-domain.tsindex 140cfd3434..4d63d06223 100644--- a/apps/web/lib/api/workspaces/is-saml-enforced-for-email-domain.ts+++ b/apps/web/lib/api/workspaces/is-saml-enforced-for-email-domain.ts@@ -1,5 +1,5 @@import { prisma } from "@/lib/prisma";-import { APP_HOSTNAMES } from "@dub/utils";+import { isAppHostname } from "@dub/utils";import { headers } from "next/headers";import { isGenericEmail } from "../../email/is-generic-email";@@ -11,7 +11,7 @@ export const isSamlEnforcedForEmailDomain = async (email: string) => {if (!hostname ||!emailDomain ||- !APP_HOSTNAMES.has(hostname) ||+ !isAppHostname(hostname) ||isGenericEmail(email)) {return false;diff --git a/apps/web/lib/cron/index.ts b/apps/web/lib/cron/index.tsindex dece18a598..c7f8930d5d 100644--- a/apps/web/lib/cron/index.ts+++ b/apps/web/lib/cron/index.ts@@ -3,6 +3,10 @@ import { Client } from "@upstash/qstash";export const qstash = new Client({baseUrl: process.env.QSTASH_URL || "https://qstash-us-east-1.upstash.io",token: process.env.QSTASH_TOKEN || "",+ headers: {+ "x-vercel-protection-bypass":+ process.env.VERCEL_AUTOMATION_BYPASS_SECRET || "",+ },});// Default batch size for cron jobs that process records in batchesdiff --git a/apps/web/lib/cron/qstash-workflow.ts b/apps/web/lib/cron/qstash-workflow.tsindex 4d6fc4151f..a301907da2 100644--- a/apps/web/lib/cron/qstash-workflow.ts+++ b/apps/web/lib/cron/qstash-workflow.ts@@ -6,6 +6,10 @@ import { Client } from "@upstash/workflow";const client = new Client({baseUrl: process.env.QSTASH_URL || "https://qstash-us-east-1.upstash.io",token: process.env.QSTASH_TOKEN || "",+ headers: {+ "x-vercel-protection-bypass":+ process.env.VERCEL_AUTOMATION_BYPASS_SECRET || "",+ },});type WorkflowType = "partner-approved" | "create-partner-commission";diff --git a/apps/web/lib/middleware/utils/parse.ts b/apps/web/lib/middleware/utils/parse.tsindex 1156f2c711..f5a50503a6 100644--- a/apps/web/lib/middleware/utils/parse.ts+++ b/apps/web/lib/middleware/utils/parse.ts@@ -1,4 +1,4 @@-import { SHORT_DOMAIN } from "@dub/utils";+import { isAppHostname, SHORT_DOMAIN } from "@dub/utils";import { NextRequest } from "next/server";export const parse = (req: NextRequest) => {@@ -8,7 +8,16 @@ export const parse = (req: NextRequest) => {// remove www. from domain and convert to lowercasedomain = domain.replace(/^www./, "").toLowerCase();- if (domain === "dub.localhost:8888" || domain.endsWith(".vercel.app")) {++ const isE2ERedirectTestRequest =+ // local development+ domain === "dub.localhost:8888" ||+ // preview environment+ (process.env.VERCEL_ENV === "preview" &&+ isAppHostname(domain) &&+ req.headers.get("x-e2e-redirect-test") === "true");++ if (isE2ERedirectTestRequest) {if (path.toLowerCase() === "/case-sensitive-test") {// special case for case-sensitive link testdomain = "dub-internal-test.com";diff --git a/apps/web/middleware.ts b/apps/web/middleware.tsindex 952f48eb82..622f95408f 100644--- a/apps/web/middleware.ts+++ b/apps/web/middleware.ts@@ -3,11 +3,10 @@ import { transformMiddlewareRequest } from "@axiomhq/nextjs";import {ADMIN_HOSTNAMES,API_HOSTNAMES,- APP_HOSTNAMES,DEFAULT_REDIRECTS,isValidUrl,} from "@dub/utils";-import { PARTNERS_HOSTNAMES } from "@dub/utils/src/constants";+import { isAppHostname, PARTNERS_HOSTNAMES } from "@dub/utils/src/constants";import { NextFetchEvent, NextRequest, NextResponse } from "next/server";import { AdminMiddleware } from "./lib/middleware/admin";import { ApiMiddleware } from "./lib/middleware/api";@@ -39,12 +38,12 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {logger.info(...transformMiddlewareRequest(req));ev.waitUntil(logger.flush());- // for App- if (APP_HOSTNAMES.has(domain)) {+ // for app.dub.co+ if (isAppHostname(domain)) {return AppMiddleware(req);}- // for API+ // for api.dub.coif (API_HOSTNAMES.has(domain)) {return ApiMiddleware(req);}diff --git a/apps/web/tests/redirects/index.test.ts b/apps/web/tests/redirects/index.test.tsindex 803fb80a41..d69d7f1ed3 100644--- a/apps/web/tests/redirects/index.test.ts+++ b/apps/web/tests/redirects/index.test.ts@@ -9,6 +9,7 @@ const fetchOptions: RequestInit = {redirect: "manual",headers: {"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)",+ "x-e2e-redirect-test": "true",},};diff --git a/apps/web/tests/setupTests.ts b/apps/web/tests/setupTests.tsindex 4d6837a326..06ca1b636b 100644--- a/apps/web/tests/setupTests.ts+++ b/apps/web/tests/setupTests.ts@@ -1,5 +1,6 @@import crypto from "node:crypto";import { vi } from "vitest";+import { getTrustedSourcesHeaders } from "./utils/trusted-sources";Object.defineProperty(globalThis, "crypto", {value: crypto,@@ -47,3 +48,35 @@ vi.mock("@axiomhq/nextjs", () => ({createOnRequestError: vi.fn(() => vi.fn()),transformMiddlewareRequest: vi.fn(() => []),}));++// Attach Vercel Trusted Sources OIDC header to requests against the e2e+// deployment. Tokens are minted on demand from the GitHub Actions runner.+const originalFetch = globalThis.fetch.bind(globalThis);+const e2eBaseUrl = process.env.E2E_BASE_URL?.replace(/\/$/, "");++globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {+ const requestUrl =+ typeof input === "string"+ ? input+ : input instanceof URL+ ? input.href+ : input.url;++ if (!e2eBaseUrl || !requestUrl.startsWith(e2eBaseUrl)) {+ return originalFetch(input, init);+ }++ const trustedHeaders = await getTrustedSourcesHeaders(originalFetch);+ if (Object.keys(trustedHeaders).length === 0) {+ return originalFetch(input, init);+ }++ const headers = new Headers(+ init?.headers ?? (input instanceof Request ? input.headers : undefined),+ );+ for (const [key, value] of Object.entries(trustedHeaders)) {+ headers.set(key, value);+ }++ return originalFetch(input, { ...init, headers });+}) as typeof fetch;diff --git a/apps/web/tests/utils/trusted-sources.ts b/apps/web/tests/utils/trusted-sources.tsnew file mode 100644index 0000000000..14dd458286--- /dev/null+++ b/apps/web/tests/utils/trusted-sources.ts@@ -0,0 +1,107 @@+const TRUSTED_OIDC_HEADER = "x-vercel-trusted-oidc-idp-token";+const EARLY_REFRESH_MS = 60_000;++type FetchFn = typeof fetch;++let cachedToken: string | null = null;+let cachedExpiresAtMs = 0;+let inflight: Promise<string | null> | null = null;++/**+ * Headers that bypass Vercel Deployment Protection via Trusted Sources.+ *+ * GitHub Actions OIDC tokens last 5 minutes, so we mint on demand from the+ * runner (`ACTIONS_ID_TOKEN_REQUEST_*`) and refresh before expiry. Falls back+ * to `VERCEL_OIDC_TOKEN` for local runs against a protected preview.+ *+ * @see https://vercel.com/docs/deployment-protection/methods-to-bypass-deployment-protection/trusted-sources+ */+export async function getTrustedSourcesHeaders(+ fetchFn: FetchFn = fetch,+): Promise<Record<string, string>> {+ const token = await getOidcToken(fetchFn);+ if (!token) return {};+ return { [TRUSTED_OIDC_HEADER]: token };+}++async function getOidcToken(fetchFn: FetchFn): Promise<string | null> {+ const now = Date.now();+ if (cachedToken && now < cachedExpiresAtMs - EARLY_REFRESH_MS) {+ return cachedToken;+ }++ if (inflight) return inflight;++ inflight = (async () => {+ try {+ const minted = await mintFromGitHubActionsRunner(fetchFn);+ if (minted) {+ cachedToken = minted.token;+ cachedExpiresAtMs = minted.expiresAtMs;+ return minted.token;+ }++ const envToken = process.env.VERCEL_OIDC_TOKEN;+ if (envToken) {+ cachedToken = envToken;+ cachedExpiresAtMs = Number.POSITIVE_INFINITY;+ return envToken;+ }++ return null;+ } finally {+ inflight = null;+ }+ })();++ return inflight;+}++async function mintFromGitHubActionsRunner(+ fetchFn: FetchFn,+): Promise<{ token: string; expiresAtMs: number } | null> {+ const url = process.env.ACTIONS_ID_TOKEN_REQUEST_URL;+ const requestToken = process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN;+ if (!url || !requestToken) return null;++ const res = await fetchFn(url, {+ method: "GET",+ headers: {+ Authorization: `Bearer ${requestToken}`,+ Accept: "application/json",+ },+ });++ if (!res.ok) {+ throw new Error(+ `Failed to mint GitHub Actions OIDC token: ${res.status} ${res.statusText}`,+ );+ }++ const body = (await res.json()) as { value?: string };+ const token = body.value;+ if (!token) {+ throw new Error(+ "Failed to mint GitHub Actions OIDC token: empty/invalid response body",+ );+ }++ return { token, expiresAtMs: readJwtExpMs(token) };+}++function readJwtExpMs(jwt: string): number {+ const fallback = Date.now() + 5 * 60 * 1000;+ const parts = jwt.split(".");+ if (parts.length !== 3) return fallback;++ try {+ const payload = JSON.parse(+ Buffer.from(parts[1], "base64url").toString("utf8"),+ );+ if (typeof payload?.exp === "number") return payload.exp * 1000;+ } catch {+ // ignore — use fallback+ }++ return fallback;+}diff --git a/packages/ui/src/hooks/use-current-subdomain.ts b/packages/ui/src/hooks/use-current-subdomain.tsindex bcd67478da..cd06b4ae00 100644--- a/packages/ui/src/hooks/use-current-subdomain.ts+++ b/packages/ui/src/hooks/use-current-subdomain.ts@@ -1,4 +1,4 @@-import { ADMIN_HOSTNAMES, APP_HOSTNAMES, PARTNERS_HOSTNAMES } from "@dub/utils";+import { ADMIN_HOSTNAMES, PARTNERS_HOSTNAMES } from "@dub/utils";import { useEffect, useState } from "react";export function useCurrentSubdomain() {@@ -7,12 +7,12 @@ export function useCurrentSubdomain() {>(null);useEffect(() => {const hostname = window.location.hostname;- if (APP_HOSTNAMES.has(hostname)) {- setSubdomain("app");- } else if (PARTNERS_HOSTNAMES.has(hostname)) {+ if (PARTNERS_HOSTNAMES.has(hostname)) {setSubdomain("partners");} else if (ADMIN_HOSTNAMES.has(hostname)) {setSubdomain("admin");+ } else {+ setSubdomain("app");}}, []);diff --git a/packages/utils/src/constants/main.ts b/packages/utils/src/constants/main.tsindex da20408f36..8ea6f94dea 100644--- a/packages/utils/src/constants/main.ts+++ b/packages/utils/src/constants/main.ts@@ -1,28 +1,5 @@export const SHORT_DOMAIN = "dub.sh";-export const APP_HOSTNAMES = new Set([- "app.dub.co",- "preview.dub.co",- "localhost:8888",- "localhost",-]);--export const APP_DOMAIN =- process.env.NEXT_PUBLIC_VERCEL_ENV === "production"- ? "https://app.dub.co"- : process.env.NEXT_PUBLIC_VERCEL_ENV === "preview"- ? "https://preview.dub.co"- : "http://localhost:8888";--export const APP_DOMAIN_WITH_NGROK =- process.env.NEXT_PUBLIC_VERCEL_ENV === "production"- ? "https://app.dub.co"- : process.env.NEXT_PUBLIC_VERCEL_ENV === "preview"- ? process.env.VERCEL_URL- ? `https://${process.env.VERCEL_URL}`- : "https://preview.dub.co"- : process.env.NEXT_PUBLIC_NGROK_URL || "http://localhost:8888";-export const API_HOSTNAMES = new Set(["api.dub.co","api-staging.dub.co",@@ -65,6 +42,28 @@ export const PARTNERS_DOMAIN_WITH_NGROK =? "https://partners-staging.dub.co": process.env.NEXT_PUBLIC_NGROK_URL || "http://partners.localhost:8888";+export const APP_DOMAIN =+ process.env.NEXT_PUBLIC_VERCEL_ENV === "production"+ ? "https://app.dub.co"+ : process.env.NEXT_PUBLIC_VERCEL_ENV === "preview"+ ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL || "preview.dub.co"}`+ : "http://localhost:8888";++export const APP_DOMAIN_WITH_NGROK =+ process.env.NEXT_PUBLIC_VERCEL_ENV === "production"+ ? "https://app.dub.co"+ : process.env.NEXT_PUBLIC_VERCEL_ENV === "preview"+ ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL || "preview.dub.co"}`+ : process.env.NEXT_PUBLIC_NGROK_URL || "http://localhost:8888";++export const isAppHostname = (hostname: string) => {+ if (process.env.NEXT_PUBLIC_VERCEL_ENV === "preview") {+ // pattern of our preview URLs are always "dub-<random-string>.dub.co"+ return hostname.startsWith("dub-") && hostname.endsWith(".dub.co");+ }+ return new Set(["app.dub.co", "localhost:8888", "localhost"]).has(hostname);+};+export const DUB_LOGO = "https://assets.dub.co/logo.png";export const DUB_LOGO_SQUARE = "https://assets.dub.co/logo-square.png";export const DUB_QR_LOGO = "https://assets.dub.co/logo.png";