我在下面的代码块中得到了跨站点脚本(XSS)攻击的反映,
const getData = async function (req, res) {
const { query } = req.body;
const requestBody = formRequestBody(query);
const { authorization } = req.headers;
try {
const { data } = await axios(url, {
body: requestBody,
headers: {
Authorization: authorization
}
});
if (data) {
res.send(data);
}
} catch (error) {
console.log(error);
}
};我尝试添加一个清理函数,并对查询字符串进行清理,如下所示
const sanitizeString = (string) => {
const escapeCharsMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
};
const reg = /[&<>"'/]/gi;
return string.replace(reg, (match) => escapeCharsMap[match]);
};我将formRequestBody(query)更改为formRequestBody(sanitizeString(query)),但仍然有问题。
我该怎么解决它呢?
发布于 2021-04-26 18:17:05
XSS攻击的工作原理是攻击者诱使用户发出特定的请求,通常是通过攻击者的网站。
更改从您的站点发出请求的代码不会对攻击者站点上运行的代码产生任何影响。
在从url指向的任何内容生成输出时,您需要防御XSS,而不是通过控制从您的站点到它的输入。
https://stackoverflow.com/questions/67264747
复制相似问题