首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何设置Web工作人员在Edge / Safari中工作的内容安全策略?

如何设置Web工作人员在Edge / Safari中工作的内容安全策略?
EN

Stack Overflow用户
提问于 2019-02-16 02:08:15
回答 1查看 485关注 0票数 5

在尝试使用Web Worker时,我不断收到错误代码: 18,来自Edge和Safari的SecurityError。然而,工作人员在Firefox / Chrome中很好。我使用的是一个内联worker,我将零依赖数据处理函数传递给它。

我的CSP看起来:

代码语言:javascript
复制
add_header Content-Security-Policy "default-src 'self'; worker-src 'self' 'inline' *.example.com";

我可以自己添加其他的好东西,比如本地样式表和googleapis.com,但是我很好奇如何让工作人员不抛出安全错误

来自worker method的代码片段

代码语言:javascript
复制
// Create an "inline" worker (1:1 at definition time)
    const worker = new Worker(
        // Use a data URI for the worker's src. It inlines the target function and an RPC handler:
        'data:,$$='+asyncFunction+';onmessage='+(e => {
            /* global $$ */

            // Invoking within then() captures exceptions in the supplied async function as rejections
            Promise.resolve(e.data[1]).then(
                v => $$.apply($$, v)
            ).then(
                // success handler - callback(id, SUCCESS(0), result)
                // if `d` is transferable transfer zero-copy
                d => {
                    postMessage([e.data[0], 0, d], [d].filter(x => (
                        (x instanceof ArrayBuffer) ||
                        (x instanceof MessagePort) ||
                        (x instanceof ImageBitmap)
                    )));
                },
                // error handler - callback(id, ERROR(1), error)
                er => { postMessage([e.data[0], 1, '' + er]); }
            );
        })
    );

Edge为worker抛出以下错误:

代码语言:javascript
复制
  [object DOMException]: {code: 18, message: "SecurityError", name: 
    "SecurityError"}
    code: 18
    message: "SecurityError"
    name: "SecurityError"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-02 09:08:50

我不确定为什么数据url会导致安全错误,但你可以使用URL.createObjectURL加载一个worker脚本,它似乎在Edge中工作正常(我没有在safari中测试它)。

下面是它应该是什么样子的:

代码语言:javascript
复制
// Create the worker script as a string
const script = '$$='+asyncFunction+';onmessage='+(e => {
        /* global $$ */

        // Invoking within then() captures exceptions in the supplied async function as rejections
        Promise.resolve(e.data[1]).then(
            v => $$.apply($$, v)
        ).then(
            // success handler - callback(id, SUCCESS(0), result)
            // if `d` is transferable transfer zero-copy
            d => {
                postMessage([e.data[0], 0, d], [d].filter(x => (
                    (x instanceof ArrayBuffer) ||
                    (x instanceof MessagePort) ||
                    (x instanceof ImageBitmap)
                )));
            },
            // error handler - callback(id, ERROR(1), error)
            er => { postMessage([e.data[0], 1, '' + er]); }
        );
    });

// Create a local url to load the worker
const blob = new Blob([script]);
const workerUrl = URL.createObjectURL(blob);
const worker = new Worker(workerUrl);

如果你需要任何澄清,请告诉我!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54714860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档