express中设置cookie的httpOnly属性防御xss攻击
程序员文章站
2024-03-20 14:03:58
...
大部分是xss攻击(跨站脚本攻攻击),都是尝试在客户的浏览器中注入脚本,然后获取cookie发送到黑客指定的地址。因为服务端的session都是通过一个记录seesionId的cookie来识别的。黑客拿到了cookie, 自然就能够伪造身份,进而获取到权限。cookie的httpOnly属性意味着,浏览器中不能通过document.cookie访问到这个cookie,从而达到防御xss攻击的目的。
在express-session中,默认已经开启了cookie的httpOnly: true,原文说明如下
cookie.httpOnly
Specifies the boolean
value
for the HttpOnly
Set-Cookie
attribute.
When truthy, the HttpOnly
attribute
is set, otherwise it is not. By default, the HttpOnly
attribute
is set.
res.cookie('rememberme', '1', {httpOnly: true });
上一篇: C语言实现顺序表(静态)
下一篇: express中使用cookie