Improve query validation

This commit is contained in:
Yarmo Mackenbach 2021-04-15 15:09:57 +02:00
parent c333229e5d
commit 2e6baef01d
No known key found for this signature in database
GPG key ID: 37367F4AF4087AD1

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
const router = require('express').Router()
const { body, validationResult } = require('express-validator')
const { query, validationResult } = require('express-validator')
const fetcher = require('../../../fetcher')
const E = require('../../../enums')
require('dotenv').config()
@ -41,19 +41,21 @@ const opts = {
// Root route
router.get('/', async (req, res) => {
return res.status(400).json({ error: 'Invalid endpoint' })
return res.status(400).json({ errors: 'Invalid endpoint' })
})
// HTTP route
router.get('/get/http', (req, res) => {
if (!req.query.url) {
return res.status(400).json({ error: 'Missing parameter(s)' })
}
if (!validUrl.isUri(req.query.url)) {
return res.status(400).json({ error: 'Invalid URL' })
router.get('/get/http',
query('url').isURL(),
query('format').isIn([E.ProofFormat.JSON, E.ProofFormat.TEXT]),
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
fetcher.http(req.query, opts)
fetcher
.http(req.query, opts)
.then(result => {
switch (req.query.format) {
case E.ProofFormat.JSON:
@ -63,21 +65,20 @@ router.get('/get/http', (req, res) => {
case E.ProofFormat.TEXT:
return res.status(200).send(result)
break;
default:
throw new Error('Invalid proof format')
break;
}
})
.catch(err => {
return res.status(400).json({ error: err.message ? err.message : err })
return res.status(400).json({ errors: err.message ? err.message : err })
})
})
// DNS route
router.get('/get/dns', (req, res) => {
if (!req.query.domain) {
return res.status(400).json({ error: 'Missing parameter(s)' })
router.get('/get/dns',
query('domain').isFQDN(),
(req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
fetcher
@ -86,36 +87,21 @@ router.get('/get/dns', (req, res) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err.message ? err.message : err })
return res.status(400).json({ errors: err.message ? err.message : err })
})
})
// XMPP route
router.get('/get/xmpp', async (req, res) => {
router.get('/get/xmpp',
query('id').isEmail(),
query('field').isIn(['fn','number','userid','url','bday','nickname','note','desc']),
async (req, res) => {
if (!opts.claims.xmpp.service || !opts.claims.xmpp.username || !opts.claims.xmpp.password) {
return res.status(501).json({ error: 'XMPP not enabled on server' })
return res.status(501).json({ errors: 'XMPP not enabled on server' })
}
if (!req.query.id || !req.query.field) {
return res.status(400).json({ error: 'Missing parameter(s)' })
}
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(req.query.id))) {
return res.status(400).json({ error: 'Invalid XMPP ID' })
}
const allowedField = [
'FN',
'NUMBER',
'USERID',
'URL',
'BDAY',
'NICKNAME',
'NOTE',
'DESC',
]
if (!allowedField.includes(req.query.field)) {
return res.status(400).json({ error: 'Invalid XMPP vCard field' })
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
fetcher
@ -124,18 +110,20 @@ router.get('/get/xmpp', async (req, res) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err.message ? err.message : err })
return res.status(400).json({ errors: err.message ? err.message : err })
})
})
// Twitter route
router.get('/get/twitter', async (req, res) => {
router.get('/get/twitter',
query('tweetId').isInt(),
async (req, res) => {
if (!opts.claims.twitter.bearerToken) {
return res.status(501).json({ error: 'Twitter not enabled on server' })
return res.status(501).json({ errors: 'Twitter not enabled on server' })
}
if (!req.query.tweetId) {
return res.status(400).json({ error: 'Missing parameter(s)' })
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
fetcher
@ -144,18 +132,21 @@ router.get('/get/twitter', async (req, res) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err.message ? err.message : err })
return res.status(400).json({ errors: err.message ? err.message : err })
})
})
// Matrix route
router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => {
router.get('/get/matrix',
query('roomId').isString(),
query('eventId').isString(),
async (req, res) => {
if (!opts.claims.matrix.instance || !opts.claims.matrix.accessToken) {
return res.status(501).json({ error: 'Matrix not enabled on server' })
return res.status(501).json({ errors: 'Matrix not enabled on server' })
}
if (!req.query.id || !req.query.field) {
return res.status(400).json({ error: 'Missing parameter(s)' })
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
fetcher
@ -164,18 +155,20 @@ router.get('/get/matrix/:matrixroomid/:matrixeventid', async (req, res) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err.message ? err.message : err })
return res.status(400).json({ errors: err.message ? err.message : err })
})
})
// IRC route
router.get('/get/irc/:ircserver/:ircnick', async (req, res) => {
router.get('/get/irc',
query('nick').isString(),
async (req, res) => {
if (!opts.claims.irc.nick) {
return res.status(501).json({ error: 'IRC not enabled on server' })
return res.status(501).json({ errors: 'IRC not enabled on server' })
}
if (!req.query.nick) {
return res.status(400).json({ error: 'Missing parameter(s)' })
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
fetcher
@ -184,7 +177,7 @@ router.get('/get/irc/:ircserver/:ircnick', async (req, res) => {
return res.status(200).send(data)
})
.catch((err) => {
return res.status(400).json({ error: err.message ? err.message : err })
return res.status(400).json({ errors: err.message ? err.message : err })
})
})