Repository navigation
feat(security): content security policy nonce generator & header hardener - #40
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Technical Deep-Dive: Content Security Policy (CSP) Nonce Generator & Security Header Hardener
Architectural Motivation & Attack Vector Analysis
Modern static and dynamic web delivery pipelines are exposed to cross-site scripting (XSS), data exfiltration, clickjacking, and MIME confusion attacks. Relying on legacy host-based allowlists often results in bypasses (e.g., via open redirects, JSONP endpoints, or unauthenticated CDNs).
This pull request implements a cryptographic CSP Level 3 policy compiler, runtime HTML nonce injector, and security header hardening suite in
src/buildsite/csp_hardener.py.Technical Implementation
1. Cryptographic Nonce Generation (
generate_csp_nonce)secrets.token_bytesto produce high-entropy pseudo-random byte sequences (2. Strict CSP Level 3 Specification Assembly (
CSPHardener)script-src 'strict-dynamic' 'nonce-{nonce}' 'unsafe-inline' https:: Strict browsers trust nonced root scripts and their dynamically generated children, while ignoring'unsafe-inline'andhttps:.object-src 'none': Prevents legacy Flash, Java applets, and ActiveX injection.base-uri 'self': Mitigates document base URL hijacking.frame-ancestors 'none': Enforces framing protection at the browser engine level.report-uri/report-to) and report-only mode.3. Full-Spectrum Defensive Security Headers
build_security_headers()generates an immutable security baseline:Content-Security-PolicyX-Content-Type-OptionsnosniffX-Frame-OptionsDENYReferrer-Policystrict-origin-when-cross-originPermissions-Policygeolocation=(), camera=(), microphone=(), payment=()Cross-Origin-Opener-Policysame-originCross-Origin-Resource-Policysame-origin4. Regex-Resilient HTML Nonce Injection
inject_nonce_into_html(html, nonce)parses<script>and<style>opening tags, injectingnonce="{nonce}"while idempotently preserving existing nonces and multi-line attributes.5. CSP Vulnerability Audit Engine
audit_csp_string(csp)evaluates policies for anti-patterns:'unsafe-eval'(arbitrary code execution).'*'origins.object-src 'none',base-uri, orframe-ancestors.Verification & Test Coverage
pytest -k "test_csp": 6/6 tests passed green in 0.47s.tests/test_csp_hardener.py: