test_registerHandler.html (4087B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=402788 5 --> 6 <head> 7 <title>Test for Bug 402788</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=402788">Mozilla Bug 402788</a> 13 <p id="display"></p> 14 <div id="content" style="display: none"> 15 16 </div> 17 <pre id="test"> 18 <script class="testbody" type="text/javascript"> 19 20 /** Test for Bug 402788 */ 21 SimpleTest.waitForExplicitFinish(); 22 23 // return false if an exception has been catched, true otherwise 24 function testRegisterHandler(aIsProtocol, aTxt, aUri, aTitle) { 25 try { 26 navigator.registerProtocolHandler(aTxt, aUri, aTitle); 27 } catch (e) { 28 return false; 29 } 30 31 return true; 32 } 33 34 // helper function to build URLs since hostname differs 35 // based on whether the test is running in a cross-origin iframe 36 function buildUrl(protocol="http", addFormat=true) { 37 return `${protocol}://${window.location.hostname}:${window.location.port}${addFormat ? "/%s" : "/"}`; 38 } 39 40 async function tests() { 41 await SpecialPowers.pushPrefEnv({ 42 set: [ 43 ["dom.registerProtocolHandler.insecure.enabled", true], 44 ], 45 }); 46 47 // testing a generic case 48 is(testRegisterHandler(true, "web+foo", buildUrl(), "Foo handler"), true, "registering a web+foo protocol handler should work"); 49 50 // testing with wrong uris 51 is(testRegisterHandler(true, "web+foo", buildUrl("http", false), "Foo handler"), false, "a protocol handler uri should contain %s"); 52 53 // the spec explicitly allows relative urls to be passed 54 is(testRegisterHandler(true, "web+foo", "foo/%s", "Foo handler"), true, "a protocol handler uri should be valid"); 55 56 // we should only accept to register when the handler has the same host as the current page (bug 402287) 57 is(testRegisterHandler(true, "fweb+oo", "http://remotehost:8888/%s", "Foo handler"), false, "registering a web+foo protocol handler with a different host should not work"); 58 59 // restriction to http(s) for the uri of the handler (bug 401343) 60 // http is already tested in the generic case 61 // ftp should not work 62 is(testRegisterHandler(true, "web+foo", buildUrl("ftp"), "Foo handler"), false, "registering a web+foo protocol handler with ftp scheme should not work"); 63 // chrome should not work 64 is(testRegisterHandler(true, "web+foo", buildUrl("chrome"), "Foo handler"), false, "registering a web+foo protocol handler with chrome scheme should not work"); 65 // foo should not work 66 is(testRegisterHandler(true, "web+foo", buildUrl("foo"), "Foo handler"), false, "registering a web+foo protocol handler with foo scheme should not work"); 67 68 // for security reasons, protocol handlers should never be registered for some schemes (chrome, vbscript, ...) (bug 402788) 69 is(testRegisterHandler(true, "chrome", buildUrl(), "chrome handler"), false, "registering a chrome protocol handler should not work"); 70 is(testRegisterHandler(true, "vbscript", buildUrl(), "vbscript handler"), false, "registering a vbscript protocol handler should not work"); 71 is(testRegisterHandler(true, "javascript", buildUrl(), "javascript handler"), false, "registering a javascript protocol handler should not work"); 72 is(testRegisterHandler(true, "moz-icon", buildUrl(), "moz-icon handler"), false, "registering a moz-icon protocol handler should not work"); 73 74 // registering anything not on the list of safe schemes and unprefixed by web+ shouldn't work 75 is(testRegisterHandler(true, "foo", buildUrl(), "chrome handler"), false, "registering a foo protocol handler should not work"); 76 is(testRegisterHandler(true, "web+", buildUrl(), "chrome handler"), false, "registering a 'web+' protocol handler should not work"); 77 is(testRegisterHandler(true, "web+1", buildUrl(), "chrome handler"), false, "registering a 'web+1' protocol handler should not work"); 78 79 80 SimpleTest.finish(); 81 } 82 83 tests(); 84 85 </script> 86 </pre> 87 </body> 88 </html>