同源政策

本頁使用了標題或全文手工轉換
維基百科,自由的百科全書

同源策略(英語:Same-origin policy)是指在Web瀏覽器中,允許某個網頁腳本訪問另一個網頁的數據,但前提是這兩個網頁必須有相同的URI主機名端口號,一旦兩個網站滿足上述條件,這兩個網站就被認定為具有相同來源。此策略可防止某個網頁上的惡意腳本通過該頁面的文檔對象模型訪問另一網頁上的敏感數據。

同源策略對Web應用程序具有特殊意義,因為Web應用程序廣泛依賴於HTTP cookie[1]來維持用戶會話,所以必須將不相關網站嚴格分隔,以防止丟失數據洩露。

值得注意的是同源策略僅適用於腳本,這意味着某網站可以通過相應的HTML標籤[2]訪問不同來源網站上的圖像CSS動態加載腳本等資源。而跨站請求偽造就是利用同源策略不適用於HTML標籤的缺陷。

相同來源網站舉例[編輯]

下表列出哪些URLURL http://www.example.com/dir/page.html 屬於相同來源:

URL 結果 原因
http://www.example.com/dir/page2.html 只有路徑不同
http://www.example.com/dir2/other.html 只有路徑不同
http://username:password@www.example.com/dir2/other.html 只有路徑不同
http://www.example.com:81/dir/other.html 不同端口(若未標明,http:// 默認端口號為80)
https://www.example.com/dir/other.html[失效連結] 不同協議httpshttp
http://en.example.com/dir/other.html 不同域名
http://example.com/dir/other.html 不同域名(需要完全匹配)
http://v2.www.example.com/dir/other.html 不同域名(需要完全匹配)

另見[編輯]

參考文獻[編輯]

  1. ^ IETF [rfc:6265 HTTP State Management Mechanism, Apr, 2011]
  2. ^ Kemp, John. Security on the Web. 2011-02-04 [2018-07-24]. (原始內容存檔於2020-09-23). The same-origin policy states that a document from one unique origin may only load resources from the origin from which the document was loaded. In particular this applies to XMLHttpRequest calls made from within a document. Images, CSS and dynamically-loaded scripts are not subject to same-origin policy. 

外部連結[編輯]