Same Origin Policy & iframe

Mariko Kosaka

在本程式碼研究室中,瞭解存取 iframe 內的資料時,相同來源政策的運作方式。

設定:來源相同 iframe 的網頁

本頁面在同一個來源中嵌入了名為 iframe.htmliframe。主機和 iframe 的來源相同,因此代管網站可以存取 iframe 中的資料,並曝露秘密訊息 (例如 blow)。

const iframe = document.getElementById('iframe');
const message = iframe.contentDocument.getElementById('message').innerText;

變更為跨來源 iframe

請嘗試將 iframesrc 變更為 https://other-iframe.glitch.me/。 代管網頁仍可存取秘密訊息嗎?

主機和嵌入的 iframe 沒有相同的來源,因此資料存取權會受到限制。