Skip to content

Conversation

@vbarzana
Copy link
Collaborator

@vbarzana vbarzana commented May 7, 2025

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-src improvements:
    • 'strict-dynamic' and 'unsafe-inline':
      When 'strict-dynamic' is present, 'unsafe-inline' is always added for backward compatibility with older browsers.
    • Host entries (e.g., CDNs) are still included for legacy support, but are ignored by CSP3+ browsers when 'strict-dynamic' is present.
  • Nonce and hash support:
    Nonces and hashes are generated and included for inline scripts, maximizing compatibility and security.

2. Robust CLI & Environment Handling

  • Boolean parsing improved:
    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.
  • requireTrustedTypes is always true by default unless explicitly set otherwise.
  • All CLI/environment options are now more reliable and predictable.

3. Test Coverage & Reliability

  • 100% function coverage, 99.87% line coverage.
  • All new logic is fully tested, including edge cases for CLI/environment parsing and CSP output.
  • Backward compatibility and modern browser support are both verified by tests.

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-content

Screenshots validating our generated output by Google CSP Validator:

image

vbarzana added 8 commits May 7, 2025 14:52
…d customize a nonce desired by the customer. This improves security policy generation.

These improvements ensure that our CSP generator is thoroughly tested across all major use cases and edge conditions, providing better confidence in the reliability of the generated security policies.
…l tests pass in CI

- Restrict SecureCSPGenerator mocking to CLI tests only
- Restore real class for core and browser tests
- Fixes CI failures caused by global mock pollution
- Ensures robust, environment-independent test suite
…nd ensure robust end-to-end coverage

- Reinstated CLI tests to use the real SecureCSPGenerator (no mocking)
- Removed timeout-based test for reliability
- Fixed test isolation and process/console mocking
- All tests now pass locally and in CI
@jkyberneees jkyberneees merged commit f09752f into BackendStack21:main May 8, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants