Repository navigation
Security review fixes: S-H1, S-M1–S-M5 and lows (restore role, site sandbox, limiter, log caps, security headers) - #43
Conversation
… Postgres superuser (S-H1) restore_replace ran pg_restore as postgres (sudo) with --role=site_<short>. A function in the site's own dump (a CHECK, a domain, a generated column) runs during COPY and can RESET ROLE, acting as the superuser of the Postgres all sites share. Any restore did it, including the automatic one after a failed update. - The superuser psql makes setu_restore_<short> for the one restore: LOGIN, no other attributes, CONNECTION LIMIT 2, VALID UNTIL +1 day, a random 24-byte hex password, member of site_<short> only. - pg_restore runs as setu without sudo, over TCP as that role (password in a 0600 passfile in the backup dir, removed after), with --no-owner --no-privileges --role=site_<short>. - After the restore, also after a failure: REASSIGN OWNED, DROP OWNED, DROP ROLE. - sudoers: pg_restore as postgres removed (assets/sudoers, check-sudoers.sh, SECURITY.md, TRUST.md); OPERATIONS.md's manual restore uses the same temporary role; AGENT.md; CHANGELOG "Security". - Tests: tests/postgres.rs (commands, and the role is dropped after a failed restore); new tests/restore_pg.rs on a real Postgres (SETU_TEST_PG_PORT, CI's service): a dump whose CHECK does RESET ROLE restores with no marker row naming a superuser, and the restore role ran the CHECK. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…and line, cleanup on every path) Review 1d508b1: - P1 1: tests/golden.rs expects the sudoers rule without pg_restore (CI was red, so restore_pg never ran). - P1 2: OPERATIONS.md's manual restore passes the password on stdin (psql \set) and in a 0600 password file (PGPASSFILE), never on a command line; the role is made as the agent makes it (attributes, CONNECTION LIMIT 1, VALID UNTIL); the cleanup runs whether or not pg_restore succeeded. - P2 3: CONNECTION LIMIT 1; the role is made if absent and then set, so a leftover role that cannot be dropped does not stop a restore; after the restore, also after a setup failure, the role is locked first (NOLOGIN PASSWORD NULL, from postgres), then dropped in the site database starting with SET default_transaction_read_only = off, falling back to DROP ROLE IF EXISTS from postgres; drop() also drops setu_restore_<short>. Tests: a failed setup still cleans up; a failed drop in the site database falls back; real Postgres: a dump that sets the restore role's password and its database read-only still restores and has the role dropped. - P2 4: CHANGELOG and OPERATIONS.md state that a dump with an object only a superuser can create no longer restores, and the manual way out. - Nits: the design spec's sudoers line and restore text; CHANGELOG says to run `setu agent install-system` to drop the old sudoers line; strip_prefix; a drop guard removes the password file; passfile is quoted in the conninfo; SECURITY.md notes log_statement=ddl logs the password statements. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…failed on ubuntu-26.04) ubuntu-latest moved to 26.04 (pg_dump 18), which writes SET transaction_timeout; the fixed postgres:16-alpine service rejected it, so both restore_pg tests failed. In production the client and server come from one package. CI now starts scripts/test-pg.sh with SETU_TEST_PG_IMAGE at pg_dump's major (test and test-arm), and the image is overridable there; the local default stays postgres:16-alpine. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…uperuser scripts pin search_path (review b6a23a6) Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
… on old Postgres minors; login limiter per /64 and per account, argon2 bounded (S-H1 follow-up, S-M5) - Every superuser script (and REVOKE_SQL, the database listing and the manual restore in OPERATIONS.md) starts with SUPERUSER_PREAMBLE: the search_path pin, RESET ROLE, statement_timeout = 0, lock_timeout = 0 (review 363518f nits 1-3). - `setu agent doctor`: a postgres-version row, WARN (not a failure) below 17.6/16.10/15.14/14.19/13.22 or on an older major (CVE-2025-8714/8715, S-H1 fix item 3). - S-M5: an IPv6 client is limited by its /64; each account takes 50 failed logins an hour from all addresses (a refusal there does not charge the address); argon2 runs at most 4 at once, more than 32 waiting is 429. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…o_many_lines) Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
… preamble Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…ectory; doctor rows set warn_only in place (S-M4) setu upgrade and sudo setu backup run as root in <data_dir>/backups/setu, which setu owns. The dump is created with openat(O_EXCL|O_NOFOLLOW) in a directory opened O_NOFOLLOW, written by pg_dump to stdout (no -f), handed to the directory's owner with fchown on the open file and renamed with renameat; a rename that moved a swapped entry is undone and fails the dump. upgrade.rs no longer chowns by path; BackupLock uses lchown. Runner::run takes the child's stdout. openapi.json regenerated for the S-M5 login doc. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…LE NONE, account limiter digest keys, eviction by hits, known addresses exempt - self_backup: <data_dir>/backups/setu is opened level by level with openat(O_DIRECTORY|O_NOFOLLOW) (mkdirat + fchown for a level made as root), fchmod and flock on that handle; dump, prune (Dir + fstatat + unlinkat) and the partial cleanup use it, nothing by path. A symlink at backups or backups/setu is a recorded failure with its target untouched. - SUPERUSER_PREAMBLE: SET ROLE NONE (RESET ROLE goes back to the database's role setting); transaction_timeout off on Postgres 17+. restore_pg case: a dump that sets the database's role and a 1 ms timeout and leaves a table owned by the restore role (fails with RESET ROLE, verified). - login limiter: account keys are SHA-256 of the normalised email; past the cap the key with the fewest live hits goes (never the one just counted); an address (/64) the account logged in from in the last 30 days (audit_log) is exempt from the account counter. - SECURITY.md states the remaining lockout; OPERATIONS.md the way out. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
… capped (S-M3); operator accounts never evicted from the limiter (review e8ded1a) S-M3: a viewer's live site-log streams shared the server's command queue and its 600-polls-a-minute budget with job steps, so enough streams made every job step on that server time out. - migration 0009: agent_commands.read_only, set at enqueue from Command::is_read_only; next_for orders read_only, created_at, id, so job commands go first (each group oldest first); index replaced to match. - proto: fetch_logs and fetch_system_logs expire after 15 s (LOG_FETCH_TIMEOUT), so a backlog of them expires instead of queueing. - api: GET /api/v1/sites/{id}/logs holds a StreamSlot (crates/api/src/ streams.rs) for the stream's life: 4 per operator, 8 per server, 429 past either; 8 streams use at most 240 of the 600 polls a minute. Review e8ded1a: - 1: the login handler looks the email up first; an email that finds an operator is counted in a table that is never evicted (one key per operator), others in the capped table. Test: a table full of two-hit junk keys, then the owner alternating with new junk emails; the owner's last attempt is refused (fails with one shared table, checked). - nit: login_ips_since lists each address once (GROUP BY ip, newest use). - nit: the restore_pg case also sets transaction_timeout on 17+. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…nted, cert_status 15 s, timeouts off at connect, refused log stream shown - 1: a poll that delivers a command takes its count back (forgive_agent_poll), so the 600-a-minute limit binds only polls of an empty queue; cert_status joins the log fetches at the 15 s TTL (READ_TIMEOUT). Test: limit 3, five cert_status drained, then a stop is delivered, then three empty polls and a 429. - 2: useSseStream reports a refused stream (EventSource CLOSED) and reopens it after 10 s; the Logs tab shows a status line. Test with a fake EventSource: a drop shows nothing, a refusal shows the line and a second EventSource opens after the delay. - 3: superuser psql runs connect with -d "dbname='..' options='-c statement_timeout=0 -c lock_timeout=0 -c transaction_timeout=0'": startup options outrank a database's settings, so no site setting is in force at any point. A server before 17 refuses transaction_timeout at startup (nothing ran): that is remembered and the call made again without it. The DO block is gone from SUPERUSER_PREAMBLE; OPERATIONS.md does the same by hand. - nits: a closed stream frees its slot within 250 ms; an API test pins is_operator (one key in each table); api-types.d.ts regenerated; restore_pg doc comment rewrapped. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…emplate1 closed to site roles (S-L2) S-M1: setu-site@.service adds PrivateDevices, ProtectKernelModules/Logs, ProtectClock, ProtectHostname, ProtectProc=invisible (a site no longer reads other sites' /proc/<pid>/cmdline), RestrictNamespaces/Realtime, LockPersonality, empty capability sets, AF_INET/AF_INET6/AF_UNIX only, @System-service minus @PRIVILEGED and @resources with EPERM instead of a kill, native syscalls only, RemoveIPC. Left out on purpose: MemoryDenyWriteExecute (JIT runtimes) and ProcSubset=pid (hides /proc/meminfo, cpuinfo, stat that runtimes size themselves from). setu-agent.service gets ProtectHome, PrivateTmp, ProtectKernelTunables and ProtectControlGroups, the directives sudo and the helper survive. S-L2: install's REVOKE_SQL closes template1 as well as postgres to PUBLIC. Real-Postgres test: after the script a site role is refused by both and still reaches its own database; it fails with the old SQL. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…ot guessed from a translated error Finding 1: superuser_psql no longer recognises a pre-17 server by the English text of its startup error (lc_messages translates it, so a de_DE cluster failed every superuser script). PsqlSudo reads SHOW server_version_num once in the postgres database with only statement_timeout and lock_timeout off, keeps it in a shared OnceCell, and adds transaction_timeout=0 from 170000. A failed or unreadable answer fails the call and is asked again next time. Tests: 160010 sends no transaction_timeout, 180001 sends it, the query goes once across two calls and a clone, a translated error and a non-number fail with nothing run. with_server_version() keeps the counted-call fake tests as they were. Nits: superuser_conninfo quotes with conninfo_quote; default_timeout doc rewrapped; the Logs notice builds its seconds from REFUSED_RETRY_MS. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…er scripts; job-log retries capped; notice wording - superuser_conninfo also sets idle_session_timeout=0 and idle_in_transaction_session_timeout=0 at startup (finding 2); doc comment narrowed to the timeouts; OPERATIONS.md the same; the restore_pg case sets both to 1 ms on the database. - The Postgres version is read per agent start (doc says so). - SiteLogs refused notice reworded; a refused job log stops after three refusals in a row (test fails without the cap). Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…ounded fds; backup refuses past 256 levels (S-L15) remove_tree_at walks iteratively and keeps at most 64 directory fds open, reopening dropped upper levels by name (O_NOFOLLOW) on the way back up, so a tree deeper than the fd limit no longer fails with EMFILE. site-remove now removes the site tree before userdel: a failed removal leaves a site that still owns its files and can be retried, never files under a freed uid. append_tree refuses a tree more than 256 levels deep, naming the directory. Tests: 2,000-level trees for site-remove (run/ and data/), backup refusal by name, user kept when the tree stays, pending siblings across dropped levels. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…alth probe follows no redirect (S-L7) nftables: output chain drops loopback TCP from site uids (30000-39999) to site ports (20000-29999) except the uid . port pairs in set site_own. Each unit's drop-in adds its element (ExecStartPre=+) and deletes it (ExecStopPost=+). The table loads with add + flush chain, so a reload keeps the set. New helper verb own-port brings an older drop-in up to the template and adds the element of a running unit; the agent calls it once per site at start. write-unit takes the port and needs the site user. E2E: sites_cannot_reach_each_other. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…-L11) StateStore::open moves an unreadable or corrupt state.json aside to state.json.bad-<unix seconds> (kept; the next save no longer overwrites it) and starts empty. Reconcile keeps the setu-site-* routes (and their -www and -alias-<n> routes) of a site the state does not know whose tree is still under sites_root. doctor: new warn-only row state-set-aside. Tests: the review's scenario (corrupt state, three sites' routes kept, a route without a tree removed), set-aside naming and survival, route_site. OPERATIONS.md: how to put a repaired state file back. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…moved with the site, row 14 checks the unit lines F1: own_ports runs on every reconcile pass (the first at start), so a table loaded after the agent started (install-system after setu upgrade, a reload by hand) gets every running site's element back; install-system ends with systemctl try-restart setu-agent so that is immediate. doctor row nftables-site-own (nft list set inet setu site_own). Changelog corrected. F2: e2e row 14 stops and starts two's unit with the agent running and asserts systemd's exit status of the ExecStopPost/ExecStartPre nft lines and the set before and after. F3: site-remove deletes the user's site_own elements itself (nft -j -n list set, delete each), after the stop and before userdel, also when the stop fails. Nits: backfill detects the ExecStartPre nft line, not the word; remove_tree_at climbs via .. checked against the recorded dev/ino (linear in depth); docs say TCP port; own_ports logs a site without a port. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…iB (S-L1) Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…2614560 nits) - own-port serialised with site-remove and unit start/stop/restart (site_ops lock); a waiting pass skips a removed site; own-port deletes its element when the unit stopped meanwhile, and adds the drop-in's element (refuses another uid) - output drop matches only new connections (SYN without ACK) - health probe: a 3xx counts as up, never followed (test: target gets no request) - route_unknown on a record made by a command other than SetRoute/RemoveRoute: reconcile keeps its routes while the tree exists (S-L11 after an update or rollback) - nft_del_uid: only a failed nft list counts as an empty set; doctor names an unreadable state dir; set-aside files matched by bytes; kept-route warning once per start - e2e: row 14 reload + pre-0.1.11 upgrade path on a scratch table, row 11 checks the deleted uid has no site_own element - download: exactly 10 redirect hops; cap test renamed Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…S-L6)
GET /api/v1/jobs/{id}/log polled the job row every 500 ms with no cap on
open tails. An operator now holds at most 4 live tails and the control
plane 16 (429 past either); a finished job's log is sent once and takes
no slot. LogStreams carries its caps, sites() and jobs() instances.
Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…nly a running unit, an unparsable own nft line is refused, e2e rows 11/14 fail on a failed list and always delete inet setuup, route_unknown cleared by SetRoute/RemoveRoute is tested Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…ritten to state.json (S-L8) A line is cut at 4 KiB, a page ends at 1 MiB of lines or 8 MiB of journalctl output with the cursor of the last entry it holds, and journalctl is read as it writes and stopped once a page is full. Read-only results (status, logs, inventory) are held in memory until posted (at most 8), so a log page no longer rewrites state.json. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
… the token before the body, and their metrics bounded (S-L13)
- POST /agent/v1/heartbeat: 30 a minute per server, POST
/agent/v1/commands/{id}/result: 600 a minute per server (429 past
either; the poll table's rules, one table each).
- Both take the body as bytes and parse it after the bearer check, with
ApiJson's content-type rule and validation envelope (json_body).
- A heartbeat may carry at most 2 metric rows per site on the server
plus 8 (400, nothing stored); a row dated more than 5 min ahead of or
1 h behind the control plane's clock is skipped, so no row outlives
the prune or stays "latest" for good.
- openapi.json and ui api-types gain the 429 responses; AGENT.md and
CHANGELOG describe the limits.
Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
… skip the save, stderr drained, cert_status listed; no-feature clippy - journal: journalctl stays without --all (that bounds an entry); a MESSAGE it writes as null (4 KiB or more) shows as '… (a line over 4 KiB is not shown)' instead of vanishing (Opus 1). The byte-budget test uses lines the real journalctl prints. Output that is not JSON counts against the raw budget. stderr past the 4 KiB kept is drained, not closed (no SIGPIPE mid-page). Tests: a chatty stderr, an entry over the raw budget, a null message. - state: mark_posted of an id held nowhere (an evicted read-only result, an already-posted one) does not rewrite state.json (Fable 1); test checks inode and bytes. cert_status in the read-only lists (Fable 3). - helper/cli: context() expects unnecessary_wraps without feature test-root. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…cb1663 review nits) Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…rap is a file (S-L14) Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…rver update and stores up to the cap; 'parse' not 'read'; a stderr test past the pipe; result-limit test; a90d5a4/7cb1663 nits Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
|
Diff review passed (a429633): krishiv-internal ops/reviews/review-web-o1-setu-s-m-a429633-fable.md, verdict pass, no findings. Its nits (two over-long lines, a dist/index.html inline-script check) go into the next push along with S-L12 and S-L4. |
…heme-init.js is served as a script (setu_ui tests); AGENT.md rewrap Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
… name is checked against site domains and reserved names (S-L4); audit rows masked below owner (S-L5) S-L12: 168.63.129.16, 192.0.0.0/24, 2001::/32, fec0::/10 and ::ffff:0:0:0/96 join is_non_public, with a test of each range and its neighbours. S-L4: turning www_redirect on refuses www.<domain> when it is another site's domain (409, in the transaction) or reserved (400: refuse_reserved_domain). Site create runs the same two checks on its www. name, and refuses a www.<x> domain while site <x> redirects it. Like the existing alias checks, these reserve www.<domain> for any domain, not only apex ones. S-L5: GET /audit stays viewer-level; below owner a row has no ip, and operator.*, auth.* and setup.create-owner rows (operator email or id) have no target. CHANGELOG entries for the three, and the S-L8 entry rewrapped (a429633 nit). Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
Conflicts: agent postgres.rs, tests/postgres.rs, tests/restore_pg.rs, AGENT.md and OPERATIONS.md on main were earlier states of this branch (363518f, 0adf3f9), so this branch's versions are kept. CHANGELOG: the 0.1.9 section is main's as released; Unreleased gets main's site-limits entry and this branch's later entries (S-L11 moved out of 0.1.9, where the merge had placed it). The branch's migration 0009_agent_command_priority is now 0010 (main's 0009_site_limits came first); a test NewSite drops the plan_memory_mb field #40 removed. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
|
Diff review of c81e9c5 (delta since a429633: 0e4288f, de6c6ec and the merge of main): changes requested.
Nits (not blocking):
No finding in S-L12, S-L4, S-L5, the a429633 nits or the merge resolution: the files kept from the branch are later versions of main's, the migration rename to 0010 is content-identical, and the merge adds no lines of its own in the conflicted source files. |
… F1) t_sites_cannot_reach_each_other used vyasa.test as the site two.test must not reach; aarch64 has no Vyasa release, so VYASA_ID stayed empty and the step always failed there. Without Vyasa it now provisions a second hello site, three.test, and checks the drop and the root control against it, so S-M2's isolation is tested on both architectures. docs/AGENT.md: finish the rewrap of the server-status sentence. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
Apps::publish_version (was upsert_version) runs in one transaction and inserts the version with ON CONFLICT DO NOTHING. A version already published with the same manifest is a no-op (POST /apps answers 200, not 201); with another manifest it is a 409 that rolls back the app row refresh too, so retry, restore and reset keep the manifest sites were created from. The app.publish audit target carries the manifest's SHA-256, marked (unchanged) for a no-op. Tests: the API cases (new, same, different with a rename, next version), the route matrix's second publish is the no-op, and the env test that relied on replacing a manifest grows the stored one in the database instead. openapi.json, ui api-types, .sqlx, CHANGELOG. Nits from the c81e9c5 Fable review: the heartbeat test's unique id uses new_id()[18..]; the S-L8 CHANGELOG entry rewrapped. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
config_file_for: as root, Config::load reads only /etc/setu/setu.toml, never ./setu.toml, so sudo setu upgrade/backup started in a directory another user can write cannot take that user's database_url and data_dir. Other users keep the current-directory preference; SETU_* env still wins. Test root_reads_only_the_system_config; OPERATIONS.md, CHANGELOG. Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…binary 0755 and synced S-I3: extra_env_name checks validate::env_name (^[A-Z][A-Z0-9_]*$, the helper's rule) and 128 characters, so a name the control plane stores is never refused by write_env and a running site no longer goes failed on '_FOO'. S-I5: setu upgrade copies the new binary to .setu.new, sets mode 0755 whatever root's umask is, syncs it, then renames (test: a 0700 source stages as 0755). Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
|
Diff review passed (3b0d022) |
- config: tests load a named file through Config::load_from, so they hold when run as root - docs: INSTALL.md and OPERATIONS.md say root reads only /etc/setu/setu.toml - follow: a version published by someone else meanwhile is not reported as followed - db: a no-op publish rolls the apps refresh back; doc says new manifest fields need skip_serializing_if - api: PUT env with extra name _FOO is 400 and changes nothing (test) - embed: the inline-script check ignores case - upgrade: sync /opt/setu after the rename - CHANGELOG: no-op publish and the followed URL; www.www.<x> Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
…remove (S-L15) entries; dated 2026-12-06 Signed-off-by: gopal tathe <44522021+techgopal@users.noreply.github.com>
Security review fixes from the December Setu review (krishiv-internal
research/security/2026-12-setu-review.md): S-H1, S-M1 to S-M5, S-L1, S-L2, S-L4 to S-L8, S-L11 to S-L15. Each was reviewed commit by commit (Opus and Fable review files in krishiv-internalops/reviews/review-web-o1-setu-s-m-*).search_path.postgresandtemplate1are closed to site roles.www.redirect, or creating a site, checkswww.<domain>against other sites' domains and against reserved names (the panel's host, localhost, server hostnames).Main is merged in. This branch's migration
0009_agent_command_priorityis now0010(main's0009_site_limitscame first).Reviews: a429633 passed (Opus and Fable). A delta review of c81e9c5 is requested.