diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1e75627f..57e15c29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -142,7 +142,7 @@ jobs: run: npx playwright install --with-deps chromium # Includes: a self-hosted install never loads Google's tag. - name: Self-host checks - run: npx playwright test e2e/selfhost.spec.ts e2e/google-analytics.spec.ts --project=chromium + run: npx playwright test e2e/selfhost.spec.ts e2e/google-analytics.spec.ts e2e/selfhost-links.spec.ts --project=chromium env: E2E_STATIC: "1" diff --git a/dashboard/e2e/selfhost-links.spec.ts b/dashboard/e2e/selfhost-links.spec.ts new file mode 100644 index 00000000..e44e1662 --- /dev/null +++ b/dashboard/e2e/selfhost-links.spec.ts @@ -0,0 +1,102 @@ +import { expect, test, type Page, type Route } from "@playwright/test"; + +// A self-hosted install's dashboard links to the install, never to the hosted +// service: its API calls, the address shown for API keys, and the GitHub App +// buttons (which go through the install's own /auth/github/app, so they open +// the install's app). Public docs on docs.coderhelm.com are the one exception. + +const ME = { + user_id: "u_1", + team_id: "t_1", + github_login: null, + email: "owner@example.org", + avatar_url: "", + role: "owner", + auth_provider: "email", + mfa_enabled: false, + platform_admin: true, +}; + +const KEYS = { + keys: [], + scopes: [{ key: "runs:read", label: "Read runs", always: true, allowed: true }], + limits: { default_rate_per_minute: 60, min_rate_per_minute: 1, max_rate_per_minute: 600, max_expiry_days: 365, max_keys: 20 }, +}; + +async function selfHosted(page: Page) { + const outside: string[] = []; + const calls: string[] = []; + const json = (route: Route, body: unknown, status = 200) => + route.fulfill({ status, contentType: "application/json", body: JSON.stringify(body) }); + await page.route((url) => url.pathname === "/runtime-config.js", (route) => + route.fulfill({ + contentType: "text/javascript", + body: `window.__CODERHELM_CONFIG__ = ${JSON.stringify({ apiUrl: "", streamUrl: "", selfHosted: true, siteUrl: null, supportEmail: "it@example.org" })};`, + }), + ); + await page.route((url) => !["localhost", "127.0.0.1"].includes(url.hostname), (route) => { + outside.push(route.request().url()); + return route.abort(); + }); + await page.route((url) => ["localhost", "127.0.0.1"].includes(url.hostname) && /^\/(api|auth)\//.test(url.pathname), (route) => { + const u = new URL(route.request().url()); + calls.push(u.pathname); + const p = u.pathname; + if (p === "/api/me") return json(route, ME); + if (p === "/api/usage") return json(route, { month: "2026-01", total_tokens: 0, tokens_in: 0, tokens_out: 0, total_runs: 0, max_tokens: 0 }); + if (p === "/api/banners") return json(route, { banners: [] }); + if (p === "/api/teams") return json(route, { teams: [{ team_id: "t_1", org: "your-team", status: "active", current: true }] }); + if (p === "/api/api-keys") return json(route, KEYS); + if (p === "/api/repos") return json(route, { repos: [] }); + if (p === "/api/github/installation-status") return json(route, { installed: false }); + if (p === "/api/integrations") return json(route, { can_edit: true, migrated: 0, items: [] }); + return json(route, {}); + }); + return { outside, calls }; +} + +/** Every link on the page that goes to coderhelm.com, other than the public docs. */ +async function hostedLinks(page: Page): Promise { + const hrefs = await page.locator("a[href]").evaluateAll((as) => as.map((a) => (a as HTMLAnchorElement).href)); + return hrefs.filter((h) => { + const host = new URL(h).hostname; + return (host === "coderhelm.com" || host.endsWith(".coderhelm.com")) && host !== "docs.coderhelm.com"; + }); +} + +test("API keys: the install's own address, and its own API", async ({ page, baseURL }) => { + const { outside, calls } = await selfHosted(page); + await page.goto("/settings/api-keys"); + await expect(page.getByText("How keys work")).toBeVisible(); + const origin = new URL(baseURL!).origin; + await expect(page.getByText(`Send the key as`).locator("code").nth(1)).toHaveText(origin); + await expect(page.getByText("api.coderhelm.com")).toHaveCount(0); + expect(calls).toContain("/api/api-keys"); + expect(await hostedLinks(page)).toEqual([]); + expect(outside.filter((u) => u.includes("coderhelm.com"))).toEqual([]); +}); + +test("Repositories: Install the GitHub App opens the install's own app", async ({ page, baseURL }) => { + const { outside } = await selfHosted(page); + await page.goto("/settings/repos"); + const install = page.getByRole("link", { name: "Install the GitHub App" }); + await expect(install).toBeVisible(); + await expect(install).toHaveAttribute("href", `${new URL(baseURL!).origin}/auth/github/app`); + expect(await hostedLinks(page)).toEqual([]); + expect(outside.filter((u) => u.includes("coderhelm.com"))).toEqual([]); +}); + +test("GitHub settings: Install goes through the install, with its state", async ({ page, baseURL }) => { + await selfHosted(page); + let opened = ""; + await page.route((url) => url.pathname === "/auth/github/app", (route) => { + opened = route.request().url(); + return route.fulfill({ status: 200, contentType: "text/html", body: "

GitHub

" }); + }); + await page.goto("/settings/integrations/github"); + const button = page.getByRole("button", { name: /^Install/ }).first(); + await expect(button).toBeVisible(); + expect(await hostedLinks(page)).toEqual([]); + await button.click(); + await expect.poll(() => opened).toContain(`${new URL(baseURL!).origin}/auth/github/app?state=`); +}); diff --git a/dashboard/package.json b/dashboard/package.json index e46821bc..b2e4838c 100644 --- a/dashboard/package.json +++ b/dashboard/package.json @@ -6,7 +6,7 @@ "dev": "next dev", "build": "next build", "start": "next start", - "lint": "node scripts/ui-lint.mjs", + "lint": "node scripts/ui-lint.mjs && node scripts/no-hosted-links.mjs", "typecheck": "tsc --noEmit", "openapi:fields": "node scripts/openapi-fields.mjs", "check:openapi": "node scripts/openapi-fields.mjs --check", diff --git a/dashboard/scripts/no-hosted-links.mjs b/dashboard/scripts/no-hosted-links.mjs new file mode 100644 index 00000000..5332b81e --- /dev/null +++ b/dashboard/scripts/no-hosted-links.mjs @@ -0,0 +1,54 @@ +#!/usr/bin/env node +// No hardcoded hosted addresses in the dashboard. The same build serves the +// hosted service and every self-hosted install, so the API, the stream and +// the GitHub App come from lib/runtime-config.ts (apiBase, apiOrigin, +// githubAppUrl), never from a literal. Runs with `npm run lint`. +// +// Flags app., api., stream. and auth.coderhelm.com, the marketing site, and +// github.com/apps/coderhelm (the hosted GitHub App) anywhere in src/ outside +// the files below. Comments are not checked. docs.coderhelm.com is always fine. +import fs from "node:fs"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), ".."); + +/** Files that may name a hosted address, and why. */ +const ALLOWED = { + "src/lib/runtime-config.ts": "the hosted defaults, used only when no runtime config says otherwise", + "src/components/google-analytics.tsx": "a hostname check that keeps the hosted dashboard's analytics off every other host; not a link", +}; + +const PATTERN = /\b(?:app|api|stream|auth)\.coderhelm\.com|:\/\/(?:www\.)?coderhelm\.com|github\.com\/apps\/coderhelm\b/i; + +function walk(dir, out = []) { + for (const e of fs.readdirSync(dir, { withFileTypes: true })) { + const p = path.join(dir, e.name); + if (e.isDirectory()) walk(p, out); + else if (/\.(tsx?|mjs|js)$/.test(e.name)) out.push(p); + } + return out; +} + +const found = []; +for (const file of walk(path.join(ROOT, "src"))) { + const rel = path.relative(ROOT, file).split(path.sep).join("/"); + if (ALLOWED[rel]) continue; + fs.readFileSync(file, "utf8") + .split("\n") + .forEach((line, i) => { + const t = line.trim(); + if (t.startsWith("//") || t.startsWith("*") || t.startsWith("/*")) return; + const m = line.match(PATTERN); + if (m) found.push(`${rel}:${i + 1}: ${m[0]}: ${t.slice(0, 160)}`); + }); +} + +if (found.length) { + console.error( + "Hardcoded hosted addresses. Use apiBase(), apiOrigin() or githubAppUrl() from lib/runtime-config.ts, or add the file to ALLOWED with the reason:\n" + + found.join("\n"), + ); + process.exit(1); +} +console.log("No hardcoded hosted addresses."); diff --git a/dashboard/src/app/settings/api-keys/page.tsx b/dashboard/src/app/settings/api-keys/page.tsx index b49e78ad..ce2f487f 100644 --- a/dashboard/src/app/settings/api-keys/page.tsx +++ b/dashboard/src/app/settings/api-keys/page.tsx @@ -19,6 +19,7 @@ import { useConfirm } from "@/components/confirm-dialog"; import { useToast } from "@/components/toast"; import { useCan } from "@/hooks/use-can"; import { relativeTime } from "@/lib/team"; +import { apiOrigin } from "@/lib/runtime-config"; import { API_DOCS_URL, apiKeysApi, @@ -208,7 +209,7 @@ function ApiKeysPage() {