Skip to content

Commit 6f09f23

Browse files
committed
Normalize double slashes into single in Workers Assets
1 parent a30c805 commit 6f09f23

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/workers-shared/asset-worker/src/handler.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export const handleRequest = async (
1818
) => {
1919
const { pathname, search } = new URL(request.url);
2020

21-
const decodedPathname = decodePath(pathname);
21+
let decodedPathname = decodePath(pathname);
22+
// normalize the path
23+
decodedPathname = decodedPathname.replaceAll('//', '/');
24+
2225
const intent = await getIntent(decodedPathname, configuration, exists);
2326

2427
if (!intent) {
@@ -42,6 +45,8 @@ export const handleRequest = async (
4245
* We combine this with other redirects (e.g. for html_handling) to avoid multiple redirects.
4346
*/
4447
if (encodedDestination !== pathname || intent.redirect) {
48+
console.log(encodedDestination, pathname);
49+
4550
return new TemporaryRedirectResponse(encodedDestination + search);
4651
}
4752

@@ -638,6 +643,8 @@ const safeRedirect = async (
638643
return null;
639644
}
640645

646+
console.log(file, destination, configuration);
647+
641648
if (!(await exists(destination))) {
642649
const intent = await getIntent(destination, configuration, exists, true);
643650
// return only if the eTag matches - i.e. not the 404 case

packages/workers-shared/asset-worker/tests/handler.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,37 @@ describe("[Asset Worker] `handleRequest`", () => {
9696

9797
expect(response.status).toBe(200);
9898
});
99+
100+
it("normalizes double slashes", async () => {
101+
const configuration: Required<AssetConfig> = {
102+
html_handling: "none",
103+
not_found_handling: "none",
104+
};
105+
const eTag = "some-etag";
106+
const exists = vi.fn().mockReturnValue(eTag);
107+
const getByETag = vi.fn().mockReturnValue({
108+
readableStream: new ReadableStream(),
109+
contentType: "text/html",
110+
});
111+
112+
let response = await handleRequest(
113+
new Request("https://example.com/abc/def//123/456"),
114+
configuration,
115+
exists,
116+
getByETag
117+
);
118+
119+
expect(response.status).toBe(307);
120+
expect(response.headers.get('location')).toBe('/abc/def/123/456');
121+
122+
response = await handleRequest(
123+
new Request("https://example.com/%2fexample.com/"),
124+
configuration,
125+
exists,
126+
getByETag
127+
);
128+
129+
expect(response.status).toBe(307);
130+
expect(response.headers.get('location')).toBe('/example.com/');
131+
});
99132
});

0 commit comments

Comments
 (0)