[PR #7815] [MERGED] fix(security): harden web origin #7941

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

📋 Pull Request Information

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

Base: staging ← Head: fix/harden-web-origin


📝 Commits (3)

  • 617c230 fix(security): harden web origin checks for encoded API paths
  • ad03575 Merge branch 'staging' into fix/harden-web-origin
  • 4e6470a test(security): check API response headers on encoded paths at runtime

📊 Changes

6 files changed (+230 additions, -5 deletions)

View changed files

📝 packages/server/src/app.ts (+3 -2)
📝 packages/server/src/middleware/compress-json.ts (+2 -1)
📝 packages/server/src/middleware/csrf-protection.ts (+3 -1)
📝 packages/server/src/middleware/rate-limit.ts (+2 -1)
➕ packages/server/src/utils/api-request.ts (+10 -0)
➕ scripts/regressions/encoded-api-path-security.regression.ts (+210 -0)

📄 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

None. This is pre-release security hardening from an internal review, so there is no public issue.

Why this change

  • Several server hooks decided whether a request was an API request by checking whether the raw URL starts with /api/. The router matches the percent-decoded path, so the raw prefix did not always describe the handler that would run.
  • The Origin / CSRF check, API rate limiting and the API no-store / JSON compression hooks should apply to every request that reaches an API handler, however its path is spelled.

What changed

  • csrfProtectionHook checks every unsafe-method request (POST, PUT, PATCH, DELETE), whatever its path. The existing exact-path exemptions for the Android self-authenticating routes stay the same. No non-API unsafe routes exist, so the app's own requests behave as before.

  • New isApiRequest() helper (packages/server/src/utils/api-request.ts). It treats a request as an API request when either the raw URL or the matched route pattern (request.routeOptions.url) starts with /api/. The rate-limit hook, the API no-store onSend hook in app.ts and compressJsonHook use it instead of their raw-URL checks.

  • New regression scripts/regressions/encoded-api-path-security.regression.ts, which runs in pnpm regression:security. It covers:

    • Cross-site multipart and bodiless POSTs to percent-encoded API paths are rejected and never reach the handler.
    • The app's own same-origin uploads and header-less local clients keep working.
    • Encoded and plain paths share one rate-limit bucket.
    • Static files are not treated as API requests.
    • Through the production app on a real socket, plain and encoded API paths both get Cache-Control: no-store and compressed JSON.

    It fails on current staging and passes with this change.

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

Commands the agent ran locally (please re-verify):

  • pnpm check: exit 0. The only lint warning is an existing react-hooks/exhaustive-deps warning in HomeProfessorMariChat.tsx, unrelated to this change.
  • pnpm regression:security: 20/20 passed, including the new regression.
  • encoded-api-path-security regression: fails against the staging versions of the changed files and passes with the fix.
  • Related regressions passed: compress-json, route-rate-limits, issue-sweep-sep5, static-cache-headers, capability-package-cache, experience-state, experience-generation, update-channel-metadata.
  • An app.inject script against the real buildApp() with a temporary data folder showed:
    • Cross-site POSTs to the import, backup, update and sidecar routes return 403 for both plain and encoded paths.
    • A same-origin import with the CSRF header still returns 200.
    • Encoded-path API responses now carry Cache-Control: no-store and rate-limit headers.
    • GET / still serves the client.

Manual verification notes

  • Manually verify in a browser that the normal app still works on http://127.0.0.1:7860: import a character card, upload a background or gallery image, save settings and run a backup.
  • Manually verify that a LAN or reverse-proxy setup that uses CSRF_TRUSTED_ORIGINS can still save and upload.
  • Manually verify on the Android APK that the login and handshake still complete, since its self-authenticating routes keep their exemptions.

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)

No user-facing docs change: the documented CSRF behaviour already describes unsafe API routes, and that is unchanged.

UI evidence (if applicable)

Not applicable (server-only change).

Summary by CodeRabbit

  • Bug Fixes
    • API security checks now recognize routes with percent-encoded prefixes.
    • Cross-site protections apply to unsafe requests across all paths, while existing exemptions remain in place.
    • Rate limiting, response caching, and JSON compression consistently recognize API requests across plain and encoded routes.

🔄 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/7815 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/19/2026 **Status:** ✅ Merged **Merged:** 10/19/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/harden-web-origin` --- ### 📝 Commits (3) - [`617c230`](https://github.com/Pasta-Devs/Marinara-Engine/commit/617c23059c5f60225cff93e0793f3b6fab841792) fix(security): harden web origin checks for encoded API paths - [`ad03575`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ad035759b940a8f5c266c588ccc0d82c67f9d82a) Merge branch 'staging' into fix/harden-web-origin - [`4e6470a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4e6470ab5fcc2659146365385aa2b91ccc2f9665) test(security): check API response headers on encoded paths at runtime ### 📊 Changes **6 files changed** (+230 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/app.ts` (+3 -2) 📝 `packages/server/src/middleware/compress-json.ts` (+2 -1) 📝 `packages/server/src/middleware/csrf-protection.ts` (+3 -1) 📝 `packages/server/src/middleware/rate-limit.ts` (+2 -1) ➕ `packages/server/src/utils/api-request.ts` (+10 -0) ➕ `scripts/regressions/encoded-api-path-security.regression.ts` (+210 -0) </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 None. This is pre-release security hardening from an internal review, so there is no public issue. ## Why this change - Several server hooks decided whether a request was an API request by checking whether the raw URL starts with `/api/`. The router matches the percent-decoded path, so the raw prefix did not always describe the handler that would run. - The Origin / CSRF check, API rate limiting and the API `no-store` / JSON compression hooks should apply to every request that reaches an API handler, however its path is spelled. ## What changed - `csrfProtectionHook` checks every unsafe-method request (POST, PUT, PATCH, DELETE), whatever its path. The existing exact-path exemptions for the Android self-authenticating routes stay the same. No non-API unsafe routes exist, so the app's own requests behave as before. - New `isApiRequest()` helper (`packages/server/src/utils/api-request.ts`). It treats a request as an API request when either the raw URL or the matched route pattern (`request.routeOptions.url`) starts with `/api/`. The rate-limit hook, the API `no-store` `onSend` hook in `app.ts` and `compressJsonHook` use it instead of their raw-URL checks. - New regression `scripts/regressions/encoded-api-path-security.regression.ts`, which runs in `pnpm regression:security`. It covers: - Cross-site multipart and bodiless POSTs to percent-encoded API paths are rejected and never reach the handler. - The app's own same-origin uploads and header-less local clients keep working. - Encoded and plain paths share one rate-limit bucket. - Static files are not treated as API requests. - Through the production app on a real socket, plain and encoded API paths both get `Cache-Control: no-store` and compressed JSON. It fails on current `staging` and passes with this change. ## 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` Commands the agent ran locally (please re-verify): - `pnpm check`: exit 0. The only lint warning is an existing `react-hooks/exhaustive-deps` warning in `HomeProfessorMariChat.tsx`, unrelated to this change. - `pnpm regression:security`: 20/20 passed, including the new regression. - `encoded-api-path-security` regression: fails against the `staging` versions of the changed files and passes with the fix. - Related regressions passed: `compress-json`, `route-rate-limits`, `issue-sweep-sep5`, `static-cache-headers`, `capability-package-cache`, `experience-state`, `experience-generation`, `update-channel-metadata`. - An `app.inject` script against the real `buildApp()` with a temporary data folder showed: - Cross-site POSTs to the import, backup, update and sidecar routes return 403 for both plain and encoded paths. - A same-origin import with the CSRF header still returns 200. - Encoded-path API responses now carry `Cache-Control: no-store` and rate-limit headers. - `GET /` still serves the client. ### Manual verification notes - Manually verify in a browser that the normal app still works on `http://127.0.0.1:7860`: import a character card, upload a background or gallery image, save settings and run a backup. - Manually verify that a LAN or reverse-proxy setup that uses `CSRF_TRUSTED_ORIGINS` can still save and upload. - Manually verify on the Android APK that the login and handshake still complete, since its self-authenticating routes keep their exemptions. ## 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) No user-facing docs change: the documented CSRF behaviour already describes unsafe API routes, and that is unchanged. ## UI evidence (if applicable) Not applicable (server-only change). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * API security checks now recognize routes with percent-encoded prefixes. * Cross-site protections apply to unsafe requests across all paths, while existing exemptions remain in place. * Rate limiting, response caching, and JSON compression consistently recognize API requests across plain and encoded routes. <!-- 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:09:01 +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#7941
No description provided.