Skip to content

Commit 3273766

Browse files
committed
fix(fetch): reject Request body for GET and HEAD methods
1 parent 651acdf commit 3273766

1 file changed

Lines changed: 14 additions & 25 deletions

File tree

core/runtime/src/fetch/request.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,9 @@ impl RequestInit {
4141
request: Option<HttpRequest<Vec<u8>>>,
4242
) -> JsResult<HttpRequest<Vec<u8>>> {
4343
let mut builder = HttpRequest::builder();
44-
let mut is_get_or_head_method = true;
45-
let mut inherited_is_get_or_head_method = true;
4644
let mut inherited_body = None;
47-
let mut request_body: Option<Vec<u8>> = None;
4845
if let Some(r) = request {
4946
let (parts, body) = r.into_parts();
50-
is_get_or_head_method = matches!(parts.method, http::Method::GET | http::Method::HEAD);
51-
inherited_is_get_or_head_method = is_get_or_head_method;
5247
// https://fetch.spec.whatwg.org/#dom-request - "Let inputBody be input's request's body if input is a Request object; otherwise null."
5348
inherited_body = Some(body);
5449
builder = builder
@@ -87,43 +82,37 @@ impl RequestInit {
8782
));
8883
}
8984

90-
is_get_or_head_method =
85+
let is_get_or_head_method =
9186
method.eq_ignore_ascii_case("GET") || method.eq_ignore_ascii_case("HEAD");
9287

88+
// Fetch Standard §5.4 Request constructor:
89+
// If either init["body"] exists and is non-null or inputBody is non-null,
90+
// and request's method is GET or HEAD, then throw a TypeError.
91+
// https://fetch.spec.whatwg.org/#dom-request
92+
if is_get_or_head_method && (self.body.is_some() || inherited_body.is_some()) {
93+
return Err(js_error!(TypeError: "Request with GET/HEAD method cannot have body."));
94+
}
9395
builder = builder.method(method.as_str());
9496
}
9597

96-
// Fetch Standard §5.4 Request constructor:
97-
// If either init["body"] exists and is non-null or inputBody is non-null,
98-
// and request's method is GET or HEAD, then throw a TypeError.
99-
// https://fetch.spec.whatwg.org/#dom-request
100-
if is_get_or_head_method
101-
&& (self.body.is_some()
102-
|| inherited_body
103-
.as_ref()
104-
.is_some_and(|body| !body.is_empty() || !inherited_is_get_or_head_method))
105-
{
106-
return Err(js_error!(TypeError: "Request with GET/HEAD method cannot have body."));
107-
}
108-
109-
if let Some(body) = &self.body {
98+
let request_body = if let Some(body) = &self.body {
11099
// TODO: add more support types.
111100
if let Some(body) = body.as_string() {
112101
let body = body.to_std_string().map_err(
113102
|_| js_error!(TypeError: "Request constructor: body is not a valid string"),
114103
)?;
115-
request_body = Some(body.into_bytes());
104+
body.into_bytes()
116105
} else {
117106
return Err(
118107
js_error!(TypeError: "Request constructor: body is not a supported type"),
119108
);
120109
}
121-
} else if let Some(body) = inherited_body {
122-
request_body = Some(body);
123-
}
110+
} else {
111+
inherited_body.unwrap_or_default()
112+
};
124113

125114
let request = builder
126-
.body(request_body.unwrap_or_default())
115+
.body(request_body)
127116
.map_err(|_| js_error!(Error: "Cannot construct request"))?;
128117
Ok(request)
129118
}

0 commit comments

Comments
 (0)