Server: do not forget to check the signature when another pod wants to
quit us
This commit is contained in:
parent
c60f2212fd
commit
0eb78d5303
|
@ -10,6 +10,7 @@ const friends = require('../../../lib/friends')
|
||||||
const middlewares = require('../../../middlewares')
|
const middlewares = require('../../../middlewares')
|
||||||
const admin = middlewares.admin
|
const admin = middlewares.admin
|
||||||
const oAuth = middlewares.oauth
|
const oAuth = middlewares.oauth
|
||||||
|
const checkSignature = middlewares.secure.checkSignature
|
||||||
const validators = middlewares.validators.pods
|
const validators = middlewares.validators.pods
|
||||||
const signatureValidator = middlewares.validators.remote.signature
|
const signatureValidator = middlewares.validators.remote.signature
|
||||||
|
|
||||||
|
@ -31,7 +32,11 @@ router.get('/quitfriends',
|
||||||
quitFriends
|
quitFriends
|
||||||
)
|
)
|
||||||
// Post because this is a secured request
|
// Post because this is a secured request
|
||||||
router.post('/remove', signatureValidator, removePods)
|
router.post('/remove',
|
||||||
|
signatureValidator,
|
||||||
|
checkSignature,
|
||||||
|
removePods
|
||||||
|
)
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ const Video = mongoose.model('Video')
|
||||||
router.post('/videos',
|
router.post('/videos',
|
||||||
validators.signature,
|
validators.signature,
|
||||||
validators.dataToDecrypt,
|
validators.dataToDecrypt,
|
||||||
|
secureMiddleware.checkSignature,
|
||||||
secureMiddleware.decryptBody,
|
secureMiddleware.decryptBody,
|
||||||
validators.remoteVideos,
|
validators.remoteVideos,
|
||||||
remoteVideos
|
remoteVideos
|
||||||
|
|
|
@ -7,10 +7,11 @@ const peertubeCrypto = require('../helpers/peertube-crypto')
|
||||||
const Pod = mongoose.model('Pod')
|
const Pod = mongoose.model('Pod')
|
||||||
|
|
||||||
const secureMiddleware = {
|
const secureMiddleware = {
|
||||||
|
checkSignature: checkSignature,
|
||||||
decryptBody: decryptBody
|
decryptBody: decryptBody
|
||||||
}
|
}
|
||||||
|
|
||||||
function decryptBody (req, res, next) {
|
function checkSignature (req, res, next) {
|
||||||
const url = req.body.signature.url
|
const url = req.body.signature.url
|
||||||
Pod.loadByUrl(url, function (err, pod) {
|
Pod.loadByUrl(url, function (err, pod) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -28,26 +29,30 @@ function decryptBody (req, res, next) {
|
||||||
const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature)
|
const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature)
|
||||||
|
|
||||||
if (signatureOk === true) {
|
if (signatureOk === true) {
|
||||||
peertubeCrypto.decrypt(req.body.key, req.body.data, function (err, decrypted) {
|
return next()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue