diff --git a/server/routes/index.test.ts b/server/routes/index.test.ts index c50b14e639..8ca29c9dee 100644 --- a/server/routes/index.test.ts +++ b/server/routes/index.test.ts @@ -1,9 +1,11 @@ +import { randomUUID } from "node:crypto"; import { IntegrationService, IntegrationType } from "@shared/types"; import env from "@server/env"; import { buildShare, buildDocument, buildIntegration, + buildTeam, } from "@server/test/factories"; import { getTestServer } from "@server/test/support"; @@ -293,3 +295,54 @@ describe("scanner path 404s", () => { expect(body.issuer).toBeDefined(); }); }); + +describe("canonical host redirects", () => { + it("should redirect to the current subdomain when a previous one is used", async () => { + const id = randomUUID(); + const subdomain = `current-${id}`; + const previousSubdomain = `previous-${id}`; + await buildTeam({ + subdomain, + previousSubdomains: [previousSubdomain], + }); + + const res = await server.get("/search?query=hello", { + headers: { Host: `${previousSubdomain}.outline.dev` }, + redirect: "manual", + }); + + expect(res.status).toEqual(302); + expect(res.headers.get("location")).toEqual( + `https://${subdomain}.outline.dev/search?query=hello` + ); + }); + + it("should redirect to the custom domain when one is set", async () => { + const id = randomUUID(); + const subdomain = `wombat-${id}`; + const domain = `docs-${id}.example.com`; + await buildTeam({ subdomain, domain }); + + const res = await server.get("/doc/getting-started", { + headers: { Host: `${subdomain}.outline.dev` }, + redirect: "manual", + }); + + expect(res.status).toEqual(302); + expect(res.headers.get("location")).toEqual( + `https://${domain}/doc/getting-started` + ); + }); + + it("should not redirect a request already on the canonical host", async () => { + const subdomain = `canonical-${randomUUID()}`; + await buildTeam({ subdomain }); + + const res = await server.get("/search", { + headers: { Host: `${subdomain}.outline.dev` }, + redirect: "manual", + }); + + expect(res.status).toEqual(200); + }); +}); diff --git a/server/routes/index.ts b/server/routes/index.ts index ba1f666d61..6151679a25 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -244,23 +244,14 @@ router.get("*", async (ctx, next) => { } } - // Redirect all requests to custom domain if one is set - else if (team?.domain) { - if (team.domain !== ctx.hostname) { - ctx.redirect(ctx.href.replace(ctx.hostname, team.domain)); - return; - } - } - - // Redirect if subdomain is not the current team's subdomain - else if (team?.subdomain) { - const { teamSubdomain } = parseDomain(ctx.href); - if (team?.subdomain !== teamSubdomain) { - ctx.redirect( - ctx.href.replace(`//${teamSubdomain}.`, `//${team.subdomain}.`) - ); - return; - } + // Redirect to the team's canonical url, taking into account custom domains + // and hosted subdomains, if the request arrived on a different host. + else if (team && !team.isTeamUrl(ctx.href)) { + const url = new URL(team.url); + url.pathname = ctx.path; + url.search = ctx.search; + ctx.redirect(url.toString()); + return; } }