之前有遇到類似跨域問題,對此作小結。主要內容包括,跨域的基礎知識和跨域攻擊。
跨域 定義:跨域HTTP請求(Cross-site HTTP request),顧名思義指發起請求的資源所在域不同於該請求所指向資源所在的域的 HTTP 請求。
同域 定義:如果兩個頁面的協議,端口(如果有指定)和域名都相同,則兩個頁面具有相同的源(域)。
跨域原因 | 域1 | 域2 |
不同端口 | whu.edu.cn:80 | whu.edu.cn:8080 |
不同域名 | drkt.whu.edu.cn | zsb.whu.edu.cn |
不同協議 | https://whu.edu.cn | http://whu.edu.cn |
起源:同源策略|SOP(Same origin policy)由Netscape公司1995年提出,它是瀏覽器最核心也最基本的安全策略。
同源策略限制:
- DOM對象
- JS對象
- Cookie,LocalStorage
- XmlHttpRequest請求(Ajax請求)
用途:用於防止 跨站請求僞造攻擊|CSRF
(Cross-site request forgery)
A cookie associated with a cross-site resource at http://hm.baidu.com/ was set without the `SameSite` attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with `SameSite=None` and `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.
上述是一個跨域請求Cookie的例子,是baidu統計代碼在谷歌遊覽器中的問題。
在開發實際遇到的問題是,用戶通過僞造請求,提交小遊戲成績。
解決方案:
使用Django時,在setting中刪除django.middleware.csrf.CsrfViewMiddleware