mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
853 lines
28 KiB
TypeScript
853 lines
28 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { addMonths, subDays } from "date-fns";
|
|
import {
|
|
IntegrationService,
|
|
IntegrationType,
|
|
TeamPreference,
|
|
} from "@shared/types";
|
|
import { parseSessionsCookie } from "@shared/utils/sessions";
|
|
import { parser } from "@server/editor";
|
|
import env from "@server/env";
|
|
import {
|
|
buildAttachment,
|
|
buildShare,
|
|
buildDocument,
|
|
buildIntegration,
|
|
buildTeam,
|
|
} from "@server/test/factories";
|
|
import { getTestServer } from "@server/test/support";
|
|
|
|
const server = getTestServer();
|
|
|
|
const botHeaders = {
|
|
"User-Agent":
|
|
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
|
|
};
|
|
|
|
describe("/s/:id", () => {
|
|
it("should return standard title in html when loading unpublished share", async () => {
|
|
const share = await buildShare({
|
|
published: false,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(404);
|
|
expect(body).toContain("<title>Outline</title>");
|
|
});
|
|
|
|
it("should return standard title in html when share does not exist", async () => {
|
|
const res = await server.get(`/s/junk`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(404);
|
|
expect(body).toContain("<title>Outline</title>");
|
|
});
|
|
|
|
it("should return standard title in html when document is deleted", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
await document.destroy();
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(404);
|
|
expect(body).toContain("<title>Outline</title>");
|
|
});
|
|
|
|
it("should return document title in html when loading published share", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(body).toContain(`<title>${document.title}</title>`);
|
|
});
|
|
|
|
it("should allow indexing of a published share that opts in", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
allowIndexing: true,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(body).toContain(`<meta name="robots" content="index, follow" />`);
|
|
});
|
|
|
|
it("should prevent indexing of a share that does not opt in", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
allowIndexing: false,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(body).toContain(
|
|
`<meta name="robots" content="noindex, nofollow" />`
|
|
);
|
|
});
|
|
|
|
it("should prevent indexing when share does not exist", async () => {
|
|
const res = await server.get(`/s/junk`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(404);
|
|
expect(body).toContain(
|
|
`<meta name="robots" content="noindex, nofollow" />`
|
|
);
|
|
});
|
|
|
|
it("should return document title in html when loading published share with nested doc route", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}/doc/${document.urlId}`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(body).toContain(`<title>${document.title}</title>`);
|
|
});
|
|
|
|
it("should render document content with signed attachment urls", async () => {
|
|
const team = await buildTeam();
|
|
const attachment = await buildAttachment({ teamId: team.id });
|
|
const document = await buildDocument({
|
|
teamId: team.id,
|
|
text: `Shared content\n\n`,
|
|
});
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`, { headers: botHeaders });
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(body).toContain("Shared content");
|
|
expect(body).toContain("<img");
|
|
expect(body).not.toContain(attachment.redirectUrl);
|
|
});
|
|
|
|
it("should not render document content for browsers", async () => {
|
|
const document = await buildDocument({ text: "Shared content" });
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`, {
|
|
headers: {
|
|
"User-Agent":
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
|
|
},
|
|
});
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(body).toContain(`<title>${document.title}</title>`);
|
|
expect(body).not.toContain('<div id="content"');
|
|
});
|
|
|
|
it("should render updated content after the document changes", async () => {
|
|
const document = await buildDocument({ text: "First version" });
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const first = await server.get(`/s/${share.id}`, { headers: botHeaders });
|
|
expect(await first.text()).toContain("First version");
|
|
|
|
document.content = parser.parse("Second version")?.toJSON();
|
|
await document.save();
|
|
const second = await server.get(`/s/${share.id}`, {
|
|
headers: botHeaders,
|
|
});
|
|
expect(await second.text()).toContain("Second version");
|
|
});
|
|
|
|
it("should return markdown when Accept header prefers text/markdown", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`, {
|
|
headers: { Accept: "text/markdown" },
|
|
});
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(res.headers.get("content-type")).toContain("text/markdown");
|
|
expect(body).toContain(`# ${document.title}`);
|
|
});
|
|
|
|
it("should return markdown when URL path ends with .md", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}.md`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(res.headers.get("content-type")).toContain("text/markdown");
|
|
expect(body).toContain(`# ${document.title}`);
|
|
});
|
|
|
|
it("should return html when Accept header prefers text/html over text/markdown", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`, {
|
|
headers: { Accept: "text/html, text/markdown" },
|
|
});
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(res.headers.get("content-type")).toContain("text/html");
|
|
expect(body).toContain(`<title>${document.title}</title>`);
|
|
});
|
|
|
|
it("should vary on Accept and advertise the markdown alternate in html", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(res.headers.get("vary")).toContain("Accept");
|
|
const origin = new URL(res.url).origin;
|
|
expect(res.headers.get("link")).toEqual(
|
|
`<${origin}/s/${share.id}.md>; rel="alternate"; type="text/markdown"`
|
|
);
|
|
expect(body).toContain(
|
|
`<link rel="alternate" type="text/markdown" href="${origin}/s/${share.id}.md" />`
|
|
);
|
|
});
|
|
|
|
it("should advertise the markdown alternate for a nested document", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}/doc/${document.urlId}`);
|
|
expect(res.status).toEqual(200);
|
|
const origin = new URL(res.url).origin;
|
|
expect(res.headers.get("link")).toEqual(
|
|
`<${origin}/s/${share.id}/doc/${document.urlId}.md>; rel="alternate"; type="text/markdown"`
|
|
);
|
|
});
|
|
|
|
it("should not advertise the markdown alternate when share is not found", async () => {
|
|
const res = await server.get(`/s/junk`);
|
|
expect(res.status).toEqual(404);
|
|
expect(res.headers.get("link")).toEqual(null);
|
|
});
|
|
|
|
it("should vary on Accept and prevent caching of markdown", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}.md`);
|
|
expect(res.status).toEqual(200);
|
|
expect(res.headers.get("vary")).toContain("Accept");
|
|
expect(res.headers.get("cache-control")).toEqual(
|
|
"no-cache, must-revalidate"
|
|
);
|
|
});
|
|
|
|
it("should include child documents list in markdown when includeChildDocuments is true", async () => {
|
|
const parent = await buildDocument();
|
|
const child1 = await buildDocument({
|
|
teamId: parent.teamId,
|
|
collectionId: parent.collectionId,
|
|
parentDocumentId: parent.id,
|
|
title: "Child Document 1",
|
|
});
|
|
const child2 = await buildDocument({
|
|
teamId: parent.teamId,
|
|
collectionId: parent.collectionId,
|
|
parentDocumentId: parent.id,
|
|
title: "Child Document 2",
|
|
});
|
|
|
|
const share = await buildShare({
|
|
documentId: parent.id,
|
|
teamId: parent.teamId,
|
|
includeChildDocuments: true,
|
|
});
|
|
|
|
const res = await server.get(`/s/${share.id}`, {
|
|
headers: { Accept: "text/markdown" },
|
|
});
|
|
const body = await res.text();
|
|
|
|
expect(res.status).toEqual(200);
|
|
expect(res.headers.get("content-type")).toContain("text/markdown");
|
|
expect(body).toContain(`# ${parent.title}`);
|
|
expect(body).toContain("---");
|
|
expect(body).toContain("**Documents**");
|
|
expect(body).toContain(`[${child1.title}]`);
|
|
expect(body).toContain(`[${child2.title}]`);
|
|
});
|
|
|
|
it("should not include child documents list in markdown when includeChildDocuments is false", async () => {
|
|
const parent = await buildDocument();
|
|
await buildDocument({
|
|
teamId: parent.teamId,
|
|
collectionId: parent.collectionId,
|
|
parentDocumentId: parent.id,
|
|
title: "Child Document",
|
|
});
|
|
|
|
const share = await buildShare({
|
|
documentId: parent.id,
|
|
teamId: parent.teamId,
|
|
includeChildDocuments: false,
|
|
});
|
|
|
|
const res = await server.get(`/s/${share.id}`, {
|
|
headers: { Accept: "text/markdown" },
|
|
});
|
|
const body = await res.text();
|
|
|
|
expect(res.status).toEqual(200);
|
|
expect(res.headers.get("content-type")).toContain("text/markdown");
|
|
expect(body).toContain(`# ${parent.title}`);
|
|
expect(body).not.toContain("**Documents**");
|
|
expect(body).not.toContain("[Child Document]");
|
|
});
|
|
|
|
it("should return 404 when the share belongs to another team", async () => {
|
|
const team = await buildTeam({ subdomain: `host-${randomUUID()}` });
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
|
|
const res = await server.get(`/s/${share.id}.md`, {
|
|
headers: { Host: `${team.subdomain}.outline.dev` },
|
|
});
|
|
const body = await res.text();
|
|
|
|
expect(res.status).toEqual(404);
|
|
expect(body).not.toContain(document.title);
|
|
});
|
|
});
|
|
|
|
describe("content security policy", () => {
|
|
// Analytics integrations only extend the policy on self-hosted installations,
|
|
// the cloud policy already includes these sources.
|
|
beforeEach(() => {
|
|
env.URL = "https://outline.example.com";
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"gitlab",
|
|
"https://gitlab.com/gitlab-org/gitlab/-/snippets/1",
|
|
"gitlab.com",
|
|
],
|
|
["github", "https://gist.github.com/user/abc123", "gist.github.com"],
|
|
["dropbox", "https://www.dropbox.com/s/abc123/file.pdf", "www.dropbox.com"],
|
|
["pinterest", "https://pinterest.com/user", "assets.pinterest.com"],
|
|
])("should allow the %s embed script source", async (path, url, source) => {
|
|
const res = await server.get(
|
|
`/embeds/${path}?url=${encodeURIComponent(url)}`
|
|
);
|
|
const csp = res.headers.get("content-security-policy") ?? "";
|
|
expect(res.status).toEqual(200);
|
|
expect(csp).toContain(source);
|
|
expect(csp).toContain("nonce-");
|
|
});
|
|
|
|
it("should still send a policy for responses that do not render the app", async () => {
|
|
const res = await server.get(`/s/junk`);
|
|
const csp = res.headers.get("content-security-policy") ?? "";
|
|
expect(res.status).toEqual(404);
|
|
expect(csp).toContain("script-src");
|
|
expect(csp).toContain("nonce-");
|
|
});
|
|
|
|
it("should only allow framing of app pages on the same origin", async () => {
|
|
const res = await server.get("/oauth/authorize?client_id=x");
|
|
const csp = res.headers.get("content-security-policy") ?? "";
|
|
expect(res.status).toEqual(200);
|
|
expect(csp).toContain("frame-ancestors 'self'");
|
|
});
|
|
|
|
it("should allow framing of shares on any origin", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const csp = res.headers.get("content-security-policy") ?? "";
|
|
expect(res.status).toEqual(200);
|
|
expect(csp).not.toContain("frame-ancestors");
|
|
});
|
|
|
|
it("should not allow framing of shares when embedding is prevented", async () => {
|
|
const domain = `embed-${randomUUID()}.example.com`;
|
|
env.URL = `https://${domain}`;
|
|
const team = await buildTeam({ domain });
|
|
team.setPreference(TeamPreference.PreventDocumentEmbedding, true);
|
|
await team.save();
|
|
const document = await buildDocument({ teamId: team.id });
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: team.id,
|
|
});
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const csp = res.headers.get("content-security-policy") ?? "";
|
|
expect(res.status).toEqual(200);
|
|
expect(csp).toContain("frame-ancestors 'self'");
|
|
});
|
|
|
|
it("should allow the Google Tag Manager script source when integration is enabled", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
await buildIntegration({
|
|
teamId: document.teamId,
|
|
type: IntegrationType.Analytics,
|
|
service: IntegrationService.GoogleAnalytics,
|
|
settings: { measurementId: "G-TEST123456" },
|
|
});
|
|
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const csp = res.headers.get("content-security-policy") ?? "";
|
|
expect(res.status).toEqual(200);
|
|
expect(csp).toContain("www.googletagmanager.com");
|
|
expect(csp).toContain(`nonce-`);
|
|
expect(csp).not.toContain("'unsafe-inline'; script-src");
|
|
});
|
|
|
|
it("should not allow the Google Tag Manager script source without integration", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const csp = res.headers.get("content-security-policy") ?? "";
|
|
expect(res.status).toEqual(200);
|
|
expect(csp).not.toContain("www.googletagmanager.com");
|
|
});
|
|
|
|
it("should allow the instance host for self-hosted analytics integrations", async () => {
|
|
const document = await buildDocument();
|
|
const share = await buildShare({
|
|
documentId: document.id,
|
|
teamId: document.teamId,
|
|
});
|
|
await buildIntegration({
|
|
teamId: document.teamId,
|
|
type: IntegrationType.Analytics,
|
|
service: IntegrationService.Matomo,
|
|
settings: {
|
|
measurementId: "1",
|
|
instanceUrl: "https://matomo.example.com/",
|
|
},
|
|
});
|
|
|
|
const res = await server.get(`/s/${share.id}`);
|
|
const csp = res.headers.get("content-security-policy") ?? "";
|
|
expect(res.status).toEqual(200);
|
|
expect(csp).toContain("matomo.example.com");
|
|
});
|
|
});
|
|
|
|
describe("/locales/:lng.json", () => {
|
|
it("should require revalidation before reusing a cached translation", async () => {
|
|
const res = await server.get("/locales/en_US.json");
|
|
expect(res.status).toEqual(200);
|
|
expect(res.headers.get("content-type")).toContain("application/json");
|
|
expect(res.headers.get("cache-control")).toEqual("public, no-cache");
|
|
expect(res.headers.get("etag")).toBeTruthy();
|
|
expect(res.headers.get("access-control-allow-origin")).toEqual("*");
|
|
const body = await res.json();
|
|
expect(typeof body).toEqual("object");
|
|
});
|
|
|
|
it("should omit entries that match their key", async () => {
|
|
const res = await server.get("/locales/en_US.json");
|
|
const body: Record<string, string> = await res.json();
|
|
expect(body.Saving).toBeUndefined();
|
|
expect(Object.entries(body).length).toBeGreaterThan(0);
|
|
for (const [key, value] of Object.entries(body)) {
|
|
expect(value).not.toEqual(key);
|
|
}
|
|
});
|
|
|
|
it("should keep translated entries", async () => {
|
|
const res = await server.get("/locales/de_DE.json");
|
|
const body: Record<string, string> = await res.json();
|
|
expect(body.Saving).toEqual("Speichert");
|
|
});
|
|
|
|
it("should return 304 when the ETag matches", async () => {
|
|
const res = await server.get("/locales/de_DE.json");
|
|
const etag = res.headers.get("etag") ?? "";
|
|
const cached = await server.get("/locales/de_DE.json", {
|
|
headers: { "If-None-Match": etag },
|
|
});
|
|
expect(cached.status).toEqual(304);
|
|
});
|
|
|
|
it("should return 404 for an unknown language", async () => {
|
|
const res = await server.get("/locales/xx_XX.json");
|
|
expect(res.status).toEqual(404);
|
|
});
|
|
});
|
|
|
|
describe("public assets", () => {
|
|
it.each([
|
|
"/images/..%2F..%2Fpackage.json",
|
|
"/fonts/..%2F..%2F..%2Fpackage.json",
|
|
"/email/%2e%2e%2f%2e%2e%2fpackage.json",
|
|
"/images/..%5C..%5Cpackage.json",
|
|
])(
|
|
"should not serve files outside the public directory for %s",
|
|
async (path) => {
|
|
const res = await server.get(path);
|
|
expect(res.status).toEqual(403);
|
|
const body = await res.text();
|
|
expect(body).not.toContain("dependencies");
|
|
}
|
|
);
|
|
|
|
it("should reject paths with null bytes", async () => {
|
|
const res = await server.get("/images/logo.png%00.txt");
|
|
expect(res.status).toEqual(400);
|
|
});
|
|
});
|
|
|
|
describe("scanner path 404s", () => {
|
|
it.each([
|
|
"/.well-known/gpc.json",
|
|
"/.env",
|
|
"/.git/config",
|
|
"/cgi-bin/test.cgi",
|
|
"/wp-admin/setup-config.php",
|
|
"/wp-login.php",
|
|
"/xmlrpc.php",
|
|
"/admin.php",
|
|
"/phpmyadmin/index.php",
|
|
"/actuator/health",
|
|
"/HNAP1/",
|
|
])("returns 404 for %s without rendering the app shell", async (path) => {
|
|
const res = await server.get(path);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(404);
|
|
expect(body).not.toContain("<title>");
|
|
});
|
|
|
|
it.each(["/", "/login", "/some-app-route"])(
|
|
"prevents indexing of the app shell at %s",
|
|
async (path) => {
|
|
const res = await server.get(path);
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(200);
|
|
expect(body).toContain(
|
|
`<meta name="robots" content="noindex, nofollow" />`
|
|
);
|
|
}
|
|
);
|
|
|
|
it("still serves the app shell for legitimate unknown paths", async () => {
|
|
const res = await server.get("/some-app-route");
|
|
expect(res.status).toEqual(200);
|
|
});
|
|
|
|
it("returns 404 for the legacy SSE transport probe", async () => {
|
|
const res = await server.get("/sse");
|
|
const body = await res.text();
|
|
expect(res.status).toEqual(404);
|
|
expect(body).not.toContain("<title>");
|
|
});
|
|
|
|
it("still serves the OAuth well-known endpoint", async () => {
|
|
const res = await server.get("/.well-known/oauth-authorization-server");
|
|
expect(res.status).toEqual(200);
|
|
const body = await res.json();
|
|
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);
|
|
});
|
|
});
|
|
|
|
describe("root domain session redirects", () => {
|
|
const host = "app.outline.dev";
|
|
|
|
const sessionsCookie = (teams: { id: string; name: string; url: string }[]) =>
|
|
`sessions=${encodeURIComponent(
|
|
JSON.stringify(
|
|
Object.fromEntries(
|
|
teams.map((team) => [
|
|
team.id,
|
|
{ name: team.name, logoUrl: "", url: team.url },
|
|
])
|
|
)
|
|
)
|
|
)}`;
|
|
|
|
it("should redirect an app path to the only signed-in workspace", async () => {
|
|
const team = await buildTeam({ subdomain: `only-${randomUUID()}` });
|
|
|
|
const res = await server.get("/settings/billing?referer=upcoming_invoice", {
|
|
headers: { Host: host, Cookie: sessionsCookie([team]) },
|
|
redirect: "manual",
|
|
});
|
|
|
|
expect(res.status).toEqual(302);
|
|
expect(res.headers.get("location")).toEqual(
|
|
`${team.url}/settings/billing?referer=upcoming_invoice`
|
|
);
|
|
});
|
|
|
|
it("should skip unusable entries and redirect to the remaining workspace", async () => {
|
|
const team = await buildTeam({ subdomain: `usable-${randomUUID()}` });
|
|
const suspended = await buildTeam({
|
|
subdomain: `suspended-${randomUUID()}`,
|
|
suspendedAt: new Date(),
|
|
});
|
|
const deleted = { id: randomUUID(), name: "Gone", url: "https://x.dev" };
|
|
|
|
const res = await server.get("/settings/billing", {
|
|
headers: {
|
|
Host: host,
|
|
Cookie: sessionsCookie([suspended, deleted, team]),
|
|
},
|
|
redirect: "manual",
|
|
});
|
|
|
|
expect(res.status).toEqual(302);
|
|
expect(res.headers.get("location")).toEqual(`${team.url}/settings/billing`);
|
|
});
|
|
|
|
it("should not redirect when signed in to several workspaces", async () => {
|
|
const first = await buildTeam({ subdomain: `first-${randomUUID()}` });
|
|
const second = await buildTeam({ subdomain: `second-${randomUUID()}` });
|
|
|
|
const res = await server.get("/settings/billing", {
|
|
headers: { Host: host, Cookie: sessionsCookie([first, second]) },
|
|
redirect: "manual",
|
|
});
|
|
|
|
expect(res.status).toEqual(200);
|
|
});
|
|
|
|
it("should not redirect public paths", async () => {
|
|
const team = await buildTeam({ subdomain: `public-${randomUUID()}` });
|
|
|
|
for (const path of ["/", "/create", "/oauth/authorize?client_id=x"]) {
|
|
const res = await server.get(path, {
|
|
headers: { Host: host, Cookie: sessionsCookie([team]) },
|
|
redirect: "manual",
|
|
});
|
|
expect(res.status).toEqual(200);
|
|
}
|
|
});
|
|
|
|
it("should clean and refresh the cookie before rendering the workspace chooser", async () => {
|
|
const team = await buildTeam({
|
|
name: "Updated workspace",
|
|
subdomain: `updated-${randomUUID()}`,
|
|
domain: `docs-${randomUUID()}.example.com`,
|
|
});
|
|
const suspended = await buildTeam({ suspendedAt: new Date() });
|
|
const loggedOut = await buildTeam();
|
|
const forgotten = await buildTeam();
|
|
const expiredAt = subDays(new Date(), 1).toISOString();
|
|
const lastUsedAt = subDays(new Date(), 2).toISOString();
|
|
const expiresAt = addMonths(new Date(), 1).toISOString();
|
|
const stale = {
|
|
name: "Old workspace",
|
|
logoUrl: null,
|
|
url: "https://old.outline.dev",
|
|
lastUsedAt,
|
|
};
|
|
const res = await server.get("/oauth/authorize?client_id=x", {
|
|
headers: {
|
|
Host: host,
|
|
Cookie: `sessions=${encodeURIComponent(
|
|
JSON.stringify({
|
|
[team.id]: { ...stale, expiresAt },
|
|
[suspended.id]: stale,
|
|
[randomUUID()]: stale,
|
|
[loggedOut.id]: {
|
|
...stale,
|
|
expiresAt: expiredAt,
|
|
},
|
|
[forgotten.id]: {
|
|
...stale,
|
|
lastUsedAt: addMonths(new Date(), -4).toISOString(),
|
|
},
|
|
invalid: stale,
|
|
})
|
|
)}`,
|
|
},
|
|
redirect: "manual",
|
|
});
|
|
expect(res.status).toEqual(200);
|
|
const cookies = res.headers.raw()["set-cookie"];
|
|
const cookie = cookies.find((value) => value.startsWith("sessions="));
|
|
const value = cookie?.split(";")[0].slice("sessions=".length);
|
|
expect(parseSessionsCookie(decodeURIComponent(value || "{}"))).toEqual({
|
|
[team.id]: {
|
|
name: team.name,
|
|
logoUrl: team.avatarUrl,
|
|
url: team.url,
|
|
expiresAt,
|
|
lastUsedAt,
|
|
},
|
|
[loggedOut.id]: {
|
|
name: loggedOut.name,
|
|
logoUrl: loggedOut.avatarUrl,
|
|
url: loggedOut.url,
|
|
expiresAt: expiredAt,
|
|
lastUsedAt,
|
|
},
|
|
});
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"should retain an expired workspace without redirecting to it (active workspace: %s)",
|
|
async (includeActive) => {
|
|
const expired = await buildTeam({ subdomain: `expired-${randomUUID()}` });
|
|
const active = await buildTeam({ subdomain: `active-${randomUUID()}` });
|
|
const expiresAt = subDays(new Date(), 1).toISOString();
|
|
const sessions = {
|
|
[expired.id]: {
|
|
name: expired.name,
|
|
logoUrl: expired.avatarUrl,
|
|
url: expired.url,
|
|
expiresAt,
|
|
lastUsedAt: subDays(new Date(), 1).toISOString(),
|
|
},
|
|
...(includeActive
|
|
? {
|
|
[active.id]: {
|
|
name: active.name,
|
|
logoUrl: active.avatarUrl,
|
|
url: active.url,
|
|
expiresAt: addMonths(new Date(), 1).toISOString(),
|
|
lastUsedAt: subDays(new Date(), 1).toISOString(),
|
|
},
|
|
}
|
|
: {}),
|
|
};
|
|
const res = await server.get("/settings/billing", {
|
|
headers: {
|
|
Host: host,
|
|
Cookie: `sessions=${encodeURIComponent(JSON.stringify(sessions))}`,
|
|
},
|
|
redirect: "manual",
|
|
});
|
|
expect(res.status).toEqual(includeActive ? 302 : 200);
|
|
expect(res.headers.get("location")).toEqual(
|
|
includeActive ? `${active.url}/settings/billing` : null
|
|
);
|
|
const cookies = res.headers.raw()["set-cookie"];
|
|
const cookie = cookies?.find((value) => value.startsWith("sessions="));
|
|
expect(cookie).toBeUndefined();
|
|
}
|
|
);
|
|
|
|
it("should not redirect without a sessions cookie", async () => {
|
|
const res = await server.get("/settings/billing", {
|
|
headers: { Host: host },
|
|
redirect: "manual",
|
|
});
|
|
|
|
expect(res.status).toEqual(200);
|
|
});
|
|
|
|
it("should ignore a malformed sessions cookie", async () => {
|
|
const cookies = [
|
|
JSON.stringify({ "not-a-uuid": { name: "x", url: "https://evil.com" } }),
|
|
"null",
|
|
'"string"',
|
|
"[]",
|
|
"{not json",
|
|
];
|
|
|
|
for (const cookie of cookies) {
|
|
const res = await server.get("/settings/billing", {
|
|
headers: {
|
|
Host: host,
|
|
Cookie: `sessions=${encodeURIComponent(cookie)}`,
|
|
},
|
|
redirect: "manual",
|
|
});
|
|
|
|
expect(res.status).toEqual(200);
|
|
}
|
|
});
|
|
});
|