利用瀏覽器自動攜帶 Cookie特性,誘導已登入用戶向信任站點發送偽造請求。
<img src="https://webapp.com/project/1/destroy">webapp.com Session 仍有效(未登出)關鍵:惡意標籤可置於任何域名(論壇、部落格、郵件)。
| GET | 查詢、讀取、安全操作、冪等 |
|---|---|
| POST | 狀態變更、訂閱、用戶負責結果 |
# Controller 限制動詞
verify :method => :post, :only => [:transfer], :redirect_to => {:action => :list}
圖片標籤只發 GET → 此層防護有效。
攻擊者可動態建構 POST 表單:
<a href="https://harmless.com/" onclick="
var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = 'https://example.com/account/destroy';
f.submit();
return false;">無害問卷</a>
或 onmouseover、AJAX 背景發送。根治:Server-side Token 驗證。
# ApplicationController
protect_from_forgery :secret => "123456789012345678901234567890..."
# CookieStore 不需 secret
authenticity_token(基於 Session + Secret 計算)ActionController::InvalidAuthenticityTokenXSS 可讀取頁面任意元素 → 竊取 Token 或直接提交表單。XSS 防護是 CSRF 防護的前提。