mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #8772] [MERGED] fix(server): keep redirected outbound requests within the caller's trust #8795
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#8795
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/8772
Author: @SpicyMarinara
Created: 11/3/2026
Status: ✅ Merged
Merged: 11/4/2026
Merged by: @SpicyMarinara
Base:
staging← Head:fix/hardening-safe-fetch-redirects📝 Commits (6)
d3d94f0fix(server): keep redirected outbound requests within the caller's trust961b50cfix(server): judge redirect trust by the address a hop connected to9212260fix(server): judge local-looking redirect hops by the address they reached25b829afix(server): trust a redirect hop only on the built-in local rangesa254296fix(server): let a remote redirect hop keep the caller's non-local allowancesa7543a9fix(server): refuse remote redirects into 192.0.0.0/24 with local URLs on📊 Changes
5 files changed (+557 additions, -31 deletions)
View changed files
📝
packages/server/src/routes/bot-browser-datacat.routes.ts(+13 -4)📝
packages/server/src/routes/bot-browser-janny.routes.ts(+16 -5)📝
packages/server/src/utils/security.ts(+139 -22)➕
scripts/regressions/bot-browser-upstream-fetch.regression.ts(+58 -0)➕
scripts/regressions/safe-fetch-redirect-local-trust.regression.ts(+331 -0)📄 Description
Linked issue
Closes #8769
Why this change
Some outbound requests are allowed to reach loopback, mDNS or local-network addresses: the user configured a local model server, image backend or TTS server, or turned on a *_LOCAL_URLS_ENABLED switch (on by default for providers on Android). That trust is meant only for the server the user chose. Until now, safeFetch checked every redirect hop against the caller's original allowances, so a remote service's redirect inherited them as well. The repository already refuses this for image result URLs, which must not inherit provider trust. Whether a hop counts as local must also come from the address the request actually reached. It must not come from a separate DNS lookup made afterwards, or from how the hostname is spelled. Names ending in .local, .internal or .localhost are answered by DNS or mDNS and can point anywhere; localhost itself is always pinned to loopback. "Local" also has to mean the built-in loopback and local-network ranges, not the broader reserved-address deny-list. That list also covers VPN fake-IP answers (198.18.0.0/15), test and multicast ranges, and any range an operator trusts for sign-in through TRUSTED_PRIVATE_NETWORKS. Separately, the DataCat and JannyAI Card Browser requests were the last catalog calls still using plain fetch. Plain fetch follows redirects with no address checks and reads replies with no size limit; every other Card Browser provider goes through safeFetch.
What changed
Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
Agent-run checks (for reference; each regression was shown to fail without its fix):
node ./scripts/pnpm-runner.mjs build:shared: succeedednode ./scripts/run-regressions.mjs --filter safe-fetch-redirect-local-trust: PASSED at961b50c873and again at25b829a4ce. With thed3d94f0236security.ts it FAILED on the dual-stack case ('Refused to fetch https://nas.test:11434/v1/models ...')Temporary per-case copy of that regression (zz-tmp-proof-relay.regression.ts), run with the earlier security.ts and again with the fix, then deleted: Before: relay.test and nat64.test redirects to loopback were FOLLOWED under both local-URL policies (4 extra /blocked hits on loopback). After: all 4 refused, relay.test looked up once, only the 4 expTemporary real-connection probe (zz-tmp-real-pin.regression.ts), then deleted: PASSED: an allowLocal DNS name with [::1, 127.0.0.1] records reached an IPv4-only server through the pinned IPv4 record, with one DNS call, and followed its local redirectnode ./scripts/run-regressions.mjs --filter <file> --concurrency 1 for each of the 109 regressions that reference utils/security, safeFetch, llmFetch, base-prov: 109/109 passed (run after the source change; the only later edit was a comment in the test file, and that regression was re-run and passed)node ./scripts/run-regressions.mjs --filter security: 32/32 passed (also at25b829a4ce)Follow-up 25b829a4ce, with the 9212260be7 security.ts restored: FAILED on the fake-IP case ('fakeip.test cannot redirect to loopback') and, in a throwaway copy, on the TRUSTED_PRIVATE_NETWORKS case; both pass with the fix. With the 0.0.0.0 branch disabled, the 0.0.0.0 server case FAILED.node ./scripts/run-regressions.mjs --filter bot-browser: 4/4 passednode ./scripts/pnpm-runner.mjs lint in packages/server (tsc -b ../shared && tsc --noEmit && tsc -p ../../scripts/regressions/tsconfig.character-activity.json): exit 0npx tsc -p <scratchpad tsconfig covering the two new regression files, same lib/types as the repo's regression tsconfig>: exit 0npx prettier --write / --check on all 5 changed files: all passeslint: not applicable: ESLint covers only packages/client; the server and scripts lint step is the tsc run aboveFollow-up a2542969c0/a7543a9fe3 (Dottore LOW: remote fake-IP redirects): node ./scripts/run-regressions.mjs --filter safe-fetch-redirect-local-trust: PASSED; with the25b829a4cesecurity.ts it FAILED on fake-IP to fake-IP, and with thea2542969c0security.ts it FAILED on the 192.0.0.4 redirect.--filter security32/32, fetch 6/6, proxy 4/4, provider 13/13, image 28/28, loopback 2/2, audio 14/14, tts 13/13, bot-browser 4/4; server lint exit 0.Independent adversarial review: changes, 2 issue(s), all addressed in a fix-up pass. Local CodeRabbit review then raised 3 issues (trust range, a wording error, the proxy caveat); all three were accepted and fixed in
9212260be7and25b829a4ce. A second local CodeRabbit pass on those commits raised 2 more: the remote-to-remote redirect finding is fixed ina2542969c0for fake-IP targets, while its suggestion to also follow redirects to .local names is rejected (a remote hop redirecting to a .local name is the case this PR closes, since mDNS can answer it with a LAN host); the request to split the loopback-hit assertion is rejected because the single assertion already lists every expected hit in order, including the 0.0.0.0 case. Dottore's later LOW finding (remote fake-IP redirects refused with local URLs on) is fixed ina2542969c0, and an adversarial review of that fix found 192.0.0.0/24 missing from the refused targets, fixed ina7543a9fe3.Known limits and risks:
Behaviour changes:
With local URLs allowed (Android providers by default, ComfyUI/SwarmUI/A1111, PocketTTS, *_LOCAL_URLS_ENABLED), a hop addressed by DNS name, including .local, .internal and .localhost names, is now resolved once and pinned to one address. The hop the caller configured is pinned IPv4 first; a remote hop's redirect target is pinned to its first record in resolver order. Previously it used Node's default connection handling, which could fall back across every record. A host that publishes an IPv4 record that cannot be reached, but serves on IPv6, would now fail to connect. Desktop provider calls already work this way.
Those pinned hops get a per-request connection that is closed after the response, instead of the shared connection pool. That costs a new TCP/TLS handshake per request for DNS-named servers in local-URL mode. IP literals and localhost are unchanged.
A hop whose connected address is unknown counts as remote, so its redirects cannot reach local addresses. That covers a hop carried by an env proxy, a name that does not resolve locally, and a hop sent through a caller's own dispatcher (no production caller passes one). Behind an env proxy, a .local, .internal or .localhost host that is not in NO_PROXY cannot follow even an http-to-https redirect on its own name; adding it to NO_PROXY is the workaround.
Only the built-in loopback, RFC 1918, link-local, ULA and CGNAT/Tailscale ranges, plus exactly 0.0.0.0 and ::, count as local when deciding whether to trust a redirecting hop. VPN fake-IP hops (198.18.0.0/15) are remote. With local URLs on (the Android provider default), a fake-IP hop's redirect is refused when the target is a local-looking name or an address in the local ranges. It is followed otherwise, including to another name the VPN answers with a fake-IP address. A fake-IP address does not show where the VPN app will connect, so for those targets the VPN app's routing decides, as with an env proxy. A name the VPN routes to a local server, or the VPN's own TUN address, can still be reached. Without local URLs (the desktop default), fake-IP addresses were already refused and still are.
A remote hop's redirect target is refused on a slightly wider set than the set that makes a hop trusted: all of 0.0.0.0/8, all of 192.0.0.0/24 (464XLAT, DS-Lite and a legacy cloud metadata endpoint), IPv4-compatible IPv6 forms such as ::7f00:1, and NAT64 addresses that embed one of the refused IPv4 addresses. If a DNS filter answers a provider's redirect target with 0.0.0.0 or ::, that redirect fails, even behind an env proxy.
With local URLs on, a redirect target that does not resolve locally is followed only when an env proxy carries the request. The proxy looks the name up itself, so the app cannot check where it leads, as with any proxied request. Without a proxy, through a caller's own dispatcher, or for a NO_PROXY host, that target is refused.
Checks are by address range only. An address this machine holds outside those ranges, such as its own global IPv6 or public IPv4 address, is not recognised as this machine. This is unchanged and applies to the desktop deny-list too.
An address that is only in TRUSTED_PRIVATE_NETWORKS no longer makes a hop local. This matches isReservedIp's existing rule that the sign-in trust list does not change outbound checks. With local URLs on, a remote hop may redirect into such a range, as it may to any public address. Without local URLs it stays refused. When deciding whether to trust a hop, all of these count as remote: TEST-NET, multicast, class E, 192.0.0.0/24, deprecated IPv4-compatible IPv6 forms and the rest of 0.0.0.0/8. The well-known NAT64 prefix (64:ff9b::/96) counts as remote because it carries IPv4 internet traffic. As a redirect target, a NAT64 address is judged by the IPv4 address it embeds.
A LAN host whose only records are global IPv6 counts as remote. Redirects to its own global address still work; redirects into private addresses do not.
DataCat and the JannyAI search calls no longer follow redirects at all. If either API starts redirecting, for example to a www host, those calls will fail until the policy is relaxed. This matches fetchBotBrowserJson and the Janny card download. Neither live endpoint redirects today.
A same-origin redirect from a remote hop that now resolves to a local address is refused as well.
Gaps:
pnpm check(client build, localization, impeccable) and the full regression suite. Only the targeted checks listed above were run; CI runs the rest. No client copy changed.Docs and release impact
docs-i18nbranch updated to match, or a[docs-i18n]follow-up issue opened (see CONTRIBUTING.md § Translated documentation)🤖 Generated with Claude Code
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.