aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/xmlhttprequest.rs
diff options
context:
space:
mode:
authorManish Goregaokar <manishsmail@gmail.com>2019-03-13 15:21:34 -0700
committerManish Goregaokar <manishsmail@gmail.com>2019-03-13 23:03:45 -0700
commit79a565930b5e876bdfb6cbad5bcf1c3cf8750a97 (patch)
tree110f083ebaedd579617b8e165eeaf48ed49c4e4d /components/script/dom/xmlhttprequest.rs
parentf340a5bf29bd3508c0cf6021b3c9cbc9b4cef582 (diff)
downloadservo-79a565930b5e876bdfb6cbad5bcf1c3cf8750a97.tar.gz
servo-79a565930b5e876bdfb6cbad5bcf1c3cf8750a97.zip
xhr: Reserve content-length worth of space in the response if possible
Diffstat (limited to 'components/script/dom/xmlhttprequest.rs')
-rw-r--r--components/script/dom/xmlhttprequest.rs19
1 files changed, 18 insertions, 1 deletions
diff --git a/components/script/dom/xmlhttprequest.rs b/components/script/dom/xmlhttprequest.rs
index 0b2fcdb2837..b8d6002c1d8 100644
--- a/components/script/dom/xmlhttprequest.rs
+++ b/components/script/dom/xmlhttprequest.rs
@@ -70,6 +70,7 @@ use servo_atoms::Atom;
use servo_url::ServoUrl;
use std::borrow::ToOwned;
use std::cell::Cell;
+use std::cmp;
use std::default::Default;
use std::ptr;
use std::ptr::NonNull;
@@ -1073,7 +1074,23 @@ impl XMLHttpRequest {
headers
.as_ref()
.map(|h| *self.response_headers.borrow_mut() = h.clone());
-
+ {
+ let len = headers.and_then(|h| h.typed_get::<ContentLength>());
+ let mut response = self.response.borrow_mut();
+ response.clear();
+ if let Some(len) = len {
+ // don't attempt to prereserve more than 4 MB of memory,
+ // to avoid giving servers the ability to DOS the client by
+ // providing arbitrarily large content-lengths.
+ //
+ // this number is arbitrary, it's basically big enough that most
+ // XHR requests won't hit it, but not so big that it allows for DOS
+ let size = cmp::min(0b100_0000000000_0000000000, len.0 as usize);
+
+ // preallocate the buffer
+ response.reserve(size);
+ }
+ }
// Substep 3
if !self.sync.get() {
self.change_ready_state(XMLHttpRequestState::HeadersReceived);