[PR #7268] [MERGED] fix: stop client extensions that flood the sandbox with heartbeats #7656

Closed
opened 2026-10-24 04:06:14 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7268
Author: @SpicyMarinara
Created: 10/7/2026
Status: ✅ Merged
Merged: 10/7/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/extension-sandbox-security-followup


📝 Commits (2)

  • 269c897 fix: count heartbeat floods toward the extension sandbox message limit
  • 7b57838 Merge origin/staging into fix/extension-sandbox-security-followup

📊 Changes

3 files changed (+55 additions, -25 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/routes/personal-extensions.routes.ts (+7 -1)
📝 scripts/regressions/extension-security.regression.ts (+47 -24)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Refs #7260

Follow-up to #7262, found during a security review of the client extension sandbox after that PR landed.

Why this change

  • The sandbox iframe stops a client extension that sends more than 200 messages in 10 seconds. Heartbeat messages returned before that count, so an extension could send them without end. The worker can post them directly with self.postMessage({ type: "heartbeat" }).
  • In a real browser this freezes Marinara. With 4,000,000 heartbeats, the host page in Chromium stopped responding for about 4.9 seconds, and the sandbox never stopped the worker. Chromium, WebKit and Firefox all let the flood through. A flood that never ends keeps the page frozen and fills memory.
  • Counting every heartbeat would break the #7260 fix. While a browser dialog is open or a tab is frozen, the worker keeps sending heartbeats and the page receives them all at once afterwards. A dialog left open for more than about 3 minutes would then stop a healthy extension.

What changed

  • packages/server/src/routes/personal-extensions.routes.ts (sandbox bootstrap): the worker bootstrap sends one heartbeat a second. Heartbeats up to that rate, counted since the worker started plus a margin of 10, are still free. Any heartbeat beyond that counts toward the existing message limit, so a flood is stopped like any other: stopped: true, its controls are removed, and the user gets Restart. A backlog after a pause stays within the rate and is not counted.
  • scripts/regressions/extension-security.regression.ts: the #7260 sandbox harness can now start a fresh sandbox. A new case checks that 300 heartbeats arriving at once after a 300 s pause do not stop the worker, and that a flood is stopped with the message-limit notice. The flood check fails on current staging. A version that counts every heartbeat fails the backlog check.
  • CHANGELOG.md: user-facing entry.

Reviewed and left unchanged (security lens on #7262):

  • Extension code cannot affect document.hidden or the iframe clock. It runs only in the worker, which has no document, and its CSS cannot change page visibility. In Chromium, WebKit and Firefox, a hung worker in the hidden (display: none) sandbox iframe on a visible page is still stopped after about 5 s.
  • Another sandbox cannot fake stopped: true for a different extension. The host matches the message source to that extension's iframe, requires origin null and checks the content hash. An extension can stop only itself this way, and it could already do that by hanging.
  • Restart checks the server again. The new iframe loads sandbox.html, which returns 404 unless the extension is enabled, approved for its current hash, allowed by policy and requested with that hash. A changed hash clears the stopped state, and only the approved new version starts.
  • The worker blob URL is blob:null/…, created from the sandbox's opaque origin. It is not same-origin with the app and connect-src 'none' applies. The browser releases it when the iframe is removed. Every stop, hash change, Restart and unmount removes the iframe.
  • Stop paths call worker.terminate(), and browsers drop the stopped worker's queued messages; one stop notice reaches the host in all three engines. The host removes the iframe, its contributions and its dispatcher.
  • Known limit, not changed: extension code shares the worker with the heartbeat sender. Approved code that loops on purpose can send heartbeats from inside the loop and keep using CPU. The watchdog catches accidental hangs; it is not a CPU quota.

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

Agent-run checks (not a substitute for the boxes above):

  • pnpm check passed.
  • extension-security.regression.ts passed. It fails on staging without the route change ("A heartbeat flood must count toward the sandbox message limit").
  • e2e/personal-extension-restart.e2e.ts passed on desktop Chromium.
  • Probe outside the repo: the real sandboxDocument output, served with the production CSP, in a hidden sandboxed iframe in Chromium, WebKit and Firefox:
    • A healthy extension starts and keeps running.
    • A hung worker is stopped after about 5 s.
    • A 4,000,000-heartbeat flood is now stopped after about 0.5 s, with at most 11 ms of host lag (before: never stopped, 4.9 s host freeze in Chromium).
    • In Chromium and WebKit, a healthy extension kept running after a 215 s confirm() dialog that blocked the page.

Manual verification notes

  • Manually verify in a browser that a normal client extension (for example one with a top-bar button) still starts, responds and keeps running after the tab has been in the background for several minutes.
  • Manually verify that an extension that runs for (let i = 0; i < 4e6; i++) self.postMessage({ type: "heartbeat" }) is stopped within about a second, its buttons disappear, Marinara stays responsive and Restart appears.
  • Manually verify that leaving a native browser confirm dialog (for example deleting a Professor Mari skill) open for over 3 minutes and then closing it does not stop a running client extension.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

Only CHANGELOG.md changed. docs/development/personal-extensions.md already says the iframe rate-limits messages.

UI evidence (if applicable)

No UI changes.

Summary by CodeRabbit

  • Bug Fixes
    • Extension “still running” signals sent more than once per second now count toward the sandbox message limit. If ongoing signals exceed the limit, Marinara stops the extension and offers a Restart option.
    • Signals delivered in a burst after a pause do not count toward the limit, so an extension can catch up without being stopped.

🔄 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/7268 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/7/2026 **Status:** ✅ Merged **Merged:** 10/7/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/extension-sandbox-security-followup` --- ### 📝 Commits (2) - [`269c897`](https://github.com/Pasta-Devs/Marinara-Engine/commit/269c8977b02ef8bef37bd5ac9711b011c824351d) fix: count heartbeat floods toward the extension sandbox message limit - [`7b57838`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7b578383d8497513b2383660c5e5f601bf8610aa) Merge origin/staging into fix/extension-sandbox-security-followup ### 📊 Changes **3 files changed** (+55 additions, -25 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/routes/personal-extensions.routes.ts` (+7 -1) 📝 `scripts/regressions/extension-security.regression.ts` (+47 -24) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Refs #7260 Follow-up to #7262, found during a security review of the client extension sandbox after that PR landed. ## Why this change - The sandbox iframe stops a client extension that sends more than 200 messages in 10 seconds. Heartbeat messages returned before that count, so an extension could send them without end. The worker can post them directly with `self.postMessage({ type: "heartbeat" })`. - In a real browser this freezes Marinara. With 4,000,000 heartbeats, the host page in Chromium stopped responding for about 4.9 seconds, and the sandbox never stopped the worker. Chromium, WebKit and Firefox all let the flood through. A flood that never ends keeps the page frozen and fills memory. - Counting every heartbeat would break the #7260 fix. While a browser dialog is open or a tab is frozen, the worker keeps sending heartbeats and the page receives them all at once afterwards. A dialog left open for more than about 3 minutes would then stop a healthy extension. ## What changed - `packages/server/src/routes/personal-extensions.routes.ts` (sandbox bootstrap): the worker bootstrap sends one heartbeat a second. Heartbeats up to that rate, counted since the worker started plus a margin of 10, are still free. Any heartbeat beyond that counts toward the existing message limit, so a flood is stopped like any other: `stopped: true`, its controls are removed, and the user gets **Restart**. A backlog after a pause stays within the rate and is not counted. - `scripts/regressions/extension-security.regression.ts`: the #7260 sandbox harness can now start a fresh sandbox. A new case checks that 300 heartbeats arriving at once after a 300 s pause do not stop the worker, and that a flood is stopped with the message-limit notice. The flood check fails on current `staging`. A version that counts every heartbeat fails the backlog check. - `CHANGELOG.md`: user-facing entry. Reviewed and left unchanged (security lens on #7262): - Extension code cannot affect `document.hidden` or the iframe clock. It runs only in the worker, which has no document, and its CSS cannot change page visibility. In Chromium, WebKit and Firefox, a hung worker in the hidden (`display: none`) sandbox iframe on a visible page is still stopped after about 5 s. - Another sandbox cannot fake `stopped: true` for a different extension. The host matches the message source to that extension's iframe, requires origin `null` and checks the content hash. An extension can stop only itself this way, and it could already do that by hanging. - Restart checks the server again. The new iframe loads `sandbox.html`, which returns 404 unless the extension is enabled, approved for its current hash, allowed by policy and requested with that hash. A changed hash clears the stopped state, and only the approved new version starts. - The worker blob URL is `blob:null/…`, created from the sandbox's opaque origin. It is not same-origin with the app and `connect-src 'none'` applies. The browser releases it when the iframe is removed. Every stop, hash change, Restart and unmount removes the iframe. - Stop paths call `worker.terminate()`, and browsers drop the stopped worker's queued messages; one stop notice reaches the host in all three engines. The host removes the iframe, its contributions and its dispatcher. - Known limit, not changed: extension code shares the worker with the heartbeat sender. Approved code that loops on purpose can send heartbeats from inside the loop and keep using CPU. The watchdog catches accidental hangs; it is not a CPU quota. ## 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` Agent-run checks (not a substitute for the boxes above): - `pnpm check` passed. - `extension-security.regression.ts` passed. It fails on `staging` without the route change ("A heartbeat flood must count toward the sandbox message limit"). - `e2e/personal-extension-restart.e2e.ts` passed on desktop Chromium. - Probe outside the repo: the real `sandboxDocument` output, served with the production CSP, in a hidden sandboxed iframe in Chromium, WebKit and Firefox: - A healthy extension starts and keeps running. - A hung worker is stopped after about 5 s. - A 4,000,000-heartbeat flood is now stopped after about 0.5 s, with at most 11 ms of host lag (before: never stopped, 4.9 s host freeze in Chromium). - In Chromium and WebKit, a healthy extension kept running after a 215 s `confirm()` dialog that blocked the page. ### Manual verification notes - Manually verify in a browser that a normal client extension (for example one with a top-bar button) still starts, responds and keeps running after the tab has been in the background for several minutes. - Manually verify that an extension that runs `for (let i = 0; i < 4e6; i++) self.postMessage({ type: "heartbeat" })` is stopped within about a second, its buttons disappear, Marinara stays responsive and **Restart** appears. - Manually verify that leaving a native browser confirm dialog (for example deleting a Professor Mari skill) open for over 3 minutes and then closing it does not stop a running client extension. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) Only `CHANGELOG.md` changed. `docs/development/personal-extensions.md` already says the iframe rate-limits messages. ## UI evidence (if applicable) No UI changes. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Extension “still running” signals sent more than once per second now count toward the sandbox message limit. If ongoing signals exceed the limit, Marinara stops the extension and offers a **Restart** option. * Signals delivered in a burst after a pause do not count toward the limit, so an extension can catch up without being stopped. <!-- 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-24 04:06:14 +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#7656
No description provided.