Bug 1438025 - Drop invisible characters from downloads filename. r=kmag, r=Paolo, a=RyanVM
authorTomislav Jovanovic <tomica@gmail.com>
Wed, 21 Mar 2018 23:23:59 +0100
changeset 460418 4702ec51711e54109358c3d7982c323543184353
parent 460417 f556bd020e0837f7c710beac70e85b3250fb2761
child 460419 17b23e75aceb41443341e8de0802f7b9d40e6040
push id8940
push userryanvm@gmail.com
push dateWed, 28 Mar 2018 19:11:14 +0000
treeherdermozilla-beta@a125dbf21643 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag, Paolo, RyanVM
bugs1438025
milestone60.0
Bug 1438025 - Drop invisible characters from downloads filename. r=kmag, r=Paolo, a=RyanVM
toolkit/components/downloads/DownloadPaths.jsm
toolkit/components/downloads/test/unit/test_DownloadPaths.js
--- a/toolkit/components/downloads/DownloadPaths.jsm
+++ b/toolkit/components/downloads/DownloadPaths.jsm
@@ -67,16 +67,17 @@ var DownloadPaths = {
    */
   sanitize(leafName) {
     if (AppConstants.platform == "win") {
       leafName = leafName.replace(/</g, "(")
                          .replace(/>/g, ")")
                          .replace(/"/g, "'");
     }
     return leafName.replace(/[\\/]+/g, "_")
+                   .replace(/[\u200e\u200f\u202a-\u202e]/g, "")
                    .replace(gConvertToSpaceRegExp, " ")
                    .replace(/^[\s\u180e.]+|[\s\u180e.]+$/g, "");
   },
 
   /**
    * Creates a uniquely-named file starting from the name of the provided file.
    * If a file with the provided name already exists, the function attempts to
    * create nice alternatives, like "base(1).ext" (instead of "base-1.ext").
--- a/toolkit/components/downloads/test/unit/test_DownloadPaths.js
+++ b/toolkit/components/downloads/test/unit/test_DownloadPaths.js
@@ -72,16 +72,20 @@ add_task(async function test_sanitize() 
   testSanitize("\u1680\u180e\u2000\u2008\u200a . txt", "txt");
   testSanitize("\u2028\u2029\u202f\u205f\u3000\ufeff . txt", "txt");
 
   // Strings with whitespace and dots only.
   testSanitize(".", "");
   testSanitize("..", "");
   testSanitize(" ", "");
   testSanitize(" . ", "");
+
+  // Stripping of BIDI formatting characters.
+  testSanitize("\u200e \u202b\u202c\u202d\u202etest\x7f\u200f", "test");
+  testSanitize("AB\x7f\u202a\x7f\u202a\x7fCD", "AB CD");
 });
 
 add_task(async function test_splitBaseNameAndExtension() {
   // Usual file names.
   testSplitBaseNameAndExtension("base", ["base", ""]);
   testSplitBaseNameAndExtension("base.ext", ["base", ".ext"]);
   testSplitBaseNameAndExtension("base.application", ["base", ".application"]);
   testSplitBaseNameAndExtension("base.x.Z", ["base", ".x.Z"]);