[PR #430] [CLOSED] fix(csp): allow 'unsafe-eval' in script-src so JS extensions run #4617

Closed
opened 2026-10-22 04:09:12 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/430
Author: @kolacheee
Created: 5/5/2026
Status: ❌ Closed

Base: main ← Head: claude/wonderful-tu-e803be


📝 Commits (1)

  • 0a33cfc fix(csp): allow 'unsafe-eval' in script-src so JS extensions run

📊 Changes

1 file changed (+7 additions, -1 deletions)

View changed files

📝 packages/server/src/middleware/security-headers.ts (+7 -1)

📄 Description

The extension system in CustomThemeInjector executes user-installed extension JS via new Function("marinara", ext.js) so extensions can receive the marinara API. The page CSP set script-src 'self' with no 'unsafe-eval', which the browser enforces against new Function/eval, so every JS extension failed with EvalError and was effectively unusable.

Grant 'unsafe-eval' on script-src only. All other directives remain strict — scripts must still be same-origin ('self'), so this does not permit inline script injection or remote script loading. The trade-off is documented inline next to the directive.

Linked issue

Closes #419

Why this change

  • Every JS extension currently fails with EvalError: ... 'unsafe-eval' is not an allowed source of script: script-src 'self'. The extension system is effectively broken — anyone who installs a JS extension hits this.
  • Root cause is internal to the engine: CustomThemeInjector.tsx runs user-installed extension JS via new Function("marinara", ext.js) so extensions can receive the marinara API, but the server CSP in security-headers.ts sets script-src 'self' with no 'unsafe-eval'. The two contradict each other.

What changed

  • packages/server/src/middleware/security-headers.ts: add 'unsafe-eval' to the script-src directive only. All other directives are unchanged — scripts must still be same-origin ('self'), so this does not permit inline script injection or remote script loading.
  • Inline comment next to the directive explains the trade-off and points at CustomThemeInjector as the consumer.

Why not a stricter fix?

The architecturally cleaner alternative — running extension JS inside a sandboxed iframe with a postMessage RPC bridge — would let the main page CSP keep its strict script-src 'self'. But it would break every existing extension's API surface (addStyle, addElement, observe, direct DOM access). That's a major rewrite, not a patch. This PR is the minimum coherent fix to restore extensions; tightening the sandbox is a follow-up.

Validation

  • pnpm check passes locally
  • Container built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Below manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • Install + enable a JS extension in a browser; confirm no EvalError and that marinara API methods (addStyle, addElement, on, observe, etc.) work.
  • Sanity-check that no other CSP-blocked errors appear in the console under normal usage.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

N/A — no UI changes.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/430 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 5/5/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `claude/wonderful-tu-e803be` --- ### 📝 Commits (1) - [`0a33cfc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0a33cfca6349e60eb3ec5399a10f12336b34b7f1) fix(csp): allow 'unsafe-eval' in script-src so JS extensions run ### 📊 Changes **1 file changed** (+7 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/middleware/security-headers.ts` (+7 -1) </details> ### 📄 Description The extension system in CustomThemeInjector executes user-installed extension JS via `new Function("marinara", ext.js)` so extensions can receive the marinara API. The page CSP set `script-src 'self'` with no 'unsafe-eval', which the browser enforces against `new Function`/`eval`, so every JS extension failed with EvalError and was effectively unusable. Grant 'unsafe-eval' on script-src only. All other directives remain strict — scripts must still be same-origin ('self'), so this does not permit inline script injection or remote script loading. The trade-off is documented inline next to the directive. ## Linked issue Closes #419 ## Why this change - Every JS extension currently fails with `EvalError: ... 'unsafe-eval' is not an allowed source of script: script-src 'self'`. The extension system is effectively broken — anyone who installs a JS extension hits this. - Root cause is internal to the engine: `CustomThemeInjector.tsx` runs user-installed extension JS via `new Function("marinara", ext.js)` so extensions can receive the marinara API, but the server CSP in `security-headers.ts` sets `script-src 'self'` with no `'unsafe-eval'`. The two contradict each other. ## What changed - `packages/server/src/middleware/security-headers.ts`: add `'unsafe-eval'` to the `script-src` directive only. All other directives are unchanged — scripts must still be same-origin (`'self'`), so this does not permit inline script injection or remote script loading. - Inline comment next to the directive explains the trade-off and points at `CustomThemeInjector` as the consumer. ### Why not a stricter fix? The architecturally cleaner alternative — running extension JS inside a sandboxed iframe with a postMessage RPC bridge — would let the main page CSP keep its strict `script-src 'self'`. But it would break every existing extension's API surface (`addStyle`, `addElement`, `observe`, direct DOM access). That's a major rewrite, not a patch. This PR is the minimum coherent fix to restore extensions; tightening the sandbox is a follow-up. ## Validation - [x] `pnpm check` passes locally - [x] Container built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Below manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - [x] Install + enable a JS extension in a browser; confirm no `EvalError` and that `marinara` API methods (`addStyle`, `addElement`, `on`, `observe`, etc.) work. - [x] Sanity-check that no other CSP-blocked errors appear in the console under normal usage. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) N/A — no UI changes. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:09:12 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4617
No description provided.