大部分是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 });