gpt4 book ai didi

node.js - 即使添加 'unsafe-eval' 后,对 function() 的调用也会被 CSP 阻止

转载 作者:太空宇宙 更新时间:2023-11-03 22:38:26 29 4
gpt4 key购买 nike

我正在开发一个 NodeJS 项目,并且正在使用 CSP ( Content Security Policy )。

我正在使用外部插件 FullCalendar,该插件被 csp 阻止,并出现以下错误:

错误:对 Function() 的调用被 CSP 阻止

我使用 script-src 'self' 'unsafe-eval'; 来覆盖它,但在 Firefox 中不起作用。在其他浏览器中它工作正常。

我在这个问题上卡住了 4 个小时。

获得解决方案将会很有帮助。

我在 CSP 限制中使用以下格式。

X-Content-Security-Policy: default-src *; script-src 'self' 'unsafe-eval'; object-src 'none'; style-src 'self' 'unsafe-inline img-src *;options eval-script;
X-WebKit-CSP: default-src *; script-src 'self' 'unsafe-eval'; object-src 'none'; style-src 'self' 'unsafe-inline img-src *;
Content-Security-Policy: default-src *; script-src 'self' 'unsafe-eval'; object-src 'none'; style-src 'self' 'unsafe-inline img-src *;

最佳答案

假设 this.disp 包含要计算的表达式。还有disp: document.getElementById("id_of_text_input_field")。例如。 this.disp.value = 123/45*67+8-9%10。它还会关心否定号。例如。 -123+3 = -120。耶!

compute: function compute() {

var sign = 1;
if (this.disp.value[0] == '-') sign = -1;
this.disp.value = this.calculate(this.disp.value,sign);
this.update(this.disp.value.length);
return this.disp.value;
},

calculate: function calculate(input,sign){

var opr_list = { add : '+'
, sub : '-'
, div : '/'
, mlt : '*'
, mod : '%'
};

opr_list.opr = [[ [opr_list.mlt] , [opr_list.div] , [opr_list.mod]],
[ [opr_list.add] , [opr_list.sub] ]];

input = input.replace(/[^0-9%^*\/()\-+.]/g,'');

var output,n;
for(var i=0, n=opr_list.opr.length; i<n; i++ ){

var re = new RegExp('(\\d+\\.?\\d*)([\\'+opr_list.opr[i].join('\\')+'])(\\d+\\.?\\d*)');
re.lastIndex = 0;
while( re.test(input) ){

output = this.compute_result(opr_list,sign*RegExp.$1,RegExp.$2,RegExp.$3);

if (isNaN(output) || !isFinite(output)) return output;
input = input.replace(re,output);
}
}

return output;
},

compute_result: function compute_result(opr_list,a,op,b){
a=a*1; b=b*1;
switch(op){
case opr_list.add: return a+b; break;
case opr_list.sub: return a-b; break;
case opr_list.div: return a/b; break;
case opr_list.mlt: return a*b; break;
case opr_list.mod: return a%b; break;
default: null;
}
}

您可以根据您的要求添加更多运算符案例。例如。正方形、x^y

关于node.js - 即使添加 'unsafe-eval' 后,对 function() 的调用也会被 CSP 阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18080509/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com