FixVibe
Covered by FixVibehigh

CSRF 保護:防禦未經授權的狀態更改 ZXCVFIXVIBESEND ZXCVFIXVIBESEG1 了解如何使用 Django 中間件和 SameSite cookie 屬性來防止跨站點請求偽造 (CSRF)。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG2 跨站請求偽造 (CSRF) 仍然是對 Web 應用程式的重大威脅。這項研究探討了 Django 等現代框架如何實現保護,以及 SameSite 等瀏覽器級屬性如何針對未經授權的請求提供深度防禦。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG3 ## 影響 ZXCVFIXVIBESEND ZXCVFIXVIBESEG4 跨站點請求偽造 (CSRF) 允許攻擊者欺騙受害者的瀏覽器,在受害者目前經過身份驗證的不同網站上執行不必要的操作。由於瀏覽器會自動在請求中包含環境憑證(例如 cookie),因此攻擊者可以在使用者不知情的情況下偽造狀態變更操作,例如變更密碼、刪除資料或啟動交易。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG5 ## 根本原因 ZXCVFIXVIBESEND ZXCVFIXVIBESEG6 CSRF 的根本原因是 Web 瀏覽器的預設行為,即每當向某個網域發出請求時,都會發送與該網域關聯的 cookie,而不管請求的來源 ZXCVFIXVIBETOKEN0ZXCV。如果沒有具體驗證請求是從應用程式自己的使用者介面有意觸發的,伺服器就無法區分合法的使用者操作和偽造的使用者操作。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG7 ## Django CSRF 保護機制 ZXCVFIXVIBESEND ZXCVFIXVIBESEG8 Django 提供了一個內建的防禦系統,透過中間件和模板整合 ZXCVFIXVIBETOKEN0ZXCV 來減輕這些風險。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG9 ### 中介軟體激活 ZXCVFIXVIBESEND ZXCVFIXVIBESEG10 ZXCVFIXVIBETOKEN0ZXCV 負責 CSRF 保護,通常預設啟用 ZXCVFIXVIBETOKEN1ZXCV。它必須位於任何假設 CSRF 攻擊已被處理的視圖中間件 ZXCVFIXVIBETOKEN2ZXCV 之前。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG11 ### 模板實現 ZXCVFIXVIBESEND ZXCVFIXVIBESEG12 對於任何內部 POST 表單,開發人員必須在 ZXCVFIXVIBETOKEN1ZXCV 元素 ZXCVFIXVIBETOKEN2ZXCV 內包含 ZXCVFIXVIBETOKEN0ZXCV 標籤。這可確保請求中包含唯一的秘密令牌,然後伺服器根據使用者的會話對其進行驗證。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG13 ### 代幣洩漏風險 ZXCVFIXVIBESEND ZXCVFIXVIBESEG14 一個關鍵的實作細節是 ZXCVFIXVIBETOKEN0ZXCV 永遠不應包含在針對外部 URL ZXCVFIXVIBETOKEN1ZXCV 的表單中。這樣做會將秘密 CSRF 令牌洩漏給第三方,可能會危及用戶的會話安全 ZXCVFIXVIBETOKEN2ZXCV。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG15 ## 瀏覽器級防禦:SameSite Cookie ZXCVFIXVIBESEND ZXCVFIXVIBESEG16 現代瀏覽器為 ZXCVFIXVIBETOKEN1ZXCV 標頭引入了 ZXCVFIXVIBETOKEN0ZXCV 屬性,以提供一層深度防禦 ZXCVFIXVIBETOKEN2ZXCV。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG17 - **嚴格:** cookie 僅在第一方上下文中傳送,這表示 URL 欄中的網站與 cookie 的網域 ZXCVFIXVIBETOKEN0ZXCV 相符。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG18 - **寬鬆:** Cookie 不會在跨站點子請求(例如圖像或框架)上發送,而是在用戶導航到來源站點時發送,例如透過標準連結 ZXCVFIXVIBETOKEN0ZXCV。 ZXCVFIXVIBESEND ZXCVFIXVIBESEG19 ## ZXCVFIXVIBETOKEN0ZXCV 如何測試它 ZXCVFIXVIBESEND ZXCVFIXVIBESEG20 ZXCVFIXVIBETOKEN1ZXCV 現在包含 CSRF 保護作為閘控主動檢查。網域驗證後,ZXCVFIXVIBETOKEN0ZXCV 檢查發現的狀態變更表單,檢查 CSRF 令牌形狀的輸入和 SameSite cookie 訊號,然後嘗試低影響的偽造來源提交,並且僅在伺服器接受時報告。 Cookie 檢查也會標記弱 SameSite 屬性,從而減少 CSRF 縱深防禦。

Cross-Site Request Forgery (CSRF) remains a significant threat to web applications. This research explores how modern frameworks like Django implement protection and how browser-level attributes like SameSite provide defense-in-depth against unauthorized requests.

CWE-352

Impact

Cross-Site Request Forgery (CSRF) allows an attacker to trick a victim's browser into performing unwanted actions on a different website where the victim is currently authenticated. Because browsers automatically include ambient credentials like cookies in requests, an attacker can forge state-changing operations—such as changing passwords, deleting data, or initiating transactions—without the user's knowledge.

Root Cause

The fundamental cause of CSRF is the web browser's default behavior of sending cookies associated with a domain whenever a request is made to that domain, regardless of the request's origin [S1]. Without specific validation that a request was intentionally triggered from the application's own user interface, the server cannot distinguish between a legitimate user action and a forged one.

Django CSRF Protection Mechanisms

Django provides a built-in defense system to mitigate these risks through middleware and template integration [S2].

Middleware Activation

The django.middleware.csrf.CsrfViewMiddleware is responsible for CSRF protection and is typically enabled by default [S2]. It must be positioned before any view middleware that assumes CSRF attacks have already been handled [S2].

Template Implementation

For any internal POST forms, developers must include the {% csrf_token %} tag inside the <form> element [S2]. This ensures that a unique, secret token is included in the request, which the server then validates against the user's session.

Token Leakage Risks

A critical implementation detail is that the {% csrf_token %} should never be included in forms targeting external URLs [S2]. Doing so would leak the secret CSRF token to a third party, potentially compromising the user's session security [S2].

Browser-Level Defense: SameSite Cookies

Modern browsers have introduced the SameSite attribute for the Set-Cookie header to provide a layer of defense-in-depth [S1].

  • Strict: The cookie is only sent in a first-party context, meaning the site in the URL bar matches the cookie's domain [S1].
  • Lax: The cookie is not sent on cross-site subrequests (such as images or frames) but is sent when a user navigates to the origin site, such as by following a standard link [S1].

How FixVibe tests for it

FixVibe now includes CSRF protection as a gated active check. After domain verification, active.csrf-protection inspects discovered state-changing forms, checks for CSRF-token-shaped inputs and SameSite cookie signals, then attempts a low-impact forged-origin submission and only reports when the server accepts it. Cookie checks also flag weak SameSite attributes that reduce CSRF defense-in-depth.