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
|
2019-05-19 04:43:48 +00:00
|
|
|
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();
|
|
|
|
}
|