替代 eval() 从服务器执行自动生成的 JS 代码
Alternate for eval() to execute auto-generated JS code from the server
var val = 3;
var code = "var a = 5; if (a >= val) { console.log(a + ' >= ' + val); a; } else { console.log(a + ' < 3 ' + val); val; }";
console.log(eval(code));
这是需要替代 eval() 的场景。
服务器可以发送任何类型的 JS 代码,可以是 运行 在特定块上。
你可以这样做。不推荐使用 Eval()
。
function looseJsonParse(obj){
return Function('"use strict";return (' + obj + ')')();
}
console.log(looseJsonParse(
"{a:(4-1), b:function(){}, c:new Date()}"
))
参考这篇 MDN 文章 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
深入研究它。
不要使用 eval(code)
或 new Function(code)
,因为两者基本上是一样的,应该被 CSP 阻止。
只需 return 来自服务器的 content-type: text/javascript
内容,然后使用 <script>
块或 import
.
将其放入您的页面
在服务器上你会有类似的东西(伪代码,因为我不知道你使用的是什么技术堆栈):
[Route("serverActionReturningCode")]
public string ActionReturningCode()
{
// return the content as JS
Response.Headers.Add("content-type", "text/javascript");
// build the response object as JS
return "window.latestResult = {" +
"a: '" + a + "', " +
"b: '" + b + "', " +
"generatedCode: function() { ... }" +
"};";
}
然后在您的页面中:
<script src="serverActionReturningCode"></script>
<script>
// Now the script above has run and set window.latestResult
console.log('a', window.latestResult.a);
console.log('b', window.latestResult.b);
console.log('function output', window.latestResult.generatedCode());
</script>
这将使您可以在服务器上动态生成 JS 函数。
但是,如果您可以避免使用这些函数而只需要传递值,那么使用 JSON 会简单得多。
似乎除了使用 eval 或更改应用程序的整个设计之外别无他法。即使我们寻找任何其他替代方案,也将是名称和语法的更改。但安全问题将是相同的。它是应用程序的设计,服务器中的 JS CodeGen 工具将生成 JS 代码片段并通过 JSON 发送到某些字段,必须在前端选择和执行。但在这个设计中,我们可以确定一件事,JS 代码仅在用户设计时生成,而不是在运行时生成。
感谢您的帮助。
var val = 3;
var code = "var a = 5; if (a >= val) { console.log(a + ' >= ' + val); a; } else { console.log(a + ' < 3 ' + val); val; }";
console.log(eval(code));
这是需要替代 eval() 的场景。
服务器可以发送任何类型的 JS 代码,可以是 运行 在特定块上。
你可以这样做。不推荐使用 Eval()
。
function looseJsonParse(obj){
return Function('"use strict";return (' + obj + ')')();
}
console.log(looseJsonParse(
"{a:(4-1), b:function(){}, c:new Date()}"
))
参考这篇 MDN 文章 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval 深入研究它。
不要使用 eval(code)
或 new Function(code)
,因为两者基本上是一样的,应该被 CSP 阻止。
只需 return 来自服务器的 content-type: text/javascript
内容,然后使用 <script>
块或 import
.
在服务器上你会有类似的东西(伪代码,因为我不知道你使用的是什么技术堆栈):
[Route("serverActionReturningCode")]
public string ActionReturningCode()
{
// return the content as JS
Response.Headers.Add("content-type", "text/javascript");
// build the response object as JS
return "window.latestResult = {" +
"a: '" + a + "', " +
"b: '" + b + "', " +
"generatedCode: function() { ... }" +
"};";
}
然后在您的页面中:
<script src="serverActionReturningCode"></script>
<script>
// Now the script above has run and set window.latestResult
console.log('a', window.latestResult.a);
console.log('b', window.latestResult.b);
console.log('function output', window.latestResult.generatedCode());
</script>
这将使您可以在服务器上动态生成 JS 函数。
但是,如果您可以避免使用这些函数而只需要传递值,那么使用 JSON 会简单得多。
似乎除了使用 eval 或更改应用程序的整个设计之外别无他法。即使我们寻找任何其他替代方案,也将是名称和语法的更改。但安全问题将是相同的。它是应用程序的设计,服务器中的 JS CodeGen 工具将生成 JS 代码片段并通过 JSON 发送到某些字段,必须在前端选择和执行。但在这个设计中,我们可以确定一件事,JS 代码仅在用户设计时生成,而不是在运行时生成。 感谢您的帮助。