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

[fix] disable nonce for script,style or both #1861

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions packages/xarc-subapp/src/node/init-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ export function initSubApp(setupContext: any, setupToken: Partial<{ props: InitP

const addScriptNonce = (text: string) => {
return !scriptNonceAttr
? text
? text && text.replace(/{{SCRIPT_NONCE}}/g, "")
: text && text.replace(/{{SCRIPT_NONCE}}/g, context.user.scriptNonceAttr);
};

const addStyleNonce = (text: string) => {
return !styleNonceAttr
? text
? text && text.replace(/{{STYLE_NONCE}}/g, "")
: text && text.replace(/{{STYLE_NONCE}}/g, context.user.styleNonceAttr);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/xarc-subapp/src/node/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type NonceInfo = {
style?: boolean;

/** nonce tokens */
tokens: {
tokens?: {
all?: string;
script?: string;
style?: string;
Expand Down
6 changes: 5 additions & 1 deletion packages/xarc-subapp/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ export function generateNonce(
if (nonce) {
if (nonce[tag] === false) {
return { attr: "" };
} else if (nonce.tokens || nonce.generator) {
nonceToken = nonce.tokens[tag] || nonce.tokens.all || nonce.generator(tag);
} else {
nonceToken = nonceGenerator(tag);
nonce = { tokens: { all: nonceToken, [tag]: nonceToken } };
}
nonceToken = nonce.tokens[tag] || nonce.tokens.all || nonce.generator(tag);
} else {
nonceToken = nonceGenerator(tag);
nonce = { tokens: { all: nonceToken, [tag]: nonceToken } };
Expand Down
4 changes: 2 additions & 2 deletions packages/xarc-subapp/test/spec/node/init-v2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ describe("Test init-v2", () => {
}
};
const result = initializer.process(context);
expect(result).contains(`<script{{SCRIPT_NONCE}}`);
expect(result).contains(`<link{{STYLE_NONCE}}`);
expect(result).to.not.contain(`<script{{SCRIPT_NONCE}}`);
expect(result).to.not.contain(`<link{{STYLE_NONCE}}`);
});

it("test initSubApp without CDN Map", () => {
Expand Down