[PR #461] [MERGED] fix(extensions): bind this to globalThis in module loader #4627

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/461
Author: @Romuromylus
Created: 5/6/2026
Status: ✅ Merged
Merged: 5/6/2026
Merged by: @Romuromylus

Base: main ← Head: fix/extension-loader-this-binding


📝 Commits (1)

  • 7bac3fd fix(extensions): bind this to globalThis in module loader

📊 Changes

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

View changed files

📝 packages/client/src/components/layout/CustomThemeInjector.tsx (+4 -1)

📄 Description

Linked issue

Closes #460

Why this change

v1.5.8 shipped a CSP-safe extension loader that uses import(blob:URL) to execute user JS without new Function(). The wrapper invokes user code as executeExtension(marinara) with no explicit this binding, which under module strict mode leaves this === undefined inside extension code. Classic-script-style extensions that use top-level this.foo = bar for global assignment therefore throw TypeError: Cannot set properties of undefined and fail silently — only the per-extension console.error("[Extension:...] Failed to execute:", e) line in the loader catches it.

The original new Function("marinara", ext.js) loader (v1.5.7) ran user code with this === globalThis. Restoring that behavior under the new module-based loader keeps the v1.5.8 CSP fix intact while not breaking the range of extension styles that worked before.

This came up while reviewing #431 by @kolacheee — their alternative <script src> proposal, since closed in favor of v1.5.8's blob-URL approach. Their work surfaced the broader question of which extension styles the new loader should support; this PR addresses the strict-mode this corner of that question without re-litigating the architectural choice.

What changed

packages/client/src/components/layout/CustomThemeInjector.tsx — one line in buildExtensionModuleSource:

- `executeExtension(marinara);`,
+ `executeExtension.call(globalThis, marinara);`,

Plus a 3-line comment above it explaining why.

No change to:

  • CSP / security-headers.ts
  • The blob-URL loading mechanism
  • The per-extension API map / cleanup tracking
  • Server-side anything

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) 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)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

What was verified during preparation (boxes left unticked — reviewer should confirm independently):

  1. pnpm check passes on the branch (TypeScript + ESLint + production build).
  2. Container built via Docker autoDeploy on a private staging instance; container restarted healthy, basicAuth gate responding HTTP 401 as expected.
  3. Manual extension test on the deployed instance:
    • Imported a test .js extension containing top-level this.MARINARA_THIS_TEST = "ok" plus a small banner that reports this's identity.
    • Pre-fix: red banner reading FAIL — threw: Cannot set properties of undefined (setting 'MARINARA_THIS_TEST').
    • Post-fix: green banner reading PASS — this === window.
  4. Regression test: @kolacheee's AIO extension (modern JS, doesn't use this patterns) verified to load and operate identically before and after the fix — full 3-column console renders, no console errors, all tab pickers populate from the API.
  5. Edge cases (themes / mobile / light mode / empty states) not directly affected — this change only touches the JS wrapper that builds extension module sources, no UI surface.

Docs and release impact

  • No docs changes needed

Reviewer to decide whether this warrants a CHANGELOG.md line for the next patch — it's a quiet bug fix, but extension authors who hit silent strict-mode breakage may appreciate the note. Happy to add it on request.

UI evidence (if applicable)

N/A — no UI surface affected.

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility for extensions to ensure proper execution in different runtime environments.

🔄 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/461 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 5/6/2026 **Status:** ✅ Merged **Merged:** 5/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `main` ← **Head:** `fix/extension-loader-this-binding` --- ### 📝 Commits (1) - [`7bac3fd`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7bac3fd2a8759a21b041c81425daa505847b9453) fix(extensions): bind `this` to globalThis in module loader ### 📊 Changes **1 file changed** (+4 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/layout/CustomThemeInjector.tsx` (+4 -1) </details> ### 📄 Description ## Linked issue Closes #460 ## Why this change v1.5.8 shipped a CSP-safe extension loader that uses `import(blob:URL)` to execute user JS without `new Function()`. The wrapper invokes user code as `executeExtension(marinara)` with no explicit `this` binding, which under module strict mode leaves `this === undefined` inside extension code. Classic-script-style extensions that use top-level `this.foo = bar` for global assignment therefore throw `TypeError: Cannot set properties of undefined` and fail silently — only the per-extension `console.error("[Extension:...] Failed to execute:", e)` line in the loader catches it. The original `new Function("marinara", ext.js)` loader (v1.5.7) ran user code with `this === globalThis`. Restoring that behavior under the new module-based loader keeps the v1.5.8 CSP fix intact while not breaking the range of extension styles that worked before. This came up while reviewing #431 by @kolacheee — their alternative `<script src>` proposal, since closed in favor of v1.5.8's blob-URL approach. Their work surfaced the broader question of which extension styles the new loader should support; this PR addresses the strict-mode `this` corner of that question without re-litigating the architectural choice. ## What changed `packages/client/src/components/layout/CustomThemeInjector.tsx` — one line in `buildExtensionModuleSource`: ```diff - `executeExtension(marinara);`, + `executeExtension.call(globalThis, marinara);`, ``` Plus a 3-line comment above it explaining why. No change to: - CSP / `security-headers.ts` - The blob-URL loading mechanism - The per-extension API map / cleanup tracking - Server-side anything ## 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) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes What was verified during preparation (boxes left unticked — reviewer should confirm independently): 1. `pnpm check` passes on the branch (TypeScript + ESLint + production build). 2. Container built via Docker autoDeploy on a private staging instance; container restarted healthy, basicAuth gate responding HTTP 401 as expected. 3. **Manual extension test on the deployed instance:** - Imported a test `.js` extension containing top-level `this.MARINARA_THIS_TEST = "ok"` plus a small banner that reports `this`'s identity. - Pre-fix: red banner reading `FAIL — threw: Cannot set properties of undefined (setting 'MARINARA_THIS_TEST')`. - Post-fix: green banner reading `PASS — this === window`. 4. **Regression test:** [@kolacheee's AIO extension](https://github.com/kolacheee/Marinara-kolaches-aio-extension) (modern JS, doesn't use `this` patterns) verified to load and operate identically before and after the fix — full 3-column console renders, no console errors, all tab pickers populate from the API. 5. Edge cases (themes / mobile / light mode / empty states) not directly affected — this change only touches the JS wrapper that builds extension module sources, no UI surface. ## Docs and release impact - [x] No docs changes needed Reviewer to decide whether this warrants a `CHANGELOG.md` line for the next patch — it's a quiet bug fix, but extension authors who hit silent strict-mode breakage may appreciate the note. Happy to add it on request. ## UI evidence (if applicable) N/A — no UI surface affected. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved compatibility for extensions to ensure proper execution in different runtime environments. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <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:17 +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#4627
No description provided.