Problem/Motivation
It would be great if we can support decoupled sites by exposing the CSP rules over GraphQL.
Websites that have a dedicated frontend (for example Next.js or Nuxt) don't currently have a way to get the current rules directly from Drupal for including in their HTML. Currently the typical approach is to hardcode a duplicated set of the CSP policy rules in the decoupled frontend and keep it in sync manually whenever the rules change. By exposing the CSP rules over GraphQL any changes made will be available instantly to decoupled frontends.
Proposed resolution
- Add a
csp_headersGraphQL data producer. It returns every enabled policy type exactly the same wayResponseCspSubscriberdoes: create aCspobject and dispatchCspEvents::POLICY_ALTER. This means the settings, libraries, reporting handlers, theme hooks and third-party alters all apply. It returns a list of{name, value}pairs, one per enabled header, and bubbles the cacheability metadata so changes to the CSP settings or libraries invalidate the result. - Add a
cspGraphQL extension that exposes the producer, so site owners can simply enable the extension to make sure the frontend developers have the CSP rules available on their GraphQL endpoint.
API changes
A new public method Csp::getHeaderDirectives(), which returns the directives as they are serialized in the header value. Csp::getHeaderValue() now builds its output from it, so the header string is unchanged.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | 3628882-6.patch | 15.67 KB | pfrenssen |
Issue fork csp-3628882
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
pfrenssenComment #4
pfrenssenI added an initial implementation on the main 2.x branch. I will also backport it to 2.2.x so I can test it out in my real-life project which is on 2.2.4. Since 2.2.x still supports Drupal 10 I will need to drop the D11 style attributes and use annotations instead.
Comment #6
pfrenssenAttaching patch that works with 2.2.4.