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

问题描述 投票:0回答:0

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

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

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

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>

sandbox.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 security iframe xss
© www.soinside.com 2019 - 2024. All rights reserved.