Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions apps/deploy-web/src/middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ describe("middleware", () => {
expect(response.headers.get("location")).toBe("http://localhost/deployments");
});

it("redirects to the requested relative path when leaving the maintenance page", () => {
const { response } = setup({ path: "/maintenance?return=%2Fdeployments%3Ftab%3Dactive" });

expect(response.headers.get("location")).toBe("http://localhost/deployments?tab=active");
});

it("ignores an absolute return url when leaving the maintenance page", () => {
const { response } = setup({ path: "/maintenance?return=https%3A%2F%2Fevil.example%2Fphish" });

expect(response.headers.get("location")).toBe("http://localhost/");
});

it("ignores a protocol relative return url when leaving the maintenance page", () => {
const { response } = setup({ path: "/maintenance?return=%2F%2Fevil.example%2Fphish" });

expect(response.headers.get("location")).toBe("http://localhost/");
});

it("ignores a backslash prefixed return url when leaving the maintenance page", () => {
const { response } = setup({ path: "/maintenance?return=%2F%5Cevil.example%2Fphish" });

expect(response.headers.get("location")).toBe("http://localhost/");
});

function setup(input: { path: string }) {
const request = new NextRequest(new URL(`http://localhost${input.path}`));
const response = middleware(request);
Expand Down
5 changes: 4 additions & 1 deletion apps/deploy-web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ function getReturnPath(request: NextRequest) {
try {
const returnParam = request.nextUrl.searchParams.get("return");
const returnPath = returnParam ? decodeURIComponent(returnParam) : "/";
const requestUrl = new URL(request.url);
const returnUrl = new URL(returnPath, requestUrl);
const isSameOrigin = returnUrl.origin === requestUrl.origin;

return returnPath;
return isSameOrigin ? `${returnUrl.pathname}${returnUrl.search}${returnUrl.hash}` : "/";
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
} catch (error) {
logger.error({ message: "Failed to get return path", error });
return "/";
Expand Down
36 changes: 36 additions & 0 deletions apps/stats-web/src/middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { NextRequest } from "next/server";
import { describe, expect, it } from "vitest";

import { middleware } from "./middleware";

describe("middleware", () => {
it("redirects to the requested relative path when leaving the maintenance page", () => {
const { response } = setup({ path: "/maintenance?return=%2Fgraph%2Fdaily-akt-spent" });

expect(response.headers.get("location")).toBe("http://localhost/graph/daily-akt-spent");
});

it("ignores an absolute return url when leaving the maintenance page", () => {
const { response } = setup({ path: "/maintenance?return=https%3A%2F%2Fevil.example%2Fphish" });

expect(response.headers.get("location")).toBe("http://localhost/");
});

it("ignores a protocol relative return url when leaving the maintenance page", () => {
const { response } = setup({ path: "/maintenance?return=%2F%2Fevil.example%2Fphish" });

expect(response.headers.get("location")).toBe("http://localhost/");
});

it("ignores a backslash prefixed return url when leaving the maintenance page", () => {
const { response } = setup({ path: "/maintenance?return=%2F%5Cevil.example%2Fphish" });

expect(response.headers.get("location")).toBe("http://localhost/");
});

function setup(input: { path: string }) {
const request = new NextRequest(new URL(`http://localhost${input.path}`));
const response = middleware(request);
return { request, response };
}
});
5 changes: 4 additions & 1 deletion apps/stats-web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ function getReturnPath(request: NextRequest) {
try {
const returnParam = request.nextUrl.searchParams.get("return");
const returnPath = returnParam ? decodeURIComponent(returnParam) : "/";
const requestUrl = new URL(request.url);
const returnUrl = new URL(returnPath, requestUrl);
const isSameOrigin = returnUrl.origin === requestUrl.origin;

return returnPath;
return isSameOrigin ? `${returnUrl.pathname}${returnUrl.search}${returnUrl.hash}` : "/";
} catch (error) {
logger.error({ message: "Failed to get return path", error });
return "/";
Expand Down