Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
status page: rewrite (#2551)
Thibault Le Ouay86f370c9c200
diff --git a/apps/status-page/src/lib/proxy/apply-page-slug-prefix.test.ts b/apps/status-page/src/lib/proxy/apply-page-slug-prefix.test.tsnew file mode 100644index 000000000..f4083a0de--- /dev/null+++ b/apps/status-page/src/lib/proxy/apply-page-slug-prefix.test.ts@@ -0,0 +1,88 @@+import { expect } from "@std/expect";+import { describe, test } from "@std/testing/bdd";++import type { ResolvedRoute } from "../resolve-route";+import { applyPageLocaleOverride } from "./apply-page-locale-override";+import { applyPageSlugPrefix } from "./apply-page-slug-prefix";++const customDomainRoute: ResolvedRoute = {+ type: "hostname",+ prefix: "status.acme.com",+ locale: "en",+ localeExplicit: false,+ rewritePath: "/status.acme.com/en",+};++describe("applyPageSlugPrefix", () => {+ test("prefix already equals the slug: returns input unchanged", () => {+ const route: ResolvedRoute = {+ type: "pathname",+ prefix: "acme",+ locale: "en",+ localeExplicit: true,+ rewritePath: "/acme/en",+ };+ expect(applyPageSlugPrefix(route, { slug: "acme" })).toBe(route);+ });++ test("custom domain prefix: swapped for the slug", () => {+ const result = applyPageSlugPrefix(customDomainRoute, { slug: "acme" });+ expect(result).not.toBe(customDomainRoute);+ expect(result).toEqual({+ type: "hostname",+ prefix: "acme",+ locale: "en",+ localeExplicit: false,+ rewritePath: "/acme/en",+ });+ });++ test("deep path: rest segments are preserved", () => {+ const route: ResolvedRoute = {+ ...customDomainRoute,+ rewritePath: "/status.acme.com/en/events/report/1",+ };+ expect(applyPageSlugPrefix(route, { slug: "acme" }).rewritePath).toBe(+ "/acme/en/events/report/1",+ );+ });++ test("no rest segments: no trailing slash", () => {+ expect(+ applyPageSlugPrefix(customDomainRoute, { slug: "acme" }).rewritePath,+ ).toBe("/acme/en");+ });++ test("explicit locale is preserved", () => {+ const route: ResolvedRoute = {+ ...customDomainRoute,+ locale: "fr",+ localeExplicit: true,+ rewritePath: "/status.acme.com/fr/monitors",+ };+ const result = applyPageSlugPrefix(route, { slug: "acme" });+ expect(result.locale).toBe("fr");+ expect(result.localeExplicit).toBe(true);+ expect(result.rewritePath).toBe("/acme/fr/monitors");+ });++ test("does not mutate input", () => {+ const route = { ...customDomainRoute };+ applyPageSlugPrefix(route, { slug: "acme" });+ expect(route).toEqual(customDomainRoute);+ });++ test("commutes with applyPageLocaleOverride", () => {+ const page = { slug: "acme", defaultLocale: "fr" } as const;+ const slugFirst = applyPageLocaleOverride(+ applyPageSlugPrefix(customDomainRoute, page),+ page,+ );+ const localeFirst = applyPageSlugPrefix(+ applyPageLocaleOverride(customDomainRoute, page),+ page,+ );+ expect(slugFirst).toEqual(localeFirst);+ expect(slugFirst.rewritePath).toBe("/acme/fr");+ });+});diff --git a/apps/status-page/src/lib/proxy/apply-page-slug-prefix.ts b/apps/status-page/src/lib/proxy/apply-page-slug-prefix.tsnew file mode 100644index 000000000..e20fc54da--- /dev/null+++ b/apps/status-page/src/lib/proxy/apply-page-slug-prefix.ts@@ -0,0 +1,27 @@+import type { Page } from "@openstatus/db/src/schema";++import type { ResolvedRoute } from "../resolve-route";++/**+ * Swaps the resolved prefix for the page slug. Custom-domain requests resolve+ * `prefix` to the domain itself (`getValidSubdomain` returns the full host), but+ * the `[domain]` segment must be the slug — the login cookie key is derived from+ * it client-side and compared against `secured-${page.slug}` in the middleware.+ *+ * Rebuilds `rewritePath` from `route.locale` rather than string-replacing, so it+ * commutes with `applyPageLocaleOverride`.+ */+export function applyPageSlugPrefix(+ route: ResolvedRoute,+ page: Pick<Page, "slug">,+): ResolvedRoute {+ if (route.prefix === page.slug) return route;++ // resolveRoute always builds rewritePath as ["", prefix, locale, ...rest]+ const [, , , ...rest] = route.rewritePath.split("/");+ return {+ ...route,+ prefix: page.slug,+ rewritePath: `/${page.slug}/${route.locale}${rest.length ? `/${rest.join("/")}` : ""}`,+ };+}diff --git a/apps/status-page/src/lib/proxy/compose-page-action.test.ts b/apps/status-page/src/lib/proxy/compose-page-action.test.tsindex f3778b40f..8b6d4e678 100644--- a/apps/status-page/src/lib/proxy/compose-page-action.test.ts+++ b/apps/status-page/src/lib/proxy/compose-page-action.test.ts@@ -113,19 +113,32 @@ describe("composePageAction — priority ordering", () => {expect(action.reason).toBe("ip-restriction-gate-in");});- test("custom-domain rewrite fires before default rewrite", () => {+ // The route is slug-normalised by applyPageSlugPrefix before it reaches the+ // composer, so a custom domain is served by one internal rewrite.+ test("custom domain: default rewrite targets the slug-normalised path", () => {const action = composePageAction(buildInput({page: {...basePage,customDomain: "status.acme.com",} as Page,+ route: {+ type: "hostname",+ prefix: "acme",+ locale: "en",+ localeExplicit: false,+ rewritePath: "/acme/en/events",+ },host: "status.acme.com",- urlHost: "localhost:3000",- pathname: "/status.acme.com/en/events",+ urlHost: "status.acme.com",+ pathname: "/events",+ requestUrl: "https://status.acme.com/events",}),);- expect(action.reason).toBe("custom-domain-rewrite-path-strip");+ expect(action.type).toBe("rewrite");+ expect(action.reason).toBe("default-rewrite");+ expect(action.url?.pathname).toBe("/acme/en/events");+ expect(action.url?.host).toBe("status.acme.com");});test("default rewrite fires when paths differ and no other stage matches", () => {diff --git a/apps/status-page/src/lib/proxy/compose-page-action.ts b/apps/status-page/src/lib/proxy/compose-page-action.tsindex be5b44fdd..8aab33ec5 100644--- a/apps/status-page/src/lib/proxy/compose-page-action.ts+++ b/apps/status-page/src/lib/proxy/compose-page-action.ts@@ -1,4 +1,3 @@-import { resolveCustomDomainRewrite } from "./resolve-custom-domain-rewrite";import { resolveDefaultRewrite } from "./resolve-default-rewrite";import { resolveEmailDomainAction } from "./resolve-email-domain-action";import { resolveIpRestrictionAction } from "./resolve-ip-restriction-action";@@ -17,8 +16,7 @@ export type { ComposeInput };* 2. password gate (in/out)* 3. email-domain gate (in/out)* 4. ip-restriction gate (in/out)- * 5. custom-domain rewrite (stpg.dev hosted)- * 6. default rewrite (openstatus.dev OR rewritePath differs)+ * 5. default rewrite (openstatus.dev OR rewritePath differs)** Note on locale-first: a user on a mis-localed URL for a gated page gets* two redirects (locale → gate) instead of one. We accept the extra hop so@@ -33,7 +31,6 @@ export function composePageAction(input: ComposeInput): Action {resolvePasswordAction(input) ??resolveEmailDomainAction(input) ??resolveIpRestrictionAction(input) ??- resolveCustomDomainRewrite(input) ??resolveDefaultRewrite(input) ??// Reached whenever resolveDefaultRewrite declines: host is not an// openstatus.dev host AND route.rewritePath === pathname. In hosteddiff --git a/apps/status-page/src/lib/proxy/resolve-custom-domain-rewrite.test.ts b/apps/status-page/src/lib/proxy/resolve-custom-domain-rewrite.test.tsdeleted file mode 100644index 67a2c1f48..000000000--- a/apps/status-page/src/lib/proxy/resolve-custom-domain-rewrite.test.ts+++ /dev/null@@ -1,169 +0,0 @@-import { expect } from "@std/expect";-import { describe, test } from "@std/testing/bdd";--import { resolveCustomDomainRewrite } from "./resolve-custom-domain-rewrite";--const page = {- slug: "acme",- customDomain: "status.acme.com",-};--describe("resolveCustomDomainRewrite", () => {- test("self-hosted: passes (null)", () => {- expect(- resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "localhost:3000",- pathname: "/en/events",- search: "",- isSelfHosted: true,- requestUrl: "http://localhost:3000/en/events",- }),- ).toBeNull();- });-- test("no customDomain configured: passes (null)", () => {- expect(- resolveCustomDomainRewrite({- page: { slug: "acme", customDomain: "" },- host: "acme.stpg.dev",- urlHost: "localhost:3000",- pathname: "/en",- search: "",- isSelfHosted: false,- requestUrl: "http://acme.stpg.dev/en",- }),- ).toBeNull();- });-- test("host === {slug}.stpg.dev: passes (null)", () => {- expect(- resolveCustomDomainRewrite({- page,- host: "acme.stpg.dev",- urlHost: "localhost:3000",- pathname: "/en",- search: "",- isSelfHosted: false,- requestUrl: "http://acme.stpg.dev/en",- }),- ).toBeNull();- });-- // Branch 1: no subdomain detected on urlHost, deep path.- test("branch 1 (path-strip): no subdomain, deep path → /{slug}/<rest>", () => {- const action = resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "localhost:3000",- pathname: "/status.acme.com/en/events",- search: "",- isSelfHosted: false,- requestUrl: "http://localhost:3000/status.acme.com/en/events",- });- expect(action?.reason).toBe("custom-domain-rewrite-path-strip");- expect(action?.url?.pathname).toBe("/acme/en/events");- });-- test("branch 1: trailing-slash-only path → /{slug} without trailing slash", () => {- const action = resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "localhost:3000",- pathname: "/status.acme.com/",- search: "",- isSelfHosted: false,- requestUrl: "http://localhost:3000/status.acme.com/",- });- expect(action?.reason).toBe("custom-domain-rewrite-path-strip");- expect(action?.url?.pathname).toBe("/acme");- });-- test("branch 1 preserves search", () => {- const action = resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "localhost:3000",- pathname: "/status.acme.com/en",- search: "?foo=bar",- isSelfHosted: false,- requestUrl: "http://localhost:3000/status.acme.com/en",- });- expect(action?.url?.search).toBe("?foo=bar");- });-- // Branch 2: subdomain on urlHost, deep path.- test("branch 2 (subdomain-subpath): subdomain + deep path → https://{slug}.stpg.dev/<rest>", () => {- const action = resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "acme.stpg.dev",- pathname: "/path/to/events",- search: "",- isSelfHosted: false,- requestUrl: "https://acme.stpg.dev/path/to/events",- });- expect(action?.reason).toBe("custom-domain-rewrite-subdomain-subpath");- expect(action?.url?.host).toBe("acme.stpg.dev");- expect(action?.url?.pathname).toBe("/path/to/events");- });-- // Branch 3: subdomain on urlHost, shallow path.- test("branch 3 (subdomain-root): subdomain + shallow path → https://{slug}.stpg.dev{pathname}", () => {- const action = resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "acme.stpg.dev",- pathname: "/en",- search: "",- isSelfHosted: false,- requestUrl: "https://acme.stpg.dev/en",- });- expect(action?.reason).toBe("custom-domain-rewrite-subdomain-root");- expect(action?.url?.host).toBe("acme.stpg.dev");- expect(action?.url?.pathname).toBe("/en");- });-- test("branch 3: root path", () => {- const action = resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "acme.stpg.dev",- pathname: "/",- search: "",- isSelfHosted: false,- requestUrl: "https://acme.stpg.dev/",- });- expect(action?.reason).toBe("custom-domain-rewrite-subdomain-root");- expect(action?.url?.pathname).toBe("/");- });-- // Branch 4: no subdomain, shallow path → fallback.- test("branch 4 (fallback): no subdomain, shallow path → /{slug}", () => {- const action = resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "localhost:3000",- pathname: "/",- search: "",- isSelfHosted: false,- requestUrl: "http://localhost:3000/",- });- expect(action?.reason).toBe("custom-domain-rewrite-fallback");- expect(action?.url?.pathname).toBe("/acme");- });-- test("branch 4 preserves search", () => {- const action = resolveCustomDomainRewrite({- page,- host: "status.acme.com",- urlHost: "localhost:3000",- pathname: "/",- search: "?x=1",- isSelfHosted: false,- requestUrl: "http://localhost:3000/",- });- expect(action?.url?.search).toBe("?x=1");- });-});diff --git a/apps/status-page/src/lib/proxy/resolve-custom-domain-rewrite.ts b/apps/status-page/src/lib/proxy/resolve-custom-domain-rewrite.tsdeleted file mode 100644index f72527f0b..000000000--- a/apps/status-page/src/lib/proxy/resolve-custom-domain-rewrite.ts+++ /dev/null@@ -1,87 +0,0 @@-import type { Page } from "@openstatus/db/src/schema";--import { getValidSubdomain } from "../domain";-import type { Action, ComposeInput } from "./types";--type Input = Pick<- ComposeInput,- "host" | "urlHost" | "pathname" | "search" | "isSelfHosted" | "requestUrl"-> & {- page: Pick<Page, "slug" | "customDomain">;-};--/**- * stpg.dev ↔ custom-domain rewrite. Runs only when the request reaches us on a- * host other than `{slug}.stpg.dev` for a page that has a configured custom- * domain (and we are not in self-hosted mode).- *- * Four branches:- * 1. subdomain absent, deep path → rewrite to `/{slug}/<rest>` (path-strip).- * 2. subdomain present, deep path → rewrite to `https://{slug}.stpg.dev/<rest>` (subdomain-subpath).- * 3. subdomain present, shallow path → rewrite to `https://{slug}.stpg.dev{pathname}` (subdomain-root).- * 4. Otherwise → rewrite to `/{slug}` (fallback).- *- * All branches preserve the incoming search string.- */-export function resolveCustomDomainRewrite({- page,- host,- urlHost,- pathname,- search,- isSelfHosted,- requestUrl,-}: Input): Action | null {- if (isSelfHosted) return null;- if (!page.customDomain) return null;- if (host === `${page.slug}.stpg.dev`) return null;-- const pathnames = pathname.split("/");- const subdomain = getValidSubdomain(urlHost);-- // Branch 1: no subdomain, pathname has >1 segment → strip leading segment.- if (pathnames.length > 2 && !subdomain) {- const rest = pathnames.slice(2).join("/");- // Trailing-slash only (e.g. "/status.acme.com/") yields empty `rest` —- // emit `/{slug}` without a trailing slash to match Branch 4's semantics- // and avoid a redundant 308 from Next.js trailing-slash handling.- const path = rest ? `/${page.slug}/${rest}` : `/${page.slug}`;- const url = new URL(path, requestUrl);- url.search = search;- return {- type: "rewrite",- url,- reason: "custom-domain-rewrite-path-strip",- };- }-- // Branch 2 & 3: subdomain present — rewrite to the stpg.dev host.- if (subdomain) {- if (pathnames.length > 2) {- const rest = pathnames.slice(1).join("/");- const url = new URL(rest, `https://${page.slug}.stpg.dev`);- url.search = search;- return {- type: "rewrite",- url,- reason: "custom-domain-rewrite-subdomain-subpath",- };- }- const url = new URL(pathname, `https://${page.slug}.stpg.dev`);- url.search = search;- return {- type: "rewrite",- url,- reason: "custom-domain-rewrite-subdomain-root",- };- }-- // Branch 4: fallback — rewrite to the bare slug.- const url = new URL(`/${page.slug}`, requestUrl);- url.search = search;- return {- type: "rewrite",- url,- reason: "custom-domain-rewrite-fallback",- };-}diff --git a/apps/status-page/src/proxy.ts b/apps/status-page/src/proxy.tsindex b93550f9c..c57aa35ac 100644--- a/apps/status-page/src/proxy.ts+++ b/apps/status-page/src/proxy.ts@@ -6,6 +6,7 @@ import { auth } from "./lib/auth";import { resolveClientIp } from "./lib/http/client-ip";import { createProtectedCookieKey } from "./lib/protected";import { applyPageLocaleOverride } from "./lib/proxy/apply-page-locale-override";+import { applyPageSlugPrefix } from "./lib/proxy/apply-page-slug-prefix";import { composePageAction } from "./lib/proxy/compose-page-action";import { detectMarkdown } from "./lib/proxy/detect-markdown";import { sanitizeRedirectParam } from "./lib/proxy/sanitize-redirect-param";@@ -67,7 +68,10 @@ export default auth(async (req) => {}const _page = validation.data;- const route = applyPageLocaleOverride(initialRoute, _page);+ const route = applyPageSlugPrefix(+ applyPageLocaleOverride(initialRoute, _page),+ _page,+ );const clientIp = resolveClientIp(req.headers);