Nginx設置HttpOnly Secure SameSite參數(shù)解決Cookie信息丟失
一、背景說明
項目中使用了Nginx作為反向代理來訪問系統(tǒng),在漏洞掃描的過程中發(fā)現(xiàn)Cookie缺少 SameSite 屬性。我們需要將SameSite屬性設置為Strict或者 Lax。
二、Cookie安全相關屬性
HttpOnly :
- 在Cookie中設置了“HttpOnly”屬性,通過程序(JS腳本、Applet等)將無法讀取到Cookie信息。
- 將HttpOnly 設置為true 防止程序獲取cookie后進行攻擊。
Secure :
- 安全性,指定Cookie是否只能通過https協(xié)議訪問,一般的Cookie使用HTTP協(xié)議既可訪問。
- 設置了Secure (沒有值),則只有當使用https協(xié)議連接時cookie才可以被頁面訪問??捎糜诜乐剐畔⒃趥鬟f的過程中被監(jiān)聽捕獲后信息泄漏。
SameSite:
Chrome瀏覽器在51版本后為 Cookie 新增的屬性,用來防止 CSRF 攻擊和用戶追蹤。可以設置三個值:Strict、 Lax、 None
- Strict:完全禁止第三方 Cookie,跨站點時,任何情況下都不會發(fā)送 Cookie。換言之,只有當前網頁的 URL 與請求目標一致,才會帶上 Cookie。Set-Cookie: CookieName=CookieValue; SameSite=Strict;
- Lax:規(guī)則稍稍放寬,大多數(shù)情況也是不發(fā)送第三方 Cookie,但是導航到目標網址的 Get 請求除外。Set-Cookie: CookieName=CookieValue; SameSite=Lax;設置了Strict或Lax以后,基本就杜絕了 CSRF 攻擊。當然,前提是用戶瀏覽器支持 SameSite 屬性。
- None:Chrome 計劃將Lax變?yōu)槟J設置。這時,網站可以選擇顯式關閉SameSite屬性,將其設為None。不過,前提是必須同時設置Secure屬性(Cookie 只能通過 HTTPS 協(xié)議發(fā)送),否則無效。Set-Cookie: key=value; SameSite=None; Secure
瀏覽器F12 在網絡下查看當前狀態(tài),SameSite屬性缺失,我們需要通過配置nginx將SameSite屬性設置為Strict或者 Lax。

三、配置路徑
在nginx.conf 的 location 節(jié)點下進行配置
add_header Set-Cookie 'Path=/;httponly; Secure; SameSite=Lax';
下面是完整的 location 節(jié)點配置
server {
listen 443 ssl;
server_name test.app.com;
root html;
index index.html index.htm;
ssl_certificate C:/nginx/cert/client.pem;
ssl_certificate_key C:/nginx/cert/private.key;
ssl_session_timeout 4h;
# Enable SSL cache to speed up
ssl_session_cache shared:SSL:10m;
# intermediate configuration
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305;
ssl_prefer_server_ciphers off;
client_max_body_size 100m;
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# HTTP Force Jump to HTTPS
proxy_redirect http:// $scheme://;
# To resolve nginx 504 issue
proxy_connect_timeout 1800s;
proxy_send_timeout 1800s;
proxy_read_timeout 1800s;
client_body_timeout 1800s;
# Nginx status
location /nginxstatus {
stub_status on;
access_log logs/nginx-status-$logdate.log;
auth_basic "NginxStatus";
}
# Application
location /api{
# 設置HttpOnly Secure SameSite參數(shù)解決Cookie跨域丟失
add_header Set-Cookie 'Path=/;httponly; Secure; SameSite=Lax';
proxy_pass http://127.0.0.1:8080;
}
}修改后的情況

到此這篇關于Nginx設置HttpOnly Secure SameSite參數(shù)解決Cookie信息丟失的文章就介紹到這了,更多相關Nginx HttpOnly Secure SameSite參數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
服務器的負載均衡nginx+tomcat實現(xiàn)動靜分離
這篇文章主要為大家介紹了服務器的負載均衡nginx+tomcat實現(xiàn)動靜分離的案例實施步驟以及環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03
lnmp環(huán)境中如何為nginx開啟pathinfo
這篇文章主要介紹了lnmp環(huán)境中如何為nginx開啟pathinfo的方法,操作很簡單,需要的朋友可以參考下2015-01-01
關于使用Keepalived實現(xiàn)Nginx的自動重啟及雙主熱備高可用問題
這篇文章主要介紹了使用Keepalived實現(xiàn)Nginx的自動重啟及雙主熱備高可用,本文通過幾個問題解析幫助大家學習Keepalived實現(xiàn)Nginx的自動重啟的相關知識,需要的朋友可以參考下2021-09-09

