[tbb-commits] [tor-browser] 11/36: Bug 1789500 - filename sanitization may cut surrogate pairs when truncating. r=NeilDeakin, a=RyanVM

gitolite role git at cupani.torproject.org
Thu Oct 13 07:50:59 UTC 2022


This is an automated email from the git hooks/post-receive script.

pierov pushed a commit to annotated tag FIREFOX_102_4_0esr_BUILD1
in repository tor-browser.

commit e0fe7669b6840138df69dba4d6f3617c5ca17299
Author: Marco Bonardo <mbonardo at mozilla.com>
AuthorDate: Fri Sep 16 09:47:05 2022 +0000

    Bug 1789500 - filename sanitization may cut surrogate pairs when truncating. r=NeilDeakin, a=RyanVM
    
    Differential Revision: https://phabricator.services.mozilla.com/D157516
---
 uriloader/exthandler/nsExternalHelperAppService.cpp       | 5 ++++-
 uriloader/exthandler/tests/unit/test_filename_sanitize.js | 8 ++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/uriloader/exthandler/nsExternalHelperAppService.cpp b/uriloader/exthandler/nsExternalHelperAppService.cpp
index 0ec02e346617..0fdfe1936a4e 100644
--- a/uriloader/exthandler/nsExternalHelperAppService.cpp
+++ b/uriloader/exthandler/nsExternalHelperAppService.cpp
@@ -3598,7 +3598,10 @@ void nsExternalHelperAppService::SanitizeFileName(nsAString& aFileName,
           nextChar = '_';
         }
 
-        lastNonTrimmable = int32_t(outFileName.Length()) + 1;
+        // Don't truncate surrogate pairs in the middle.
+        lastNonTrimmable =
+            int32_t(outFileName.Length()) +
+            (NS_IS_HIGH_SURROGATE(H_SURROGATE(nextChar)) ? 2 : 1);
       }
     }
 
diff --git a/uriloader/exthandler/tests/unit/test_filename_sanitize.js b/uriloader/exthandler/tests/unit/test_filename_sanitize.js
index 11c18f2e1361..1eefdeff6729 100644
--- a/uriloader/exthandler/tests/unit/test_filename_sanitize.js
+++ b/uriloader/exthandler/tests/unit/test_filename_sanitize.js
@@ -10,8 +10,8 @@ http://creativecommons.org/publicdomain/zero/1.0/ */
 add_task(async function validate_filename_method() {
   let mimeService = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
 
-  function checkFilename(filename, flags) {
-    return mimeService.validateFileNameForSaving(filename, "image/png", flags);
+  function checkFilename(filename, flags, mime = "image/png") {
+    return mimeService.validateFileNameForSaving(filename, mime, flags);
   }
 
   Assert.equal(checkFilename("basicfile.png", 0), "basicfile.png");
@@ -160,6 +160,10 @@ add_task(async function validate_filename_method() {
     "test_テスト_T E_S T.png"
   );
 
+  // Check we don't invalidate surrogate pairs when trimming.
+  Assert.equal(checkFilename("test😀", 0, ""), "test😀");
+  Assert.equal(checkFilename("test😀😀", 0, ""), "test😀😀");
+
   // Now check some media types
   Assert.equal(
     mimeService.validateFileNameForSaving("video.ogg", "video/ogg", 0),

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the tbb-commits mailing list