tor-browser

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

test_bug412457.js (2445B)


      1 "use strict";
      2 
      3 function run_test() {
      4  // check if hostname is unescaped before applying IDNA
      5  var newURI = Services.io.newURI("http://\u5341%2ecom/");
      6  Assert.equal(newURI.asciiHost, "xn--kkr.com");
      7 
      8  // escaped UTF8
      9  newURI = newURI.mutate().setSpec("http://%e5%8d%81.com").finalize();
     10  Assert.equal(newURI.asciiHost, "xn--kkr.com");
     11 
     12  // There should be only allowed characters in hostname after
     13  // unescaping and attempting to apply IDNA. "\x80" is illegal in
     14  // UTF-8, so IDNA fails, and 0x80 is illegal in DNS too.
     15  Assert.throws(
     16    () => {
     17      newURI = newURI.mutate().setSpec("http://%80.com").finalize();
     18    },
     19    /NS_ERROR_MALFORMED_URI/,
     20    "illegal UTF character"
     21  );
     22 
     23  // test parsing URL with all possible host terminators
     24  newURI = newURI.mutate().setSpec("http://example.com?foo").finalize();
     25  Assert.equal(newURI.asciiHost, "example.com");
     26 
     27  newURI = newURI.mutate().setSpec("http://example.com#foo").finalize();
     28  Assert.equal(newURI.asciiHost, "example.com");
     29 
     30  newURI = newURI.mutate().setSpec("http://example.com:80").finalize();
     31  Assert.equal(newURI.asciiHost, "example.com");
     32 
     33  newURI = newURI.mutate().setSpec("http://example.com/foo").finalize();
     34  Assert.equal(newURI.asciiHost, "example.com");
     35 
     36  // Test an intuitively weird character that is not actually on the
     37  // forbidden domain code point list in the WHATWG URL Standard.
     38  newURI = newURI.mutate().setSpec("http://example.com%3bfoo").finalize();
     39  Assert.equal(newURI.asciiHost, "example.com;foo");
     40 
     41  // Characters that are invalid in the host
     42  Assert.throws(
     43    () => {
     44      newURI = newURI.mutate().setSpec("http://example.com%3ffoo").finalize();
     45    },
     46    /NS_ERROR_MALFORMED_URI/,
     47    "bad escaped character"
     48  );
     49  Assert.throws(
     50    () => {
     51      newURI = newURI.mutate().setSpec("http://example.com%23foo").finalize();
     52    },
     53    /NS_ERROR_MALFORMED_URI/,
     54    "bad escaped character"
     55  );
     56  Assert.throws(
     57    () => {
     58      newURI = newURI.mutate().setSpec("http://example.com%3a80").finalize();
     59    },
     60    /NS_ERROR_MALFORMED_URI/,
     61    "bad escaped character"
     62  );
     63  Assert.throws(
     64    () => {
     65      newURI = newURI.mutate().setSpec("http://example.com%2ffoo").finalize();
     66    },
     67    /NS_ERROR_MALFORMED_URI/,
     68    "bad escaped character"
     69  );
     70  Assert.throws(
     71    () => {
     72      newURI = newURI.mutate().setSpec("http://example.com%00").finalize();
     73    },
     74    /NS_ERROR_MALFORMED_URI/,
     75    "bad escaped character"
     76  );
     77 }