fix: Build canonical redirect url from team, not request host (#13344)

* fix: Build canonical redirect url from team, not request host

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* test: Cover canonical host redirects

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom MoorandClaude Opus 5 authored and GitHub committed 2026-08-07 19:29:29 -04:00
1 parent 399cd4041d
commit b82c57cedf
2 files changed
+61 -17

No files matched your search

+53
View File
@@ -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);
});
});
+8 -17
View File
@@ -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;
}
}