Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何对其他(第三方)脚本隐藏DOM?_Javascript_Security_Dom - Fatal编程技术网

Javascript 如何对其他(第三方)脚本隐藏DOM?

Javascript 如何对其他(第三方)脚本隐藏DOM?,javascript,security,dom,Javascript,Security,Dom,我想也许我可以这样做,但这是不可能的: // My app module, contains ALL my app code. // This is the first script to execute. (function () { var document = window.document delete window.document // doesn't work. :( Object.freeze(window) // at least this works. //

我想也许我可以这样做,但这是不可能的:

// My app module, contains ALL my app code.
// This is the first script to execute.
(function () {
  var document = window.document
  delete window.document // doesn't work. :(
  Object.freeze(window) // at least this works.

  // now my app can do whatever it needs to do, securely...
})()

// 3rd party code (f.e. Chrome extensions) can not access DOM,
// because window.document doesn't exist.
不幸的是,它不起作用,因为我们无法更改window.document的值。我还尝试了
document.body=null
document.children=null
等,以防止遍历DOM,但这是不可能的


是否有一种方法可以阻止(可能未知)第三方代码,使第三方代码无法读取我们的DOM?

否,如果代码已经在运行,则不会。如果您想从一开始就防止这种情况发生,请查看内容安全策略。Chrome扩展或用户脚本可以在页面加载之前运行,因此总会有办法摆脱任何保护。
(函数(窗口、文档等){“严格使用”;eval(someCode)}()。。。它不是100%协同攻击,但它应该“摧毁”99%你不想工作的东西…@dandavis我猜在你的例子中
someCode
是第三方代码。我指的是我无法访问的第三方代码(例如,chrome扩展或@Gothdo提到的用户脚本)。您可以将页面上的内容包装到web组件的影子dom中,这当然会减少可以随意读取dom的工具数量。。。除此之外,不:如果你能跑,你就不能隐藏。