tor-browser

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

non-local-ports.sub.window.js (3289B)


      1 // Verifies that non-local HTTP(S) ports are open and serve correctly.
      2 //
      3 // See the corresponding WPT RFC:
      4 // https://github.com/web-platform-tests/rfcs/blob/master/rfcs/address_space_overrides.md
      5 //
      6 // These ports are used to test the Local Network Access specification:
      7 // https://github.com/explainers-by-googlers/local-network-access
      8 //
      9 // More tests can be found in `fetch/local-network-access/`.
     10 
     11 const alternatePorts = {
     12  httpLocal:  "{{ports[http-local][0]}}",
     13  httpsLocal: "{{ports[https-local][0]}}",
     14  httpPublic:   "{{ports[http-public][0]}}",
     15  httpsPublic:  "{{ports[https-public][0]}}",
     16 };
     17 
     18 // Resolves a URL relative to the current location, returning an absolute URL.
     19 //
     20 // `url` specifies the relative URL, e.g. "foo.html" or "http://foo.example".
     21 // `options.protocol` and `options.port`, if defined, override the respective
     22 // properties of the returned URL object.
     23 function resolveUrl(url, options) {
     24  const result = new URL(url, window.location);
     25  if (options === undefined) {
     26    return result;
     27  }
     28 
     29  const { port, protocol } = options;
     30  if (port !== undefined) {
     31    result.port = port;
     32  }
     33  if (protocol !== undefined) {
     34    result.protocol = protocol;
     35  }
     36 
     37  return result;
     38 }
     39 
     40 const alternateOrigins = {
     41  httpLocal: {
     42    protocol: "http:",
     43    port: alternatePorts.httpLocal,
     44  },
     45  httpsLocal: {
     46    protocol: "https:",
     47    port: alternatePorts.httpsLocal,
     48  },
     49  httpPublic: {
     50    protocol: "http:",
     51    port: alternatePorts.httpPublic,
     52  },
     53  httpsPublic: {
     54    protocol: "https:",
     55    port: alternatePorts.httpsPublic,
     56  },
     57 };
     58 
     59 promise_test(async () => {
     60  const url =
     61    resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpsLocal);
     62  const response = await fetch(url);
     63  assert_true(response.ok);
     64 }, "Fetch from https-local port works.");
     65 
     66 promise_test(async () => {
     67  const url =
     68    resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpLocal);
     69  const response = await fetch(url);
     70  assert_true(response.ok);
     71 }, "Fetch from http-local port works.");
     72 
     73 promise_test(async () => {
     74  const url =
     75    resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpsPublic);
     76  const response = await fetch(url);
     77  assert_true(response.ok);
     78 }, "Fetch from https-public port works.");
     79 
     80 promise_test(async () => {
     81  const url =
     82    resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpPublic);
     83  const response = await fetch(url);
     84  assert_true(response.ok);
     85 }, "Fetch from http-public port works.");
     86 
     87 promise_test(async (t) => {
     88  const futureMessage = new Promise((resolve) => {
     89    window.addEventListener("message", resolve);
     90  });
     91 
     92  const iframe = await new Promise((resolve, reject) => {
     93    const iframe = document.createElement("iframe");
     94    iframe.src = resolveUrl("resources/fetch-and-post-result.html",
     95                            alternateOrigins.httpPublic);
     96 
     97    iframe.onload = () => { resolve(iframe); };
     98    iframe.onerror = reject;
     99 
    100    document.body.appendChild(iframe);
    101    t.add_cleanup(() => {
    102      document.body.removeChild(iframe);
    103    });
    104  });
    105 
    106  iframe.contentWindow.postMessage(
    107    resolveUrl("/common/blank-with-cors.html").toString(), "*");
    108 
    109  const evt = await futureMessage;
    110  assert_equals(evt.data, "failure: error = TypeError");
    111 }, "Fetch from http-public to local http fails.");