Server: catch JSON.parse exceptions
authorChocobozzz <florian.bigard@gmail.com>
Tue, 23 Aug 2016 12:37:36 +0000 (14:37 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Tue, 23 Aug 2016 12:37:36 +0000 (14:37 +0200)
server/lib/friends.js
server/middlewares/secure.js

index 667055d4c554f69cc5b8852d5b88ec92a0811323..6c4383d8edbcc548d03560c82e20c8c3b533600b 100644 (file)
@@ -198,7 +198,12 @@ function getForeignPodsList (url, callback) {
   request.get(url + path, function (err, response, body) {
     if (err) return callback(err)
 
-    callback(null, JSON.parse(body))
+    try {
+      const json = JSON.parse(body)
+      return callback(null, json)
+    } catch (err) {
+      return callback(err)
+    }
   })
 }
 
index 9779c14ac2a4e30d53b50bfcccbb26b9192096ff..fa000c6f077b74b0dc418d5f5950ccee3160a300 100644 (file)
@@ -34,8 +34,13 @@ function decryptBody (req, res, next) {
           return res.sendStatus(500)
         }
 
-        req.body.data = JSON.parse(decrypted)
-        delete req.body.key
+        try {
+          req.body.data = JSON.parse(decrypted)
+          delete req.body.key
+        } catch (err) {
+          logger.error('Error in JSON.parse', { error: err })
+          return res.sendStatus(500)
+        }
 
         next()
       })