[PR #343] [MERGED] feat(server): HTTP Basic Auth + safe-by-default remote lockdown #4568

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

📋 Pull Request Information

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

Base: main ← Head: feat/basic-auth-and-ip-allowlist-toggle


📝 Commits (1)

  • 52da81d feat(server): HTTP Basic Auth + safe-by-default remote lockdown

📊 Changes

6 files changed (+267 additions, -0 deletions)

View changed files

📝 .env.example (+17 -0)
📝 docs/CONFIGURATION.md (+52 -0)
📝 packages/server/src/app.ts (+4 -0)
📝 packages/server/src/config/runtime-config.ts (+21 -0)
➕ packages/server/src/middleware/basic-auth.ts (+145 -0)
📝 packages/server/src/middleware/ip-allowlist.ts (+28 -0)

📄 Description

Summary

Adds optional HTTP Basic Auth and an explicit on/off toggle for the existing IP allowlist, plus a safe-by-default lockdown for public-internet IPs only so an accidentally exposed port 7860 cannot be hit by random internet traffic — without breaking LAN, Docker, Kubernetes, or Tailscale traffic.

Motivated by a Discord request from a user hosting Marinara on a Hetzner Cloud server who wanted simple username/password protection for their public deployment, and by the broader concern that any user who flips HOST=0.0.0.0 (every shell launcher does) and forwards a port becomes immediately reachable from the open internet with no auth at all.

Why this strengthens an exposed port 7860

Today, HOST=0.0.0.0 plus an exposed port = anyone on the internet can use the entire app, browse chats, exfiltrate data, and burn API credits. With this change:

  • Public-only lockdown. With no BASIC_AUTH_* configured, the server returns 403 Forbidden to any IP that is not loopback, RFC 1918 private, CGNAT (Tailscale), link-local, or in IP_ALLOWLIST. The error response tells the operator exactly how to unlock it. Local use, LAN access from your phone, container-to-container traffic on Docker bridges, Kubernetes pods, and Tailscale peers all keep working with zero configuration.
  • Cleared path for legitimate remote access. Operators who want to reach the server from a public IP (or from a non-private network) have three documented options:
    1. Set BASIC_AUTH_USER + BASIC_AUTH_PASS — the browser shows a native password prompt; credentials are checked with timingSafeEqual. Pair with HTTPS via the existing SSL_CERT / SSL_KEY.
    2. Add the public IP / network to IP_ALLOWLIST.
    3. Set ALLOW_UNAUTHENTICATED_REMOTE=true to opt back into the legacy open-access behaviour for public IPs as well.
  • Layered, not exclusive. Basic Auth and IP_ALLOWLIST compose cleanly: the allowlist runs first (so blocked IPs never even reach the auth path), and any IP it vouches for is exempt from the password prompt — so e.g. a remote office at a fixed public IP can be allowlisted and skip the password.
  • Principle of least surprise. The private-network auto-exemption applies only to the no-auth lockdown. Once you set BASIC_AUTH_*, the password is required from every IP except loopback and explicit IP_ALLOWLIST matches — including from your LAN. If you set a password, you mean it.
  • Operational endpoints still work. /api/health is exempt so external uptime monitors and load balancers can probe the server without credentials.

Behaviour matrix

POST UPDATED MATRIX SCREENSHOT HERE

New configuration

All env vars are documented in docs/CONFIGURATION.md and .env.example:

Variable Default Purpose
BASIC_AUTH_USER (empty) Username for HTTP Basic Auth.
BASIC_AUTH_PASS (empty) Password for HTTP Basic Auth.
BASIC_AUTH_REALM Marinara Engine Realm string shown in the browser prompt.
IP_ALLOWLIST_ENABLED true Master switch — keep the list configured but disable enforcement.
ALLOW_UNAUTHENTICATED_REMOTE false Opt-in escape hatch to allow unauthenticated public access.

There are no default credentials baked in anywhere — BASIC_AUTH_USER and BASIC_AUTH_PASS start unset, and the lockdown rejects all public-internet requests rather than accepting anything until the operator sets real values.

Files

  • packages/server/src/middleware/basic-auth.ts — new onRequest hook, timing-safe credential comparison, public-only lockdown logic.
  • packages/server/src/middleware/ip-allowlist.ts — extracted isLoopbackIp, isInIpAllowlist, and isPrivateNetworkIp helpers (no behaviour change to the existing hook).
  • packages/server/src/config/runtime-config.ts — getBasicAuthConfig, isUnauthenticatedRemoteAllowed, and IP_ALLOWLIST_ENABLED toggle.
  • packages/server/src/app.ts — registers the new hook after the IP allowlist.
  • .env.example, docs/CONFIGURATION.md — documents the new vars and the new "Access Control" section.

🔄 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/343 **Author:** [@LukaTheHero](https://github.com/LukaTheHero) **Created:** 5/1/2026 **Status:** ✅ Merged **Merged:** 5/1/2026 **Merged by:** [@LukaTheHero](https://github.com/LukaTheHero) **Base:** `main` ← **Head:** `feat/basic-auth-and-ip-allowlist-toggle` --- ### 📝 Commits (1) - [`52da81d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/52da81d9613ae10712b5a277d77375f2c7d585aa) feat(server): HTTP Basic Auth + safe-by-default remote lockdown ### 📊 Changes **6 files changed** (+267 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `.env.example` (+17 -0) 📝 `docs/CONFIGURATION.md` (+52 -0) 📝 `packages/server/src/app.ts` (+4 -0) 📝 `packages/server/src/config/runtime-config.ts` (+21 -0) ➕ `packages/server/src/middleware/basic-auth.ts` (+145 -0) 📝 `packages/server/src/middleware/ip-allowlist.ts` (+28 -0) </details> ### 📄 Description ## Summary Adds optional HTTP Basic Auth and an explicit on/off toggle for the existing IP allowlist, plus a safe-by-default lockdown for **public-internet IPs only** so an accidentally exposed port 7860 cannot be hit by random internet traffic — without breaking LAN, Docker, Kubernetes, or Tailscale traffic. Motivated by [a Discord request](https://discord.com/) from a user hosting Marinara on a Hetzner Cloud server who wanted simple username/password protection for their public deployment, and by the broader concern that any user who flips `HOST=0.0.0.0` (every shell launcher does) and forwards a port becomes immediately reachable from the open internet with no auth at all. ## Why this strengthens an exposed port 7860 Today, `HOST=0.0.0.0` plus an exposed port = anyone on the internet can use the entire app, browse chats, exfiltrate data, and burn API credits. With this change: - **Public-only lockdown.** With no `BASIC_AUTH_*` configured, the server returns `403 Forbidden` to any IP that is not loopback, RFC 1918 private, CGNAT (Tailscale), link-local, or in `IP_ALLOWLIST`. The error response tells the operator exactly how to unlock it. Local use, LAN access from your phone, container-to-container traffic on Docker bridges, Kubernetes pods, and Tailscale peers all keep working with zero configuration. - **Cleared path for legitimate remote access.** Operators who want to reach the server from a *public* IP (or from a non-private network) have three documented options: 1. Set `BASIC_AUTH_USER` + `BASIC_AUTH_PASS` — the browser shows a native password prompt; credentials are checked with `timingSafeEqual`. Pair with HTTPS via the existing `SSL_CERT` / `SSL_KEY`. 2. Add the public IP / network to `IP_ALLOWLIST`. 3. Set `ALLOW_UNAUTHENTICATED_REMOTE=true` to opt back into the legacy open-access behaviour for public IPs as well. - **Layered, not exclusive.** Basic Auth and `IP_ALLOWLIST` compose cleanly: the allowlist runs first (so blocked IPs never even reach the auth path), and any IP it vouches for is exempt from the password prompt — so e.g. a remote office at a fixed public IP can be allowlisted and skip the password. - **Principle of least surprise.** The private-network auto-exemption applies *only* to the no-auth lockdown. Once you set `BASIC_AUTH_*`, the password is required from every IP except loopback and explicit `IP_ALLOWLIST` matches — including from your LAN. If you set a password, you mean it. - **Operational endpoints still work.** `/api/health` is exempt so external uptime monitors and load balancers can probe the server without credentials. ## Behaviour matrix POST UPDATED MATRIX SCREENSHOT HERE ## New configuration All env vars are documented in `docs/CONFIGURATION.md` and `.env.example`: | Variable | Default | Purpose | | --- | --- | --- | | `BASIC_AUTH_USER` | _(empty)_ | Username for HTTP Basic Auth. | | `BASIC_AUTH_PASS` | _(empty)_ | Password for HTTP Basic Auth. | | `BASIC_AUTH_REALM` | `Marinara Engine` | Realm string shown in the browser prompt. | | `IP_ALLOWLIST_ENABLED` | `true` | Master switch — keep the list configured but disable enforcement. | | `ALLOW_UNAUTHENTICATED_REMOTE` | `false` | Opt-in escape hatch to allow unauthenticated **public** access. | There are **no default credentials** baked in anywhere — `BASIC_AUTH_USER` and `BASIC_AUTH_PASS` start unset, and the lockdown rejects all public-internet requests rather than accepting anything until the operator sets real values. ## Files - `packages/server/src/middleware/basic-auth.ts` — new `onRequest` hook, timing-safe credential comparison, public-only lockdown logic. - `packages/server/src/middleware/ip-allowlist.ts` — extracted `isLoopbackIp`, `isInIpAllowlist`, and `isPrivateNetworkIp` helpers (no behaviour change to the existing hook). - `packages/server/src/config/runtime-config.ts` — `getBasicAuthConfig`, `isUnauthenticatedRemoteAllowed`, and `IP_ALLOWLIST_ENABLED` toggle. - `packages/server/src/app.ts` — registers the new hook after the IP allowlist. - `.env.example`, `docs/CONFIGURATION.md` — documents the new vars and the new "Access Control" section. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:08:48 +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#4568
No description provided.