Mod*_*rmo 1 javascript sanitization node.js express express-validator
我已经实施express-validator并正在尝试清理用户正在搜索特定查询的输入字段。
我使用的测试查询是<script>Malicious code</script. 当请求进来时,我使用:
req.sanitizeQuery('searchQuery');
Run Code Online (Sandbox Code Playgroud)
当我然后检查查询是否已被清理时,该字符串没有以任何方式被更改/清理。
我在这里可能从根本上误解了消毒,在这种情况下,请指出。如果我是,那么我可以去填补我的知识空白,但与此同时,我可以向我的消毒剂抛出什么“测试”查询以检查它是否有效?
查看文档,express-validator旨在用作中间件。
所以我会说你想要一些看起来像这样的代码:
const { validationResult } = require('express-validator/check');
const { sanitizeQuery } = require('express-validator/filter');
// Setup the request handler, give it some validation middleware
// then the main request handler
app.get('/search', [sanitizeQuery('searchQuery').escape()], function(req, res, next) {
// Deal with any errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.mapped() });
}
// req.query.searchQuery was sanitised via the middleware, it should now
// be clean.
console.log(req.query.searchQuery);
});
Run Code Online (Sandbox Code Playgroud)
我们使用 sanitizeQuery 函数作为中间件来清理 value req.query.searchQuery。我假设因为它是一个消毒功能,它不会触发来自验证结果的任何错误,而是会为您返回一个干净的响应。
然后,您应该能够在您的服务主机{{host}}/search?searchQuery= <script>Malicious code</script>所在的位置请求您的服务,{{host}}例如http://localhost:8080.