如何使 iframe 内部的 eval() 安全?

How to make eval() inside of an iframe safe?

提问人:128912891289 提问时间:7/30/2023 更新时间:7/30/2023 访问量:60

问:

我的目标(不使用 Web 应用程序或 Node.js)是拥有它,以便我可以运行可以引用外部库的用户创建的 Javascript 代码(即。Google Maps API),将变量从主网页发送到 iframe,并且从 iframe 到主网页的交互为零(即弹出窗口)。我正在使用 eval 在 iframe 中执行以创建一个隔离的环境。

在我的主页上,我计划访问存储在 AWS 上的敏感数据。当前的设置是否足以防止 XSS 和任何违反数据库信息的行为(这是我这样做的主要目的)?或者它的沙盒化方式是否存在任何显着缺陷?我能做些什么来让它更安全?欢迎提出建议。相关代码如下。谢谢。

我试过使用这个这个,但似乎它们不是很安全,很难在其中使用外部库。

index.html(主网页):

<iframe id="sandbox-iframe" src="sandbox.html" frameBorder="0" style="flex-grow: 1; border: none; margin: 0; padding: 0; height: calc(100% - 30px)" sandbox="allow-scripts" referrerpolicy="no-referrer"></iframe>

<script type="module" lang="js">
   const iframe = document.getElementById('sandbox-iframe');
   var myString = "Hello from the parent page!";
   iframe.contentWindow.postMessage(myString, '*');
</script>

沙盒.html(沙盒网页):

<script src="https://maps.googleapis.com/maps/api/js?key=KEY&callback=Map"></script>
<script type="module" lang="js">
   var receivedString = ""; 
   window.addEventListener('message', function(event) { 
      receivedString = event.data; 
      console.log('Received string:', receivedString); 
      eval(receivedString);
   });
</script>
JavaScript HTML 安全 iframe XSS

评论


答: 暂无答案