Bug 1422643 - deal with tabs in the protocol in js paste detection code, r=florian,valentin
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Wed, 20 Dec 2017 18:57:48 +0000
changeset 398004 ffd3fd96a76a7d90598e91b6d7e21243dc32b4b9
parent 398003 cd99a1f959ad1de4166c493fd5bf1ca86fbd2500
child 398005 58070175527e314d6061a04464ec4538232315b8
push id57609
push usergijskruitbosch@gmail.com
push dateFri, 05 Jan 2018 19:15:38 +0000
treeherderautoland@ffd3fd96a76a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersflorian, valentin
bugs1422643
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1422643 - deal with tabs in the protocol in js paste detection code, r=florian,valentin MozReview-Commit-ID: Ax5LGkIedkY
browser/base/content/browser.js
browser/base/content/test/urlbar/browser_removeUnsafeProtocolsFromURLBarPaste.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -6048,24 +6048,28 @@ function middleMousePaste(event) {
   });
 
   event.stopPropagation();
 }
 
 function stripUnsafeProtocolOnPaste(pasteData) {
   // Don't allow pasting javascript URIs since we don't support
   // LOAD_FLAGS_DISALLOW_INHERIT_PRINCIPAL for those.
-  let changed = false;
-  let pasteDataNoJS = pasteData.replace(/\r?\n/g, "")
-                               .replace(/^(?:\W*javascript:)+/i,
-                                        () => {
-                                                changed = true;
-                                                return "";
-                                              });
-  return changed ? pasteDataNoJS : pasteData;
+  while (true) {
+    let scheme = "";
+    try {
+      scheme = Services.io.extractScheme(pasteData);
+    } catch (ex) { }
+    if (scheme != "javascript") {
+      break;
+    }
+
+    pasteData = pasteData.substring(pasteData.indexOf(":") + 1);
+  }
+  return pasteData;
 }
 
 // handleDroppedLink has the following 2 overloads:
 //   handleDroppedLink(event, url, name, triggeringPrincipal)
 //   handleDroppedLink(event, links, triggeringPrincipal)
 function handleDroppedLink(event, urlOrLinks, nameOrTriggeringPrincipal, triggeringPrincipal) {
   let links;
   if (Array.isArray(urlOrLinks)) {
--- a/browser/base/content/test/urlbar/browser_removeUnsafeProtocolsFromURLBarPaste.js
+++ b/browser/base/content/test/urlbar/browser_removeUnsafeProtocolsFromURLBarPaste.js
@@ -4,27 +4,42 @@ function test() {
 }
 
 var pairs = [
   ["javascript:", ""],
   ["javascript:1+1", "1+1"],
   ["javascript:document.domain", "document.domain"],
   [" \u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009javascript:document.domain", "document.domain"],
   ["java\nscript:foo", "foo"],
+  ["java\tscript:foo", "foo"],
   ["http://\nexample.com", "http://example.com"],
   ["http://\nexample.com\n", "http://example.com"],
   ["data:text/html,<body>hi</body>", "data:text/html,<body>hi</body>"],
   // Nested things get confusing because some things don't parse as URIs:
   ["javascript:javascript:alert('hi!')", "alert('hi!')"],
   ["data:data:text/html,<body>hi</body>", "data:data:text/html,<body>hi</body>"],
   ["javascript:data:javascript:alert('hi!')", "data:javascript:alert('hi!')"],
   ["javascript:data:text/html,javascript:alert('hi!')", "data:text/html,javascript:alert('hi!')"],
   ["data:data:text/html,javascript:alert('hi!')", "data:data:text/html,javascript:alert('hi!')"],
 ];
 
+let supportsNullBytes = AppConstants.platform == "macosx";
+// Note that \u000d (\r) is missing here; we test it separately because it
+// makes the test sad on Windows.
+let gobbledygook = "\u000a\u000b\u000c\u000e\u000f\u0010\u0011\u0012\u0013\u0014javascript:foo";
+if (supportsNullBytes) {
+  gobbledygook = "\u0000" + gobbledygook;
+}
+pairs.push([gobbledygook, "foo"]);
+
+let supportsReturnWithoutNewline = AppConstants.platform != "win";
+if (supportsReturnWithoutNewline) {
+  pairs.push(["java\rscript:foo", "foo"]);
+}
+
 var clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
 
 function paste(input, cb) {
   waitForClipboard(input, function() {
     clipboardHelper.copyString(input);
   }, function() {
     document.commandDispatcher.getControllerForCommand("cmd_paste").doCommand("cmd_paste");
     cb();