micro-app icon indicating copy to clipboard operation
micro-app copied to clipboard

请问code2Function中用到的new Function 有其他替代的方案吗?

Open axlroseart opened this issue 2 years ago • 2 comments

web服务器响应头配置有Content-Security-Policy,因此无法使用 new Function 或者 eval() 这样的方式来执行JS代码,所以想请教一下,有其他不会被CSP限制的方式来实现吗?

axlroseart avatar May 27 '22 03:05 axlroseart

可以配置inline模式,此时通过script标签执行js https://micro-zoe.github.io/micro-app/docs.html#/zh-cn/configure?id=inline

bailicangdu avatar May 27 '22 05:05 bailicangdu

Content-Security-Policy

刚刚跑 Nginx 测试了一下,大佬说的这个好像并不正确,改用 script 执行的方式并不能完全解决这个问题,假如其设置的为 default-src 或者设置了 script-src 一样会限制,如果仅设置了 connect-src 则不会受影响。

那么怎么解决呢?答案很简单,就是在 Nginx 加上被限制的域名即可。

dream2023 avatar May 28 '22 01:05 dream2023

Nginx配置限制的域名,这个不行吧,现在code2Function方法里面有new Function 逻辑,需要script-src配置unsafe-eval才能执行,否则会报错。然后如果csp的script-src开启unsafe-eval,那就引入新的风险了

enpenguc avatar Mar 21 '23 13:03 enpenguc