[PR #8016] [MERGED] feat(server): stored login, setup code and /login page (#7912) #8058

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

📋 Pull Request Information

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

Base: feat/7912-remote-login ← Head: feat/remote-login-7912


📝 Commits (8)

  • 15c1128 feat(server): stored login, setup code and /login page (#7912)
  • 366be8a style: format remote login changes
  • 3196b94 feat(server): server-side remote login sessions and review fixes (#7912)
  • 201f0b0 fix(server): show an error when /login cannot start a session
  • e06c415 fix(server): count wrong passwords per browser behind a local proxy
  • 6d12c1f test(server): wrong passwords on health checks count toward the lockout
  • 4901db8 fix(server): remote login gate-review fixes (#7912)
  • e3d4d0d fix(server): retry an unreadable login file at the normal interval

📊 Changes

15 files changed (+1434 additions, -199 deletions)

View changed files

📝 .env.example (+4 -1)
📝 package.json (+1 -0)
📝 packages/client/src/components/diagnostics/use-access-warning-banner.tsx (+2 -0)
➕ packages/server/src/config/login-credential.ts (+341 -0)
📝 packages/server/src/index.ts (+5 -2)
📝 packages/server/src/middleware/basic-auth.ts (+446 -153)
📝 packages/server/src/middleware/csrf-protection.ts (+6 -0)
📝 packages/server/src/middleware/ip-allowlist.ts (+2 -3)
📝 packages/server/src/middleware/rate-limit.ts (+10 -0)
📝 packages/server/src/routes/access-status.routes.ts (+80 -1)
📝 packages/server/src/routes/index.ts (+2 -0)
📝 scripts/regressions/access-status.regression.ts (+1 -0)
📝 scripts/regressions/basic-auth-throttle-security.regression.ts (+72 -39)
➕ scripts/regressions/remote-login.regression.ts (+433 -0)
➕ scripts/reset-login.mjs (+29 -0)

📄 Description

Part of #7912 (parent #7909); PR 1 of 4 into the #7912 integration branch feat/7912-remote-login. All four PRs merge there; one PR takes the result to staging at the end.

Why

Owners must be able to protect remote access without editing .env. Also, the browser's Basic Auth dialog does not work in iPhone apps pinned to the home screen. Setting a login must be the easy path. Local use on this computer (loopback) stays passwordless.

What changed (server only)

  • One login resolver. The new config/login-credential.ts has getLoginConfig(). The Basic Auth gate, the access policy and /api/access/status all use it, so they always agree. When both BASIC_AUTH_USER and BASIC_AUTH_PASS are set, they win. Otherwise the login saved in the app applies: DATA_DIR/.remote-login.json, mode 0600, written atomically. It holds the username and a scrypt hash (node:crypto, no new dependency). The server checks the file at most every 2 s, so deleting it removes the login at once.
  • Set, change and remove the app login. PUT /api/access/login and DELETE /api/access/login use the same privileged gate as /api/access/status. CSRF protection and a route rate limit apply. Minimum password length is 10. While .env sets the login, writes return 409.
  • First login from Docker. The browser on the Docker host connects through the Docker gateway, not loopback. When no login exists and the server listens beyond loopback, the server log (docker logs) shows a one-time setup code. This code replaces ADMIN_SECRET only to set the first login. It works once and is cleared when any login exists. When the login is removed later (in the app, with pnpm reset-login or by deleting the file), the server logs a new code within a few seconds, without a restart. Older codes stop working.
  • Login page instead of the browser dialog. GET /login serves a small self-contained form (lockdown page style, no external assets). POST /login checks the login and counts wrong passwords toward the existing lockout. A browser page load without a session redirects to /login?next=…; only same-origin paths are accepted for next. API calls and scripts still get the 401 Basic challenge, and Basic Authorization still works. The form post is accepted only when the browser marks it as same-origin; other posts get the usual Origin/Referer check.
  • Server-side sessions. A successful sign-in creates a random session token in the new marinara_session cookie (HttpOnly, SameSite=Lax, 365 days, Secure on HTTPS). The server stores only a hash of the token with an id, created/last-seen times and a short device label from the User-Agent (no IP) in DATA_DIR/.remote-sessions.json (mode 0600, atomic). At most 50 sessions are kept; the oldest goes first. A missing or damaged file means no sessions. A valid session is not affected by the lockout from other visitors' wrong passwords. This works the same for .env and app logins.
    • POST /logout ends its own session on the server.
    • Setting, changing or removing the login ends every session; the browser that made the change gets a new one.
    • GET /api/access/sessions lists sessions (id, label, created, last seen, current). DELETE /api/access/sessions/:id ends one and DELETE /api/access/sessions ends all. Same privileged gate and CSRF as the login routes. No token or hash leaves the server.
    • The old marinara_signed_in cookie is no longer accepted. Browsers that used it sign in once more.
  • Wrong-password count per browser behind a proxy. A reverse proxy or tunnel on this computer makes every visitor look like loopback, so one shared count let anyone lock the owner's new device out. Now GET /login gives each browser a random pre-auth cookie, and wrong passwords count per browser. Visitors without that cookie share one count as before. All visitors through the same address also share a larger cap, so new cookies cannot buy unlimited tries. A signed-in browser is still never locked out.
  • Fewer slow checks. After a Basic header passes the scrypt check, the server keeps an HMAC of it in memory. At most 4 scrypt checks run at the same time. More checks get 429. A password that a client already got wrong is refused at once, without a new slow check. Parallel tries from one client cannot pass the wrong-password limit.
  • /api/access/status gets loginSource: "env" | "app" | null. The client type gets the field; the Settings UI comes in PR 3.
  • pnpm reset-login deletes the app login and all sessions. It reads DATA_DIR from the same .env as the server (MARINARA_ENV_FILE first). With Docker, use docker exec <container> rm -f /app/data/.remote-login.json /app/data/.remote-sessions.json. .env.example has a short note.

Docs for the app login, /login, setup code and reset-login follow in PR 4 of #7912.

Tests

  • New scripts/regressions/remote-login.regression.ts: session create/validate/expire/cap, file modes, logout and login changes end sessions on the server, the sessions API (no secrets in the JSON), next handling, the same-origin check for the form, a new setup code after a reset, and reset-login with MARINARA_ENV_FILE.
  • basic-auth-throttle-security now signs in through /login and checks that an old-format cookie is rejected. This follows the maintainer decision on #7912 (server-side sessions replace the old cookie).
  • basic-auth-throttle-security also checks that behind a proxy, one browser's wrong passwords lock only that browser, another browser can still sign in, and the shared cap holds.
  • remote-login also checks that a repeated wrong password does not wait for a slow check, that parallel tries stay within the limit, that a login file which cannot be read for a moment keeps the last login, and that only POST /logout skips the login.
  • access-status checks the new field.

Manual verification

  • Docker with no login: copy the setup code from docker logs. Set a login from the host browser (PUT /api/access/login with setupCode). Make sure that the same browser stays signed in.
  • Docker: make sure that the same setup code is refused a second time.
  • Docker: remove the login and make sure that docker logs shows a new setup code without a restart.
  • iPhone home-screen app on a remote install: sign in on /login. Close and open the pinned app again. Make sure that it is still signed in.
  • Desktop browser with an .env login: a page load shows /login, not the browser dialog.
  • Sign in on two devices. Make sure that GET /api/access/sessions lists both with readable labels, and that ending one signs only that device out.
  • Sign out with POST /logout and make sure that the old cookie value no longer works.
  • Change the password and make sure that other signed-in devices must sign in again.
  • curl -u user:pass against an API route works with an app login and with an .env login.
  • On a running native install, pnpm reset-login removes the login and all sessions within a few seconds. Local use still works.
  • Behind a reverse proxy or tunnel: enter wrong passwords on /login in one browser until it is refused. Make sure that a second device can still sign in.
  • Behind an HTTPS reverse proxy on a domain name: sign in on /login. Make sure that the cookie is Secure.

🔄 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/8016 **Author:** [@Gunterlie](https://github.com/Gunterlie) **Created:** 10/21/2026 **Status:** ✅ Merged **Merged:** 10/22/2026 **Merged by:** [@Gunterlie](https://github.com/Gunterlie) **Base:** `feat/7912-remote-login` ← **Head:** `feat/remote-login-7912` --- ### 📝 Commits (8) - [`15c1128`](https://github.com/Pasta-Devs/Marinara-Engine/commit/15c1128879e75e43cebb03bf8b33e2a7382fe0f3) feat(server): stored login, setup code and /login page (#7912) - [`366be8a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/366be8a4113d45d3a69228ef68197e06a454251b) style: format remote login changes - [`3196b94`](https://github.com/Pasta-Devs/Marinara-Engine/commit/3196b94887ccff5262506156015e5bd3c312e02d) feat(server): server-side remote login sessions and review fixes (#7912) - [`201f0b0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/201f0b0749e2fa6f5bb6232ac9d1b72eca552eb1) fix(server): show an error when /login cannot start a session - [`e06c415`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e06c415259e09a640fd0ebe3dbdb5c2306d69f91) fix(server): count wrong passwords per browser behind a local proxy - [`6d12c1f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/6d12c1f5545f473e5deaf13f4b6acab4cdc3a5d4) test(server): wrong passwords on health checks count toward the lockout - [`4901db8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4901db863ac323299c82611a8b32308547fa5153) fix(server): remote login gate-review fixes (#7912) - [`e3d4d0d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e3d4d0d4551ad99748f1711acbddd6b4d345f082) fix(server): retry an unreadable login file at the normal interval ### 📊 Changes **15 files changed** (+1434 additions, -199 deletions) <details> <summary>View changed files</summary> 📝 `.env.example` (+4 -1) 📝 `package.json` (+1 -0) 📝 `packages/client/src/components/diagnostics/use-access-warning-banner.tsx` (+2 -0) ➕ `packages/server/src/config/login-credential.ts` (+341 -0) 📝 `packages/server/src/index.ts` (+5 -2) 📝 `packages/server/src/middleware/basic-auth.ts` (+446 -153) 📝 `packages/server/src/middleware/csrf-protection.ts` (+6 -0) 📝 `packages/server/src/middleware/ip-allowlist.ts` (+2 -3) 📝 `packages/server/src/middleware/rate-limit.ts` (+10 -0) 📝 `packages/server/src/routes/access-status.routes.ts` (+80 -1) 📝 `packages/server/src/routes/index.ts` (+2 -0) 📝 `scripts/regressions/access-status.regression.ts` (+1 -0) 📝 `scripts/regressions/basic-auth-throttle-security.regression.ts` (+72 -39) ➕ `scripts/regressions/remote-login.regression.ts` (+433 -0) ➕ `scripts/reset-login.mjs` (+29 -0) </details> ### 📄 Description Part of #7912 (parent #7909); PR 1 of 4 into the #7912 integration branch `feat/7912-remote-login`. All four PRs merge there; one PR takes the result to `staging` at the end. ## Why Owners must be able to protect remote access without editing `.env`. Also, the browser's Basic Auth dialog does not work in iPhone apps pinned to the home screen. Setting a login must be the easy path. Local use on this computer (loopback) stays passwordless. ## What changed (server only) - **One login resolver.** The new `config/login-credential.ts` has `getLoginConfig()`. The Basic Auth gate, the access policy and `/api/access/status` all use it, so they always agree. When both `BASIC_AUTH_USER` and `BASIC_AUTH_PASS` are set, they win. Otherwise the login saved in the app applies: `DATA_DIR/.remote-login.json`, mode `0600`, written atomically. It holds the username and a scrypt hash (`node:crypto`, no new dependency). The server checks the file at most every 2 s, so deleting it removes the login at once. - **Set, change and remove the app login.** `PUT /api/access/login` and `DELETE /api/access/login` use the same privileged gate as `/api/access/status`. CSRF protection and a route rate limit apply. Minimum password length is 10. While `.env` sets the login, writes return `409`. - **First login from Docker.** The browser on the Docker host connects through the Docker gateway, not loopback. When no login exists and the server listens beyond loopback, the server log (`docker logs`) shows a one-time setup code. This code replaces `ADMIN_SECRET` only to set the first login. It works once and is cleared when any login exists. When the login is removed later (in the app, with `pnpm reset-login` or by deleting the file), the server logs a new code within a few seconds, without a restart. Older codes stop working. - **Login page instead of the browser dialog.** `GET /login` serves a small self-contained form (lockdown page style, no external assets). `POST /login` checks the login and counts wrong passwords toward the existing lockout. A browser page load without a session redirects to `/login?next=…`; only same-origin paths are accepted for `next`. API calls and scripts still get the `401` Basic challenge, and Basic Authorization still works. The form post is accepted only when the browser marks it as same-origin; other posts get the usual Origin/Referer check. - **Server-side sessions.** A successful sign-in creates a random session token in the new `marinara_session` cookie (HttpOnly, SameSite=Lax, 365 days, Secure on HTTPS). The server stores only a hash of the token with an id, created/last-seen times and a short device label from the User-Agent (no IP) in `DATA_DIR/.remote-sessions.json` (mode `0600`, atomic). At most 50 sessions are kept; the oldest goes first. A missing or damaged file means no sessions. A valid session is not affected by the lockout from other visitors' wrong passwords. This works the same for `.env` and app logins. - `POST /logout` ends its own session on the server. - Setting, changing or removing the login ends every session; the browser that made the change gets a new one. - `GET /api/access/sessions` lists sessions (id, label, created, last seen, current). `DELETE /api/access/sessions/:id` ends one and `DELETE /api/access/sessions` ends all. Same privileged gate and CSRF as the login routes. No token or hash leaves the server. - The old `marinara_signed_in` cookie is no longer accepted. Browsers that used it sign in once more. - **Wrong-password count per browser behind a proxy.** A reverse proxy or tunnel on this computer makes every visitor look like loopback, so one shared count let anyone lock the owner's new device out. Now `GET /login` gives each browser a random pre-auth cookie, and wrong passwords count per browser. Visitors without that cookie share one count as before. All visitors through the same address also share a larger cap, so new cookies cannot buy unlimited tries. A signed-in browser is still never locked out. - **Fewer slow checks.** After a Basic header passes the scrypt check, the server keeps an HMAC of it in memory. At most 4 scrypt checks run at the same time. More checks get `429`. A password that a client already got wrong is refused at once, without a new slow check. Parallel tries from one client cannot pass the wrong-password limit. - `/api/access/status` gets `loginSource: "env" | "app" | null`. The client type gets the field; the Settings UI comes in PR 3. - `pnpm reset-login` deletes the app login and all sessions. It reads `DATA_DIR` from the same `.env` as the server (`MARINARA_ENV_FILE` first). With Docker, use `docker exec <container> rm -f /app/data/.remote-login.json /app/data/.remote-sessions.json`. `.env.example` has a short note. Docs for the app login, /login, setup code and reset-login follow in PR 4 of #7912. ## Tests - New `scripts/regressions/remote-login.regression.ts`: session create/validate/expire/cap, file modes, logout and login changes end sessions on the server, the sessions API (no secrets in the JSON), `next` handling, the same-origin check for the form, a new setup code after a reset, and `reset-login` with `MARINARA_ENV_FILE`. - `basic-auth-throttle-security` now signs in through `/login` and checks that an old-format cookie is rejected. This follows the maintainer decision on #7912 (server-side sessions replace the old cookie). - `basic-auth-throttle-security` also checks that behind a proxy, one browser's wrong passwords lock only that browser, another browser can still sign in, and the shared cap holds. - `remote-login` also checks that a repeated wrong password does not wait for a slow check, that parallel tries stay within the limit, that a login file which cannot be read for a moment keeps the last login, and that only `POST /logout` skips the login. - `access-status` checks the new field. ## Manual verification - [ ] Docker with no login: copy the setup code from `docker logs`. Set a login from the host browser (`PUT /api/access/login` with `setupCode`). Make sure that the same browser stays signed in. - [ ] Docker: make sure that the same setup code is refused a second time. - [ ] Docker: remove the login and make sure that `docker logs` shows a new setup code without a restart. - [ ] iPhone home-screen app on a remote install: sign in on `/login`. Close and open the pinned app again. Make sure that it is still signed in. - [ ] Desktop browser with an `.env` login: a page load shows `/login`, not the browser dialog. - [ ] Sign in on two devices. Make sure that `GET /api/access/sessions` lists both with readable labels, and that ending one signs only that device out. - [ ] Sign out with `POST /logout` and make sure that the old cookie value no longer works. - [ ] Change the password and make sure that other signed-in devices must sign in again. - [ ] `curl -u user:pass` against an API route works with an app login and with an `.env` login. - [ ] On a running native install, `pnpm reset-login` removes the login and all sessions within a few seconds. Local use still works. - [ ] Behind a reverse proxy or tunnel: enter wrong passwords on `/login` in one browser until it is refused. Make sure that a second device can still sign in. - [ ] Behind an HTTPS reverse proxy on a domain name: sign in on `/login`. Make sure that the cookie is `Secure`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:10:01 +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#8058
No description provided.