mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #725] [MERGED] fix(csrf): surface untrusted-origin saves in the UI instead of failing silently #4746
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#4746
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/725
Author: @LukaTheHero
Created: 5/12/2026
Status: ✅ Merged
Merged: 5/12/2026
Merged by: @LukaTheHero
Base:
main← Head:fix/csrf-origin-trust-banner📝 Commits (2)
d76dfaffix(csrf): surface untrusted-origin saves in the UI instead of failing silentlyf2bc9e5fix(csrf): extract env line correctly when the hinted origin contains dots📊 Changes
12 files changed (+598 additions, -9 deletions)
View changed files
📝
.env.example(+18 -4)📝
CHANGELOG.md(+1 -0)📝
docs/REMOTE_ACCESS.md(+1 -1)📝
docs/TROUBLESHOOTING.md(+1 -1)📝
packages/client/src/App.tsx(+2 -0)➕
packages/client/src/components/diagnostics/CsrfOriginWarningBanner.tsx(+209 -0)📝
packages/client/src/lib/csrf-fetch.ts(+70 -3)📝
packages/server/src/index.ts(+2 -0)📝
packages/server/src/middleware/csrf-protection.ts(+108 -0)➕
packages/server/src/routes/csrf-diagnostics.routes.ts(+23 -0)📝
packages/server/src/routes/index.ts(+2 -0)📝
packages/server/test/security-middleware.test.ts(+161 -0)📄 Description
Summary
Closes #722.
A self-hosted Marinara instance reached through a public IP, reverse-proxy domain, or Tailscale MagicDNS hostname is not auto-trusted by the CSRF hook, so any unsafe API request gets a 403. The rejection was invisible to the user — chats stayed
promptPresetId: null, settings reverted on reload, and the failure mode looked like a model-quality regression instead of a config issue.Why this matters
The reporter spent significant time chasing what looked like a prompt-quality bug in SillyTavern-style roleplay, because Marinara's UI accepted a preset selection that the server then silently refused to persist. They ended up reaching out on Discord to debug it. Other self-hosters on public IPs / reverse proxies / Tailscale MagicDNS are hitting the same wall and probably quitting rather than diagnosing it.
What changed
Three layers cover the silent-failure case:
GET /api/csrf/origin-statusendpoint that reports the trust verdict without triggering the CSRF-hook reject-log cache. The banner shows the offending origin, the exact.envline, a one-click Copy button, and a dismiss-for-session button.Save blocked: origin not trusted/: cross-site request rejected/: missing CSRF headerwith the.envhint as the description. Debounced per origin (10s) so parallel mutations don't stack identical toasts.codefield on every CSRF 403:CSRF_ORIGIN_NOT_TRUSTED,CSRF_REFERER_NOT_TRUSTED,CSRF_CROSS_SITE,CSRF_MISSING_HEADER. The client detects viacode(with a fallback regex for older servers paired with newer clients).Plus a startup diagnostic — the server logs the active CSRF auto-trust scope on boot:
[csrf] Auto-trusted (loopback): http://127.0.0.1:<PORT>, http://localhost:<PORT>[csrf] Auto-trusted (HOST): <protocol>://<host>:<PORT>(when HOST is bound to a specific address)[csrf] Auto-trusted (pattern): any RFC 1918 / CGNAT (Tailscale) / Docker / link-local IP literal where Origin matches Host[csrf] CSRF_TRUSTED_ORIGINS: <comma-separated list>WARNwhen CSRF_TRUSTED_ORIGINS contains*Auto-trust scope (unchanged)
These never had the bug and don't need to be added to
CSRF_TRUSTED_ORIGINS:127.0.0.1:<PORT>,localhost:<PORT>)10.0.0.0/8,172.16.0.0/12,192.168.0.0/16)100.64.0.0/10)172.16.0.0/12)What still needs an explicit entry (and now surfaces the banner instead of failing silently):
http://203.0.113.10:7831)https://chat.example.com)http://my-box.tailnet.ts.net:7860)Multiple origins are comma-separated — parsing was always supported;
.env.exampleand docs now show the syntax.Files changed
Server
packages/server/src/middleware/csrf-protection.ts— addedcodefield to all 4 reject bodies; new exportedevaluateRequestOriginTrust()pure predicate; newgetCsrfTrustSummary()/logCsrfTrustSummary()helpers.packages/server/src/routes/csrf-diagnostics.routes.ts(new) —GET /api/csrf/origin-status.packages/server/src/routes/index.ts— register the new route.packages/server/src/index.ts— log auto-trust scope after listen.packages/server/test/security-middleware.test.ts— assertions for the newcodefield, a comma-separated multi-origin test, and three new tests for the diagnostic endpoint.Client
packages/client/src/lib/csrf-fetch.ts— fetch shim now inspects 403 bodies and pops a sonner toast on CSRF rejection.packages/client/src/components/diagnostics/CsrfOriginWarningBanner.tsx(new) — sticky top banner mounted inApp.tsxalongside<Toaster>. Usesvar(--destructive)so it picks up the active theme.packages/client/src/App.tsx— mount the banner.Docs / config
.env.example— full rewrite of theCSRF_TRUSTED_ORIGINScomment with auto-trust list, when-to-add guidance, multi-origin example, and the*warning.docs/TROUBLESHOOTING.mdanddocs/REMOTE_ACCESS.md— describe the new banner, toast,codefield, and multi-origin syntax.CHANGELOG.md— Unreleased > Fixed entry.What's deliberately out of scope
CSRF_TRUSTED_ORIGINSfrom the browser — the banner has a copy-paste flow today; a Settings editor is a separate UX call worth its own PR..env.Reporter context
Originally surfaced by chai on Discord, where a user thought their roleplay output had regressed because Marinara's universal preset wasn't being applied. The actual cause was the public-IP origin rejection silently nulling out
promptPresetId. Issue #722 was filed with the full repro.Summary by CodeRabbit
New Features
CSRF_TRUSTED_ORIGINS.Bug Fixes
Documentation
.env.examplewith detailed guidance on auto-trusted origins and manual configuration.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.