mirror of
https://github.com/denoland/deno.git
synced 2025-10-03 15:44:36 +00:00
fix(op_crates/fetch): correct regexp for fetch header (#8927)
Fix bug in regular expression and make the regular expression more strict. In a string passed to new RegExp(), '[\t\s]' is identical to '[ts]' and not `/[\t\s]/`. For that, the backslash needs to be escaped in the string. Futhermore, `\t` is the tab character and is included in the special regexp value `\s` so is unnecessary. That would reduce the RegExp to new RegExp(`^${value}\\s*;?`) but there's no point in matching 0 or more space characters followed by 0 or one semi-colons as that will match no matter what follows `value`. To make it more strict, require one of space, semicolon, or end-of-string after value.
This commit is contained in:
parent
22e0ee92a6
commit
e568ddf996
3 changed files with 41 additions and 1 deletions
|
@ -249,6 +249,25 @@ unitTest(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
unitTest(
|
||||||
|
{ perms: { net: true } },
|
||||||
|
async function fetchMultipartFormBadContentType(): Promise<void> {
|
||||||
|
const response = await fetch(
|
||||||
|
"http://localhost:4545/multipart_form_bad_content_type",
|
||||||
|
);
|
||||||
|
assert(response.body !== null);
|
||||||
|
|
||||||
|
await assertThrowsAsync(
|
||||||
|
async (): Promise<void> => {
|
||||||
|
await response.formData();
|
||||||
|
},
|
||||||
|
TypeError,
|
||||||
|
"Invalid form data",
|
||||||
|
);
|
||||||
|
await response.body.cancel();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
unitTest(
|
unitTest(
|
||||||
{ perms: { net: true } },
|
{ perms: { net: true } },
|
||||||
async function fetchURLEncodedFormDataSuccess(): Promise<void> {
|
async function fetchURLEncodedFormDataSuccess(): Promise<void> {
|
||||||
|
|
|
@ -138,7 +138,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasHeaderValueOf(s, value) {
|
function hasHeaderValueOf(s, value) {
|
||||||
return new RegExp(`^${value}[\t\s]*;?`).test(s);
|
return new RegExp(`^${value}(?:[\\s;]|$)`).test(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHeaderValueParams(value) {
|
function getHeaderValueParams(value) {
|
||||||
|
|
|
@ -391,6 +391,27 @@ async fn main_server(req: Request<Body>) -> hyper::Result<Response<Body>> {
|
||||||
);
|
);
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
(_, "/multipart_form_bad_content_type") => {
|
||||||
|
let b = "Preamble\r\n\
|
||||||
|
--boundary\t \r\n\
|
||||||
|
Content-Disposition: form-data; name=\"field_1\"\r\n\
|
||||||
|
\r\n\
|
||||||
|
value_1 \r\n\
|
||||||
|
\r\n--boundary\r\n\
|
||||||
|
Content-Disposition: form-data; name=\"field_2\";\
|
||||||
|
filename=\"file.js\"\r\n\
|
||||||
|
Content-Type: text/javascript\r\n\
|
||||||
|
\r\n\
|
||||||
|
console.log(\"Hi\")\
|
||||||
|
\r\n--boundary--\r\n\
|
||||||
|
Epilogue";
|
||||||
|
let mut res = Response::new(Body::from(b));
|
||||||
|
res.headers_mut().insert(
|
||||||
|
"content-type",
|
||||||
|
HeaderValue::from_static("multipart/form-datatststs;boundary=boundary"),
|
||||||
|
);
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
(_, "/bad_redirect") => {
|
(_, "/bad_redirect") => {
|
||||||
let mut res = Response::new(Body::empty());
|
let mut res = Response::new(Body::empty());
|
||||||
*res.status_mut() = StatusCode::FOUND;
|
*res.status_mut() = StatusCode::FOUND;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue