summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_auth_proxy.js
blob: 35201961ec27ea2f0d57efe831c7414a25ee7ce3 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * This tests the automatic login to the proxy with password,
 * if the password is stored and the browser is restarted.
 *
 * <copied from="test_authentication.js"/>
 */

"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

const FLAG_RETURN_FALSE = 1 << 0;
const FLAG_WRONG_PASSWORD = 1 << 1;
const FLAG_PREVIOUS_FAILED = 1 << 2;

function AuthPrompt2(proxyFlags, hostFlags) {
  this.proxyCred.flags = proxyFlags;
  this.hostCred.flags = hostFlags;
}
AuthPrompt2.prototype = {
  proxyCred: {
    user: "proxy",
    pass: "guest",
    realmExpected: "intern",
    flags: 0,
  },
  hostCred: { user: "host", pass: "guest", realmExpected: "extern", flags: 0 },

  QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt2"]),

  promptAuth: function ap2_promptAuth(channel, encryptionLevel, authInfo) {
    try {
      // never HOST and PROXY set at the same time in prompt
      Assert.equal(
        (authInfo.flags & Ci.nsIAuthInformation.AUTH_HOST) != 0,
        (authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY) == 0
      );

      var isProxy = (authInfo.flags & Ci.nsIAuthInformation.AUTH_PROXY) != 0;
      var cred = isProxy ? this.proxyCred : this.hostCred;

      dump(
        "with flags: " +
          ((cred.flags & FLAG_WRONG_PASSWORD) != 0 ? "wrong password" : "") +
          " " +
          ((cred.flags & FLAG_PREVIOUS_FAILED) != 0 ? "previous failed" : "") +
          " " +
          ((cred.flags & FLAG_RETURN_FALSE) != 0 ? "return false" : "") +
          "\n"
      );

      // PROXY properly set by necko (checked using realm)
      Assert.equal(cred.realmExpected, authInfo.realm);

      // PREVIOUS_FAILED properly set by necko
      Assert.equal(
        (cred.flags & FLAG_PREVIOUS_FAILED) != 0,
        (authInfo.flags & Ci.nsIAuthInformation.PREVIOUS_FAILED) != 0
      );

      if (cred.flags & FLAG_RETURN_FALSE) {
        cred.flags |= FLAG_PREVIOUS_FAILED;
        cred.flags &= ~FLAG_RETURN_FALSE;
        return false;
      }

      authInfo.username = cred.user;
      if (cred.flags & FLAG_WRONG_PASSWORD) {
        authInfo.password = cred.pass + ".wrong";
        cred.flags |= FLAG_PREVIOUS_FAILED;
        // Now clear the flag to avoid an infinite loop
        cred.flags &= ~FLAG_WRONG_PASSWORD;
      } else {
        authInfo.password = cred.pass;
        cred.flags &= ~FLAG_PREVIOUS_FAILED;
      }
    } catch (e) {
      do_throw(e);
    }
    return true;
  },

  asyncPromptAuth: function ap2_async(
    channel,
    callback,
    context,
    encryptionLevel,
    authInfo
  ) {
    var me = this;
    var allOverAndDead = false;
    executeSoon(function () {
      try {
        if (allOverAndDead) {
          throw new Error("already canceled");
        }
        var ret = me.promptAuth(channel, encryptionLevel, authInfo);
        if (!ret) {
          callback.onAuthCancelled(context, true);
        } else {
          callback.onAuthAvailable(context, authInfo);
        }
        allOverAndDead = true;
      } catch (e) {
        do_throw(e);
      }
    });
    return new Cancelable(function () {
      if (allOverAndDead) {
        throw new Error("can't cancel, already ran");
      }
      callback.onAuthAvailable(context, authInfo);
      allOverAndDead = true;
    });
  },
};

function Cancelable(onCancelFunc) {
  this.onCancelFunc = onCancelFunc;
}
Cancelable.prototype = {
  QueryInterface: ChromeUtils.generateQI(["nsICancelable"]),
  cancel: function cancel() {
    try {
      this.onCancelFunc();
    } catch (e) {
      do_throw(e);
    }
  },
};

function Requestor(proxyFlags, hostFlags) {
  this.proxyFlags = proxyFlags;
  this.hostFlags = hostFlags;
}
Requestor.prototype = {
  QueryInterface: ChromeUtils.generateQI(["nsIInterfaceRequestor"]),

  getInterface: function requestor_gi(iid) {
    if (iid.equals(Ci.nsIAuthPrompt)) {
      dump("authprompt1 not implemented\n");
      throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
    }
    if (iid.equals(Ci.nsIAuthPrompt2)) {
      try {
        // Allow the prompt to store state by caching it here
        if (!this.prompt2) {
          this.prompt2 = new AuthPrompt2(this.proxyFlags, this.hostFlags);
        }
        return this.prompt2;
      } catch (e) {
        do_throw(e);
      }
    }
    throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
  },

  prompt2: null,
};

var listener = {
  expectedCode: -1, // uninitialized

  onStartRequest: function test_onStartR(request) {
    try {
      // Proxy auth cancellation return failures to avoid spoofing
      if (
        !Components.isSuccessCode(request.status) &&
        this.expectedCode != 407
      ) {
        do_throw("Channel should have a success code!");
      }

      if (!(request instanceof Ci.nsIHttpChannel)) {
        do_throw("Expecting an HTTP channel");
      }

      Assert.equal(this.expectedCode, request.responseStatus);
      // If we expect 200, the request should have succeeded
      Assert.equal(this.expectedCode == 200, request.requestSucceeded);

      var cookie = "";
      try {
        cookie = request.getRequestHeader("Cookie");
      } catch (e) {}
      Assert.equal(cookie, "");
    } catch (e) {
      do_throw("Unexpected exception: " + e);
    }

    throw Components.Exception("", Cr.NS_ERROR_ABORT);
  },

  onDataAvailable: function test_ODA() {
    do_throw("Should not get any data!");
  },

  onStopRequest: function test_onStopR(request, status) {
    Assert.equal(status, Cr.NS_ERROR_ABORT);

    if (current_test < tests.length - 1) {
      // First, need to clear the auth cache
      Cc["@mozilla.org/network/http-auth-manager;1"]
        .getService(Ci.nsIHttpAuthManager)
        .clearAll();

      current_test++;
      tests[current_test]();
    } else {
      do_test_pending();
      httpserv.stop(do_test_finished);
    }

    do_test_finished();
  },
};

function makeChan(url) {
  if (!url) {
    url = "http://somesite/";
  }

  return NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
}

var current_test = 0;
var httpserv = null;

function run_test() {
  httpserv = new HttpServer();
  httpserv.registerPathHandler("/", proxyAuthHandler);
  httpserv.identity.add("http", "somesite", 80);
  httpserv.start(-1);

  Services.prefs.setCharPref("network.proxy.http", "localhost");
  Services.prefs.setIntPref(
    "network.proxy.http_port",
    httpserv.identity.primaryPort
  );
  Services.prefs.setCharPref("network.proxy.no_proxies_on", "");
  Services.prefs.setIntPref("network.proxy.type", 1);

  // Turn off the authentication dialog blocking for this test.
  Services.prefs.setIntPref("network.auth.subresource-http-auth-allow", 2);
  Services.prefs.setBoolPref(
    "network.auth.non-web-content-triggered-resources-http-auth-allow",
    true
  );

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("network.proxy.http");
    Services.prefs.clearUserPref("network.proxy.http_port");
    Services.prefs.clearUserPref("network.proxy.no_proxies_on");
    Services.prefs.clearUserPref("network.proxy.type");
    Services.prefs.clearUserPref("network.auth.subresource-http-auth-allow");
    Services.prefs.clearUserPref(
      "network.auth.non-web-content-triggered-resources-http-auth-allow"
    );
  });

  tests[current_test]();
}

function test_proxy_returnfalse() {
  dump("\ntest: proxy returnfalse\n");
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(FLAG_RETURN_FALSE, 0);
  listener.expectedCode = 407; // Proxy Unauthorized
  chan.asyncOpen(listener);

  do_test_pending();
}

function test_proxy_wrongpw() {
  dump("\ntest: proxy wrongpw\n");
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(FLAG_WRONG_PASSWORD, 0);
  listener.expectedCode = 200; // Eventually OK
  chan.asyncOpen(listener);
  do_test_pending();
}

function test_all_ok() {
  dump("\ntest: all ok\n");
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(0, 0);
  listener.expectedCode = 200; // OK
  chan.asyncOpen(listener);
  do_test_pending();
}

function test_proxy_407_cookie() {
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(FLAG_RETURN_FALSE, 0);
  chan.setRequestHeader("X-Set-407-Cookie", "1", false);
  listener.expectedCode = 407; // Proxy Unauthorized
  chan.asyncOpen(listener);

  do_test_pending();
}

function test_proxy_200_cookie() {
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(0, 0);
  chan.setRequestHeader("X-Set-407-Cookie", "1", false);
  listener.expectedCode = 200; // OK
  chan.asyncOpen(listener);
  do_test_pending();
}

function test_host_returnfalse() {
  dump("\ntest: host returnfalse\n");
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(0, FLAG_RETURN_FALSE);
  listener.expectedCode = 401; // Host Unauthorized
  chan.asyncOpen(listener);

  do_test_pending();
}

function test_host_wrongpw() {
  dump("\ntest: host wrongpw\n");
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(0, FLAG_WRONG_PASSWORD);
  listener.expectedCode = 200; // Eventually OK
  chan.asyncOpen(listener);
  do_test_pending();
}

function test_proxy_wrongpw_host_wrongpw() {
  dump("\ntest: proxy wrongpw, host wrongpw\n");
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(
    FLAG_WRONG_PASSWORD,
    FLAG_WRONG_PASSWORD
  );
  listener.expectedCode = 200; // OK
  chan.asyncOpen(listener);
  do_test_pending();
}

function test_proxy_wrongpw_host_returnfalse() {
  dump("\ntest: proxy wrongpw, host return false\n");
  var chan = makeChan();
  chan.notificationCallbacks = new Requestor(
    FLAG_WRONG_PASSWORD,
    FLAG_RETURN_FALSE
  );
  listener.expectedCode = 401; // Host Unauthorized
  chan.asyncOpen(listener);
  do_test_pending();
}

var tests = [
  test_proxy_returnfalse,
  test_proxy_wrongpw,
  test_all_ok,
  test_proxy_407_cookie,
  test_proxy_200_cookie,
  test_host_returnfalse,
  test_host_wrongpw,
  test_proxy_wrongpw_host_wrongpw,
  test_proxy_wrongpw_host_returnfalse,
];

// PATH HANDLERS

// Proxy
function proxyAuthHandler(metadata, response) {
  try {
    var realm = "intern";
    // btoa("proxy:guest"), but that function is not available here
    var expectedHeader = "Basic cHJveHk6Z3Vlc3Q=";

    var body;
    if (
      metadata.hasHeader("Proxy-Authorization") &&
      metadata.getHeader("Proxy-Authorization") == expectedHeader
    ) {
      dump("proxy password ok\n");
      response.setHeader(
        "Proxy-Authenticate",
        'Basic realm="' + realm + '"',
        false
      );

      hostAuthHandler(metadata, response);
    } else {
      dump("proxy password required\n");
      response.setStatusLine(
        metadata.httpVersion,
        407,
        "Unauthorized by HTTP proxy"
      );
      response.setHeader(
        "Proxy-Authenticate",
        'Basic realm="' + realm + '"',
        false
      );
      if (metadata.hasHeader("X-Set-407-Cookie")) {
        response.setHeader("Set-Cookie", "chewy", false);
      }
      body = "failed";
      response.bodyOutputStream.write(body, body.length);
    }
  } catch (e) {
    do_throw(e);
  }
}

// Host /auth
function hostAuthHandler(metadata, response) {
  try {
    var realm = "extern";
    // btoa("host:guest"), but that function is not available here
    var expectedHeader = "Basic aG9zdDpndWVzdA==";

    var body;
    if (
      metadata.hasHeader("Authorization") &&
      metadata.getHeader("Authorization") == expectedHeader
    ) {
      dump("host password ok\n");
      response.setStatusLine(
        metadata.httpVersion,
        200,
        "OK, authorized for host"
      );
      response.setHeader(
        "WWW-Authenticate",
        'Basic realm="' + realm + '"',
        false
      );
      body = "success";
    } else {
      dump("host password required\n");
      response.setStatusLine(
        metadata.httpVersion,
        401,
        "Unauthorized by HTTP server host"
      );
      response.setHeader(
        "WWW-Authenticate",
        'Basic realm="' + realm + '"',
        false
      );
      body = "failed";
    }
    response.bodyOutputStream.write(body, body.length);
  } catch (e) {
    do_throw(e);
  }
}