aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/xmlhttprequest.rs
diff options
context:
space:
mode:
authorHenri Sivonen <hsivonen@hsivonen.fi>2017-11-01 16:50:53 +0200
committerHenri Sivonen <hsivonen@hsivonen.fi>2017-11-02 14:28:51 +0200
commit18a52ea0e9510b60ed7c2b1463e32f5e96e45d73 (patch)
tree883283d6a592a348f1d8e500ade1f74c1e8175c8 /components/script/dom/xmlhttprequest.rs
parentedb2db55b7fd72370a90fc65c5984d6b7e0f792f (diff)
downloadservo-18a52ea0e9510b60ed7c2b1463e32f5e96e45d73.tar.gz
servo-18a52ea0e9510b60ed7c2b1463e32f5e96e45d73.zip
Avoid decoding XHR type="json" responses as UTF-16BE/LE
https://infra.spec.whatwg.org/#parse-json-from-bytes says to use "UTF-8 decode" rather than "decode", so UTF-16BE/LE BOM should not be honored.
Diffstat (limited to 'components/script/dom/xmlhttprequest.rs')
-rw-r--r--components/script/dom/xmlhttprequest.rs11
1 files changed, 8 insertions, 3 deletions
diff --git a/components/script/dom/xmlhttprequest.rs b/components/script/dom/xmlhttprequest.rs
index a20d8c90fb7..81133ea6bc9 100644
--- a/components/script/dom/xmlhttprequest.rs
+++ b/components/script/dom/xmlhttprequest.rs
@@ -1164,8 +1164,8 @@ impl XMLHttpRequest {
return NullValue();
}
// Step 4
- fn decode_to_utf16(bytes: &[u8], encoding: &'static Encoding) -> Vec<u16> {
- let mut decoder = encoding.new_decoder();
+ fn decode_to_utf16_with_bom_removal(bytes: &[u8], encoding: &'static Encoding) -> Vec<u16> {
+ let mut decoder = encoding.new_decoder_with_bom_removal();
let capacity = decoder.max_utf16_buffer_length(bytes.len()).expect("Overflow");
let mut utf16 = Vec::with_capacity(capacity);
let extra = unsafe {
@@ -1179,7 +1179,12 @@ impl XMLHttpRequest {
}
utf16
}
- let json_text = decode_to_utf16(&bytes, UTF_8);
+ // https://xhr.spec.whatwg.org/#json-response refers to
+ // https://infra.spec.whatwg.org/#parse-json-from-bytes which refers to
+ // https://encoding.spec.whatwg.org/#utf-8-decode which means
+ // that the encoding is always UTF-8 and the UTF-8 BOM is removed,
+ // if present, but UTF-16BE/LE BOM must not be honored.
+ let json_text = decode_to_utf16_with_bom_removal(&bytes, UTF_8);
// Step 5
rooted!(in(cx) let mut rval = UndefinedValue());
unsafe {