summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_proxy_authorization_via_proxyinfo.js
blob: 27f537b73bf36da8d66b819c020e574be865ad52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"use strict";

XPCOMUtils.defineLazyServiceGetter(
  this,
  "authManager",
  "@mozilla.org/network/http-auth-manager;1",
  "nsIHttpAuthManager"
);

const proxy = createHttpServer();
const proxyToken = "this_is_my_pass";

// accept proxy connections for mozilla.org
proxy.identity.add("http", "mozilla.org", 80);

proxy.registerPathHandler("/", (request, response) => {
  if (request.hasHeader("Proxy-Authorization")) {
    response.setStatusLine(request.httpVersion, 200, "OK");
    response.setHeader("Content-Type", "text/plain", false);
    response.write(request.getHeader("Proxy-Authorization"));
  } else {
    response.setStatusLine(
      request.httpVersion,
      407,
      "Proxy authentication required"
    );
    response.setHeader("Content-Type", "text/plain", false);
    response.setHeader("Proxy-Authenticate", "UnknownMeantToFail", false);
    response.write("auth required");
  }
});

function getExtension(background) {
  return ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["proxy", "webRequest", "webRequestBlocking", "<all_urls>"],
    },
    background: `(${background})(${proxy.identity.primaryPort}, "${proxyToken}")`,
  });
}

add_task(async function test_webRequest_auth_proxy() {
  function background(port, proxyToken) {
    browser.webRequest.onCompleted.addListener(
      details => {
        browser.test.log(`onCompleted ${JSON.stringify(details)}\n`);
        browser.test.assertEq(
          "localhost",
          details.proxyInfo.host,
          "proxy host"
        );
        browser.test.assertEq(port, details.proxyInfo.port, "proxy port");
        browser.test.assertEq("http", details.proxyInfo.type, "proxy type");
        browser.test.assertEq(
          "",
          details.proxyInfo.username,
          "proxy username not set"
        );
        browser.test.assertEq(
          proxyToken,
          details.proxyInfo.proxyAuthorizationHeader,
          "proxy authorization header"
        );
        browser.test.assertEq(
          proxyToken,
          details.proxyInfo.connectionIsolationKey,
          "proxy connection isolation"
        );

        browser.test.notifyPass("requestCompleted");
      },
      { urls: ["<all_urls>"] }
    );

    browser.webRequest.onAuthRequired.addListener(
      details => {
        // Using proxyAuthorizationHeader should prevent an auth request coming to us in the extension.
        browser.test.fail("onAuthRequired");
      },
      { urls: ["<all_urls>"] },
      ["blocking"]
    );

    // Handle the proxy request.
    browser.proxy.onRequest.addListener(
      details => {
        browser.test.log(`onRequest ${JSON.stringify(details)}`);
        return [
          {
            host: "localhost",
            port,
            type: "http",
            proxyAuthorizationHeader: proxyToken,
            connectionIsolationKey: proxyToken,
          },
        ];
      },
      { urls: ["<all_urls>"] },
      ["requestHeaders"]
    );
  }

  let extension = getExtension(background);

  await extension.startup();

  authManager.clearAll();

  let contentPage = await ExtensionTestUtils.loadContentPage(
    `http://mozilla.org/`
  );

  await extension.awaitFinish("requestCompleted");
  await contentPage.close();
  await extension.unload();
});