express中設置cookie的httpOnly屬性防禦xss攻擊

大部分是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.

但是express本身提供的cookie的api默認值卻是false, 需要手動設置爲true. 代碼如下:

res.cookie('rememberme', '1', {httpOnly: true });


發佈了97 篇原創文章 · 獲贊 18 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章