[英]Establish parent origin for postMessage communication
我有一個嵌入到 3rd 方站點的 iframe,我希望這些站點能夠通過postMessage
與所述 iframe 進行通信。 我想將可以與 iframe 通信的來源限制為原始(實例化)網頁的來源。 我目前通過將原點附加到 iframe 來實現這一點:
<iframe src = "https://whatever.com/iframe?host=${window.location.href}" />
但是,由於超出了這個問題的范圍,我想放棄這種方法。 因此,我正在考慮通過window.parent
和ducument.referrer
的組合來驗證消息。 就像是:
const originatingOrigin = parse(document.referrer).origin;
document.addEventListener("message", function (message)
{
if (message.origin !== originatingOrigin || window.parent !== message.source)
return;
});
這是否 1) 與我在自己中傳遞 URL 時一樣安全,2) 萬無一失,因為沒有阻止 document.referrer 的情況,比如說,在這種情況下 API 將被破壞。
因此,關注的范圍是:
null/unavailable/incorrect
。 我知道如果您例如通過重定向,它可能會有所不同,但鑒於我的腳本將創建 iframe 並設置 src,這種情況不應該成為問題。似乎設置 Referrer-Policy: no-referrer 會使 document.referrer 不起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.