Explorar el Código

refactor: try password encodings in a loop when deciphering

Replace the nested retry of the legacy password encoding with a list of
password encodings that get tried in order, until decryption succeeds.
Dropping support for the legacy encoding only requires removing it from
that list. Errors now only get logged once all encodings failed, not
also when the legacy encoding succeeds.
El RIDO hace 1 semana
padre
commit
2ac8a4f9cf
Se han modificado 2 ficheros con 20 adiciones y 26 borrados
  1. 19 25
      js/privatebin.js
  2. 1 1
      lib/Configuration.php

+ 19 - 25
js/privatebin.js

@@ -1293,35 +1293,29 @@ window.PrivateBin = (function () {
                     throw 'Error decompressing document, your browser does not support WebAssembly. Please use another browser to view this document.';
                 }
             }
-            const settings = cryptoSettings(adataString, spec),
-                cipherArray = stringToArraybuffer(
-                    atob(cipherMessage)
-                ),
-                decrypt = async passwordArray => window.crypto.subtle.decrypt(
-                    settings,
-                    await deriveKey(key, passwordArray, spec),
-                    cipherArray
-                );
-            try {
-                plaintext = await decrypt(
-                    new TextEncoder().encode(password)
-                );
-            } catch (err) {
-                // documents created by earlier versions truncated the
-                // characters of non-ASCII passwords to their lowest byte
-                if (!/[\u0080-\uffff]/.test(password)) {
-                    console.error(err);
-                    return '';
-                }
+            const passwordArray = [new TextEncoder().encode(password)];
+            // documents created by earlier versions truncated the characters
+            // of non-ASCII passwords to their lowest byte
+            if (/[\u0080-\uffff]/.test(password)) {
+                passwordArray.push(stringToArraybuffer(password));
+            }
+            let decryptionError;
+            for (const passwordAttempt of passwordArray) {
                 try {
-                    plaintext = await decrypt(
-                        stringToArraybuffer(password)
+                    plaintext = await window.crypto.subtle.decrypt(
+                        cryptoSettings(adataString, spec),
+                        await deriveKey(key, passwordAttempt, spec),
+                        stringToArraybuffer(atob(cipherMessage))
                     );
-                } catch (legacyErr) {
-                    console.error(legacyErr);
-                    return '';
+                    break;
+                } catch (err) {
+                    decryptionError = err;
                 }
             }
+            if (typeof plaintext === 'undefined') {
+                console.error(decryptionError);
+                return '';
+            }
             try {
                 return await decompress(plaintext, spec[7], zlib);
             } catch (err) {

+ 1 - 1
lib/Configuration.php

@@ -118,7 +118,7 @@ class Configuration
             'js/kjua-0.10.0.js'      => 'sha512-BYj4xggowR7QD150VLSTRlzH62YPfhpIM+b/1EUEr7RQpdWAGKulxWnOvjFx1FUlba4m6ihpNYuQab51H6XlYg==',
             'js/legacy.js'           => 'sha512-pRofxsrf5UItjiP22Dcjh3FAcBjF/n7h8U9/W5xqJk17U0N2U1oajhXypq/omo9jhwS1iVGOhWrRepoPeFns+w==',
             'js/prettify.js'         => 'sha512-puO0Ogy++IoA2Pb9IjSxV1n4+kQkKXYAEUtVzfZpQepyDPyXk8hokiYDS7ybMogYlyyEIwMLpZqVhCkARQWLMg==',
-            'js/privatebin.js'       => 'sha512-CzVyPPes9QMiL83NxOmw2TKQYfHFuhx7TSD67vfsqXXmVnuO2b3F2w07BERXiWWCO5U/lkWmwup9Cd88dBMghA==',
+            'js/privatebin.js'       => 'sha512-7rBeKMkm/Rr4kuzrz7+DJ7+QTrHBII1ryqzxVR61aLlYqaE/a+QIOusMeZWiZfE5pA1xtHVxnCnePiy5p+IGaQ==',
             'js/purify-3.4.17.js'    => 'sha512-udF2zPdEyVCAxIGeBbeSGFK1Fj5c4eHGi6NqOSsr8M75lGs39POzgUNxO3SFxOg4cHfAoZJgR2EaXdmDi6v6AQ==',
             'js/showdown-2.1.0.js'   => 'sha512-WYXZgkTR0u/Y9SVIA4nTTOih0kXMEd8RRV6MLFdL6YU8ymhR528NLlYQt1nlJQbYz4EW+ZsS0fx1awhiQJme1Q==',
             'js/zlib-1.3.2.js'       => 'sha512-RAhJgxg9siMIA8ky4c10Rc2zUgnK80olHB8Tt1IOYWY4Eh1WmrviQkDn+sgBlb38ZHq3tzufGC41kP360gmosQ==',