Skip to content

Commit 2fb5263

Browse files
committed
fixup!
1 parent f69db5f commit 2fb5263

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/utils.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,23 @@ function getRequestURL(req) {
316316
// Fastify decodes URI by default, our logic is based on encoded URI.
317317
// req.originalUrl preserves the original encoded URL; req.url may be
318318
// modified by middleware (e.g. connect-history-api-fallback), in which
319-
// case we re-encode it and use it instead.
319+
// case we use req.url instead.
320320
if (typeof req.originalUrl !== "undefined") {
321-
const encodedUrl =
322-
typeof req.url !== "undefined" ? encodeURI(req.url) : undefined;
321+
// If req.url is just the decoded form of req.originalUrl (Fastify behavior),
322+
// return the original encoded URL. Otherwise middleware modified req.url.
323+
try {
324+
if (
325+
req.url === req.originalUrl ||
326+
(typeof req.url !== "undefined" &&
327+
decodeURI(req.originalUrl) === req.url)
328+
) {
329+
return req.originalUrl;
330+
}
331+
} catch {
332+
// decodeURI can throw on malformed sequences, fall through
333+
}
323334

324-
return encodedUrl !== req.originalUrl && encodedUrl !== undefined
325-
? req.url
326-
: req.originalUrl;
335+
return req.url;
327336
}
328337

329338
return req.url;

test/utils/getRequestURL.test.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,24 @@ describe("getRequestURL", () => {
2121
expect(getRequestURL(req)).toBe("/path%20with%20spaces");
2222
});
2323

24-
it("should return encoded req.url when middleware (e.g. connect-history-api-fallback) modified it", () => {
24+
it("should return req.originalUrl for URLs with reserved characters", () => {
25+
// Fastify partially decodes: %20 → space, but %26 stays as %26
26+
const req = {
27+
url: "/test %26 test %26 %20.txt",
28+
originalUrl: "/test%20%26%20test%20%26%20%2520.txt",
29+
};
30+
expect(getRequestURL(req)).toBe("/test%20%26%20test%20%26%20%2520.txt");
31+
});
32+
33+
it("should return req.url when middleware (e.g. connect-history-api-fallback) modified it", () => {
2534
// Middleware changed req.url to a different path
2635
const req = { url: "/index.html", originalUrl: "/path%20with%20spaces" };
2736
expect(getRequestURL(req)).toBe("/index.html");
2837
});
2938

30-
it("should encode req.url when middleware sets a path with special chars", () => {
39+
it("should return req.url when middleware sets a path with special chars", () => {
3140
const req = { url: "/new path", originalUrl: "/old%20path" };
32-
expect(getRequestURL(req)).toBe("/new%20path");
41+
expect(getRequestURL(req)).toBe("/new path");
3342
});
3443

3544
it("should preserve query string from modified req.url", () => {
@@ -39,5 +48,22 @@ describe("getRequestURL", () => {
3948
};
4049
expect(getRequestURL(req)).toBe("/index.html?redirect=/other");
4150
});
51+
52+
it("should return req.originalUrl when req.url equals req.originalUrl (Express)", () => {
53+
const req = {
54+
url: "/path%20foo",
55+
originalUrl: "/path%20foo",
56+
};
57+
expect(getRequestURL(req)).toBe("/path%20foo");
58+
});
59+
60+
it("should handle malformed percent-encoding in originalUrl gracefully", () => {
61+
// decodeURI would throw on malformed sequences
62+
const req = {
63+
url: "/foo",
64+
originalUrl: "/%GG%bad",
65+
};
66+
expect(getRequestURL(req)).toBe("/foo");
67+
});
4268
});
4369
});

0 commit comments

Comments
 (0)