Skip to content

CSP nonce replacement in style-src blocks inline style attributes #114

@ZechCodes

Description

@ZechCodes

Problem

The SecurityHeadersMiddleware replaces 'unsafe-inline' with a per-request nonce in style-src:

csp_str = _STYLE_SRC_UNSAFE_INLINE.sub(rf"\1'nonce-{nonce}'", self.csp_value)

CSP nonces only cover <style> elements — they do not cover inline style="" attributes on HTML elements. When 'unsafe-inline' is replaced with a nonce, all elements with style="" attributes trigger CSP violations.

Impact

Any Skrift app using csp_nonce: true (the default) with 'unsafe-inline' in style-src will have all inline style attributes blocked. This breaks legitimate template rendering that uses inline styles.

Suggested Fix

Stop replacing 'unsafe-inline' in style-src. Only inject nonces into script-src. CSS cannot execute JavaScript, so nonces for styles provide minimal security benefit while breaking inline style attributes.

Alternatively, use the style-src-attr 'unsafe-inline' directive alongside style-src-elem with nonces, but browser support is more limited.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions