#504·Mock

响应头Content-Security-Policy "script-src 'self' 导致不能使用eval【BUG】

Author: seafronthuCreated Mar 29, 2023Updated Aug 26, 2025
javascript
 try {
	            // 1. 尝试保持参数的类型
	            /*
	                #24 [Window Firefox 30.0 引用 占位符 抛错](https://github.com/nuysoft/Mock/issues/24)
	                [BX9056: 各浏览器下 window.eval 方法的执行上下文存在差异](http://www.w3help.org/zh-cn/causes/BX9056)
	                应该属于 Window Firefox 30.0 的 BUG
	            */
	            /* jshint -W061 */
	            params = eval('(function(){ return [].splice.call(arguments, 0 ) })(' + params + ')')
	        } catch (error) {
	            // 2. 如果失败,只能解析为字符串
	            // console.error(error)
	            // if (error instanceof ReferenceError) params = parts[2].split(/,\s*/);
	            // else throw error
	            params = parts[2].split(/,\s*/)
	        }

使用mock("@cname")中,发现有使用eval,但该方法在Content-Security-Policy "script-src 'self' 不能使用,于是进入到catch,可是parts下标为2的元素是undefined。 image