简体   繁体   English

关闭对全局变量的访问javascript

[英]Closing access to global variables javascript

I'm coding a js API that is going to be used by external customers and executed by my customers in their web browsers. 我正在编写一个js API,供外部客户使用,并由我的客户在其Web浏览器中执行。

As this is potentially harmful for my web users (security holes etc.), I'd like to allow or disallow access to, at least, the document global variable and others like the XMLHTTPRequest API. 由于这可能会对我的Web用户造成危害(安全漏洞等),因此我想允许或禁止访问至少document全局变量和XMLHTTPRequest API等其他变量。

How can I do this? 我怎样才能做到这一点?

EDIT: I was thinking on doing things like document = null on a wrapper to the functions the API users write, but it doesn't work. 编辑:我正在考虑对API用户编写的函数进行包装时,将document = null用作类似的操作,但是它不起作用。 Also, using this kind of approach it is difficult to be thorough as there are too many workarounds and too many possibilities to take in account all of them. 同样,使用这种方法很难做到彻底,因为有太多的解决方法,太多的可能性无法考虑到所有这些。

The answer is simple: You can't. 答案很简单:您不能。

This may not be answer want, but those global variables can't be modified. 这可能不是想要的答案,但是这些全局变量无法修改。

Try for yourself: 自己尝试:

window = 1;
console.log(window) // Window {top: Window, window: Window, ...

document = false;
console.log(document) // #document (as in the document object)
document = null;
console.log(document) // #document (same)

window.document = false;
console.log(window.document) // #document

However, this does seem to be possible for XMLHttpRequest : 但是,这对于XMLHttpRequest似乎确实可行:

XMLHttpRequest = null
console.log(XMLHttpRequest)        // null
console.log(window.XMLHttpRequest) // null

So, you might be able to disable individual functions. 因此,您可能可以禁用单个功能。

However, messing with native functionality like this is a bad idea, since it can have unintended side effects. 但是,将这样的本机功能弄乱是一个主意,因为它可能会产生意想不到的副作用。 For example, jQuery uses XMLHttpRequest for it's ajax functions. 例如,jQuery将XMLHttpRequest用于其ajax函数。

Caja 卡哈

The Caja Compiler is a tool for making third party HTML, CSS and JavaScript safe to embed in your website. Caja编译器是一种使第三方HTML,CSS和JavaScript安全地嵌入您的网站的工具。 It enables rich interaction between the embedding page and the embedded applications. 它使嵌入页面和嵌入式应用程序之间可以进行丰富的交互。 Caja uses an object-capability security model to allow for a wide range of flexible security policies, so that your website can effectively control what embedded third party code can do with user data. Caja使用对象能力安全模型来实现各种灵活的安全策略,以便您的网站可以有效地控制嵌入式第三方代码对用户数据的作用。


ADSafe ADSafe

JavaScript, the programming language of the web browser, is not a secure language. JavaScript(Web浏览器的编程语言)不是安全的语言。 Any script in a page has intimate access to all of the information and relationships of the page. 页面中的任何脚本都可以直接访问页面的所有信息和关系。 This makes use of mashups and scripted advertising unacceptably risky. 这使得混搭和脚本广告的使用风险不可接受。

ADsafe makes it safe to put guest code (such as third party scripted advertising or widgets) on a web page. 使用ADsafe可以安全地将访客代码(例如第三方脚本广告或窗口小部件)放置在网页上。 ADsafe defines a subset of JavaScript that is powerful enough to allow guest code to perform valuable interactions, while at the same time preventing malicious or accidental damage or intrusion. ADsafe定义了JavaScript的子集,该子集的功能足以允许来宾代码执行有价值的交互,同时防止恶意或意外损坏或入侵。 The ADsafe subset can be verified mechanically by tools like JSLint so that no human inspection is necessary to review guest code for safety. 可以通过诸如JSLint之类的工具对ADsafe子集进行机械验证,因此无需人工检查即可查看来宾代码的安全性。 The ADsafe subset also enforces good coding practices, increasing the likelihood that guest code will run correctly. ADsafe子集还执行良好的编码习惯,从而增加了来宾代码正确运行的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM