Pārlūkot izejas kodu

Don't load the Crypto libraries if the browser doesn't have CSRNG

JC Brand 11 gadi atpakaļ
vecāks
revīzija
490d96fd26
2 mainītis faili ar 30 papildinājumiem un 3 dzēšanām
  1. 5 0
      CHANGES.rst
  2. 25 3
      converse.js

+ 5 - 0
CHANGES.rst

@@ -1,6 +1,11 @@
 Changelog
 =========
 
+0.7.1 (Unreleased)
+------------------
+
+* Don't load OTR crypto if the browser doesn't have a CSRNG [jcbrand]
+
 0.7.0 (2013-11-13)
 ------------------
 

+ 25 - 3
converse.js

@@ -12,7 +12,7 @@
         console = { log: function () {}, error: function () {} };
     }
     if (typeof define === 'function' && define.amd) {
-        define("converse", [
+        var dependencies = [
             "crypto",
             "otr",
             "locales",
@@ -23,13 +23,27 @@
             "strophe.roster",
             "strophe.vcard",
             "strophe.disco"
-            ], function(CryptoJS, otr) {
+        ];
+
+        if ((typeof crypto === 'undefined') ||
+            (    (typeof crypto.randomBytes !== 'function') &&
+                 (typeof crypto.getRandomValues !== 'function')
+            )) {
+            // Don't load crypto stuff if the browser doesn't have a CSRNG
+            dependencies.splice(0, 2);
+        }
+        define("converse", dependencies, function(CryptoJS, otr) {
                 // Use Mustache style syntax for variable interpolation
                 _.templateSettings = {
                     evaluate : /\{\[([\s\S]+?)\]\}/g,
                     interpolate : /\{\{([\s\S]+?)\}\}/g
                 };
-                return factory(jQuery, _, CryptoJS, otr.OTR, otr.DSA, console);
+                if (typeof otr !== "undefined") {
+                    return factory(jQuery, _, CryptoJS, otr.OTR, otr.DSA, console);
+                } else {
+                    return factory(jQuery, _, undefined, undefined, undefined, console);
+                }
+
             }
         );
     } else {
@@ -54,6 +68,11 @@
         var KEY = {
             ENTER: 13
         };
+        var HAS_CRYPTO = (
+            (typeof CryptoJS !== "undefined") &&
+            (typeof OTR !== "undefined") &&
+            (typeof DSA !== "undefined")
+        );
 
         // Default configuration values
         // ----------------------------
@@ -105,6 +124,9 @@
             'xhr_user_search_url'
         ]));
 
+        // Only allow OTR if we have the capability
+        this.allow_otr = this.allow_otr && HAS_CRYPTO;
+
         // Translation machinery
         // ---------------------
         var __ = $.proxy(function (str) {