tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

send-data-string-invalid-unicode.any.js (1761B)


      1 // META: title=XMLHttpRequest.send(invalidUnicodeString)
      2 
      3 const LEFT_SURROGATE = '\ud83d';
      4 const RIGHT_SURROGATE = '\udc94';
      5 
      6 // Unmatched surrogates should be replaced with the unicode replacement
      7 // character, 0xFFFD. '$' in these templates is replaced with one of
      8 // LEFT_SURROGATE or RIGHT_SURROGATE according to the test.
      9 const TEMPLATES = {
     10  '$': [239, 191, 189],
     11  '$ab': [239, 191, 189, 97, 98],
     12  'a$b': [97, 239, 191, 189, 98],
     13  'ab$': [97, 98, 239, 191, 189],
     14 };
     15 
     16 for (const surrogate of [LEFT_SURROGATE, RIGHT_SURROGATE]) {
     17  for (const [template, expected] of Object.entries(TEMPLATES)) {
     18    const invalidString = template.replace('$', surrogate);
     19    const printableString = template.replace(
     20        '$', '\\u{' + surrogate.charCodeAt(0).toString(16) + '}');
     21    async_test(t => {
     22      xhrSendStringAndCheckResponseBody(t, invalidString, expected);
     23    }, `invalid unicode '${printableString}' should be fixed with ` +
     24               `replacement character`);
     25  }
     26 }
     27 
     28 // For the sake of completeness, verify that matched surrogates work.
     29 async_test(t => {
     30  xhrSendStringAndCheckResponseBody(t, LEFT_SURROGATE + RIGHT_SURROGATE,
     31                                    [240, 159, 146, 148]);
     32 }, 'valid unicode should be sent correctly');
     33 
     34 function xhrSendStringAndCheckResponseBody(t, string, expected) {
     35  const xhr = new XMLHttpRequest();
     36  xhr.responseType = 'arraybuffer';
     37  xhr.onload = t.step_func(() => {
     38    assert_equals(xhr.status, 200, 'status should be 200');
     39    const actualBody = new Uint8Array(xhr.response);
     40    assert_array_equals(actualBody, expected, 'content should match');
     41    t.done();
     42  });
     43  xhr.onerror = t.unreached_func('no error should occur');
     44  xhr.open('POST', 'resources/content.py', true);
     45  xhr.send(string);
     46 }