From 0eb78d530376c43d228e3e071e032fe9849149ed Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 1 Oct 2016 09:09:07 +0200 Subject: Server: do not forget to check the signature when another pod wants to quit us --- server/middlewares/secure.js | 45 ++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'server/middlewares/secure.js') diff --git a/server/middlewares/secure.js b/server/middlewares/secure.js index fa000c6f0..33a52e8d9 100644 --- a/server/middlewares/secure.js +++ b/server/middlewares/secure.js @@ -7,10 +7,11 @@ const peertubeCrypto = require('../helpers/peertube-crypto') const Pod = mongoose.model('Pod') const secureMiddleware = { + checkSignature: checkSignature, decryptBody: decryptBody } -function decryptBody (req, res, next) { +function checkSignature (req, res, next) { const url = req.body.signature.url Pod.loadByUrl(url, function (err, pod) { if (err) { @@ -28,26 +29,30 @@ function decryptBody (req, res, next) { const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature) if (signatureOk === true) { - peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) { - if (err) { - logger.error('Cannot decrypt data.', { error: err }) - return res.sendStatus(500) - } - - 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() - }) - } else { - logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url) - return res.sendStatus(403) + return next() + } + + logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url) + return res.sendStatus(403) + }) +} + +function decryptBody (req, res, next) { + peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) { + if (err) { + logger.error('Cannot decrypt data.', { error: err }) + return res.sendStatus(500) } + + 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() }) } -- cgit v1.2.3