tor-browser

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

browser_net_headers-proxy.js (6255B)


      1 /* Any copyright is dedicated to the Public Domain.
      2  http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that the proxy information is displayed in the netmonitor
      7 add_task(async function () {
      8  const { monitor } = await initNetMonitor(HTTPS_CUSTOM_GET_URL, {
      9    requestCount: 1,
     10  });
     11  info("Starting test... ");
     12 
     13  const { document } = monitor.panelWin;
     14 
     15  const wait = waitForNetworkEvents(monitor, 1);
     16  await reloadBrowser();
     17  await wait;
     18 
     19  // Wait until the tab panel summary is displayed
     20  const waitForTab = waitUntil(() =>
     21    document.querySelector(".tabpanel-summary-label")
     22  );
     23  EventUtils.sendMouseEvent(
     24    { type: "mousedown" },
     25    document.querySelector(".request-list-item")
     26  );
     27  await waitForTab;
     28 
     29  // Expand preview
     30  await toggleUrlPreview(true, monitor);
     31 
     32  const proxyAddressEl = [...document.querySelectorAll(".treeRow")].find(
     33    el => el.querySelector(".treeLabelCell")?.textContent === "Proxy Address"
     34  );
     35 
     36  is(
     37    proxyAddressEl.querySelector(".treeValueCell").innerText,
     38    "127.0.0.1:4443",
     39    "The remote proxy address summary value is correct."
     40  );
     41 
     42  is(
     43    document.querySelector(".headers-proxy-status .headers-summary-label")
     44      .textContent,
     45    "Proxy Status",
     46    "The proxy status header is displayed"
     47  );
     48 
     49  is(
     50    document.querySelector(".headers-proxy-status .tabpanel-summary-value")
     51      .textContent,
     52    "200Connected",
     53    "The proxy status value showed correctly"
     54  );
     55 
     56  is(
     57    document.querySelector(".headers-proxy-version .tabpanel-summary-label")
     58      .textContent,
     59    "Proxy Version",
     60    "The proxy http version header is displayed"
     61  );
     62 
     63  is(
     64    document.querySelector(".headers-proxy-version .tabpanel-summary-value")
     65      .textContent,
     66    "HTTP/1.1",
     67    "The proxy http version value showed correctly"
     68  );
     69 
     70  await teardown(monitor);
     71 });
     72 
     73 const noProxyServerUrl = createTestHTTPServer();
     74 noProxyServerUrl.registerPathHandler("/index.html", (request, response) => {
     75  response.setStatusLine(request.httpVersion, 200, "OK");
     76  response.setHeader("Content-Type", "text/html", true);
     77  response.write("<html> SIMPLE DOCUMENT </html>");
     78 });
     79 
     80 const NO_PROXY_SERVER_URL = `http://localhost:${noProxyServerUrl.identity.primaryPort}`;
     81 
     82 // Test that the proxy information is not displayed in the netmonitor
     83 add_task(async function () {
     84  const { monitor } = await initNetMonitor(NO_PROXY_SERVER_URL, {
     85    requestCount: 1,
     86  });
     87  info("Starting test... ");
     88 
     89  const { document } = monitor.panelWin;
     90 
     91  const wait = waitForNetworkEvents(monitor, 1);
     92  await reloadBrowser();
     93  await wait;
     94 
     95  // Wait until the tab panel summary is displayed
     96  const waitForTab = waitUntil(() =>
     97    document.querySelector(".tabpanel-summary-label")
     98  );
     99  EventUtils.sendMouseEvent(
    100    { type: "mousedown" },
    101    document.querySelector(".request-list-item")
    102  );
    103  await waitForTab;
    104 
    105  // Expand preview
    106  await toggleUrlPreview(true, monitor);
    107 
    108  const addressEl = [...document.querySelectorAll(".treeRow")].find(
    109    el => el.querySelector(".treeLabelCell")?.textContent === "Address"
    110  );
    111 
    112  ok(addressEl, "The address is not the proxy address");
    113 
    114  ok(
    115    !document.querySelector(".headers-proxy-status"),
    116    "The proxy status header is not displayed"
    117  );
    118 
    119  ok(
    120    !document.querySelector(".headers-proxy-version"),
    121    "The proxy http version header is not displayed"
    122  );
    123 
    124  await teardown(monitor);
    125 });
    126 
    127 const serverBehindFakeProxy = createTestHTTPServer();
    128 const fakeProxy = createTestHTTPServer();
    129 
    130 fakeProxy.identity.add(
    131  "http",
    132  "localhost",
    133  serverBehindFakeProxy.identity.primaryPort
    134 );
    135 fakeProxy.registerPrefixHandler("/", (request, response) => {
    136  if (request.hasHeader("Proxy-Authorization")) {
    137    response.setStatusLine(request.httpVersion, 200, "OK");
    138    response.setHeader("Content-Type", "text/html", true);
    139    response.write("ok, got proxy auth");
    140  } else {
    141    response.setStatusLine(
    142      request.httpVersion,
    143      407,
    144      "Proxy authentication required"
    145    );
    146    response.setHeader("Content-Type", "text/plain", false);
    147    response.setHeader("Proxy-Authenticate", 'Basic realm="foobar"', false);
    148    response.write("auth required");
    149  }
    150 });
    151 
    152 const SERVER_URL = `http://localhost:${serverBehindFakeProxy.identity.primaryPort}`;
    153 
    154 // Test that `Proxy-Authorization` request header is not shown for in the headers panel
    155 add_task(async function () {
    156  await pushPref("network.proxy.type", 1);
    157  await pushPref("network.proxy.http", "localhost");
    158  await pushPref("network.proxy.http_port", fakeProxy.identity.primaryPort);
    159  await pushPref("network.proxy.allow_hijacking_localhost", true);
    160 
    161  // Wait for initial primary password dialog after opening the tab.
    162  const onDialog = TestUtils.topicObserved("common-dialog-loaded");
    163 
    164  const tab = await addTab(SERVER_URL, { waitForLoad: false });
    165 
    166  const toolbox = await gDevTools.showToolboxForTab(tab, {
    167    toolId: "netmonitor",
    168  });
    169  info("Network monitor pane shown successfully.");
    170 
    171  const monitor = toolbox.getCurrentPanel();
    172  const { document, store, windowRequire } = monitor.panelWin;
    173  const Actions = windowRequire("devtools/client/netmonitor/src/actions/index");
    174  store.dispatch(Actions.batchEnable(false));
    175 
    176  const [subject] = await onDialog;
    177  const dialog = subject.Dialog;
    178 
    179  ok(true, `Authentication dialog displayed`);
    180 
    181  info("Fill in login and password, and validate dialog");
    182  dialog.ui.loginTextbox.value = "user";
    183  dialog.ui.password1Textbox.value = "pass";
    184 
    185  const onDialogClosed = BrowserTestUtils.waitForEvent(
    186    window,
    187    "DOMModalDialogClosed"
    188  );
    189  dialog.ui.button0.click();
    190  await onDialogClosed;
    191  ok(true, "Dialog is closed");
    192 
    193  const requestEl = await waitFor(() =>
    194    document.querySelector(".request-list-item")
    195  );
    196  EventUtils.sendMouseEvent({ type: "mousedown" }, requestEl);
    197 
    198  await waitUntil(() => document.querySelector(".headers-overview"));
    199 
    200  const headersPanel = document.querySelector("#headers-panel");
    201  const headerIsFound = [
    202    ...headersPanel.querySelectorAll("tr .treeLabelCell .treeLabel"),
    203  ].some(headerEl => headerEl.innerText == "Proxy-Authorization");
    204 
    205  ok(
    206    !headerIsFound,
    207    "The `Proxy-Authorization` should not be displayed in the Headers panel"
    208  );
    209 
    210  await teardown(monitor);
    211 });