Skip to content

Open source alternative to Semrush and Ahrefs

Keep the public host when a request path starts with // (#1057)

Ben Senescudf3f48d3d774
diff --git a/src/server/mcp/public-origin.test.ts b/src/server/mcp/public-origin.test.ts
index 369a6ca7..0473aa6a 100644
--- a/src/server/mcp/public-origin.test.ts
+++ b/src/server/mcp/public-origin.test.ts
@@ -49,4 +49,17 @@ describe("requestWithPublicOrigin", () => {
method: "initialize",
});
});
+
+ it("keeps the public host when the path starts with //", () => {
+ const request = new Request("http://localhost:3102//evil.example/mcp", {
+ headers: {
+ "x-forwarded-proto": "https",
+ "x-forwarded-host": "seo.example.com",
+ },
+ });
+
+ expect(requestWithPublicOrigin(request).url).toBe(
+ "https://seo.example.com//evil.example/mcp",
+ );
+ });
});
diff --git a/src/server/mcp/public-origin.ts b/src/server/mcp/public-origin.ts
index c4b21e62..7540329d 100644
--- a/src/server/mcp/public-origin.ts
+++ b/src/server/mcp/public-origin.ts
@@ -35,9 +35,10 @@ export function requestWithPublicOrigin(request: Request) {
return request;
}
- const publicUrl = new URL(
- `${url.pathname}${url.search}${url.hash}`,
- publicOrigin,
+ // Concatenate rather than resolve the path against the origin: a path like
+ // `//evil.example/mcp` would otherwise replace the host.
+ return new Request(
+ `${publicOrigin}${url.pathname}${url.search}${url.hash}`,
+ request,
);
- return new Request(publicUrl, request);
}