V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Zhuzhuchenyan
V2EX  ›  程序员

分享一道今天面试遇到的面试题,前端开发相关

  •  
  •   Zhuzhuchenyan · 8 小时 22 分钟前 · 301 次点击

    给定一个 host.html

    <!doctype html>
    <meta charset="utf-8" />
    <title>host</title>
    
    <iframe
      id="outer"
      sandbox="allow-scripts"
      src="https://another-site/sandbox.html"
    ></iframe>
    

    其中 sandbox.html

    <!doctype html>
    <meta charset="utf-8" />
    <title>sandbox</title>
    
    <script>
      const inner = document.createElement('iframe');
      inner.src = 'https://another-another-site/inner.html';
      document.body.appendChild(inner);
    </script>
    

    其中 inner.html

    <!doctype html>
    <meta charset="utf-8" />
    <title>inner</title>
    
    <script>
        const v = localStorage.getItem('no_such_key');
        console.log('value:', v);
    </script>
    

    问:试分析localStorage.getItem('no_such_key')的行为


    我的回答:这大概率会抛出一个 SecurityError ,我的映像里因为 sandbox 的缘故 iframe 里面的网页浏览器是不会指定 origin 的。没有 origin 的话大概率 localstorage, session storage, index db 这一类都不可用

    Follow up

    1. iframe src 和 iframe origin 的关系
    2. sandbox 里哪个属性控制这个行为?
    3. 在 inner html 里类似 fetch(..., { credentials: "include" })请求在哪些情况会带上 cookie
    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   868 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 22:12 · PVG 06:12 · LAX 14:12 · JFK 17:12
    ♥ Do have faith in what you're doing.