小编Rei*_*ann的帖子

在 sandbox-iframe 中嵌入 youtube-iframe 失败

当将 youtube-iframe 嵌入到另一个已沙箱化的 iframe 中(以防止 XSS)时,播放器在所有主要浏览器中都会保持黑色。

请参阅https://jsfiddle.net/ms9fwLbk/

<!DOCTYPE html><html lang='en'><head><title>Sandbox iframe youtube problem</title></head><body>
<iframe sandbox='allow-scripts allow-presentation' width='600' height='400' srcdoc='
     <!DOCTYPE html><html lang="en"><head><title>iframe</title></head><body>
     <!-- original source-code shared from youtube: -->
     <iframe width="560" height="315"
         src="https://www.youtube.com/embed/QwS1r1mc888?controls=0"
         frameborder="0"
         allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
         allowfullscreen></iframe>
     </body></html>
'></iframe></body></html>
Run Code Online (Sandbox Code Playgroud)

浏览器的控制台日志显示以下错误,因为 youtube-api 在 CORS 方面存在问题(此处没有allow-same-origin 选项,因为允许脚本已启用并且会使 cookie 容易受到攻击)

Uncaught DOMException: Failed to read the 'cookie' property from 'Document':
The document is sandboxed and lacks the 'allow-same-origin' flag.
at Vb.m.get (https://www.youtube.com/yts/jsbin/www-embed-player-vflwaq4V_/www-embed-player.js:142:47)
Run Code Online (Sandbox Code Playgroud)
Access to XMLHttpRequest at 'https://www.youtube.com/error_204?...'
from origin …
Run Code Online (Sandbox Code Playgroud)

youtube-api youtube-iframe-api

6
推荐指数
1
解决办法
6204
查看次数

标签 统计

youtube-api ×1

youtube-iframe-api ×1