Skip to content

fix(inspector): preserve strict CSP compatibility on Hyvä Checkout - #305

Merged
dermatz merged 3 commits into
mainfrom
fix/inspector-hyva-checkout-csp
Dec 1, 2026
Merged

dermatz merged 3 commits into
mainfrom
fix/inspector-hyva-checkout-csp

Conversation

@dermatz

@dermatz dermatz commented Dec 1, 2026

Copy link
Copy Markdown
Member

Summary

Fixes strict Content Security Policy (CSP) violations on Hyvä Checkout (1.3.x+) when dev/mageforge_inspector/enabled=1 is active in developer mode.

Why this broke Hyvä Checkout

  1. Duplicate data-mageforge-* attributes invalidated <script nonce="..."> tags (W3C CSP3 § 6.7.2.2)
    • InspectorHints::injectInspectorAttributes() previously injected data-mageforge-id and data-mageforge-block onto every root HTML element, including <script> and <style> tags.
    • Whenever Hyvä rendered an inline script through a wrapper template ($block->fetchView(...) in htmldialog.phtml, intersect.phtml, html-safe.phtml, snap-slider.phtml, defer.phtml, or getChildHtml() in container.phtml / alpinejs.phtml), the same <script nonce="..."> tag received a second data-mageforge-id and data-mageforge-block attribute.
    • Per W3C CSP3 "Is element nonceable?", any element with a duplicate-attribute HTML parse error is marked non-nonceable and blocked by the browser even when its nonce matches the Content-Security-Policy header. On Hyvä Checkout (script-src without 'unsafe-inline' or 'unsafe-eval'), this blocked x-htmldialog and other Alpine plugins, breaking native <dialog> modals.
  2. Inline script & @alpinejs/csp load-order race in inspector.phtml
    • inspector.phtml emitted an inline <script> via $secureRenderer->renderTag(), forcing nonce generation on cached pages under global strict CSP.
    • Because Hyva_Theme loads before OpenForgeProject_MageForge, alpine3-csp.min.js evaluated x-data="mageforgeToolbar" and x-data="mageforgeInspector" before toolbar.js / inspector.js registered their Alpine.data() factories.
  3. Inline style attributes in inspector DOM fragments
    • Replaced remaining style="font-weight: 600;" attributes in performance.js and tabs.js with .mageforge-note-title so strict style-src policies without 'unsafe-inline' are respected.

What Changed

  • InspectorHints:
    • Skip non-visual and CSP-governed root elements (script, style, link, meta, template, noscript, title, base, html, head, body).
    • Use a quote-aware opening-tag regex and skip injection when the root element already carries data-mageforge-id (preserving the inner template's metadata and preventing duplicate attributes).
  • inspector.phtml + alpine-loader.js + toolbar.js + inspector.js:
    • Removed the inline <script> from inspector.phtml and moved the local Alpine fallback check into alpine-loader.js (loaded via type="module" src scripts).
    • Added x-ignore on .mageforge-toolbar-root and .mageforge-inspector, removing it once Alpine.data() is registered and calling Alpine.initTree() if Alpine has already started.
  • CSS / UI: Replaced inline style attributes in performance.js and tabs.js with .mageforge-note-title in inspector.css.
  • Unit tests: Added coverage in InspectorHintsTest for <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 analyze
  • ddev phpcs & ddev phpstan
  • ./vendor/bin/phpunit
  • trunk check

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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 13:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
Copilot AI balanced review requested due to automatic review settings December 1, 2026 13:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

@dermatz
dermatz merged commit cb615a4 into main Dec 1, 2026
22 checks passed
@dermatz
dermatz deleted the fix/inspector-hyva-checkout-csp branch December 1, 2026 14:07
@github-actions github-actions Bot mentioned this pull request Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants