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

feat: support apply environment nonce config #2685

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions e2e/cases/nonce/basic/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,49 @@ test('should apply nonce to script and style tags', async () => {
);
expect(html).toContain(`<style nonce="CSP_NONCE_PLACEHOLDER">body{`);
});

test('should apply environment nonce', async () => {
const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
environments: {
web: {
security: {
nonce: 'CSP_NONCE_PLACEHOLDER',
},
output: {
distPath: {
root: 'dist',
},
},
},
web1: {
security: {
nonce: 'CSP_NONCE_PLACEHOLDER1',
},
output: {
distPath: {
root: 'dist/dist1',
},
},
},
},
},
});
const files = await rsbuild.unwrapOutputJSON();
const html =
files[Object.keys(files).find((file) => file.endsWith('dist/index.html'))!];
expect(html).toContain(
`<script defer="defer" nonce="CSP_NONCE_PLACEHOLDER">`,
);
expect(html).toContain(`<style nonce="CSP_NONCE_PLACEHOLDER">body{`);

const html1 =
files[
Object.keys(files).find((file) => file.endsWith('dist1/index.html'))!
];
expect(html1).toContain(
`<script defer="defer" nonce="CSP_NONCE_PLACEHOLDER1">`,
);
expect(html1).toContain(`<style nonce="CSP_NONCE_PLACEHOLDER1">body{`);
});
4 changes: 2 additions & 2 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,12 @@ export function partition<T>(

export const applyToCompiler = (
compiler: Rspack.Compiler | Rspack.MultiCompiler,
apply: (c: Rspack.Compiler) => void,
apply: (c: Rspack.Compiler, index: number) => void,
) => {
if (isMultiCompiler(compiler)) {
compiler.compilers.forEach(apply);
} else {
apply(compiler);
apply(compiler, 0);
}
};

Expand Down
19 changes: 13 additions & 6 deletions packages/core/src/plugins/nonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ export const pluginNonce = (): RsbuildPlugin => ({

setup(api) {
api.onAfterCreateCompiler(({ compiler }) => {
const { nonce } = api.getNormalizedConfig().security;
const nonces = Object.keys(api.context.environments).map(
(environment) => {
const { nonce } = api.getNormalizedConfig({ environment }).security;

if (!nonce) {
return nonce;
},
);

if (!nonces.find((nonce) => !!nonce)) {
9aoy marked this conversation as resolved.
Show resolved Hide resolved
return;
}

applyToCompiler(compiler, (compiler) => {
applyToCompiler(compiler, (compiler, index) => {
const nonce = nonces[index];
const { plugins } = compiler.options;
const hasHTML = plugins.some(
(plugin) => plugin && plugin.constructor.name === 'HtmlBasicPlugin',
);
if (!hasHTML) {
if (!hasHTML || !nonce) {
return;
}

Expand All @@ -35,8 +42,8 @@ export const pluginNonce = (): RsbuildPlugin => ({
api.modifyHTMLTags({
// ensure `nonce` can be applied to all tags
order: 'post',
handler: ({ headTags, bodyTags }) => {
const config = api.getNormalizedConfig();
handler: ({ headTags, bodyTags }, { environment }) => {
const config = api.getNormalizedConfig({ environment });
const { nonce } = config.security;
const allTags = [...headTags, ...bodyTags];

Expand Down
Loading