From d7b8370bf90a4930a7b9078a2f16a8797aa8bd97 Mon Sep 17 00:00:00 2001 From: "sayrer@gmail.com" Date: Mon, 28 May 2007 20:42:40 -0700 Subject: [PATCH] Bug 381262, Remembered username and password are garbled if they have non-ASCII characters. Patch by Justin Dolske . r=mconnor/sayrer --- .../passwordmgr/src/storage-Legacy.js | 16 ++++- .../test/unit/data/signons-381262.txt | 9 +++ .../test/unit/test_storage_legacy_3.js | 58 +++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 toolkit/components/passwordmgr/test/unit/data/signons-381262.txt diff --git a/toolkit/components/passwordmgr/src/storage-Legacy.js b/toolkit/components/passwordmgr/src/storage-Legacy.js index ec36504dc1c0..c99addab4527 100644 --- a/toolkit/components/passwordmgr/src/storage-Legacy.js +++ b/toolkit/components/passwordmgr/src/storage-Legacy.js @@ -612,7 +612,12 @@ LoginManagerStorage_legacy.prototype = { * */ _encrypt : function (plainText) { - return this._decoderRing.encryptString(plainText); + var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] + .createInstance(Ci.nsIScriptableUnicodeConverter); + converter.charset = "UTF-8"; + var plainOctet = converter.ConvertFromUnicode(plainText); + plainOctet += converter.Finish(); + return this._decoderRing.encryptString(plainOctet); }, @@ -624,14 +629,19 @@ LoginManagerStorage_legacy.prototype = { var plainText = null; try { + var plainOctet; if (cipherText.charAt(0) == '~') { // The older file format obscured entries by // base64-encoding them. These entries are signaled by a // leading '~' character. - plainText = atob(cipherText.substring(1)); + plainOctet = atob(cipherText.substring(1)); } else { - plainText = this._decoderRing.decryptString(cipherText); + plainOctet = this._decoderRing.decryptString(cipherText); } + var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] + .createInstance(Ci.nsIScriptableUnicodeConverter); + converter.charset = "UTF-8"; + plainText = converter.ConvertToUnicode(plainOctet); } catch (e) { this.log("Failed to decrypt string: " + cipherText); } diff --git a/toolkit/components/passwordmgr/test/unit/data/signons-381262.txt b/toolkit/components/passwordmgr/test/unit/data/signons-381262.txt new file mode 100644 index 000000000000..6a37be67f253 --- /dev/null +++ b/toolkit/components/passwordmgr/test/unit/data/signons-381262.txt @@ -0,0 +1,9 @@ +#2d +. +https://site.org +username +MEIEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECCSKaJ7xRN+pBBgIZONGV3hhPmYgNGWx076wG7t4IfY9AOM= +*password +MDoEEPgAAAAAAAAAAAAAAAAAAAEwFAYIKoZIhvcNAwcECEBHDZz893BwBBAtfya3S/aHmc7dzxs3v3Dv +https://site.org +. diff --git a/toolkit/components/passwordmgr/test/unit/test_storage_legacy_3.js b/toolkit/components/passwordmgr/test/unit/test_storage_legacy_3.js index 0e8460350ac4..acb3ecd704dd 100644 --- a/toolkit/components/passwordmgr/test/unit/test_storage_legacy_3.js +++ b/toolkit/components/passwordmgr/test/unit/test_storage_legacy_3.js @@ -194,6 +194,64 @@ initStorage(OUTDIR, "output-380961-3.txt"); checkStorageData([], [dummyuser1, dummyuser2, dummyuser3]); +/* + * ---------------------- Bug 381262 ---------------------- + * The SecretDecoderRing can't handle UCS2, failure to + * convert to UTF8 garbles the result. + * + * Note: dump()ing to the console on OS X (at least) outputs + * garbage, whereas the "bad" UCS2 looks ok! + */ + +/* ========== 5 ========== */ +testnum++; + +testdesc = "initializing login with non-ASCII data." +var dummyuser4 = Cc["@mozilla.org/login-manager/loginInfo;1"] + .createInstance(Ci.nsILoginInfo); + +dummyuser4.hostname = "https://site.org"; +dummyuser4.username = String.fromCharCode( + 355, 277, 349, 357, 533, 537, 101, 345, 185); + // "testuser1" using similar-looking glyphs +dummyuser4.usernameField = "username"; +dummyuser4.password = "testpa" + String.fromCharCode(223) + "1"; + // "ss" replaced with German eszett. +dummyuser4.passwordField = "password"; +dummyuser4.formSubmitURL = "https://site.org"; +dummyuser4.httpRealm = null; + + +/* ========== 6 ========== */ +testnum++; + +testdesc = "testing import of non-ascii username and password." +initStorage(INDIR, "signons-381262.txt", OUTDIR, "output-381262-1.txt"); +var logins = storage.getAllLogins({}); +checkStorageData([], [dummyuser4]); + +testdesc = "[flush and reload for verification]" +initStorage(OUTDIR, "output-381262-1.txt"); +checkStorageData([], [dummyuser4]); + + +/* ========== 7 ========== */ +testnum++; + +testdesc = "testing storage of non-ascii username and password." +initStorage(INDIR, "signons-empty.txt", OUTDIR, "output-381262-2.txt"); +checkStorageData([], []); +storage.addLogin(dummyuser4); +checkStorageData([], [dummyuser4]); + +testdesc = "[flush and reload for verification]" +initStorage(OUTDIR, "output-381262-2.txt"); +var logins = storage.getAllLogins({}); +checkStorageData([], [dummyuser4]); + + + + } catch (e) { throw ("FAILED in test #" + testnum + " -- " + testdesc + ": " + e); } };