Here is the shortcode:
[web_stories title=”true” excerpt=”false” author=”false” date=”false” archive_link=”true” archive_link_label=”View all” circle_size=”150″ sharp_corners=”false” image_alignment=”left” number_of_columns=”1″ number_of_stories=”5″ order=”DESC” orderby=”post_date” view=”carousel” /]
The shortcode outputs web stories on the https://localhost:8000/insights/
blog page. On this page I can see 4 CSP errors. All them have the same link on the first story in the carousel:
https://localhost:8000/web-stories/all-you-need-to-knowabout-iomt/#visibilityState=prerender&origin=http%3A%2F%2Flocalhost%3A8000&showStoryUrlInfo=0&storyPlayer=v0&cap=swipe
Scripts: https://cdn.ampproject.org/v0.js
& https://cdn.ampproject.org/v0/amp-story-1.0.js.
It seems the same two CSP errors were triggered twice: 2+2 = 4 errors.
When I click (or open in a new tab) on the first story on the carousel I see the color-filled screen and the story excerpt super small text and the same 4 CSP errors, but with URL: https://localhost:8000/web-stories/all-you-need-to-knowabout-iomt/
.
So, the issue is the same for the blog page and the single story page. But the blog page looks normal at least.
Let me provide a bit more details about the CSP implementation.
How it currently works:
- we don’t use CSP on the server level (.htaccess);
- we add our CSP on the WP/PHP level based on standard WP functions and filters for JS scripts;
- we use the whole PHP page buffering and build the dynamic SCP (start on the ‘init’ hook), calculate hashes and add the nonce to scripts, update HTTP headers with the CSP, add <meta> with the CSP to <head>, and output the page on the ‘shutdown’ WP action (right after the mentioned our
sendHeaders()
method that updates headers with the CSP);
The error message example:
“Refused to load the script https://cdn.ampproject.org/v0.js because it violates the following Content Security Policy directive: “script-src ‘self’ ‘strict-dynamic’ {domains} ‘nonce-2295cb6ff3′”. Note that ‘strict-dynamic’ is present, so host-based allowlisting is disabled. Note that ‘script-src-elem’ was not explicitly set, so ‘script-src’ is used as a fallback.”
Important:
When I use a dirty fix with str_replace()
before buffer outputting on the $page everything works well.
I’m logged and the WP-Rocket cache doesn’t work. I don’t remember that we changed Web Stories functionality somehow; only styles.
Debug:
$document in your method has: <$cript async="" src="https://cdn.ampproject.org/v0.mjs" type="module" crossorigin="anonymous" nonce="2295cb6ff3">
The buffered $page on the echo moment has: <$cript async="" src="https://cdn.ampproject.org/v0.js">