test_webchannel-urls.js (2144B)
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 const { validateProfilerWebChannelUrl } = ChromeUtils.importESModule( 8 "resource:///modules/DevToolsStartup.sys.mjs" 9 ); 10 11 add_task(function test() { 12 info( 13 "Since the WebChannel can communicate with a content page, test that only " + 14 "trusted URLs can be used with this mechanism." 15 ); 16 17 const { checkUrlIsValid, checkUrlIsInvalid } = setup(); 18 19 info("Check all of the valid URLs"); 20 checkUrlIsValid("https://profiler.firefox.com"); 21 checkUrlIsValid("http://example.com"); 22 checkUrlIsValid("http://localhost:4242"); 23 checkUrlIsValid("http://localhost:32343434"); 24 checkUrlIsValid("http://localhost:4242/"); 25 checkUrlIsValid("https://deploy-preview-1234--perf-html.netlify.com"); 26 checkUrlIsValid("https://deploy-preview-1234--perf-html.netlify.com/"); 27 checkUrlIsValid("https://deploy-preview-1234--perf-html.netlify.app"); 28 checkUrlIsValid("https://deploy-preview-1234--perf-html.netlify.app/"); 29 checkUrlIsValid("https://main--perf-html.netlify.app/"); 30 31 info("Check all of the invalid URLs"); 32 checkUrlIsInvalid("https://profiler.firefox.com/some-other-path"); 33 checkUrlIsInvalid("http://localhost:4242/some-other-path"); 34 checkUrlIsInvalid("http://profiler.firefox.com.example.com"); 35 checkUrlIsInvalid("http://mozilla.com"); 36 checkUrlIsInvalid("https://deploy-preview-1234--perf-html.netlify.dev"); 37 checkUrlIsInvalid("https://anything--perf-html.netlify.app/"); 38 }); 39 40 function setup() { 41 function checkUrlIsValid(url) { 42 info(`Check that ${url} is valid`); 43 equal( 44 validateProfilerWebChannelUrl(url), 45 url, 46 `"${url}" is a valid WebChannel URL.` 47 ); 48 } 49 50 function checkUrlIsInvalid(url) { 51 info(`Check that ${url} is invalid`); 52 equal( 53 validateProfilerWebChannelUrl(url), 54 "https://profiler.firefox.com", 55 `"${url}" was not valid, and was reset to the base URL.` 56 ); 57 } 58 59 return { 60 checkUrlIsValid, 61 checkUrlIsInvalid, 62 }; 63 }