Close the read-side privilege escalation and fix proxy-aware IPs (0.44.0)
CI / build-and-push (push) Successful in 3m53s

F1 — Any authenticated user could read any file the backend could see.
/api/files/read and /download hung on get_current_user, and the sandbox that
should have caught that was open by default: ALLOWED_BROWSE_ROOTS contained
"/", for which _is_allowed() waves through every path. So the `user` role could
download stackpilot.db (password hashes, agent tokens, backup credentials),
every stack's .env and every .secrets/* file — with no audit trail, because
only mutations were logged.

Implementing that turned up three more doors into the same room, all fixed
here since closing only the first would have made the fix cosmetic:
GET /api/stacks/{id} handed the .env to any user, /export tarred the whole
stack dir including .secrets/*, and both the agent file proxies and
/api/agents/{id}/stacks/{id} repeated the leak for every remote host. All 24
filesystem-touching routes are now admin-only; reads and downloads are audited
(listing is not — the Files page polls it). DATA_DIR is refused outright, since
the API deliberately masks agent tokens and destination secrets and the browser
would otherwise be the way around that. "/" is out of the default browse roots.

F2 — Backup destination credentials were plaintext JSON in the DB, which is
what made F1 worth exploiting. They are now Fernet-encrypted at rest behind
parse_config/dump_config, with existing rows migrated at startup.

This needed a prerequisite from F6: the key is derived from SECRET_KEY, which
was regenerated on every boot when unset. Encrypting against a key that changes
per restart would be worse than plaintext, so an auto-generated SECRET_KEY is
now persisted to ${DATA_DIR}/secret_key at mode 0600. Sessions surviving a
restart is a welcome side effect.

F3 — /api/audit is admin-only. Also hidden from the dashboard and the nav for
non-admins, so nobody polls into a 403.

F4 — uvicorn now runs with --proxy-headers, so nginx's X-Forwarded-For is
honoured. Without it request.client.host was the frontend container's IP for
every request, which made the login rate limit global instead of per-IP (10
failures locked out everyone) and filled the audit log's IP column with one
useless value.

Verified: encrypt/decrypt round-trip incl. plaintext passthrough, idempotent
re-encryption and wrong-key handling; sandbox denial for DATA_DIR, traversal
into it, and paths outside the roots, with the allowed roots still reachable.
Both against stubbed settings — there is no Docker here, so nothing was run
end to end.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dk43rmEeRfYi5wsLDbmfyG
This commit is contained in:
menzelj
2026-08-31 13:01:53 +02:00
co-authored by Claude Opus 5
parent b3af0c2109
commit 54c835b032
21 changed files with 352 additions and 59 deletions
+15 -5
View File
@@ -23,15 +23,24 @@ import { useThemeStore } from "@/store/theme";
import { agentsApi } from "@/api/agents";
import { VersionBadge } from "./VersionBadge";
export const NAV_ITEMS = [
type NavItem = {
to: string;
label: string;
icon: typeof LayoutDashboard;
end?: boolean;
/** Hidden for non-admins — the matching API routes require the admin role. */
adminOnly?: boolean;
};
export const NAV_ITEMS: NavItem[] = [
{ to: "/", label: "Dashboard", icon: LayoutDashboard, end: true },
{ to: "/stacks", label: "Stacks", icon: Boxes },
{ to: "/networks", label: "Networks", icon: Network },
{ to: "/images", label: "Images", icon: Image },
{ to: "/volumes", label: "Volumes", icon: Database },
{ to: "/files", label: "Files", icon: FolderTree },
{ to: "/files", label: "Files", icon: FolderTree, adminOnly: true },
{ to: "/templates", label: "Templates", icon: LayoutTemplate },
{ to: "/audit", label: "Audit", icon: ScrollText },
{ to: "/audit", label: "Audit", icon: ScrollText, adminOnly: true },
{ to: "/settings", label: "Settings", icon: Settings },
];
@@ -76,6 +85,7 @@ export function TopNav() {
const menuRef = useRef<HTMLDivElement>(null);
const navigate = useNavigate();
const user = useAuthStore((s) => s.user);
const navItems = NAV_ITEMS.filter((i) => !i.adminOnly || user?.role === "admin");
const logout = useAuthStore((s) => s.logout);
const { theme, toggle } = useThemeStore();
@@ -125,7 +135,7 @@ export function TopNav() {
aria-label="Primary"
className="mx-auto hidden items-center gap-0.5 rounded-pill border border-sp-border bg-sp-surface p-1 lg:flex"
>
{NAV_ITEMS.map(({ to, label, end }) => (
{navItems.map(({ to, label, end }) => (
<NavLink key={to} to={to} end={end}>
{({ isActive }) => (
<span
@@ -229,7 +239,7 @@ export function TopNav() {
</button>
</div>
<nav className="flex-1 space-y-1 overflow-y-auto px-3" aria-label="Primary">
{NAV_ITEMS.map(({ to, label, icon: Icon, end }) => (
{navItems.map(({ to, label, icon: Icon, end }) => (
<NavLink
key={to}
to={to}