Coder Social home page Coder Social logo

Comments (5)

cengbin avatar cengbin commented on July 26, 2024 4

目前确实没有好的办法...

<script>
  let handler = {
    has(target, key) {
      if (key in target) {
        return target[key]
      } else {
        throw `无法访问属性或方法 ${key}, 详情请参考:https://www.google.com`
        // throw new Error(`无法访问属性或方法 ${key}, 详情请参考:https://www.google.com`)
        return undefined
      }
    }
  }

  let target = {
    console: window.console,
    Math: window.Math,
    Date: window.Date,

    name: "i am module"
  }

  let proxy = new Proxy(target, handler)

  let moduleCode = `

      console.log('this1:',this); // 非严格模式下,this指向proxy

      function foo(){
          console.log(name);

          // this === window 非严格模式下,此写法会造成沙箱逃逸
          console.log("this2:", this); // 非严格模式下,this是AO,ECMAScript规范规定AO作为this值返回时通常为null,this不能为null,重置为全局对象。
          // console.log(this.document);

          function bar(){
              var b = 10;
              console.log("b: ",b);
              console.log(Math.random());
              console.log(new Date());
              console.log(document.getElementById('p'));
              console.log(window);
          }

          bar();
      }

      foo()
  `

  let fun = new Function(`
        with(this){
            (function(){
              "use strict";
              ${moduleCode}
            })()
        }
    `)

  fun.call(proxy)
</script>

此问题已解决,在严格模式下执行用户模块代码即可。

from front-end-articles.

Zian502 avatar Zian502 commented on July 26, 2024 2

学习了 🐂

from front-end-articles.

NameWjp avatar NameWjp commented on July 26, 2024 1

越看越觉得微前端的各种实现很 hack,项目复杂度一旦上来就会有各种问题。

from front-end-articles.

cengbin avatar cengbin commented on July 26, 2024

通过Proxy+Function+with这种形式建立的安全沙箱,如果code中的函数内通过this仍然可以访问到window,请问有什么好的办法解决这种写法造成的沙箱逃逸吗?

如下示例代码:

<script>
  let handler = {
    has(target, key) {
      if (key in target) {
        return target[key]
      } else {
        throw `无法访问属性或方法 ${key}, 详情请参考:https://www.google.com`
        return undefined
      }
    }
  }

  let target = {
    console: window.console,
    Math: window.Math,
    Date: window.Date,

    name: "i am module"
  }

  let proxy = new Proxy(target, handler)

  let moduleCode = `
      function foo(){
          console.log(name);

          // this===window 此写法会造成沙箱逃逸
          console.log("this:", this);
          console.log(this.document);

      }

      foo()
  `

  let fun = new Function("window", `
        "use stric";

        with(window){
            ${moduleCode}
        }
    `)

  fun.call(proxy, proxy)
</script>

from front-end-articles.

woai3c avatar woai3c commented on July 26, 2024

目前确实没有好的办法...

from front-end-articles.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.