Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Security Policy unsafe-eval violation in runtime #3103

Open
5 tasks done
danhorvath opened this issue Oct 22, 2024 · 0 comments · May be fixed by #3108
Open
5 tasks done

Content Security Policy unsafe-eval violation in runtime #3103

danhorvath opened this issue Oct 22, 2024 · 0 comments · May be fixed by #3108

Comments

@danhorvath
Copy link

Describe the bug

Our setup uses rsbuild & the rspack module-federation plugin. It seems a line in the module federation runtime violates the unsafe-eval content security policy, and then swallows the exception. This results in spamming our tracking with millions of CSP violation reports, making it impossible to identify real malicious attempts. This issue is similar to #3053 and #2759, but on a different file/line.

The following code is causing this issue:

export const nativeGlobal: typeof global = (() => {
try {
return new Function('return this')();
} catch {
return globalThis;
}
})() as typeof global;

We previously had a webpack setup which used similar code to the one below, and so it never ran new Function('return this')():

__webpack_require__.g = (() => {
if (typeof globalThis === 'object') return globalThis;
try {
return this || new Function('return this')();
} catch (e) {
if (typeof window === 'object') return window;
}
})();

Would it be possible to revert to an approach like with the webpack runtime?

Reproduction

unneeded, see source.

Used Package Manager

pnpm

System Info

System:
    OS: macOS 15.0.1
    CPU: (12) arm64 Apple M3 Pro
    Memory: 39.31 MB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.nvm/versions/node/v20.17.0/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.17.0/bin/npm
    pnpm: 9.12.2 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 129.0.6668.103
    Edge: 130.0.2849.46
    Safari: 18.0.1

Validations

@danhorvath danhorvath changed the title Content Security Policy blocking Content Security Policy unsafe-eval violation in runtime Oct 22, 2024
danhorvath pushed a commit to danhorvath/module-federation-core that referenced this issue Oct 23, 2024
The workaround to getting globalThis via evaluating "return this" gets blocked by commonly used CSPs
and running it can results in an increased number of CSP violations being tracked.

fix module-federation#3103
danhorvath pushed a commit to danhorvath/module-federation-core that referenced this issue Oct 23, 2024
The workaround to getting globalThis via evaluating "return this" gets blocked by commonly used CSPs
and running it can results in an increased number of CSP violations being tracked.

fix module-federation#3103
danhorvath pushed a commit to danhorvath/module-federation-core that referenced this issue Oct 23, 2024
The workaround to getting globalThis via evaluating "return this" gets blocked by commonly used CSPs
and running it can results in an increased number of CSP violations being tracked.

fix module-federation#3103
danhorvath pushed a commit to danhorvath/module-federation-core that referenced this issue Oct 23, 2024
The workaround to getting globalThis via evaluating "return this" gets blocked by commonly used CSPs
and running it can results in an increased number of CSP violations being tracked.

fix module-federation#3103
@danhorvath danhorvath linked a pull request Oct 23, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant