Problem/Motivation
With live preview enabled (the default), an embedded component never shows its preview in the editor. The request to the preview endpoint is denied with a 403, and the widget only shows the component's label and ID.
Steps to reproduce
- Install Drupal with the Standard profile and enable SDC Embed. Keep the default settings (
live_preview: true). - At Configuration > Text formats and editors > Full HTML, add the "Embed component" button to the toolbar and enable the "Embed Single Directory Components" filter.
- Create a Basic page, switch the body to Full HTML, click "Embed component", choose "Teaser — olivero", type some text in the Content slot and click "Embed component".
Expected: the widget shows the rendered teaser.
Actual: the widget only shows "Teaser" and "olivero:teaser". The console logs [sdc_embed] Preview fetch failed: HTTP 403, and the network tab shows:
POST /sdc-embed/preview/full_html?_format=json&token=… → 403 Forbidden
The same URL is already wrong in drupalSettings.editor.formats.full_html.editorSettings.config.sdcEmbed.previewUrl.
Reproduced on SDC Embed 1.x (same code as 1.2.0) with Drupal 11.4.9 and Olivero as the default theme.
Root cause
The preview and stash routes both require _csrf_token: TRUE. Their URLs are generated while the editor loads, during an HTML request, so RouteProcessorCsrf puts a placeholder in place of the token. SdcEmbed::renderUrl() renders the URL to replace that placeholder, wrapping it in ['#plain_text' => $url].
#plain_text also runs the string through Html::escape(), so the & between the query parameters becomes &. PHP then reads the parameter as amp;token, the token parameter is missing, and CsrfAccessCheck denies the request.
Only the preview URL has two query parameters (_format=json and token). The stash URL has just the token (/sdc-embed/stash/full_html?token=…), with no & to escape, so it is not affected.
Proposed resolution
Keep the render (it is what replaces the placeholder and bubbles the token's cacheability), and decode the entities on its result with Html::decodeEntities(). This cannot corrupt the URL: Url::toString() percent-encodes query values, so the only & that can be there is the one #plain_text introduced.
With the fix, the same steps give POST /sdc-embed/preview/full_html?_format=json&token=… → 200 OK and the widget shows the rendered teaser.
The MR adds a kernel test, SdcEmbedPluginConfigTest, that builds the plugin's dynamic config during an HTML request and checks that both URLs carry a token parameter that validates for its path. Without the fix it fails on previewUrl containing &.
Remaining tasks
Review.
User interface changes
None.
API changes
None.
Data model changes
None.
AI usage
AI-Generated: Yes. I used Claude Code (Anthropic) to write the fix, the kernel test and a first draft of this summary. I reproduced the bug and verified the fix in the browser, reviewed the code, and confirmed the new test fails without the fix and passes with it.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | sdc_embed-csrf-token-separator-escaped-3630614-3.patch | 4.09 KB | tiago |
Issue fork sdc_embed-3630614
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 #3
tiago commentedMR !3 is ready for review and its pipeline passes.
Attached below is the MR diff as a patch for 1.2.0.
Comment #5
c2h2o5 commentedIt will be released with the next module version.
Comment #6
c2h2o5 commented