0.2.26: security hardening from audit (P2/P3 fixes)

Address the security-auditor findings the user selected (items 2, 3, 4):

Default admin credentials (P2): remove the fixed `Ten31` default. First
boot now generates a strong random admin password (secrets.token_urlsafe),
records it 0600 at /data/.admin-password, and surfaces it once via a new
"Show Initial Admin Password" StartOS action (CLI `show-admin-password`).
The stored password is cleared when the admin is reset (CLI reset-password)
or self-changes it (change-password endpoint).

Login hardening (P2): add a per-IP in-memory sliding-window rate limiter
(10 failures / 5 min -> 429 + Retry-After) in ratelimit.py; run a dummy
argon2 verify when the user is unknown so timing can't enumerate usernames;
keep a single generic 401 for unknown-user and wrong-password.

Hardening (P3): server process now runs unprivileged -- Dockerfile adds
uid 10001 appuser; start.sh (still root) chowns the mounted /data then
drops via `setpriv` before exec'ing uvicorn. Spreadsheet imports are
size-capped via storage.read_capped (413 past MAX_UPLOAD_SIZE) in the
schedule, capital preview, and batch paths. batch_import no longer returns
raw exception text (generic per-file messages).

Verified in the packed amd64 container: PID1 uvicorn runs as uid 10001,
/data owned 10001 with 0600 secrets; generated admin password retrievable
via CLI and logs in (200); 11th bad login -> 429; admin reset clears the
stored password. Tests: test_auth_hardening.py (4). Full suite 21 passed;
frontend tsc + StartOS bundle clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jonathan Kirkwood
2026-07-01 19:09:27 -05:00
co-authored by Claude Opus 4.8
parent 025aff4fac
commit 69f12b0519
17 changed files with 324 additions and 23 deletions
+47
View File
@@ -0,0 +1,47 @@
"""A tiny in-process sliding-window rate limiter for the login endpoint.
The portal runs as a single uvicorn process, so an in-memory counter is enough to blunt
online password guessing without adding a dependency or a shared store. It is keyed by client
IP; only failed attempts are counted, and a successful login clears the key. This deliberately
does NOT lock accounts (which would let anyone lock out a user by name) — it throttles the
source of the guessing instead.
"""
import time
from collections import defaultdict, deque
from threading import Lock
class SlidingWindowLimiter:
def __init__(self, max_attempts: int, window_seconds: float):
self.max_attempts = max_attempts
self.window = window_seconds
self._hits: dict[str, deque] = defaultdict(deque)
self._lock = Lock()
def _prune(self, key: str, now: float) -> deque:
dq = self._hits[key]
cutoff = now - self.window
while dq and dq[0] <= cutoff:
dq.popleft()
if not dq:
self._hits.pop(key, None)
return dq
def retry_after(self, key: str) -> float:
"""Seconds until `key` may try again, or 0.0 if it is under the limit right now."""
now = time.monotonic()
with self._lock:
dq = self._prune(key, now)
if len(dq) < self.max_attempts:
return 0.0
return self.window - (now - dq[0])
def record_failure(self, key: str) -> None:
now = time.monotonic()
with self._lock:
self._hits[key].append(now)
def reset(self, key: str) -> None:
with self._lock:
self._hits.pop(key, None)