Skip to content

feat(security): content security policy nonce generator & header hardener - #40

Merged
ATLURI0001 merged 1 commit into
mainfrom
feat/csp-hardener
Nov 22, 2026
Merged

ATLURI0001 merged 1 commit into
mainfrom
feat/csp-hardener

Conversation

@ATLURI0001

Copy link
Copy Markdown
Owner

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)

  • Employs secrets.token_bytes to produce high-entropy pseudo-random byte sequences ($\ge 16$ bytes).
  • Encodes output as standard Base64, guaranteeing uniqueness across rendering passes and resisting replay or brute-force predictability.

2. Strict CSP Level 3 Specification Assembly (CSPHardener)

  • Compiles hardened policy directives with backward compatibility fallbacks:
    • script-src 'strict-dynamic' 'nonce-{nonce}' 'unsafe-inline' https:: Strict browsers trust nonced root scripts and their dynamically generated children, while ignoring 'unsafe-inline' and https:.
    • 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.
    • Granular configuration for reporting endpoints (report-uri / report-to) and report-only mode.

3. Full-Spectrum Defensive Security Headers

build_security_headers() generates an immutable security baseline:

Header Value Defense Vector
Content-Security-Policy Hardened Directive String Script injection, inline execution
X-Content-Type-Options nosniff MIME-type sniffing confusion
X-Frame-Options DENY Clickjacking / iframe embedding
Referrer-Policy strict-origin-when-cross-origin Credential leakage in referrer strings
Permissions-Policy geolocation=(), camera=(), microphone=(), payment=() Unauthorized hardware sensor access
Cross-Origin-Opener-Policy same-origin Cross-window attack isolation
Cross-Origin-Resource-Policy same-origin Cross-origin resource leakage

4. Regex-Resilient HTML Nonce Injection

  • inject_nonce_into_html(html, nonce) parses <script> and <style> opening tags, injecting nonce="{nonce}" while idempotently preserving existing nonces and multi-line attributes.

5. CSP Vulnerability Audit Engine

  • audit_csp_string(csp) evaluates policies for anti-patterns:
    • Flags 'unsafe-eval' (arbitrary code execution).
    • Flags wildcard '*' origins.
    • Detects missing object-src 'none', base-uri, or frame-ancestors.

Verification & Test Coverage

  • pytest -k "test_csp": 6/6 tests passed green in 0.47s.
  • Total repository suite: 115 tests passing.
  • Verified test matrix in tests/test_csp_hardener.py:
    • Entropy uniqueness and minimum bit-length clamping.
    • Strict mode directive generation with nonces.
    • HTML injection idempotency across complex tags.
    • Audit detection of insecure vs hardened policies.

@ATLURI0001
ATLURI0001 merged commit 2de3f66 into main Nov 22, 2026
9 checks passed
@ATLURI0001
ATLURI0001 deleted the feat/csp-hardener branch November 22, 2026 10:24
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.

1 participant