Repository navigation
fix(inspector): preserve strict CSP compatibility on Hyvä Checkout - #305
Conversation
Skip data-mageforge-* attribute injection on non-inspectable/CSP-governed root elements (<script>, <style>, <link>, <meta>, <template>, etc.) and on elements already annotated by inner templates so nested wrapper templates (fetchView/container.phtml) cannot produce duplicate attributes that invalidate W3C CSP3 script nonces. Move the local Alpine.js fallback loader out of an inline script in inspector.phtml into an external ES module (alpine-loader.js), guard MageForge root elements with x-ignore for @alpinejs/csp load-order safety, and replace inline style attributes in inspector UI fragments with a CSS class. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
Active issues can cause the root-attribute check to skip template and cache metadata annotation and the Alpine script selector to suppress the fallback incorrectly, making the toolbar unavailable.
2 open issues
What changed in this PR
This PR improves MageForge Inspector compatibility with strict Content Security Policy on Hyvä Checkout while retaining the local Alpine fallback for other themes.
Changes:
- Avoid annotating CSP-sensitive root elements and preserve existing inspector metadata.
- Move Alpine bootstrap logic into a shared module and defer component initialization until registration.
- Replace inline title styles with a CSS class and add annotation regression tests.
| File | Description |
|---|---|
tests/Unit/Model/TemplateEngine/Decorator/InspectorHintsTest.php |
Adds coverage for skipped tags, existing annotations, and quoted attributes. |
src/view/frontend/web/js/toolbar.js |
Uses the shared Alpine bootstrap for toolbar registration. |
src/view/frontend/web/js/inspector/tabs.js |
Replaces inline note-title styles with a CSS class. |
src/view/frontend/web/js/inspector/performance.js |
Replaces the cache-warning title’s inline style. |
src/view/frontend/web/js/inspector.js |
Uses the shared Alpine bootstrap for inspector registration. |
src/view/frontend/web/js/alpine-loader.js |
Adds component registration, initialization, and fallback loading. |
src/view/frontend/web/css/inspector.css |
Defines the shared note-title class. |
src/view/frontend/templates/inspector.phtml |
Removes inline bootstrap code and adds initialization guards. |
src/Model/TemplateEngine/Decorator/InspectorHints.php |
Adds root-tag filtering, quote-aware matching, and duplicate-annotation checks. |
Review effort: Balanced
Parse root opening-tag attributes with a quote-aware tokenizer so data-mageforge-id substrings inside quoted attribute values no longer skip root block annotation. Resolve the bundled Alpine.js fallback via import.meta.url instead of a DOM attribute (eliminating DOM-to-script.src taint), match candidate Alpine scripts strictly by runtime filename/package path rather than arbitrary URL substrings, and recheck window.Alpine after candidate scripts load before suppressing the fallback. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The tokenizer incorrectly treats slashes as delimiters in unquoted HTML attribute values, causing URLs such as /data-mageforge-id to be mistaken for inspector attributes and preventing block metadata annotation.
1 open issue
2 resolved since last review
Review effort: Balanced
Allow '/' inside unquoted attribute values in InspectorHints::ATTR_REGEX per HTML5 attribute tokenization so unquoted URLs such as href=/data-mageforge-id do not suppress root block annotation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
The snapshot lacks Hyvä Checkout’s CSP Alpine runtime, so both bootstrap load orders and checkout dialogs still require browser validation under strict CSP.
No open issues 🎉
1 resolved since last review
Review effort: Balanced

Summary
Fixes strict Content Security Policy (CSP) violations on Hyvä Checkout (1.3.x+) when
dev/mageforge_inspector/enabled=1is active in developer mode.Why this broke Hyvä Checkout
data-mageforge-*attributes invalidated<script nonce="...">tags (W3C CSP3 § 6.7.2.2)InspectorHints::injectInspectorAttributes()previously injecteddata-mageforge-idanddata-mageforge-blockonto every root HTML element, including<script>and<style>tags.$block->fetchView(...)inhtmldialog.phtml,intersect.phtml,html-safe.phtml,snap-slider.phtml,defer.phtml, orgetChildHtml()incontainer.phtml/alpinejs.phtml), the same<script nonce="...">tag received a seconddata-mageforge-idanddata-mageforge-blockattribute.duplicate-attributeHTML parse error is marked non-nonceable and blocked by the browser even when itsnoncematches theContent-Security-Policyheader. On Hyvä Checkout (script-srcwithout'unsafe-inline'or'unsafe-eval'), this blockedx-htmldialogand other Alpine plugins, breaking native<dialog>modals.@alpinejs/cspload-order race ininspector.phtmlinspector.phtmlemitted an inline<script>via$secureRenderer->renderTag(), forcing nonce generation on cached pages under global strict CSP.Hyva_Themeloads beforeOpenForgeProject_MageForge,alpine3-csp.min.jsevaluatedx-data="mageforgeToolbar"andx-data="mageforgeInspector"beforetoolbar.js/inspector.jsregistered theirAlpine.data()factories.styleattributes in inspector DOM fragmentsstyle="font-weight: 600;"attributes inperformance.jsandtabs.jswith.mageforge-note-titleso strictstyle-srcpolicies without'unsafe-inline'are respected.What Changed
InspectorHints:script,style,link,meta,template,noscript,title,base,html,head,body).data-mageforge-id(preserving the inner template's metadata and preventing duplicate attributes).inspector.phtml+alpine-loader.js+toolbar.js+inspector.js:<script>frominspector.phtmland moved the local Alpine fallback check intoalpine-loader.js(loaded viatype="module"srcscripts).x-ignoreon.mageforge-toolbar-rootand.mageforge-inspector, removing it onceAlpine.data()is registered and callingAlpine.initTree()if Alpine has already started.styleattributes inperformance.jsandtabs.jswith.mageforge-note-titleininspector.css.InspectorHintsTestfor<script nonce="...">,<style>,<link>,<meta>,<template>, pre-annotated root elements, nested annotated children, and>inside quoted attributes.Verification
ddev mago lint,ddev mago fmt --dry-run,ddev mago analyzeddev phpcs&ddev phpstan./vendor/bin/phpunittrunk check