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: render nonce on scripts and links to scripts #22

Merged
merged 1 commit into from
Sep 15, 2023
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
8 changes: 4 additions & 4 deletions src/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ beforeEach(() => {
helpers = getRenderHelpers({nonce: 'random'});
});

test('should not render nonce for regular `<script>` tags', () => {
test('should render nonce for regular `<script>` tags', () => {
const script = helpers.renderScript({
src: 'foo.js',
});

expect(script).not.toEqual(expect.stringContaining('nonce="random"'));
expect(script).toEqual(expect.stringContaining('nonce="random"'));
});

test('should render `nonce` for inline `<script>` tags', () => {
Expand All @@ -26,8 +26,8 @@ test('should render `nonce` for inline styles', () => {
expect(style).toEqual(expect.stringContaining('nonce="random"'));
});

test('should not render `nonce` for `<link>` tags', () => {
test('should render `nonce` for `<link>` tags', () => {
const link = helpers.renderLink({href: 'foo.js', as: 'script'});

expect(link).not.toEqual(expect.stringContaining('nonce="random"'));
expect(link).toEqual(expect.stringContaining('nonce="random"'));
});
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function getRenderHelpers(params: {nonce?: string}): RenderHelpers {
defer,
async,
crossorigin: crossOrigin,
nonce: params.nonce,
})}></script>`
: '';
}
Expand All @@ -41,6 +42,7 @@ export function getRenderHelpers(params: {nonce?: string}): RenderHelpers {
href,
crossorigin: crossOrigin,
as,
nonce: as === 'script' ? params.nonce : undefined,
...rest,
})}>`
: '';
Expand Down
Loading