如何使eval功能安全?

时间:2017-09-27 13:20:18

标签: javascript

我正在开发一个交互式Canvas教程。我希望学生能够将JavaScript输入到一个可信的元素中,但我担心它会引入漏洞。

在考虑代码之前,我考虑过重新分配危险的对象或方法,然后再将其分配回来。

如何确定危险物品?

这是正确的方法,还是我应该扫描他们的输入并且只允许某些陈述?

3 个答案:

答案 0 :(得分:1)

如果不了解详情,很难提供一般性建议,但您可能需要调查将可修改内容放入沙盒iframe中。

ERROR TypeError: _co.archive is undefined
Trace de la pile :
View_UploadScriptFormComponent_0/<@ng:///AppModule/UploadScriptFormComponent.ngfactory.js:346:9
debugUpdateRenderer@http://localhost:4200/vendor.bundle.js:67835:12
checkAndUpdateView@http://localhost:4200/vendor.bundle.js:66982:5
callViewAction@http://localhost:4200/vendor.bundle.js:67342:21
execComponentViewsAction@http://localhost:4200/vendor.bundle.js:67274:13
checkAndUpdateView@http://localhost:4200/vendor.bundle.js:66983:5
callViewAction@http://localhost:4200/vendor.bundle.js:67342:21
execComponentViewsAction@http://localhost:4200/vendor.bundle.js:67274:13
checkAndUpdateView@http://localhost:4200/vendor.bundle.js:66983:5
callViewAction@http://localhost:4200/vendor.bundle.js:67342:21
execEmbeddedViewsAction@http://localhost:4200/vendor.bundle.js:67300:17
checkAndUpdateView@http://localhost:4200/vendor.bundle.js:66978:5
callViewAction@http://localhost:4200/vendor.bundle.js:67342:21
execComponentViewsAction@http://localhost:4200/vendor.bundle.js:67274:13
[...]
../../../../rxjs/Subject.js/Subject.prototype.next@http://localhost:4200/vendor.bundle.js:10145:17
../../../core/@angular/core.es5.js/</EventEmitter.prototype.emit@http://localhost:4200/vendor.bundle.js:58343:54
checkStable@http://localhost:4200/vendor.bundle.js:58577:13
onHasTask@http://localhost:4200/vendor.bundle.js:58625:21
../../../../zone.js/dist/zone.js/</</ZoneDelegate.prototype.hasTask@http://localhost:4200/polyfills.bundle.js:2962:21
../../../../zone.js/dist/zone.js/</</ZoneDelegate.prototype._updateTaskCount@http://localhost:4200/polyfills.bundle.js:2982:17
../../../../zone.js/dist/zone.js/</</Zone.prototype._updateTaskCount@http://localhost:4200/polyfills.bundle.js:2806:17
../../../../zone.js/dist/zone.js/</</Zone.prototype.runTask@http://localhost:4200/polyfills.bundle.js:2726:25
drainMicroTaskQueue@http://localhost:4200/polyfills.bundle.js:3119:25

您需要测试https://modernizr.com库支持的支持。根据您的实施方式,可能还需要设置适当的Content Security Policy标题。

答案 1 :(得分:0)

提供一堆学生可以分叉编辑的JSBin个游戏围栏怎么样?

这会解决您手中的安全问题。

答案 2 :(得分:-1)

如何在https://www.npmjs.com/package/vm2这样的JS'VM中运行他们的JS代码呢?它基本上是一个像eval这样的JS运行时,你可以在其中声明可用的内容。例如,您可以阻止访问fs lib。

相关问题