[PR #446] [MERGED] feat(security): friendly HTML page when remote browsers hit the auth lockdown #4621

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/446
Author: @LukaTheHero
Created: 5/5/2026
Status: ✅ Merged
Merged: 5/5/2026
Merged by: @LukaTheHero

Base: main ← Head: feat/lockdown-friendly-page


📝 Commits (2)

  • 73272ad feat(security): serve a friendly HTML page when the lockdown blocks a browser
  • 26c8083 docs(remote-access): bundle chai's Remote Access guide and deep-link from the lockdown page

📊 Changes

5 files changed (+415 additions, -11 deletions)

View changed files

📝 docs/CONFIGURATION.md (+2 -0)
📝 docs/FAQ.md (+1 -1)
➕ docs/REMOTE_ACCESS.md (+156 -0)
📝 packages/server/src/middleware/basic-auth.ts (+198 -10)
📝 packages/server/test/security-middleware.test.ts (+58 -0)

📄 Description

Why

A lot of Docker / Tailscale / phone-on-LAN users are getting confused by the no-auth lockdown that ships with the safe-by-default Basic Auth behavior. When they open the server in a browser without setting BASIC_AUTH_USER/BASIC_AUTH_PASS/IP_ALLOWLIST, they get a raw JSON 403 that just says "Forbidden" — no instructions, no link, no obvious next step. Discord has been seeing this report repeatedly.

This PR keeps the security posture identical (still 403, still fails closed) but replaces the JSON body with a styled, self-contained HTML page only when the request looks like a browser navigation (Accept: text/html, non-/api/ path). Programmatic clients (curl, fetch, /api/*) still get the original JSON, so nothing scripted breaks.

What the page shows

  • The user's detected IP (so they don't have to figure it out for the IP_ALLOWLIST path)
  • Option 1 — Basic Auth with the exact two .env lines to add
  • Option 2 — IP allowlist with IP_ALLOWLIST=<their-ip> pre-filled
  • A "trusted network escape hatch" footnote pointing at ALLOW_UNAUTHENTICATED_PRIVATE_NETWORK=true
  • A "Detailed walkthrough →" link inside each option panel that deep-links to the matching anchor in docs/REMOTE_ACCESS.md, plus link cards at the bottom for the full walkthrough, FAQ, and Troubleshooting

Bundled with chai's docs PR (#447)

To avoid the day-one broken-link risk chai called out in #447, this PR also picks up chai's docs/REMOTE_ACCESS.md walkthrough plus the CONFIGURATION.md / FAQ.md cross-reference lines. Either #446 or #447 can land first; the other becomes a no-op merge. Once both have landed, #447 can be closed without merging since its content is included here.

Implementation notes

  • sendLockdown(request, reply) now branches on wantsHtml(request).
  • wantsHtml returns false for /api/* and only true if Accept includes text/html.
  • The IP and other dynamic content go through an escapeHtml() helper so we can't be tricked into XSS via a header-spoofed remote address.
  • HTML is fully inline (CSS in <style>) so it works without any client build artifacts and won't 404 on a misconfigured static server.
  • The existing JSON message is preserved as LOCKDOWN_JSON_MESSAGE.

Tests

Added two unit tests in packages/server/test/security-middleware.test.ts:

  • A browser navigation (Accept: text/html) hitting a non-API path gets a 403 with Content-Type: text/html, the doctype, the BASIC_AUTH_USER token, and IP_ALLOWLIST=<their-ip>.
  • A non-browser (Accept: application/json) request to /api/headers still gets the original JSON 403.

The pre-existing "non-loopback fails closed" test still passes unchanged. All 18 security-middleware tests pass.

Manual verification (not yet done — please confirm before merging)

  • Start Marinara Engine without setting BASIC_AUTH_USER/BASIC_AUTH_PASS/IP_ALLOWLIST
  • From another device on the LAN (or via Tailscale), open http://<server-ip>:7860/ in a browser
  • Confirm the styled HTML page renders, the IP pill matches the device's address, and both options' code blocks are readable
  • On mobile (narrow viewport), confirm nothing is cut off and the <pre> blocks scroll horizontally instead of overflowing
  • Click "Detailed walkthrough →" on each option panel and confirm it opens the matching anchor in REMOTE_ACCESS.md on GitHub
  • From the same blocked device, run curl -i http://<server-ip>:7860/api/headers and confirm the JSON 403 still comes back with Content-Type: application/json
  • Set BASIC_AUTH_USER/BASIC_AUTH_PASS, restart, and confirm the lockdown page is gone (browser gets the normal 401 challenge instead)
  • Set ALLOW_UNAUTHENTICATED_PRIVATE_NETWORK=true, restart, and confirm the LAN browser loads the app normally

Summary by CodeRabbit

  • New Features

    • Lockdown now serves a friendly HTML page to browser-like clients while API clients continue to receive JSON, adapting responses by client characteristics.
  • Tests

    • Added a navigation-style test verifying unauthenticated browser requests return the HTML lockdown page with environment placeholders.
  • Documentation

    • Added Remote Access guide and updated Configuration and FAQ with access-control setup and troubleshooting.

🔄 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/446 **Author:** [@LukaTheHero](https://github.com/LukaTheHero) **Created:** 5/5/2026 **Status:** ✅ Merged **Merged:** 5/5/2026 **Merged by:** [@LukaTheHero](https://github.com/LukaTheHero) **Base:** `main` ← **Head:** `feat/lockdown-friendly-page` --- ### 📝 Commits (2) - [`73272ad`](https://github.com/Pasta-Devs/Marinara-Engine/commit/73272ada07ae83c95a43ca8265c1ca0a77fd218d) feat(security): serve a friendly HTML page when the lockdown blocks a browser - [`26c8083`](https://github.com/Pasta-Devs/Marinara-Engine/commit/26c808376c39a7bc2c05fbaf731d2bc9c617bb93) docs(remote-access): bundle chai's Remote Access guide and deep-link from the lockdown page ### 📊 Changes **5 files changed** (+415 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `docs/CONFIGURATION.md` (+2 -0) 📝 `docs/FAQ.md` (+1 -1) ➕ `docs/REMOTE_ACCESS.md` (+156 -0) 📝 `packages/server/src/middleware/basic-auth.ts` (+198 -10) 📝 `packages/server/test/security-middleware.test.ts` (+58 -0) </details> ### 📄 Description ## Why A lot of Docker / Tailscale / phone-on-LAN users are getting confused by the no-auth lockdown that ships with the safe-by-default Basic Auth behavior. When they open the server in a browser without setting `BASIC_AUTH_USER`/`BASIC_AUTH_PASS`/`IP_ALLOWLIST`, they get a raw JSON 403 that just says "Forbidden" — no instructions, no link, no obvious next step. Discord has been seeing this report repeatedly. This PR keeps the security posture identical (still 403, still fails closed) but replaces the JSON body with a styled, self-contained HTML page **only when the request looks like a browser navigation** (Accept: text/html, non-/api/ path). Programmatic clients (curl, fetch, /api/*) still get the original JSON, so nothing scripted breaks. ## What the page shows - The user's detected IP (so they don't have to figure it out for the IP_ALLOWLIST path) - **Option 1 — Basic Auth** with the exact two `.env` lines to add - **Option 2 — IP allowlist** with `IP_ALLOWLIST=<their-ip>` pre-filled - A "trusted network escape hatch" footnote pointing at `ALLOW_UNAUTHENTICATED_PRIVATE_NETWORK=true` - A "Detailed walkthrough →" link inside each option panel that deep-links to the matching anchor in `docs/REMOTE_ACCESS.md`, plus link cards at the bottom for the full walkthrough, FAQ, and Troubleshooting ## Bundled with chai's docs PR (#447) To avoid the day-one broken-link risk chai called out in #447, this PR also picks up chai's `docs/REMOTE_ACCESS.md` walkthrough plus the `CONFIGURATION.md` / `FAQ.md` cross-reference lines. Either #446 or #447 can land first; the other becomes a no-op merge. **Once both have landed, #447 can be closed without merging** since its content is included here. ## Implementation notes - `sendLockdown(request, reply)` now branches on `wantsHtml(request)`. - `wantsHtml` returns false for `/api/*` and only true if `Accept` includes `text/html`. - The IP and other dynamic content go through an `escapeHtml()` helper so we can't be tricked into XSS via a header-spoofed remote address. - HTML is fully inline (CSS in `<style>`) so it works without any client build artifacts and won't 404 on a misconfigured static server. - The existing JSON message is preserved as `LOCKDOWN_JSON_MESSAGE`. ## Tests Added two unit tests in `packages/server/test/security-middleware.test.ts`: - A browser navigation (Accept: text/html) hitting a non-API path gets a 403 with `Content-Type: text/html`, the doctype, the `BASIC_AUTH_USER` token, and `IP_ALLOWLIST=<their-ip>`. - A non-browser (Accept: application/json) request to `/api/headers` still gets the original JSON 403. The pre-existing "non-loopback fails closed" test still passes unchanged. All 18 security-middleware tests pass. ## Manual verification (not yet done — please confirm before merging) - [ ] Start Marinara Engine without setting `BASIC_AUTH_USER`/`BASIC_AUTH_PASS`/`IP_ALLOWLIST` - [ ] From another device on the LAN (or via Tailscale), open `http://<server-ip>:7860/` in a browser - [ ] Confirm the styled HTML page renders, the IP pill matches the device's address, and both options' code blocks are readable - [ ] On mobile (narrow viewport), confirm nothing is cut off and the `<pre>` blocks scroll horizontally instead of overflowing - [ ] Click "Detailed walkthrough →" on each option panel and confirm it opens the matching anchor in REMOTE_ACCESS.md on GitHub - [ ] From the same blocked device, run `curl -i http://<server-ip>:7860/api/headers` and confirm the JSON 403 still comes back with `Content-Type: application/json` - [ ] Set `BASIC_AUTH_USER`/`BASIC_AUTH_PASS`, restart, and confirm the lockdown page is gone (browser gets the normal 401 challenge instead) - [ ] Set `ALLOW_UNAUTHENTICATED_PRIVATE_NETWORK=true`, restart, and confirm the LAN browser loads the app normally <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Lockdown now serves a friendly HTML page to browser-like clients while API clients continue to receive JSON, adapting responses by client characteristics. * **Tests** * Added a navigation-style test verifying unauthenticated browser requests return the HTML lockdown page with environment placeholders. * **Documentation** * Added Remote Access guide and updated Configuration and FAQ with access-control setup and troubleshooting. <!-- 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-22 04:09:14 +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#4621
No description provided.