[PR #8792] [MERGED] fix: enforce declared route rate limits and reclaim the stranded pnpm 10 store #8808

Closed
opened 2026-11-07 17:39:51 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8792
Author: @SpicyMarinara
Created: 11/3/2026
Status: ✅ Merged
Merged: 11/3/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/rate-limits-and-pnpm-store


📝 Commits (3)

  • f7a006d fix(server): enforce declared route rate limits and match the routed path
  • 6185221 fix(launcher): reclaim the pnpm 10 store stranded by the pnpm 12 switch
  • b8665c5 fix(server): keep a prefix root's trailing-slash HEAD in its GET bucket

📊 Changes

6 files changed (+151 additions, -25 deletions)

View changed files

📝 packages/server/src/middleware/rate-limit.ts (+19 -15)
📝 scripts/ensure-native-deps.mjs (+7 -1)
📝 scripts/pnpm-runner.mjs (+15 -1)
📝 scripts/regressions/pnpm-runner.regression.mjs (+36 -2)
📝 scripts/regressions/route-rate-limits.regression.ts (+70 -6)
📝 start-termux.sh (+4 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.

Linked issue

Closes #8791

Why this change

This fixes two release-prep audit findings.

(1) Routes added this release declare rate limits that were never enforced. The rate-limit hook only matched the raw URL against its own table, so /api/access/*, /api/notices, the utility sidecar, game rulesets, Beholder state and sprite rename all fell into the 600/min default. An absolute-form request line (POST http://host/api/generate) also skipped every specific wall: generate, updates/apply, admin/restart and connection tests got 600/min, and /login was not throttled at all.

(2) The switch to pnpm 12 leaves every existing checkout with a dead 2-3 GB .pnpm-store/v10 that pnpm store prune cannot see. On Termux, the first pnpm 12 install can hit ENOSPC while both stores are on disk, and each later start then retries into the same full disk.

There is no linked issue for either finding; one should be opened before the PR is submitted.

What changed

  • Behaviour change: a client above a route's declared rate now gets 429, where it used to pass under the shared 600/min default:

    • 30/min: /api/access/status, setup-code, sessions and refusals
    • 10/min: /api/access/sessions/:id, DELETE /api/access/sessions, pairing-codes and /api/notices
    • 60/min: utility-sidecar, Beholder state and sprite rename
    • 120/min: game-rulesets

    Absolute-form request lines now hit the same walls as normal requests.

  • The rate limiter matches its rule table against the route the router dispatched to, not the raw URL. Percent-encoded, duplicate-slash and absolute-form targets can no longer reach the 600/min default bucket.

  • Routes the table does not cover get their declared config.rateLimit in their own bucket per method and route pattern. HEAD shares GET's bucket, including Fastify's automatic HEAD route for a prefix root's trailing-slash form (HEAD /api/notices/). Existing shared table buckets are unchanged.

  • After a pnpm 11+ install, the root postinstall deletes the checkout's leftover pnpm 10 store (.pnpm-store/v10) and logs one line. This covers the launchers, the in-app updater, the Windows installer and manual installs. The first update from a pnpm 10 checkout still holds both stores during that install; v10 goes once it succeeds.

  • The Termux launcher removes .pnpm-store/v10 before it installs dependencies. If the first pnpm 12 install runs out of space, the next ./start-termux.sh frees v10 before retrying instead of failing again.

  • Regression coverage:

    • route-rate-limits.regression.ts: routes that only declare a limit (notices is registered under a prefix like the real route, with HEAD on both slash forms) and absolute-form targets over a real socket.
    • pnpm-runner.regression.mjs: the v10 store cleanup and its launcher/postinstall wiring.

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

  • Manually verify in the browser that Settings > Remote login still works without 429s in normal use: the sessions list, sign out other browsers, and Add a device.
  • Manually verify that the notice banner, the refused-device banner (polled every 30s) and the access warning banner still load in several open tabs without 429s.
  • Manually verify that opening the Agent editor (utility sidecar status) and the Beholder chat settings panel still loads without 429s.
  • Manually verify on an existing checkout that still has .pnpm-store/v10: after ./start.sh (or start.bat), check that .pnpm-store/v10 is gone, .pnpm-store/v11 is still there, and the install log shows the 'Removed the pnpm 10 dependency cache' line.
  • Manually verify on Termux, updating a pnpm 10 install with ./start-termux.sh: after the first successful dependency install, check that .pnpm-store/v10 is gone. If that install fails for lack of space, check that the next ./start-termux.sh removes .pnpm-store/v10 before 'Refreshing dependencies...' and the install completes.
  • Manually verify that curl --request-target http://127.0.0.1:7860/api/generate -X POST http://127.0.0.1:7860/ returns RateLimit-Limit: 60.
  • Manually verify that after 10 GET /api/notices requests within a minute, curl -I http://127.0.0.1:7860/api/notices/ returns 429.

Agent-run checks (for reference; each regression was shown to fail without its fix):

  • node ./scripts/pnpm-runner.mjs build:shared: succeeded
  • node ./scripts/run-regressions.mjs --filter route-rate-limits (fix / previous commit's rate-limit.ts / origin/staging's rate-limit.ts, swapped by copying files : PASSED with the fix. FAILED against the previous commit (HEAD /api/notices/ shares the GET bucket). FAILED against staging (/api/access/pairing-codes got 200, expected 429).
  • Reviewer's all-routes comparison (tsx, hooked onRoute over the real buildApp()), previous commit's selectRule vs the new one, 1317 method/route pairs: 2 differences, HEAD /api/notices/ and HEAD /api/avatar-models/, both now in their GET bucket at the declared limit. No other change.
  • 29 regressions run one at a time: route-rate-limits, encoded-api-path-security, basic-auth-throttle-security, remote-login, rate-limit-clock-jump, request-host-: all 29 passed
  • node ./scripts/run-regressions.mjs --filter scripts/regressions/pnpm-runner.regression.mjs: passed
  • node ./scripts/pnpm-runner.mjs --filter @marinara-engine/server lint (tsc -b ../shared && tsc --noEmit && tsc -p character-activity), with pipefail: exit 0
  • tsc --ignoreConfig --noEmit --strict --module nodenext --target es2022 --skipLibCheck scripts/regressions/route-rate-limits.regression.ts: exit 0
  • npx prettier --write then --check on rate-limit.ts and route-rate-limits.regression.ts: all files use Prettier style
  • Earlier round (unchanged files): pnpm-runner regression fails-before with all three or each wiring file reverted; launcher/ regressions, termux-sharp, dev-launc: passed as reported in the earlier round. 5 regressions timed out under load and passed on re-run alone. shellcheck is not installed.
  • ESLint: not run: the repo's only ESLint config is packages/client/eslint.config.js, and no client files changed

Independent adversarial review: changes, 3 issue(s), all addressed in a fix-up pass.

Known limits and risks:

  • Proxy users sharing one address. The newly enforced walls are per address. Behind a reverse proxy without trusted-proxy forwarding, every browser shares one budget, for example 30/min for /api/access/refusals, which each tab polls every 30s. Many tabs behind one proxy could see 429s on those banners. The limits are the values the route authors declared.
  • Per-route buckets. A route the table does not cover gets a bucket per route pattern, not one shared across its router file. For example, each utility-sidecar route has its own 60/min. Trailing slashes on routed patterns are trimmed (except "/"), so "/x" and "/x/" share a bucket.
  • First Termux update still holds both stores. The first update from a pnpm 10 checkout runs the old start-termux.sh (no re-exec after git reset) or the in-app updater. v10 and v11 therefore sit on disk together during that first pnpm 12 install. This change reclaims v10 after that install succeeds, or on the next launcher start if it ran out of space. It does not prevent the first peak.
  • Switching back to Stable. Until this release reaches main, a checkout that switches back to Stable/main (pnpm 10) re-downloads the whole v10 store, where it used to reuse the stranded copy.
  • Slow transition install. On the transition install, removing a multi-GB v10 tree runs inside the in-app updater's install step and its timeout. Exceeding it has not been observed, but slow storage (Termux, Windows Defender scanning) lengthens that step.
  • Docker cache mounts. The postinstall also runs in Docker builds, so it would delete a stale v10 inside a shared BuildKit cache mount. That only matters if a pnpm 10 build used the same cache mount at the same moment.
  • Docs. docs/TROUBLESHOOTING.md is unchanged: its Android recovery steps already end in ./start-termux.sh, which now removes v10. No docs-i18n follow-up is needed.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) 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)

🤖 Generated with Claude Code


🔄 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/8792 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 11/3/2026 **Status:** ✅ Merged **Merged:** 11/3/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/rate-limits-and-pnpm-store` --- ### 📝 Commits (3) - [`f7a006d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f7a006d06db256d32ac7ef508b047c3e10f83649) fix(server): enforce declared route rate limits and match the routed path - [`6185221`](https://github.com/Pasta-Devs/Marinara-Engine/commit/61852210f1e223b987edd6fe9c89171c8cf71da5) fix(launcher): reclaim the pnpm 10 store stranded by the pnpm 12 switch - [`b8665c5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b8665c5668611bdcee9c4064e49613fc56d13a17) fix(server): keep a prefix root's trailing-slash HEAD in its GET bucket ### 📊 Changes **6 files changed** (+151 additions, -25 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/middleware/rate-limit.ts` (+19 -15) 📝 `scripts/ensure-native-deps.mjs` (+7 -1) 📝 `scripts/pnpm-runner.mjs` (+15 -1) 📝 `scripts/regressions/pnpm-runner.regression.mjs` (+36 -2) 📝 `scripts/regressions/route-rate-limits.regression.ts` (+70 -6) 📝 `start-termux.sh` (+4 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. ## Linked issue Closes #8791 ## Why this change This fixes two release-prep audit findings. (1) Routes added this release declare rate limits that were never enforced. The rate-limit hook only matched the raw URL against its own table, so /api/access/*, /api/notices, the utility sidecar, game rulesets, Beholder state and sprite rename all fell into the 600/min default. An absolute-form request line (`POST http://host/api/generate`) also skipped every specific wall: generate, updates/apply, admin/restart and connection tests got 600/min, and /login was not throttled at all. (2) The switch to pnpm 12 leaves every existing checkout with a dead 2-3 GB .pnpm-store/v10 that `pnpm store prune` cannot see. On Termux, the first pnpm 12 install can hit ENOSPC while both stores are on disk, and each later start then retries into the same full disk. There is no linked issue for either finding; one should be opened before the PR is submitted. ## What changed - Behaviour change: a client above a route's declared rate now gets 429, where it used to pass under the shared 600/min default: - 30/min: /api/access/status, setup-code, sessions and refusals - 10/min: /api/access/sessions/:id, DELETE /api/access/sessions, pairing-codes and /api/notices - 60/min: utility-sidecar, Beholder state and sprite rename - 120/min: game-rulesets Absolute-form request lines now hit the same walls as normal requests. - The rate limiter matches its rule table against the route the router dispatched to, not the raw URL. Percent-encoded, duplicate-slash and absolute-form targets can no longer reach the 600/min default bucket. - Routes the table does not cover get their declared config.rateLimit in their own bucket per method and route pattern. HEAD shares GET's bucket, including Fastify's automatic HEAD route for a prefix root's trailing-slash form (HEAD /api/notices/). Existing shared table buckets are unchanged. - After a pnpm 11+ install, the root postinstall deletes the checkout's leftover pnpm 10 store (.pnpm-store/v10) and logs one line. This covers the launchers, the in-app updater, the Windows installer and manual installs. The first update from a pnpm 10 checkout still holds both stores during that install; v10 goes once it succeeds. - The Termux launcher removes .pnpm-store/v10 before it installs dependencies. If the first pnpm 12 install runs out of space, the next ./start-termux.sh frees v10 before retrying instead of failing again. - Regression coverage: - route-rate-limits.regression.ts: routes that only declare a limit (notices is registered under a prefix like the real route, with HEAD on both slash forms) and absolute-form targets over a real socket. - pnpm-runner.regression.mjs: the v10 store cleanup and its launcher/postinstall wiring. ## Validation <!-- These are human tasks. Never tick a box for a check that was not actually performed. --> - [ ] `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 - Manually verify in the browser that Settings > Remote login still works without 429s in normal use: the sessions list, sign out other browsers, and Add a device. - Manually verify that the notice banner, the refused-device banner (polled every 30s) and the access warning banner still load in several open tabs without 429s. - Manually verify that opening the Agent editor (utility sidecar status) and the Beholder chat settings panel still loads without 429s. - Manually verify on an existing checkout that still has .pnpm-store/v10: after ./start.sh (or start.bat), check that .pnpm-store/v10 is gone, .pnpm-store/v11 is still there, and the install log shows the 'Removed the pnpm 10 dependency cache' line. - Manually verify on Termux, updating a pnpm 10 install with ./start-termux.sh: after the first successful dependency install, check that .pnpm-store/v10 is gone. If that install fails for lack of space, check that the next ./start-termux.sh removes .pnpm-store/v10 before 'Refreshing dependencies...' and the install completes. - Manually verify that `curl --request-target http://127.0.0.1:7860/api/generate -X POST http://127.0.0.1:7860/` returns RateLimit-Limit: 60. - Manually verify that after 10 GET /api/notices requests within a minute, `curl -I http://127.0.0.1:7860/api/notices/` returns 429. Agent-run checks (for reference; each regression was shown to fail without its fix): - `node ./scripts/pnpm-runner.mjs build:shared`: succeeded - `node ./scripts/run-regressions.mjs --filter route-rate-limits (fix / previous commit's rate-limit.ts / origin/staging's rate-limit.ts, swapped by copying files `: PASSED with the fix. FAILED against the previous commit (HEAD /api/notices/ shares the GET bucket). FAILED against staging (/api/access/pairing-codes got 200, expected 429). - `Reviewer's all-routes comparison (tsx, hooked onRoute over the real buildApp()), previous commit's selectRule vs the new one, 1317 method/route pairs`: 2 differences, HEAD /api/notices/ and HEAD /api/avatar-models/, both now in their GET bucket at the declared limit. No other change. - `29 regressions run one at a time: route-rate-limits, encoded-api-path-security, basic-auth-throttle-security, remote-login, rate-limit-clock-jump, request-host-`: all 29 passed - `node ./scripts/run-regressions.mjs --filter scripts/regressions/pnpm-runner.regression.mjs`: passed - `node ./scripts/pnpm-runner.mjs --filter @marinara-engine/server lint (tsc -b ../shared && tsc --noEmit && tsc -p character-activity), with pipefail`: exit 0 - `tsc --ignoreConfig --noEmit --strict --module nodenext --target es2022 --skipLibCheck scripts/regressions/route-rate-limits.regression.ts`: exit 0 - `npx prettier --write then --check on rate-limit.ts and route-rate-limits.regression.ts`: all files use Prettier style - `Earlier round (unchanged files): pnpm-runner regression fails-before with all three or each wiring file reverted; launcher/ regressions, termux-sharp, dev-launc`: passed as reported in the earlier round. 5 regressions timed out under load and passed on re-run alone. shellcheck is not installed. - `ESLint`: not run: the repo's only ESLint config is packages/client/eslint.config.js, and no client files changed Independent adversarial review: changes, 3 issue(s), all addressed in a fix-up pass. Known limits and risks: - **Proxy users sharing one address.** The newly enforced walls are per address. Behind a reverse proxy without trusted-proxy forwarding, every browser shares one budget, for example 30/min for /api/access/refusals, which each tab polls every 30s. Many tabs behind one proxy could see 429s on those banners. The limits are the values the route authors declared. - **Per-route buckets.** A route the table does not cover gets a bucket per route pattern, not one shared across its router file. For example, each utility-sidecar route has its own 60/min. Trailing slashes on routed patterns are trimmed (except "/"), so "/x" and "/x/" share a bucket. - **First Termux update still holds both stores.** The first update from a pnpm 10 checkout runs the old start-termux.sh (no re-exec after git reset) or the in-app updater. v10 and v11 therefore sit on disk together during that first pnpm 12 install. This change reclaims v10 after that install succeeds, or on the next launcher start if it ran out of space. It does not prevent the first peak. - **Switching back to Stable.** Until this release reaches main, a checkout that switches back to Stable/main (pnpm 10) re-downloads the whole v10 store, where it used to reuse the stranded copy. - **Slow transition install.** On the transition install, removing a multi-GB v10 tree runs inside the in-app updater's install step and its timeout. Exceeding it has not been observed, but slow storage (Termux, Windows Defender scanning) lengthens that step. - **Docker cache mounts.** The postinstall also runs in Docker builds, so it would delete a stale v10 inside a shared BuildKit cache mount. That only matters if a pnpm 10 build used the same cache mount at the same moment. - **Docs.** docs/TROUBLESHOOTING.md is unchanged: its Android recovery steps already end in ./start-termux.sh, which now removes v10. No docs-i18n follow-up is needed. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) 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) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 17:39:51 +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#8808
No description provided.