@@ -50,11 +50,26 @@ <h2>Denied</h2>
50
50
import '../../entry'
51
51
import json , { msg } from '../../safe.json'
52
52
53
+ function joinUrlSegments ( a , b ) {
54
+ if ( ! a || ! b ) {
55
+ return a || b || ''
56
+ }
57
+ if ( a [ a . length - 1 ] === '/' ) {
58
+ a = a . substring ( 0 , a . length - 1 )
59
+ }
60
+ if ( b [ 0 ] !== '/' ) {
61
+ b = '/' + b
62
+ }
63
+ return a + b
64
+ }
65
+
53
66
text ( '.full' , JSON . stringify ( json ) )
54
67
text ( '.named' , msg )
55
68
69
+ const base = typeof BASE !== 'undefined' ? BASE : ''
70
+
56
71
// inside allowed dir, safe fetch
57
- fetch ( '/src/safe.txt' )
72
+ fetch ( joinUrlSegments ( base , '/src/safe.txt' ) )
58
73
. then ( ( r ) => {
59
74
text ( '.safe-fetch-status' , r . status )
60
75
return r . text ( )
@@ -64,7 +79,7 @@ <h2>Denied</h2>
64
79
} )
65
80
66
81
// inside allowed dir with query, safe fetch
67
- fetch ( '/src/safe.txt?query' )
82
+ fetch ( joinUrlSegments ( base , '/src/safe.txt?query' ) )
68
83
. then ( ( r ) => {
69
84
text ( '.safe-fetch-query-status' , r . status )
70
85
return r . text ( )
@@ -74,7 +89,7 @@ <h2>Denied</h2>
74
89
} )
75
90
76
91
// inside allowed dir, safe fetch
77
- fetch ( '/src/subdir/safe.txt' )
92
+ fetch ( joinUrlSegments ( base , '/src/subdir/safe.txt' ) )
78
93
. then ( ( r ) => {
79
94
text ( '.safe-fetch-subdir-status' , r . status )
80
95
return r . text ( )
@@ -84,7 +99,12 @@ <h2>Denied</h2>
84
99
} )
85
100
86
101
// inside allowed dir, with special characters, safe fetch
87
- fetch ( '/src/special%20characters%20%C3%A5%C3%A4%C3%B6/safe.txt' )
102
+ fetch (
103
+ joinUrlSegments (
104
+ base ,
105
+ '/src/special%20characters%20%C3%A5%C3%A4%C3%B6/safe.txt' ,
106
+ ) ,
107
+ )
88
108
. then ( ( r ) => {
89
109
text ( '.safe-fetch-subdir-special-characters-status' , r . status )
90
110
return r . text ( )
@@ -94,7 +114,7 @@ <h2>Denied</h2>
94
114
} )
95
115
96
116
// outside of allowed dir, treated as unsafe
97
- fetch ( '/unsafe.txt' )
117
+ fetch ( joinUrlSegments ( base , '/unsafe.txt' ) )
98
118
. then ( ( r ) => {
99
119
text ( '.unsafe-fetch-status' , r . status )
100
120
return r . text ( )
@@ -107,7 +127,7 @@ <h2>Denied</h2>
107
127
} )
108
128
109
129
// outside of allowed dir with special characters #8498
110
- fetch ( '/src/%2e%2e%2funsafe%2etxt' )
130
+ fetch ( joinUrlSegments ( base , '/src/%2e%2e%2funsafe%2etxt' ) )
111
131
. then ( ( r ) => {
112
132
text ( '.unsafe-fetch-8498-status' , r . status )
113
133
return r . text ( )
@@ -120,7 +140,7 @@ <h2>Denied</h2>
120
140
} )
121
141
122
142
// outside of allowed dir with special characters 2 #8498
123
- fetch ( '/src/%252e%252e%252funsafe%252etxt' )
143
+ fetch ( joinUrlSegments ( base , '/src/%252e%252e%252funsafe%252etxt' ) )
124
144
. then ( ( r ) => {
125
145
text ( '.unsafe-fetch-8498-2-status' , r . status )
126
146
return r . text ( )
@@ -133,7 +153,7 @@ <h2>Denied</h2>
133
153
} )
134
154
135
155
// imported before, should be treated as safe
136
- fetch ( '/@fs/' + ROOT + '/safe.json' )
156
+ fetch ( joinUrlSegments ( base , joinUrlSegments ( '/@fs/' , ROOT ) + '/safe.json' ) )
137
157
. then ( ( r ) => {
138
158
text ( '.safe-fs-fetch-status' , r . status )
139
159
return r . json ( )
@@ -143,7 +163,9 @@ <h2>Denied</h2>
143
163
} )
144
164
145
165
// imported before with query, should be treated as safe
146
- fetch ( '/@fs/' + ROOT + '/safe.json?query' )
166
+ fetch (
167
+ joinUrlSegments ( base , joinUrlSegments ( '/@fs/' , ROOT ) + '/safe.json?query' ) ,
168
+ )
147
169
. then ( ( r ) => {
148
170
text ( '.safe-fs-fetch-query-status' , r . status )
149
171
return r . json ( )
@@ -153,7 +175,7 @@ <h2>Denied</h2>
153
175
} )
154
176
155
177
// not imported before, outside of root, treated as unsafe
156
- fetch ( '/@fs/' + ROOT + '/unsafe.json' )
178
+ fetch ( joinUrlSegments ( base , joinUrlSegments ( '/@fs/' , ROOT ) + '/unsafe.json' ) )
157
179
. then ( ( r ) => {
158
180
text ( '.unsafe-fs-fetch-status' , r . status )
159
181
return r . json ( )
@@ -166,7 +188,13 @@ <h2>Denied</h2>
166
188
} )
167
189
168
190
// outside root with special characters #8498
169
- fetch ( '/@fs/' + ROOT + '/root/src/%2e%2e%2f%2e%2e%2funsafe%2ejson' )
191
+ fetch (
192
+ joinUrlSegments (
193
+ base ,
194
+ joinUrlSegments ( '/@fs/' , ROOT ) +
195
+ '/root/src/%2e%2e%2f%2e%2e%2funsafe%2ejson' ,
196
+ ) ,
197
+ )
170
198
. then ( ( r ) => {
171
199
text ( '.unsafe-fs-fetch-8498-status' , r . status )
172
200
return r . json ( )
@@ -177,7 +205,11 @@ <h2>Denied</h2>
177
205
178
206
// outside root with special characters 2 #8498
179
207
fetch (
180
- '/@fs/' + ROOT + '/root/src/%252e%252e%252f%252e%252e%252funsafe%252ejson' ,
208
+ joinUrlSegments (
209
+ base ,
210
+ joinUrlSegments ( '/@fs/' , ROOT ) +
211
+ '/root/src/%252e%252e%252f%252e%252e%252funsafe%252ejson' ,
212
+ ) ,
181
213
)
182
214
. then ( ( r ) => {
183
215
text ( '.unsafe-fs-fetch-8498-2-status' , r . status )
@@ -189,9 +221,11 @@ <h2>Denied</h2>
189
221
190
222
// not imported before, inside root with special characters, treated as safe
191
223
fetch (
192
- '/@fs/' +
193
- ROOT +
194
- '/root/src/special%20characters%20%C3%A5%C3%A4%C3%B6/safe.json' ,
224
+ joinUrlSegments (
225
+ base ,
226
+ joinUrlSegments ( '/@fs/' , ROOT ) +
227
+ '/root/src/special%20characters%20%C3%A5%C3%A4%C3%B6/safe.json' ,
228
+ ) ,
195
229
)
196
230
. then ( ( r ) => {
197
231
text ( '.safe-fs-fetch-special-characters-status' , r . status )
@@ -202,7 +236,7 @@ <h2>Denied</h2>
202
236
} )
203
237
204
238
// .env, denied by default
205
- fetch ( '/@fs/' + ROOT + '/root/.env' )
239
+ fetch ( joinUrlSegments ( base , joinUrlSegments ( '/@fs/' , ROOT ) + '/root/.env' ) )
206
240
. then ( ( r ) => {
207
241
text ( '.unsafe-dotenv' , r . status )
208
242
} )
0 commit comments