Skip to content

Consider having ContentSecurityPolicyHeaderWriter supply a script nonce #10826

Open

Description

@jzheaux

In order to have inline scripts and use a strict CSP, it's important to provide a nonce.

Spring Security's ContentSecurityPolicyHeaderWriter could generate a nonce if a {nonce} placeholder is in the policy directive configuration.

Then, it could be made available as a request attribute for use in views.

Activity

  1. ziqin commented on Jan 10, 2026

    @ziqin
    Contributor

    Instead of a {nonce} placeholder in the directives string, would it be a better idea to accept a Function<String, String> in ContentSecurityPolicyConfig?

    @Bean
    SecurityFilterChain filterChain(HttpSecurity http) {
        http
            .headers(headers -> headers
                .contentSecurityPolicy(csp -> csp
                    .nonceAttributeName("_csp_nonce")
                    .policyDirectivesWithNonce(nonce -> "script-src 'self' 'nonce-" + nonce + "'")
                )
            );
        return http.build();
    }
  2. ziqin commented on Jan 14, 2026

    @ziqin
    Contributor

    I tried to implement nonce support in two approaches. It turns out that the {nonce} placeholder approach can be implemented with less changes under the constraint of not breaking existing APIs.

  3. ziqin commented on Jan 14, 2026

    @ziqin
    Contributor

    Hi, @jzheaux!

    I have implemented the {nonce} approach in PR #18499. Would you like to review it?

  4. self-assigned this
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webAn issue in web modules (web, webmvc)type: enhancementA general enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions