unpkg/modules/middleware/validateQuery.js

33 lines
723 B
JavaScript
Raw Normal View History

2019-01-06 00:50:05 +00:00
import createSearch from '../utils/createSearch';
2018-05-19 15:34:36 +00:00
const knownQueryParams = {
main: true, // Deprecated, see #63
meta: true,
module: true
2018-05-19 15:34:36 +00:00
};
function isKnownQueryParam(param) {
return !!knownQueryParams[param];
}
function sanitizeQuery(originalQuery) {
const query = {};
Object.keys(originalQuery).forEach(param => {
if (isKnownQueryParam(param)) query[param] = originalQuery[param];
});
return query;
}
/**
* Reject URLs with invalid query parameters to increase cache hit rates.
*/
2019-01-06 00:50:05 +00:00
export default function validateQuery(req, res, next) {
2018-05-19 15:34:36 +00:00
if (!Object.keys(req.query).every(isKnownQueryParam)) {
return res.redirect(302, req.path + createSearch(sanitizeQuery(req.query)));
}
next();
}