Skip to content

threejs-server example cannot work within current spec due to unsafe-eval #199

@connor4312

Description

@connor4312

Describe the bug

The three.js app uses this code:

async function executeThreeCode(
code: string,
canvas: HTMLCanvasElement,
width: number,
height: number,
): Promise<void> {
const fn = new Function(
"ctx",
"canvas",
"width",
"height",
`const { THREE, OrbitControls, EffectComposer, RenderPass, UnrealBloomPass } = ctx;
return (async () => { ${code} })();`,
);
await fn(threeContext, canvas, width, height);
}

This requires the unsafe-eval CSP policy. However, the server does not define a CSP policy and from my understanding of the spec it should not be provided by default

- **Restrictive Default:** If `ui.csp` is omitted, Host MUST use:
```
default-src 'none';
script-src 'self' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' data:;
media-src 'self' data:;
connect-src 'none';
```

Additionally, there is no way to make it work as the spec currently does not provide servers a way to request unsafe-eval:

```typescript
const csp = resource._meta?.ui?.csp;
const cspValue = `
default-src 'none';
script-src 'self' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
connect-src 'self' ${csp?.connectDomains?.join(' ') || ''};
img-src 'self' data: ${csp?.resourceDomains?.join(' ') || ''};
font-src 'self' ${csp?.resourceDomains?.join(' ') || ''};
media-src 'self' data: ${csp?.resourceDomains?.join(' ') || ''};
frame-src 'none';
object-src 'none';
base-uri 'self';
`;
```

To Reproduce
Steps to reproduce the behavior:

  1. Build a complient client with CSP policies
  2. Try to run the threejs example

You get an error:

Image

Expected behavior

I think the spec should be revised to support trusted types or let the UI request unsafe-eval

Logs
N/A

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions