Expose nonce for javascript libraries

Created on 1 November 2023, 8 months ago
Updated 16 November 2023, 7 months ago

Problem/Motivation

A nonce service was added so the CSP module can provide a single value for a request to all modules that require it, but it's currently still up to modules to apply the nonce to the policy directives and relevant markup as required.

Some consumers may need the nonce in JavaScript - e.g. google_tag needs the nonce applied to its inserted script which it then propagates to any additional scripts it includes ( ✨ Support Content Security Policy Needs review ).

Proposed resolution

- add a csp/nonce library which other libraries can add as a dependency. The library will not have any files itself.
- other modules will be responsible for altering relevant directives to include the nonce value from the csp.nonce service via their csp.policy_alter event subscriber.
- when the csp/nonce library is present on a response and a directive includes the nonce value, csp's response event subscriber will add the nonce value to drupalSettings

Remaining tasks

User interface changes

n/a

API changes

a new library is available

Data model changes

n/a

✨ Feature request
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada gapple

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @gapple
  • πŸ‡¨πŸ‡¦Canada gapple

    ✨ Enable conditional/alternate directive values Active

    If a directive includes 'unsafe-inline', modules may need to fall back to adding domains to the directive instead of a nonce (which would disable 'unsafe-inline' for other modules that rely on it. In that case the nonce should possibly not be added to drupalSettingsΒΉ

    ΒΉ I don't think it would be harmful - any scripts should be allowed by 'unsafe-inline' or a domain as necessary and the nonce on their tag would just be ignored.
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    113 pass, 4 fail
  • @gapple opened merge request.
  • Status changed to Needs review 8 months ago
  • πŸ‡¨πŸ‡¦Canada gapple

    I previously had a concern about exposing the nonce value to JavaScript like this, but since the nonce attribute on any page element is accessible to JavaScript, this does not expose anything not already available to any script via document.querySelector('script[nonce]').nonce.

    Any script not conforming to the page's policy (e.g. an inline script element injected into the page which does not contain the correct nonce attribute) will be blocked, but if a bad script is able to bypass the policy (e.g. by being injected with a correct nonce attribute, or from a domain otherwise allowed by the policy), then it could already do significant harm regardless of being able to propagate the nonce to additional harmful scriptsΒΉ.

    See https://github.com/w3c/webappsec-csp/issues/458

    Note that browser dev tools will hide the nonce attribute's value when inspecting script elements, but it is still available to any scripts run on the page or in the console.

    ΒΉ Propagating the nonce could potentially do more harm by it's ability to allow additional scripts from any domain, while a domain-only policy would restrict the ability to load additional scripts to domains that have some measure of trust. A nonce-only policy (or with a very limited set of domains) could make an initial compromise more difficult though.
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 8 months ago
    131 pass
  • πŸ‡§πŸ‡ͺBelgium DieterHolvoet Brussels

    Note for module maintainers: you can add csp/nonce to your library dependencies without adding the csp module as dependency of your module. Not sure if it's a bug, but Drupal\Core\Asset\LibraryDependencyResolver::doGetDependencies ignores any libraries that are listed under dependencies but don't actually exist.

  • πŸ‡§πŸ‡ͺBelgium DieterHolvoet Brussels

    when the csp/nonce library is present on a response and a directive includes a nonce value, the csp.policy_alter event listener will add the nonce value to drupalSettings

    How would a directive currently include a nonce value? Shouldn't the module provide an option to do this? The nonce is random, so we can't hardcode it in config.

  • Status changed to Needs work 8 months ago
  • πŸ‡§πŸ‡ͺBelgium DieterHolvoet Brussels
  • πŸ‡¨πŸ‡¦Canada gapple

    Note for module maintainers: you can add csp/nonce to your library dependencies without adding the csp module as dependency of your module.

    Interesting; good to know. I think the recommended way will be to use hook_library_info_alter() to add the dependency if the csp module is enabled.

    How would a directive currently include a nonce value? Shouldn't the module provide an option to do this? The nonce is random, so we can't hardcode it in config.

    The csp.nonce service will generate a value as needed so that all modules have the same value for a single response, and modules can use Nonce::getSource() to get the formatted value for adding to the necessary directives in their csp.policy_alter event listener. (CSP handles removing duplicates in the header if multiple modules use the nonce value on the same directive).

    (I don't think the nonce-source should be automatically added to a policy header if the library is present as a dependency for the response; it's uncertain if any module is always adding a nonce source, which directives it's needed for, or if it will be needed for either or both of a report-only and enforced policy).

  • πŸ‡§πŸ‡ͺBelgium DieterHolvoet Brussels

    Makes sense, thanks! Is it possible for themes to alter the policy? Otherwise we won't be able to use this for ✨ Support Content Security Policy Postponed .

  • πŸ‡¨πŸ‡¦Canada gapple

    Is it possible for themes to alter the policy?

    It looks like no, since themes can't define services πŸ˜”: #2002606: Allow themes to provide services.yml β†’

    I've opened a new plan issue for figuring out changes for better support of themes (and maybe easier options for modules too): 🌱 Better CSP support for themes Active

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.2 & MySQL 8
    last update 7 months ago
    132 pass
    • gapple β†’ committed 14694fb6 on 8.x-1.x
      Issue #3398535 by gapple: Expose nonce for javascript libraries
      
  • Status changed to Fixed 7 months ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024