feat: generating nonce by default and cleaning up the tests #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR introduces significant enhancements to the CSP generator, focusing on security best practices, modern browser compatibility, and robust CLI/environment handling. The changes ensure that the generated Content Security Policy is both secure by default and backward compatible, while also improving test coverage and reliability.
Key Improvements
1. Security Best Practices
base-uri 'self'always included:Prevents attackers from injecting
<base>tags and changing the base URL for relative links.require-trusted-types-for 'script'enabled by default:Enforces Trusted Types for scripts, mitigating DOM XSS risks in modern browsers.
script-srcimprovements:'strict-dynamic'and'unsafe-inline':When
'strict-dynamic'is present,'unsafe-inline'is always added for backward compatibility with older browsers.'strict-dynamic'is present.Nonces and hashes are generated and included for inline scripts, maximizing compatibility and security.
2. Robust CLI & Environment Handling
The CLI now robustly parses boolean options from both CLI arguments and environment variables, handling empty, undefined, and boolean values, with support for default values.
requireTrustedTypesis alwaystrueby default unless explicitly set otherwise.3. Test Coverage & Reliability
Example Output
A generated CSP now looks like:
Content-Security-Policy: default-src 'self'; object-src 'none'; script-src https://cdn.cookie-script.com https://www.googletagmanager.com 'sha256-AqR2q4mEeEy4Klon15BI5AtufOZVE5OD0k6RylQCMis=' 'nonce-0MPzwlbRwl9dmL6s8GCKXw==' 'strict-dynamic'; style-src https://fonts.googleapis.com; img-src https://21no.de https://img.shields.io; upgrade-insecure-requests; block-all-mixed-contentScreenshots validating our generated output by Google CSP Validator: