|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +/* import-globals-from head_cache.js */ |
| 8 | +/* import-globals-from head_cookies.js */ |
| 9 | +/* import-globals-from head_channels.js */ |
| 10 | +/* import-globals-from head_servers.js */ |
| 11 | + |
| 12 | +function makeChan(uri, loadingUrl) { |
| 13 | + let principal = Services.scriptSecurityManager.createContentPrincipal( |
| 14 | + Services.io.newURI(loadingUrl), |
| 15 | + {} |
| 16 | + ); |
| 17 | + return NetUtil.newChannel({ |
| 18 | + uri, |
| 19 | + loadingPrincipal: principal, |
| 20 | + securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL, |
| 21 | + contentPolicyType: Ci.nsIContentPolicy.TYPE_OTHER, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +class AuthPrompt { |
| 26 | + constructor() { |
| 27 | + this.QueryInterface = ChromeUtils.generateQI(["nsIAuthPrompt2"]); |
| 28 | + } |
| 29 | + asyncPromptAuth(channel, callback, context, encryptionLevel, authInfo) { |
| 30 | + executeSoon(function () { |
| 31 | + authInfo.username = "guest"; |
| 32 | + authInfo.password = "guest"; |
| 33 | + callback.onAuthAvailable(context, authInfo); |
| 34 | + }); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class AuthRequestor { |
| 39 | + constructor(prompt) { |
| 40 | + this.prompt = prompt; |
| 41 | + this.QueryInterface = ChromeUtils.generateQI(["nsIInterfaceRequestor"]); |
| 42 | + } |
| 43 | + getInterface(iid) { |
| 44 | + if (iid.equals(Ci.nsIAuthPrompt2)) { |
| 45 | + return this.prompt(); |
| 46 | + } |
| 47 | + throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Verify HTTP/2 auth retry behavior: the server issues two 401 challenges |
| 53 | + * and only returns 200 OK on the third request. |
| 54 | + * |
| 55 | + * This test ensures the channel performs two auth retries and succeeds on the |
| 56 | + * third attempt. |
| 57 | + * |
| 58 | + */ |
| 59 | +add_task(async function test_http2_auth_retry_twice() { |
| 60 | + Services.prefs.setIntPref("network.auth.subresource-http-auth-allow", 2); |
| 61 | + |
| 62 | + let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService( |
| 63 | + Ci.nsIX509CertDB |
| 64 | + ); |
| 65 | + addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u"); |
| 66 | + |
| 67 | + let server = new NodeHTTP2Server(); |
| 68 | + await server.start(); |
| 69 | + registerCleanupFunction(async () => { |
| 70 | + await server.stop(); |
| 71 | + }); |
| 72 | + |
| 73 | + await server.registerPathHandler("/test", (req, res) => { |
| 74 | + const hasAuth = |
| 75 | + typeof req.headers.authorization === "string" && |
| 76 | + !!req.headers.authorization.length; |
| 77 | + global.count ??= 0; |
| 78 | + global.count++; |
| 79 | + if (!hasAuth || global.count < 3) { |
| 80 | + res.stream.respond({ |
| 81 | + ":status": 401, |
| 82 | + "content-type": "text/plain; charset=utf-8", |
| 83 | + "www-authenticate": 'Basic realm="secret"', |
| 84 | + }); |
| 85 | + res.end("Unauthorized\n"); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + res.stream.respond({ |
| 90 | + ":status": 200, |
| 91 | + "content-type": "text/plain; charset=utf-8", |
| 92 | + }); |
| 93 | + res.end("OK\n"); |
| 94 | + }); |
| 95 | + |
| 96 | + let chan = makeChan( |
| 97 | + `https://localhost:${server.port()}/test`, |
| 98 | + `https://localhost:${server.port()}` |
| 99 | + ); |
| 100 | + chan.notificationCallbacks = new AuthRequestor(() => new AuthPrompt()); |
| 101 | + |
| 102 | + let req = await new Promise(resolve => { |
| 103 | + chan.asyncOpen(new ChannelListener(resolve, null, CL_ALLOW_UNKNOWN_CL)); |
| 104 | + }); |
| 105 | + equal(req.status, Cr.NS_OK); |
| 106 | + equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200); |
| 107 | + equal(req.QueryInterface(Ci.nsIHttpChannel).protocolVersion, "h2"); |
| 108 | +}); |
0 commit comments