Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(pages): keep an owner page's query string through its redirects

erywime180c0399d3b
diff --git a/server/routes/published-pages.test.ts b/server/routes/published-pages.test.ts
index f9d4d07c6b..7ddc074968 100644
--- a/server/routes/published-pages.test.ts
+++ b/server/routes/published-pages.test.ts
@@ -103,24 +103,27 @@ describe("an owner-only page's stable link", () => {
return { ownerOnly: () => only, sessionLive, folder: (pageId, viewer) => `/api/owner-page-files/${pageId}-${viewer.key}/` };
}
- function serve(ownerPages: OwnerPages | undefined, path = `/api/published-pages/${PAGE}/`, method = "GET", auth: RequestAuth = OWNER) {
+ function serve(ownerPages: OwnerPages | undefined, target = `/api/published-pages/${PAGE}/`, method = "GET", auth: RequestAuth = OWNER) {
const sent: { status?: number; body?: any; headers?: Record<string, string>; written?: unknown; out?: typeof PASS | void } = {};
const res = {
headersSent: false, writableEnded: false,
writeHead: (status: number, headers: Record<string, string>) => { sent.status = status; sent.headers = headers; },
end: (body?: unknown) => { sent.written = body; },
};
+ const url = new URL(target, "https://cloud.example.test");
return createPublishedPageRoutes(fakePublisher(), undefined, ownerPages)({ req: {} as never, res: res as never,
- url: new URL(path, "https://cloud.example.test"), path, method, auth,
+ url, path: url.pathname, method, auth,
json: ((_res: unknown, status: number, body: unknown) => { sent.status = status; sent.body = body; }) as never,
readBody: async () => ({}) } as RouteContext).then((out) => Object.assign(sent, { out }));
}
- it("moves the owner to the token folder the page loads from, at any file of it", async () => {
+ it("moves the owner to the token folder the page loads from, at any file of it, keeping the query string", async () => {
expect(await serve(ownerPage())).toMatchObject({ status: 303, headers: { location: `/api/owner-page-files/${PAGE}-session:s1/`,
"cache-control": "private, no-store" } });
expect(await serve(ownerPage(), `/api/published-pages/${PAGE}/data.csv`, "HEAD", HOME)).toMatchObject({ status: 303,
headers: { location: `/api/owner-page-files/${PAGE}-loopback/data.csv` } });
+ expect(await serve(ownerPage(), `/api/published-pages/${PAGE}/data.csv?period=2026-11`, "GET", HOME)).toMatchObject({ status: 303,
+ headers: { location: `/api/owner-page-files/${PAGE}-loopback/data.csv?period=2026-11` } });
});
it("refuses a viewer who is not the owner, and any page that is not owner-only", async () => {
@@ -130,7 +133,9 @@ describe("an owner-only page's stable link", () => {
expect((await serve(undefined)).out).toBe(PASS);
});
- it("moves a page opened without its trailing slash to the folder its files are relative to", async () => {
+ it("moves a page opened without its trailing slash to the folder its files are relative to, keeping the query string", async () => {
expect(await serve(ownerPage(), `/api/published-pages/${PAGE}`)).toMatchObject({ status: 308, headers: { location: `/api/published-pages/${PAGE}/` } });
+ expect(await serve(ownerPage(), `/api/published-pages/${PAGE}?period=2026-11`)).toMatchObject({ status: 308,
+ headers: { location: `/api/published-pages/${PAGE}/?period=2026-11` } });
});
});
diff --git a/server/routes/published-pages.ts b/server/routes/published-pages.ts
index 786bd9132c..26f8d78350 100644
--- a/server/routes/published-pages.ts
+++ b/server/routes/published-pages.ts
@@ -103,7 +103,7 @@ export interface OwnerPages {
* answers first. POST /api/published-pages/<pageId>/unpublish: the card's Unpublish. POST .../messages/<id>/app/publish: a
* page card's Share. All admin-scope routes, so only the owner's own app. */
export function createPublishedPageRoutes(publisher: PagePublisher, apps?: AppPages, ownerPages?: OwnerPages): RouteHandler {
- return async ({ req, res, path, method, auth, json, readBody }) => {
+ return async ({ req, res, url, path, method, auth, json, readBody }) => {
const match = UNPUBLISH_ROUTE.exec(path);
if (match && method === "POST") {
const outcome = await publisher.unpublish({ pageId: match[1] });
@@ -115,14 +115,14 @@ export function createPublishedPageRoutes(publisher: PagePublisher, apps?: AppPa
// Security: a member, a guest and a service-trust bot shell read this as a page that does not exist.
if (!ownerOrAdmin(auth)) return void json(res, 404, { error: "Not found." });
if (page[2] === undefined) {
- res.writeHead(308, { location: `${path}/`, "cache-control": "private, no-store" });
+ res.writeHead(308, { location: `${path}/${url.search}`, "cache-control": "private, no-store" });
res.end();
return;
}
// Its sandbox sends no cookie with the page's own files, so it loads from a token folder minted for this viewer.
const viewer: OwnerPageViewer = { key: auth.kind === "session" ? `session:${auth.session.id}` : auth.kind,
allowed: () => auth.kind !== "session" || (ownerPages.sessionLive?.(auth.session.id) ?? true) };
- res.writeHead(303, { location: `${ownerPages.folder(page[1]!, viewer)}${page[2].slice(1)}`, "cache-control": "private, no-store" });
+ res.writeHead(303, { location: `${ownerPages.folder(page[1]!, viewer)}${page[2].slice(1)}${url.search}`, "cache-control": "private, no-store" });
res.end();
return;
}