Support Content Security Policy (csp)

Created on 18 September 2023, 9 months ago
Updated 4 October 2023, 9 months ago

Problem/Motivation

When using a Content Security Policy β†’ this module requires unsafe-inline style-src directives. If unsafe-inline styles are not allowed, the editoria11y UI does not load properly.

Steps to reproduce

Proposed resolution

Add unsafe-inline style-src to the Content Security Policy directives only on the pages where editoria11y's UI is loaded.

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Fixed

Version

2.0

Component

Conflicts with other modules

Created by

πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland

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

Comments & Activities

  • Issue created by @ChrisSnyder
  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update 9 months ago
    4 fail
  • @chrissnyder opened merge request.
  • Status changed to Needs review 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update 9 months ago
    4 fail
  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland

    Attaching patch matching current MR

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update 9 months ago
    2 pass
  • The last submitted patch, 5: editoria11y-add_csp_support-3388093-5.patch, failed testing. View results β†’
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • Status changed to Needs work 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    Alright...this code is straightforward, but I need convincing that controlling these settings in Editoria11y rather than CSP is a good idea. Having Editoria11y undo these settings dynamically is going to affect every node on the site for logged in users, meaning the de facto CSP policy would be dramatically different from what it looks like on the CSP settings page. Is that scope of an override better than just documenting that you shouldn't check "disable inline styles" in CSP?

  • Assigned to itmaybejj
  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj
  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland
  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland

    Is it possible to load Editoria11y's styles from a stylesheet instead of inline? That would avoid the need for this CSP alteration altogether.

  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland

    Is that scope of an override better than just documenting that you shouldn't check "disable inline styles" in CSP?

    Only adding the unsafe-inline CSP directive to pages where the user has the ability to use Editoria11y is better than adding unsafe-inline to ALL pages when not needed, like for anonymous visitors.

  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    Is it possible to load Editoria11y's styles from a stylesheet instead of inline? That would avoid the need for this CSP alteration altogether.

    I'm afraid it is not feasible -- abstracting the fixed styles is easy, but there are quite a few things I'm calculating on the fly.

    That said -- from what I'm reading, it looks like CSP restrictions can permit inline tags if they include a server-generated hash or nonce. Adding a that would not be difficult at all if the CSP module supports that approach.

  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    If you were able to get me generated nonces in the .module file, I could handle modifying the library to accept it as a parameter and add it to all the injections!

  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland

    Leveraging nonces instead would definitely be ideal. However, I don't believe the CSP module currently has an API to support it. See #3086924: Allow script / style by nonce β†’

  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    Bother. I've spent some time on this and identified two ways I might be able to rewrite and not need inline styles, but both would require substantial rewrites and may hurt performance. It seems like nonces really are the "right" way to do this.

    I'll test/merge your PR and document the issue until CSP adds nonce support.

  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    @ChrisSnyder can you help me with https://git.drupalcode.org/issue/editoria11y-3388093/-/jobs/96695#L48 ? I pulled this to try to make the tests happier with the syntax, and I'm not succeeding.

  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland

    Would it be possible to update the version of PHP you are testing with?

    You are testing with an EOL version of PHP. PHP 7.4 is no longer supported by Drupal β†’ . Constructor property promotion was not available until PHP 8.0.

  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    Oh that makes sense. I can do that.

  • πŸ‡¨πŸ‡¦Canada gapple

    Alright...this code is straightforward, but I need convincing that controlling these settings in Editoria11y rather than CSP is a good idea. Having Editoria11y undo these settings dynamically is going to affect every node on the site for logged in users, meaning the de facto CSP policy would be dramatically different from what it looks like on the CSP settings page. Is that scope of an override better than just documenting that you shouldn't check "disable inline styles" in CSP?

    The default config settings for the CSP module do not enable 'unsafe-inline', and the module only conditionally adds it for core libraries when required (e.g. for ckeditor and core/drupal.ajax in the earlier versions of core where it's still required). My philosophy is that it is better to only add a significant exception when necessary (and hopefully it will become increasingly less necessary), and modules should implement an event subscriber if they require any changes not captured by the default library asset parsing. It's a tradeoff that the effective policy for any page may vary but modules that implement should just work out of the box for site builders - being able to see the effective value in browser tools and any violation reports should make debugging straightforward.

    it looks like CSP restrictions can permit inline tags if they include a server-generated nonce. Adding a that would not be difficult at all if the CSP module supports that approach.

    Nonces present a bit of an integration challenge. Because applying a nonce (or hash) to a directive disables 'unsafe-inline', a module that wishes to use it must only add a nonce if the directive doesn't already include 'unsafe-inline' (without a nonce), so that functionality from other modules is not blocked. For now it might be enough to just lower the module event listener's priority so that other modules add their 'unsafe-inline' first if they need to. (this is what Attach Inline does, but it will currently probably have issues with another module also adding a nonce).
    Nonces can also only be applied to elements (<link rel="stylesheet">, <script><code>, <code><style>), and not attributes (style="...", onclick="..."). (I would need to investigate if all browsers support 'unsafe-hashes' for attributes yet, but that can potentially make for a large policy header).

  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    In the longer term, nonces would definitely be easier than hashes at my end.

    It looks like inline style attribute manipulation by approved scripts is still permitted, so long as they do not manipulate the style tag as text. Am I reading that correctly? Moz Docs suggest that this would be permitted from an approved script:
    document.querySelector("div").style.display = "none";

    If so, I could eventually rewrite my necessarily-inline styles as a serious of querySelectorAll loops. Less performant, but so it goes.

    For now -- I do want to maintain PHP 7.4 as an option for some of my legacy users, so I need to rewrite that function before merging.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update 9 months ago
    2 pass
  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland

    It looks like inline style attribute manipulation by approved scripts is still permitted, so long as they do not manipulate the style tag as text. Am I reading that correctly?

    Yes, you are reading that correctly. That is how I was able to resolve the CSP issue in πŸ› Avoid use of inline styles Fixed .

    I updated the MR to remove the use of the PHP 8.0 feature.

  • πŸ‡ΊπŸ‡ΈUnited States ChrisSnyder Maryland

    Update patch to match MR.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update 9 months ago
    2 pass
  • Status changed to RTBC 9 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update 9 months ago
    Patch Failed to Apply
  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    Thank you! Will leave open until I tag a release.

  • Status changed to Fixed 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.69.0 2024