Skip to content

Commit 008912b

Browse files
Filter out invalid header names when initializing Headers instances (#196)
This also fixes small bugs in how requests are created in fetch and what Response#type returns. Together, these fix a bunch of WPT tests.
1 parent 58ae515 commit 008912b

7 files changed

Lines changed: 59 additions & 38 deletions

File tree

builtins/web/fetch/fetch-api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ bool fetch(JSContext *cx, unsigned argc, Value *vp) {
3434
}
3535

3636
if (!Request::initialize(cx, request_obj, args[0], args.get(1),
37-
Headers::HeadersGuard::Immutable)) {
37+
Headers::HeadersGuard::Request)) {
3838
return ReturnPromiseRejectedWithPendingError(cx, args);
3939
}
4040

builtins/web/fetch/headers.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,14 @@ bool Headers::delete_(JSContext *cx, unsigned argc, JS::Value *vp) {
967967
bool Headers::append_valid_header(JSContext *cx, JS::HandleObject self,
968968
host_api::HostString valid_key, JS::HandleValue value,
969969
const char *fun_name) {
970+
bool is_valid;
971+
if (!validate_guard(cx, self, valid_key, "Headers constructor", &is_valid)) {
972+
return false;
973+
}
974+
if (!is_valid) {
975+
return true;
976+
}
977+
970978
auto value_chars = normalize_and_validate_header_value(cx, value, fun_name);
971979
if (!value_chars.ptr)
972980
return false;

builtins/web/fetch/request-response.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2045,13 +2045,25 @@ bool Response::url_get(JSContext *cx, unsigned argc, JS::Value *vp) {
20452045

20462046
namespace {
20472047
JSString *type_default_atom;
2048+
JSString *type_basic_atom;
20482049
JSString *type_error_atom;
20492050
} // namespace
20502051

20512052
bool Response::type_get(JSContext *cx, unsigned argc, JS::Value *vp) {
20522053
METHOD_HEADER(0)
20532054

2054-
args.rval().setString(status(self) == 0 ? type_error_atom : type_default_atom);
2055+
// Per the fetch spec, `type` is one of "basic", "cors", "default", "error", or "opaque".
2056+
// Of those, "error" is always returned if `status` is 0.
2057+
// Otherwise, "basic" is returned for incoming responses, and "default" for outgoing responses.
2058+
// Note that we don't implement the parts of the spec that'd allow "cors" or "opaque" to be
2059+
// returned.
2060+
JSString* type = type_default_atom;
2061+
if (status(self) == 0) {
2062+
type = type_error_atom;
2063+
} else if (RequestOrResponse::is_incoming(self)) {
2064+
type = type_basic_atom;
2065+
}
2066+
args.rval().setString(type);
20552067
return true;
20562068
}
20572069

@@ -2471,6 +2483,7 @@ bool Response::init_class(JSContext *cx, JS::HandleObject global) {
24712483
// Initialize a pinned (i.e., never-moved, living forever) atom for the
24722484
// response type values.
24732485
return (type_default_atom = JS_AtomizeAndPinString(cx, "default")) &&
2486+
(type_basic_atom = JS_AtomizeAndPinString(cx, "basic")) &&
24742487
(type_error_atom = JS_AtomizeAndPinString(cx, "error"));
24752488
}
24762489

tests/wpt-harness/expectations/fetch/api/basic/accept-header.any.js.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
"status": "FAIL"
44
},
55
"Request through fetch should have 'accept' header with value 'custom/*'": {
6-
"status": "FAIL"
6+
"status": "PASS"
77
},
88
"Request through fetch should have a 'accept-language' header": {
99
"status": "FAIL"
1010
},
1111
"Request through fetch should have 'accept-language' header with value 'bzh'": {
12-
"status": "FAIL"
12+
"status": "PASS"
1313
}
1414
}

tests/wpt-harness/expectations/fetch/api/basic/mode-same-origin.any.js.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"Fetch ../resources/top.txt with same-origin mode": {
3-
"status": "FAIL"
3+
"status": "PASS"
44
},
55
"Fetch http://web-platform.test:8000/fetch/api/resources/top.txt with same-origin mode": {
6-
"status": "FAIL"
6+
"status": "PASS"
77
},
88
"Fetch https://web-platform.test:8443/fetch/api/resources/top.txt with same-origin mode": {
99
"status": "PASS"

tests/wpt-harness/expectations/fetch/api/basic/request-forbidden-headers.any.js.json

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"status": "FAIL"
3434
},
3535
"Host is a forbidden request header": {
36-
"status": "FAIL"
36+
"status": "PASS"
3737
},
3838
"Keep-Alive is a forbidden request header": {
3939
"status": "FAIL"
@@ -198,93 +198,93 @@
198198
"status": "FAIL"
199199
},
200200
"header x-http-method-override is forbidden to use value GET,track ": {
201-
"status": "FAIL"
201+
"status": "PASS"
202202
},
203203
"header x-http-method is forbidden to use value GET,track ": {
204-
"status": "FAIL"
204+
"status": "PASS"
205205
},
206206
"header x-method-override is forbidden to use value GET,track ": {
207-
"status": "FAIL"
207+
"status": "PASS"
208208
},
209209
"header X-HTTP-METHOD-OVERRIDE is forbidden to use value GET,track ": {
210-
"status": "FAIL"
210+
"status": "PASS"
211211
},
212212
"header X-HTTP-METHOD is forbidden to use value GET,track ": {
213-
"status": "FAIL"
213+
"status": "PASS"
214214
},
215215
"header X-METHOD-OVERRIDE is forbidden to use value GET,track ": {
216-
"status": "FAIL"
216+
"status": "PASS"
217217
},
218218
"header x-http-method-override is forbidden to use value connect": {
219-
"status": "FAIL"
219+
"status": "PASS"
220220
},
221221
"header x-http-method is forbidden to use value connect": {
222-
"status": "FAIL"
222+
"status": "PASS"
223223
},
224224
"header x-method-override is forbidden to use value connect": {
225-
"status": "FAIL"
225+
"status": "PASS"
226226
},
227227
"header X-HTTP-METHOD-OVERRIDE is forbidden to use value connect": {
228-
"status": "FAIL"
228+
"status": "PASS"
229229
},
230230
"header X-HTTP-METHOD is forbidden to use value connect": {
231-
"status": "FAIL"
231+
"status": "PASS"
232232
},
233233
"header X-METHOD-OVERRIDE is forbidden to use value connect": {
234-
"status": "FAIL"
234+
"status": "PASS"
235235
},
236236
"header x-http-method-override is allowed to use value GETTRACE": {
237-
"status": "FAIL"
237+
"status": "PASS"
238238
},
239239
"header x-http-method is allowed to use value GETTRACE": {
240-
"status": "FAIL"
240+
"status": "PASS"
241241
},
242242
"header x-method-override is allowed to use value GETTRACE": {
243-
"status": "FAIL"
243+
"status": "PASS"
244244
},
245245
"header X-HTTP-METHOD-OVERRIDE is allowed to use value GETTRACE": {
246-
"status": "FAIL"
246+
"status": "PASS"
247247
},
248248
"header X-HTTP-METHOD is allowed to use value GETTRACE": {
249-
"status": "FAIL"
249+
"status": "PASS"
250250
},
251251
"header X-METHOD-OVERRIDE is allowed to use value GETTRACE": {
252-
"status": "FAIL"
252+
"status": "PASS"
253253
},
254254
"header x-http-method-override is allowed to use value GET": {
255-
"status": "FAIL"
255+
"status": "PASS"
256256
},
257257
"header x-http-method is allowed to use value GET": {
258-
"status": "FAIL"
258+
"status": "PASS"
259259
},
260260
"header x-method-override is allowed to use value GET": {
261-
"status": "FAIL"
261+
"status": "PASS"
262262
},
263263
"header X-HTTP-METHOD-OVERRIDE is allowed to use value GET": {
264-
"status": "FAIL"
264+
"status": "PASS"
265265
},
266266
"header X-HTTP-METHOD is allowed to use value GET": {
267-
"status": "FAIL"
267+
"status": "PASS"
268268
},
269269
"header X-METHOD-OVERRIDE is allowed to use value GET": {
270-
"status": "FAIL"
270+
"status": "PASS"
271271
},
272272
"header x-http-method-override is allowed to use value \",TRACE\",": {
273-
"status": "FAIL"
273+
"status": "PASS"
274274
},
275275
"header x-http-method is allowed to use value \",TRACE\",": {
276-
"status": "FAIL"
276+
"status": "PASS"
277277
},
278278
"header x-method-override is allowed to use value \",TRACE\",": {
279-
"status": "FAIL"
279+
"status": "PASS"
280280
},
281281
"header X-HTTP-METHOD-OVERRIDE is allowed to use value \",TRACE\",": {
282-
"status": "FAIL"
282+
"status": "PASS"
283283
},
284284
"header X-HTTP-METHOD is allowed to use value \",TRACE\",": {
285-
"status": "FAIL"
285+
"status": "PASS"
286286
},
287287
"header X-METHOD-OVERRIDE is allowed to use value \",TRACE\",": {
288-
"status": "FAIL"
288+
"status": "PASS"
289289
}
290290
}

tests/wpt-harness/expectations/fetch/api/basic/request-headers.any.js.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"status": "FAIL"
5858
},
5959
"Fetch with GET and mode \"cors\" does not need an Origin header": {
60-
"status": "FAIL"
60+
"status": "PASS"
6161
},
6262
"Fetch with POST and mode \"same-origin\" needs an Origin header": {
6363
"status": "FAIL"

0 commit comments

Comments
 (0)