同源政策

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

同源政策(英語: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. 

外部連結[編輯]