[PR #7920] [MERGED] fix(server): treat a relayed Host on a loopback socket as remote #8005

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7920
Author: @Gunterlie
Created: 10/21/2026
Status: ✅ Merged
Merged: 10/21/2026
Merged by: @Gunterlie

Base: staging ← Head: fix/loopback-relayed-host


📝 Commits (3)

  • 5676850 fix(server): treat a relayed Host on a loopback socket as remote
  • d47dc01 fix(server): document relayed-Host detection and harden its regression
  • 5d38b6e fix(client): mark LAN visitors of the Vite dev proxy as forwarded

📊 Changes

8 files changed (+93 additions, -6 deletions)

View changed files

📝 .env.example (+2 -1)
📝 docs/REMOTE_ACCESS.md (+1 -1)
📝 docs/TROUBLESHOOTING.md (+1 -0)
📝 packages/client/vite.config.ts (+8 -1)
📝 packages/server/src/middleware/host-validation.ts (+1 -1)
📝 packages/server/src/middleware/ip-allowlist.ts (+25 -2)
📝 packages/server/src/routes/generate/prompt-preview.ts (+2 -0)
📝 scripts/regressions/loopback-proxy-auth-security.regression.ts (+53 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote the 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

Part of #7909. @SpicyMarinara

Why this change

REQUIRE_AUTH_FOR_LOOPBACK_PROXY treats a loopback request as a remote visitor only when it carries proxy forwarding headers. A proxy or tunnel on the same computer that passes the visitor's public Host but adds no forwarding headers still looked like the owner's own browser, so its visitors skipped Basic Auth, the allowlist and admin-secret checks.

A browser on this computer always addresses Marinara by a local name. A loopback socket that announces a domain or a LAN address was relayed, so it now gets the same checks as other proxied traffic.

What changed

  • A loopback request whose Host is not localhost, *.localhost, a loopback/unspecified IP, or the machine's own hostname (<hostname>, <hostname>.local, e.g. Debian's 127.0.1.1 mapping) counts as relayed by a local proxy. REQUIRE_AUTH_FOR_LOOPBACK_PROXY=false still turns this off.
  • Docker gateway trust is unchanged and stays header-only, so LAN access through Docker Desktop/OrbStack published ports behaves as before (pinned by docker-gateway-auth-security).
  • The internal prompt-preview hop no longer copies the visitor's Host, so it stays a local call.
  • The Vite dev proxy keeps the browser's Host (changeOrigin: false), so LAN devices using pnpm dev are no longer seen as local.
  • docs/REMOTE_ACCESS.md, docs/TROUBLESHOOTING.md and .env.example describe the Host-based detection and the custom /etc/hosts alias case.
  • Regressions: relayed domain/LAN Hosts need auth; local Host forms (localhost, LOCALHOST., [::1], [::ffff:127.0.0.1], 0.0.0.0, [::], 127.0.1.1, own hostname) stay local; relayed Host with correct Basic Auth works; :authority-only requests; prompt-preview inner Host.

Known limit, unchanged by design: a relay that rewrites Host to a loopback name and adds no forwarding headers (for example nginx with default proxy_pass and no X-Forwarded-For, or raw TCP forwards) cannot be told apart from a local browser. The docs already require forwarding headers or Basic Auth for those setups.

Validation

Run by the agent on devbox:

  • loopback-proxy-auth-security, docker-proxy-auth, docker-gateway-auth-security, basic-auth-throttle-security regressions: pass

  • pnpm regression:android-local-auth: pass

  • server tsc --noEmit: pass; client typecheck: pass

  • pnpm regression:security: multiplayer-peer-server-security fails with ERR_TLS_CERT_ALTNAME_INVALID in this environment; it fails the same way on the unchanged tree

  • pnpm check: pass (exit 0; 1 existing lint warning, 0 errors)

  • 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 that http://localhost:7860, http://127.0.0.1:7860 and http://<hostname>:7860 on the host computer still open without a password when no auth is configured.
  • Manually verify that a same-host reverse proxy for a domain that sends no X-Forwarded-For now asks for Basic Auth.
  • Manually verify that pnpm dev on localhost:5173 works as before. LAN phone testing through pnpm dev now needs Basic Auth or ALLOW_UNAUTHENTICATED_PRIVATE_NETWORK=true, like a direct LAN connection.
  • Manually verify that Docker Desktop/OrbStack LAN access through the published port is unchanged.

Local review

  • Opus gate review: no code defects; doc and test findings fixed in this PR.
  • CodeRabbit (local): 1 finding, not applied. It asked to drop the own-hostname exemption. The visitor controls Host, so any pass-through proxy already lets them send Host: localhost; removing the exemption closes nothing there. It only matters for a proxy routed by the machine's own LAN-only name without forwarding headers, which belongs to the documented header-less relay limit above. Removing it would lock out the default Debian/Ubuntu local URL http://<hostname>:7860.

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)

Docs-i18n follow-up: #7921

UI evidence (if applicable)

Not applicable: server-side access check, docs and dev proxy only.

Summary by CodeRabbit

  • Bug Fixes

    • Improved loopback proxy authentication to correctly recognize relayed requests using public domains, LAN addresses, or custom hostnames.
    • Preserved local access without authentication for recognized local hosts.
    • Prevented relayed host information from affecting internal prompt previews.
    • Preserved the original host during development API proxying.
  • Documentation

    • Updated remote-access guidance to explain relayed-host detection.
    • Added troubleshooting steps for custom hostnames mapped to 127.0.0.1.

🔄 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/7920 **Author:** [@Gunterlie](https://github.com/Gunterlie) **Created:** 10/21/2026 **Status:** ✅ Merged **Merged:** 10/21/2026 **Merged by:** [@Gunterlie](https://github.com/Gunterlie) **Base:** `staging` ← **Head:** `fix/loopback-relayed-host` --- ### 📝 Commits (3) - [`5676850`](https://github.com/Pasta-Devs/Marinara-Engine/commit/567685064c2887e3c9e9fb33a5b96fc0f54e9e74) fix(server): treat a relayed Host on a loopback socket as remote - [`d47dc01`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d47dc01dd8aebab055646a1a5934836a3b80b0fd) fix(server): document relayed-Host detection and harden its regression - [`5d38b6e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5d38b6ed0b304fb6db7093e7b899d3b93da227fc) fix(client): mark LAN visitors of the Vite dev proxy as forwarded ### 📊 Changes **8 files changed** (+93 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `.env.example` (+2 -1) 📝 `docs/REMOTE_ACCESS.md` (+1 -1) 📝 `docs/TROUBLESHOOTING.md` (+1 -0) 📝 `packages/client/vite.config.ts` (+8 -1) 📝 `packages/server/src/middleware/host-validation.ts` (+1 -1) 📝 `packages/server/src/middleware/ip-allowlist.ts` (+25 -2) 📝 `packages/server/src/routes/generate/prompt-preview.ts` (+2 -0) 📝 `scripts/regressions/loopback-proxy-auth-security.regression.ts` (+53 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote the 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 Part of #7909. @SpicyMarinara ## Why this change `REQUIRE_AUTH_FOR_LOOPBACK_PROXY` treats a loopback request as a remote visitor only when it carries proxy forwarding headers. A proxy or tunnel on the same computer that passes the visitor's public Host but adds no forwarding headers still looked like the owner's own browser, so its visitors skipped Basic Auth, the allowlist and admin-secret checks. A browser on this computer always addresses Marinara by a local name. A loopback socket that announces a domain or a LAN address was relayed, so it now gets the same checks as other proxied traffic. ## What changed - A loopback request whose `Host` is not `localhost`, `*.localhost`, a loopback/unspecified IP, or the machine's own hostname (`<hostname>`, `<hostname>.local`, e.g. Debian's `127.0.1.1` mapping) counts as relayed by a local proxy. `REQUIRE_AUTH_FOR_LOOPBACK_PROXY=false` still turns this off. - Docker gateway trust is unchanged and stays header-only, so LAN access through Docker Desktop/OrbStack published ports behaves as before (pinned by `docker-gateway-auth-security`). - The internal prompt-preview hop no longer copies the visitor's `Host`, so it stays a local call. - The Vite dev proxy keeps the browser's `Host` (`changeOrigin: false`), so LAN devices using `pnpm dev` are no longer seen as local. - `docs/REMOTE_ACCESS.md`, `docs/TROUBLESHOOTING.md` and `.env.example` describe the Host-based detection and the custom `/etc/hosts` alias case. - Regressions: relayed domain/LAN Hosts need auth; local Host forms (`localhost`, `LOCALHOST.`, `[::1]`, `[::ffff:127.0.0.1]`, `0.0.0.0`, `[::]`, `127.0.1.1`, own hostname) stay local; relayed Host with correct Basic Auth works; `:authority`-only requests; prompt-preview inner Host. Known limit, unchanged by design: a relay that rewrites `Host` to a loopback name and adds no forwarding headers (for example nginx with default `proxy_pass` and no `X-Forwarded-For`, or raw TCP forwards) cannot be told apart from a local browser. The docs already require forwarding headers or Basic Auth for those setups. ## Validation <!-- Required: include commands run and/or manual checks performed --> Run by the agent on devbox: - `loopback-proxy-auth-security`, `docker-proxy-auth`, `docker-gateway-auth-security`, `basic-auth-throttle-security` regressions: pass - `pnpm regression:android-local-auth`: pass - server `tsc --noEmit`: pass; client typecheck: pass - `pnpm regression:security`: `multiplayer-peer-server-security` fails with `ERR_TLS_CERT_ALTNAME_INVALID` in this environment; it fails the same way on the unchanged tree - `pnpm check`: pass (exit 0; 1 existing lint warning, 0 errors) - [ ] `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 that `http://localhost:7860`, `http://127.0.0.1:7860` and `http://<hostname>:7860` on the host computer still open without a password when no auth is configured. - Manually verify that a same-host reverse proxy for a domain that sends no `X-Forwarded-For` now asks for Basic Auth. - Manually verify that `pnpm dev` on `localhost:5173` works as before. LAN phone testing through `pnpm dev` now needs Basic Auth or `ALLOW_UNAUTHENTICATED_PRIVATE_NETWORK=true`, like a direct LAN connection. - Manually verify that Docker Desktop/OrbStack LAN access through the published port is unchanged. ### Local review - Opus gate review: no code defects; doc and test findings fixed in this PR. - CodeRabbit (local): 1 finding, not applied. It asked to drop the own-hostname exemption. The visitor controls `Host`, so any pass-through proxy already lets them send `Host: localhost`; removing the exemption closes nothing there. It only matters for a proxy routed by the machine's own LAN-only name without forwarding headers, which belongs to the documented header-less relay limit above. Removing it would lock out the default Debian/Ubuntu local URL `http://<hostname>:7860`. ## 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) Docs-i18n follow-up: #7921 ## UI evidence (if applicable) Not applicable: server-side access check, docs and dev proxy only. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit - **Bug Fixes** - Improved loopback proxy authentication to correctly recognize relayed requests using public domains, LAN addresses, or custom hostnames. - Preserved local access without authentication for recognized local hosts. - Prevented relayed host information from affecting internal prompt previews. - Preserved the original host during development API proxying. - **Documentation** - Updated remote-access guidance to explain relayed-host detection. - Added troubleshooting steps for custom hostnames mapped to `127.0.0.1`. <!-- 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:35 +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#8005
No description provided.