[Pkg-javascript-commits] [sockjs-client] 213/350: try/catch rest of JSON3.parse

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:04:22 UTC 2016


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

tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.

commit 4274efe405e37c3f2739146a3b6374cbb2b3a394
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Tue Oct 21 12:07:43 2014 -0400

    try/catch rest of JSON3.parse
---
 lib/iframe-bootstrap.js | 26 ++++++++++++++++++++++----
 lib/info-ajax.js        |  8 +++++++-
 lib/info-iframe.js      | 16 +++++++++++++++-
 3 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/lib/iframe-bootstrap.js b/lib/iframe-bootstrap.js
index 97337b7..264d991 100644
--- a/lib/iframe-bootstrap.js
+++ b/lib/iframe-bootstrap.js
@@ -9,6 +9,11 @@ var urlUtils = require('./utils/url')
   , loc = require('./location')
   ;
 
+var debug = function() {};
+if (process.env.NODE_ENV !== 'production') {
+  debug = require('debug')('sockjs-client:iframe-bootstrap');
+}
+
 module.exports = function(SockJS, availableTransports) {
   var transportMap = {};
   availableTransports.forEach(function(at) {
@@ -37,13 +42,27 @@ module.exports = function(SockJS, availableTransports) {
       if (e.origin !== parentOrigin) {
         return;
       }
-      var iframeMessage = JSON3.parse(e.data);
+
+      var iframeMessage;
+      try {
+        iframeMessage = JSON3.parse(e.data);
+      } catch (ignored) {
+        debug('bad json', e.data);
+        return;
+      }
+
       if (iframeMessage.windowId !== iframeUtils.currentWindowId) {
         return;
       }
       switch (iframeMessage.type) {
       case 's':
-        var p = JSON3.parse(iframeMessage.data);
+        var p;
+        try {
+          p = JSON3.parse(iframeMessage.data);
+        } catch (ignored) {
+          debug('bad json', iframeMessage.data);
+          break;
+        }
         var version = p[0];
         var transport = p[1];
         var transUrl = p[2];
@@ -58,8 +77,7 @@ module.exports = function(SockJS, availableTransports) {
         if (!urlUtils.isOriginEqual(transUrl, loc.href) ||
             !urlUtils.isOriginEqual(baseUrl, loc.href)) {
           throw new Error('Can\'t connect to different domain from within an ' +
-                    'iframe. (' + JSON3.stringify([loc.href, transUrl, baseUrl]) +
-                    ')');
+                    'iframe. (' + loc.href + ', ' + transUrl + ', ' + baseUrl + ')');
         }
         facade = new FacadeJS(new transportMap[transport](transUrl, baseUrl));
         break;
diff --git a/lib/info-ajax.js b/lib/info-ajax.js
index 2610ea8..dda9ac9 100644
--- a/lib/info-ajax.js
+++ b/lib/info-ajax.js
@@ -6,6 +6,11 @@ var EventEmitter = require('events').EventEmitter
   , objectUtils = require('./utils/object')
   ;
 
+var debug = function() {};
+if (process.env.NODE_ENV !== 'production') {
+  debug = require('debug')('sockjs-client:info-ajax');
+}
+
 function InfoAjax(url, AjaxObject) {
   EventEmitter.call(this);
 
@@ -20,8 +25,9 @@ function InfoAjax(url, AjaxObject) {
       if (text) {
         try {
           info = JSON3.parse(text);
+        } catch (e) {
+          debug('bad json', text);
         }
-        catch (e) {}
       }
 
       if (!objectUtils.isObject(info)) {
diff --git a/lib/info-iframe.js b/lib/info-iframe.js
index 74d5f63..f5623bd 100644
--- a/lib/info-iframe.js
+++ b/lib/info-iframe.js
@@ -8,6 +8,11 @@ var EventEmitter = require('events').EventEmitter
   , InfoReceiverIframe = require('./info-iframe-receiver')
   ;
 
+var debug = function() {};
+if (process.env.NODE_ENV !== 'production') {
+  debug = require('debug')('sockjs-client:info-iframe');
+}
+
 function InfoIframe(url) {
   var self = this;
   EventEmitter.call(this);
@@ -17,7 +22,16 @@ function InfoIframe(url) {
 
     ifr.once('message', function(msg) {
       if (msg) {
-        var d = JSON3.parse(msg);
+        var d;
+        try {
+          d = JSON3.parse(msg);
+        } catch (e) {
+          debug('bad json', msg);
+          self.emit('finish');
+          self.close();
+          return;
+        }
+
         var info = d[0], rtt = d[1];
         self.emit('finish', info, rtt);
       }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list