[PR #431] [CLOSED] feat(extensions): load extension JS via same-origin <script src> instead of new Function #4618

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/431
Author: @kolacheee
Created: 5/5/2026
Status: ❌ Closed

Base: main ← Head: feat/csp-safe-extension-loader


📝 Commits (2)

  • 4d31210 feat(extensions): load extension JS via same-origin
## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/431 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 5/5/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `feat/csp-safe-extension-loader` --- ### 📝 Commits (2) - [`4d31210`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4d31210879f50b0a38c1f1602b58449636b4a0c5) feat(extensions): load extension JS via same-origin <script src> instead of new Function - [`221cc7c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/221cc7cd1de343b64611f62f5c06648a15f7db14) Update file-backed-store.ts ### 📊 Changes **16 files changed** (+569 additions, -152 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/App.tsx` (+2 -0) 📝 `packages/client/src/components/layout/CustomThemeInjector.tsx` (+132 -103) 📝 `packages/client/src/components/panels/SettingsPanel.tsx` (+33 -30) ➕ `packages/client/src/hooks/use-extensions.ts` (+120 -0) 📝 `packages/client/src/stores/ui.store.ts` (+27 -19) 📝 `packages/server/drizzle.config.cts` (+1 -0) 📝 `packages/server/src/db/file-backed-store.ts` (+1 -0) 📝 `packages/server/src/db/migrate.ts` (+11 -0) ➕ `packages/server/src/db/schema/extensions.ts` (+16 -0) 📝 `packages/server/src/db/schema/index.ts` (+1 -0) ➕ `packages/server/src/routes/extensions.routes.ts` (+94 -0) 📝 `packages/server/src/routes/index.ts` (+2 -0) ➕ `packages/server/src/services/storage/extensions.storage.ts` (+73 -0) 📝 `packages/shared/src/index.ts` (+2 -0) ➕ `packages/shared/src/schemas/extension.schema.ts` (+28 -0) ➕ `packages/shared/src/types/extension.ts` (+26 -0) </details> ### 📄 Description ## Linked issue <!-- Every PR should reference a feature request or issue report. If there isn't one yet, open one first to gather opinions: --> <!-- https://github.com/Pasta-Devs/Marinara-Engine/issues/new/choose --> Closes #419 ## Why this change Marinara's extension loader currently executes user JS via `new Function("marinara", ext.js)`, which requires `'unsafe-eval'` in `script-src`. The shipped CSP at [`security-headers.ts`](packages/server/src/middleware/security-headers.ts) is `script-src 'self'` — no eval — so on a stock build no JS extension runs. Currently, published Marinara JS extension currently require users to manually add `'unsafe-eval'` to `security-headers.ts` to make extensions work, which defeats the strict policy the project chose to ship. This PR makes JS extensions work under the existing strict CSP without any policy relaxation. The loader stops using `new Function` and instead loads each enabled extension's JS via a same-origin `<script src>` tag pointing at a new endpoint, `GET /api/extensions/:id/script.js`. `<script src>` from the same origin is allowed by `script-src 'self'` and needs no `'unsafe-eval'`. ## Approach Mirrors the existing browser-local → server-storage migration that shipped for `customThemes`. Same hook shape, same migration mechanics, same storage/route layout — just for `installed_extensions` instead. ## What changed **Loader ([`CustomThemeInjector.tsx`](packages/client/src/components/layout/CustomThemeInjector.tsx)):** - Builds the per-extension `marinara` helper API. Surface is unchanged from today: `addStyle`, `addElement`, `apiFetch`, `on`, `setInterval`, `setTimeout`, `observe`, `onCleanup`, `extensionId`, `extensionName`. Existing extensions need no source changes. - Registers the API on a `Map` at `window.__marinaraExt` keyed by extension id, immediately before injecting `<script src="/api/extensions/<id>/script.js?v=<updatedAt>">`. - On cleanup (extension toggled, uninstalled, or list refetched), removes the script tag and the registry entry, then runs the auto-cleanups the API helper tracked (event listeners, intervals, observers, etc.). - If a script is mid-flight when cleanup runs, the IIFE finds `null` in the registry and returns silently — no orphaned execution. **Endpoint ([`extensions.routes.ts`](packages/server/src/routes/extensions.routes.ts)):** - Returns the user's stored JS verbatim, wrapped server-side in a static IIFE that pulls the `marinara` API back out of the window registry and bails if cleanup ran first. The user's JS is appended to the wrapper as a substring; nothing in the wrapper interprets it. No `eval`, `Function`, or `node:vm` usage. - `Content-Type: application/javascript; charset=utf-8`, `Cache-Control: no-store`. `X-Content-Type-Options: nosniff` is already set globally. - Strict id validation (`^[A-Za-z0-9_-]{1,64}$`). 404 if disabled, missing, or has no JS. Toggle/uninstall propagates to subsequent loads immediately because nothing is cacheable. - The `?v=<updatedAt>` cache-buster on the loader side defends against any intermediary caching as a belt-and-braces. **Storage:** - New `installed_extensions` table created via the existing idempotent startup migration in [`migrate.ts`](packages/server/src/db/migrate.ts). - Registered with the file-native store at [`file-backed-store.ts`](packages/server/src/db/file-backed-store.ts) so the runtime JSON-snapshot persistence layer recognizes it (this was the missing piece that needed a follow-up commit during testing). - Drizzle schema, Zod schemas, shared types — all follow the `customThemes` template. **Migration:** - `useLegacyExtensionMigration` reads pre-existing `installedExtensions` out of localStorage on first load, POSTs each one that isn't already on the server, then clears the legacy array and flips a one-shot flag (mirrors `useLegacyThemeMigration`). - Persist version bumped to v17 with the new flag. ## Compatibility for extension authors The `marinara` helper API surface is unchanged. Existing `.json` / `.js` / `.css` extension imports work without modification. Tested end-to-end with [kolache's AIO extension](https://github.com/kolacheee/Marinara-kolaches-aio-extension) — installs, runs, edits, saves, and uninstalls cleanly under stock CSP with no `'unsafe-eval'` patch. ## Files **New:** - `packages/shared/src/types/extension.ts` - `packages/shared/src/schemas/extension.schema.ts` - `packages/server/src/db/schema/extensions.ts` - `packages/server/src/services/storage/extensions.storage.ts` - `packages/server/src/routes/extensions.routes.ts` - `packages/client/src/hooks/use-extensions.ts` **Modified:** - `packages/shared/src/index.ts` — export new type/schema - `packages/server/drizzle.config.cts` — register new schema file - `packages/server/src/db/migrate.ts` — add `CREATE TABLE installed_extensions` - `packages/server/src/db/schema/index.ts` — re-export new schema - `packages/server/src/db/file-backed-store.ts` — add `installed_extensions` to `FILE_BACKED_TABLES` - `packages/server/src/routes/index.ts` — register `/api/extensions` - `packages/client/src/App.tsx` — call legacy migration hook - `packages/client/src/components/layout/CustomThemeInjector.tsx` — switch loader from `new Function` to `<script src>` - `packages/client/src/components/panels/SettingsPanel.tsx` — switch import/list/toggle/delete to server hooks - `packages/client/src/stores/ui.store.ts` — add migration flag, persist v17 ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) 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) - [ ] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ## Manual verification still needed - [x] Install a JS extension on a stock build (no `'unsafe-eval'` patch in `security-headers.ts`) and confirm it loads and runs - [ ] Existing localStorage extensions migrate server-side on first load after upgrade — install on old build → upgrade → confirm extensions appear in `/api/extensions` and the legacy localStorage array is cleared - [x] Toggling enabled / uninstalling stops execution immediately on next page mount - [ ] CSS-only extensions still inject as `<style>` (they don't go through the script endpoint at all) - [ ] Multiple enabled extensions get independent `marinara` API instances and independent cleanups — uninstalling one doesn't break the others - [ ] Disabling an extension while its `<script>` is mid-load doesn't throw — the IIFE bail path should log a warn and return - [x] Extension imports from `.json`, `.js`, `.css` files all flow through the new server-backed install correctly Unchecked ones I am unsure how/unable to test and verify. ## 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) --- <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#4618
No description provided.