test_dns_override_for_localhost.js (2539B)
1 "use strict"; 2 3 const override = Cc["@mozilla.org/network/native-dns-override;1"].getService( 4 Ci.nsINativeDNSResolverOverride 5 ); 6 const defaultOriginAttributes = {}; 7 const mainThread = Services.tm.currentThread; 8 9 class Listener { 10 constructor() { 11 this.promise = new Promise(resolve => { 12 this.resolve = resolve; 13 }); 14 } 15 16 onLookupComplete(inRequest, inRecord, inStatus) { 17 this.resolve([inRequest, inRecord, inStatus]); 18 } 19 20 async addresses() { 21 let [, inRecord] = await this.promise; 22 let addresses = []; 23 if (!inRecord) { 24 return addresses; // returns [] 25 } 26 inRecord.QueryInterface(Ci.nsIDNSAddrRecord); 27 while (inRecord.hasMore()) { 28 addresses.push(inRecord.getNextAddrAsString()); 29 } 30 return addresses; 31 } 32 33 then() { 34 return this.promise.then.apply(this.promise, arguments); 35 } 36 } 37 Listener.prototype.QueryInterface = ChromeUtils.generateQI(["nsIDNSListener"]); 38 39 const DOMAINS = [ 40 "localhost", 41 "localhost.", 42 "vhost.localhost", 43 "vhost.localhost.", 44 ]; 45 DOMAINS.forEach(domain => { 46 add_task(async function test_() { 47 let listener1 = new Listener(); 48 const overrides = ["1.2.3.4", "5.6.7.8"]; 49 overrides.forEach(ip_address => { 50 override.addIPOverride(domain, ip_address); 51 }); 52 53 // Verify that loopback host names are not overridden. 54 Services.dns.asyncResolve( 55 domain, 56 Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT, 57 0, 58 null, 59 listener1, 60 mainThread, 61 defaultOriginAttributes 62 ); 63 Assert.deepEqual( 64 await listener1.addresses(), 65 ["127.0.0.1", "::1"], 66 `${domain} is not overridden` 67 ); 68 69 // Verify that if localhost hijacking is enabled, the overrides 70 // registered above are taken into account. 71 Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true); 72 Services.prefs.setBoolPref( 73 "network.proxy.testing_localhost_is_secure_when_hijacked", 74 false 75 ); 76 let listener2 = new Listener(); 77 Services.dns.asyncResolve( 78 domain, 79 Ci.nsIDNSService.RESOLVE_TYPE_DEFAULT, 80 0, 81 null, 82 listener2, 83 mainThread, 84 defaultOriginAttributes 85 ); 86 Assert.deepEqual( 87 await listener2.addresses(), 88 overrides, 89 `${domain} is overridden` 90 ); 91 Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost"); 92 Services.prefs.clearUserPref( 93 "network.proxy.testing_localhost_is_secure_when_hijacked" 94 ); 95 Services.dns.clearCache(false); 96 override.clearOverrides(); 97 }); 98 });