Skip to content

Module Federation not working with Content Security Policy (CSP) #3053

@TheSharpieOne

Description

@TheSharpieOne

Describe the bug

If you have a strict CSP then the remotes for won't load in module federation

EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'unsafe-inline' [...trimmed urls...]

Adding unsafe-eval defeats the purpose of having a CSP, as it opens a security hole - can you advise how you to round this when using the module federation plugin 2.0?

Note: there is a similar issue, but in the other issue, the root cause was webpack related. I am not using webpack, I am using vite with module-federation/vite which uses core. Core does have eval going on which is triggering CSP

return new Promise<RemoteEntryExports>((resolve, reject) => {
try {
if (!remoteEntryExports) {
// eslint-disable-next-line no-eval
new Function(
'callbacks',
`import("${entry}").then(callbacks[0]).catch(callbacks[1])`,
)([resolve, reject]);
} else {
resolve(remoteEntryExports);
}
} catch (e) {
reject(e);
}
});

return new Promise<RemoteEntryExports>((resolve, reject) => {
try {
if (!remoteEntryExports) {
// eslint-disable-next-line no-eval
new Function(
'callbacks',
`System.import("${entry}").then(callbacks[0]).catch(callbacks[1])`,
)([resolve, reject]);
} else {
resolve(remoteEntryExports);
}
} catch (e) {
reject(e);
}

Is there anything that can be done about this or should I attempt to convince my company's security team to allow unsafe-eval (they will take one look at the name "unsafe" and give the standard response "NO")

Reproduction

unneeded, see source.

Used Package Manager

pnpm

System Info

System:
    OS: macOS 14.6.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 98.71 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    Yarn: 1.22.22 - /usr/local/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.15.0/bin/npm
    pnpm: 8.1.0 - ~/Library/pnpm/pnpm
    Watchman: 2024.05.06.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 129.0.6668.90
    Safari: 17.6

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions