File tree 2 files changed +41
-1
lines changed
packages/workers-shared/asset-worker
2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,10 @@ export const handleRequest = async (
18
18
) => {
19
19
const { pathname, search } = new URL ( request . url ) ;
20
20
21
- const decodedPathname = decodePath ( pathname ) ;
21
+ let decodedPathname = decodePath ( pathname ) ;
22
+ // normalize the path
23
+ decodedPathname = decodedPathname . replaceAll ( '//' , '/' ) ;
24
+
22
25
const intent = await getIntent ( decodedPathname , configuration , exists ) ;
23
26
24
27
if ( ! intent ) {
@@ -42,6 +45,8 @@ export const handleRequest = async (
42
45
* We combine this with other redirects (e.g. for html_handling) to avoid multiple redirects.
43
46
*/
44
47
if ( encodedDestination !== pathname || intent . redirect ) {
48
+ console . log ( encodedDestination , pathname ) ;
49
+
45
50
return new TemporaryRedirectResponse ( encodedDestination + search ) ;
46
51
}
47
52
@@ -638,6 +643,8 @@ const safeRedirect = async (
638
643
return null ;
639
644
}
640
645
646
+ console . log ( file , destination , configuration ) ;
647
+
641
648
if ( ! ( await exists ( destination ) ) ) {
642
649
const intent = await getIntent ( destination , configuration , exists , true ) ;
643
650
// return only if the eTag matches - i.e. not the 404 case
Original file line number Diff line number Diff line change @@ -96,4 +96,37 @@ describe("[Asset Worker] `handleRequest`", () => {
96
96
97
97
expect ( response . status ) . toBe ( 200 ) ;
98
98
} ) ;
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
+ } ) ;
99
132
} ) ;
You can’t perform that action at this time.
0 commit comments