Skip to content

Harden login, agent registration, the GitHub token and three fail-open defaults - #170

Open
blakebauman wants to merge 8 commits into
mainfrom
fix/api-auth-hardening
Open

blakebauman wants to merge 8 commits into
mainfrom
fix/api-auth-hardening

Conversation

@blakebauman

Copy link
Copy Markdown
Contributor

Audit findings SEC-2 to SEC-6 and SEC-8 (11 Nov). Eight commits; the last four are the follow-ups from an independent security review of the first four.

Operators: before upgrading

  • Compose now requires FIBER_SECRETS_KEY (openssl rand -hex 32). A contributor's existing deploy/.env needs it too, or make infra fails at interpolation.
  • The API refuses to boot on a non-loopback address while admin/fiber can still log in. Change the password, or set FIBER_ALLOW_DEFAULT_ADMIN=1. The bare binary now listens on 127.0.0.1:18080 by default; the image sets 0.0.0.0:8080 itself.
  • FIBER_GITHUB_TOKEN in the API's environment is no longer used. Path-filtered pull request pipelines need a GITHUB_TOKEN project secret.
  • An agent's labels and concurrency are capped by its registration. An agent registered with concurrency 1 and started with 2 gets one slot until the row is raised; an agent whose row has no labels only receives label-less steps (SELECT id, name FROM agents WHERE labels = '[]'::jsonb;).

What was wrong, and the fix

  1. Login throttle bypassed by parallel requests. The handler checked the throttle, awaited argon2, then recorded the failure, so N concurrent requests all passed. Now an attempt is reserved under the lock before verifying (begin_attempt → a guard that settles on finish or drop). An unknown username costs the same argon2id verify as a real one, as does a legacy-format hash. Eviction under pressure takes expired, then fewest-failure entries first, never one with attempts in flight.
  2. argon2 ran on async workers and was unbounded. All hashing goes through one process-wide gate of 4, held until the hash finishes even if the caller disconnects. A busy gate makes callers wait in order for up to 5 s (at most 1024 waiting), then 503 "server busy; nothing was checked" — which never counts as a failed attempt.
  3. An agent's Hello set its own labels and concurrency. The effective registration is now the row's labels that the agent also claims, and the smaller concurrency. Applied at Hello, on every heartbeat from the row it already re-reads, and on PUT /api/agents/{id} (which narrows a live agent at once; widening lands on its next heartbeat).
  4. PR file listing sent the instance GitHub token to whatever repo a signed payload named. Any user can create a project, set its webhook secret and its pipeline's repo, so a "repo must match a pipeline" rule would not have closed it; the env fallback is removed.
  5. A malformed FIBER_SECRETS_KEY meant plaintext secrets. Set-but-invalid is now a boot error that names the problem and never echoes the value.
  6. 0.0.0.0 with admin/fiber by default. See the operator notes. The boot check asks the database whether the configured admin — and the literal admin — can still log in with the default password.

Verified

  • make check green; cargo test --workspace green (477 tests). Each new test was broken, seen red and restored — including one that drops the caller mid-hash and asserts the slot is still held, and a 64-thread barrier test on the login budget.
  • Independent security review: nothing blocking; it found that the first version's login cap (refuse at 8 in flight) let one client deny login to everyone and did not actually bound argon2. Both are fixed in 3f4de4c.
  • docker compose config fails without the key with the new message and resolves with it.

Not verified

  • Nothing was run against a live API or database. Store::password_is is runtime sqlx and has not executed (it is the same one-column select as change_password).
  • The heartbeat re-registration, the live PUT narrowing and the 503 path have unit coverage of their pure parts only; no smoke edits a connected agent's row.
  • The image was not built locally (one ENV line).
  • A sustained login flood above what four hash slots clear still pushes legitimate logins to the 5 s timeout. That needs a per-client rate limit at the reverse proxy; docs/authz.md says so.

Conflicts with #168

Both touch the register_agent call in the Hello arm of ws.rs. Whichever merges second needs a small rebase there.

🤖 Generated with Claude Code

blakebauman and others added 8 commits November 11, 2026 15:47
…c workers

The login handler checked the lockout, awaited the argon2 verify, and only then
recorded the failure, so a parallel burst all passed the check and the guess rate was
bounded by CPU rather than by the lockout. LoginGuard now hands out a reservation
(begin_attempt) that counts failures plus attempts in flight, settled by finish() and
released on drop so an early return cannot leak a slot.

Alongside: argon2 hash/verify run in spawn_blocking (login, change password, create
user and so add member), an unknown username is verified against a dummy hash so it
costs the same as a wrong password, at most 8 logins are verified at once (429 beyond
that), and a full guard table evicts expired then lowest-count entries instead of
dropping every entry that is not locked out.

fiber-core's tokio moves from dev-dependencies to dependencies; it was already in the
graph through sqlx, and Cargo.lock is unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Hello reloaded the agent row for pool scope but registered the agent with the labels
and concurrency the client sent, so any agent token could reconnect claiming every
label and u32::MAX slots and lease other agents' steps, with their secrets.

The row is now the ceiling: labels are the row's labels the agent also claims,
concurrency is the smaller of the two, and asking for more is logged with the agent id.
The decision is a pure function, effective_registration, with tests.

Intersection rather than taking the row verbatim, because both sides are configured
today and their defaults disagree (the UI pre-fills os=linux,docker=true where the
installer defaults to os=linux): an agent must not be offered work for a label it did
not claim. A row with no labels restricts nothing, so an agent registered with an empty
label set is not stripped on upgrade.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
resolve_token fell back to FIBER_GITHUB_TOKEN / GITHUB_TOKEN in fiber-api's environment,
and the repository it was spent on came from the webhook payload. A project admin sets
the webhook secret that payload is trusted on, and any user can create a project, so a
user could sign a pull_request payload naming a private repository and learn from the
response what the instance token can read there.

Both GitHub calls now go through resolve_project_token (project secrets only), as commit
statuses already did. Matching the payload repository against a pipeline's
workspace.repo was the alternative and is not a boundary: the same user writes the
pipeline. The environment variable is read only to warn, once, that it is ignored.

Docs said the environment was also a fallback for commit statuses, which the code had
already stopped doing; corrected alongside.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…s, and admin/fiber

A FIBER_SECRETS_KEY that was set but not 64 hex characters was treated as unset: the
boot log said 'unset (dev only)' and every secret written afterwards was plaintext. It
is now a boot error naming the problem (never the value), via parse_key; encrypt and
decrypt fail on such a key too instead of passing plaintext through. Unset is still
allowed and warned about.

Compose now requires FIBER_SECRETS_KEY like the other credentials. The Makefile's dev
credentials supply a throwaway one so 'make infra' still interpolates the file;
scripts/smoke_compose.sh already exported one.

fiber-api's default listen address is 127.0.0.1:18080 instead of 0.0.0.0:18080, and on
a non-loopback address it refuses to start while the bootstrap admin can log in with
the default password, unless FIBER_ALLOW_DEFAULT_ADMIN=1. The check asks the database
once users exist, because FIBER_ADMIN_PASSWORD only applies to an empty one, and runs
before the admin is created so a refused first boot can be fixed by setting the
variable. The Compose smoke logs in as admin/fiber on 0.0.0.0, so it sets the override.
The image now sets FIBER_LISTEN=0.0.0.0:8080 to match its EXPOSE and HEALTHCHECK.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… of refusing

The login cap was a handler-side try_acquire: it refused at once, so eight concurrent
junk logins kept every real login at 429, and because the permit lived in the handler
future while argon2 ran in a detached blocking task, a client that hung up freed the
permit with the hash still running. Change password, create user and add member had no
cap at all.

One gate in fiber-core::tokens now bounds every *_blocking hash and verify to
MAX_CONCURRENT_HASHES (4). The permit moves into the blocking closure and is released
when the hash finishes. Callers queue in arrival order for up to HASH_SLOT_WAIT (5 s),
then fail with HashBusy, which the API answers as 503 with Retry-After and its own
message; the login reservation is dropped, so it is never a failed attempt. The queue
is bounded by that timeout and by MAX_WAITING_HASHES. LOGIN_SLOTS is gone.

A legacy salt$sha256 hash now also pays for one argon2id verify and is compared with
constant_time_eq, which moves to fiber-core so the webhook check shares it.

Also folds upstream's '### Changed' heading and this branch's into one section, which
the rebase onto #166 had left as two.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…dits

A row with no labels took the agent's Hello labels as sent, which left the original
hole open for those rows and inverted an admin clearing labels to restrict an agent.
The intersection is now unconditional: an empty row grants none, and the agent is
offered only label-less steps (the queue match is step.labels <@ agent.labels).

PUT /api/agents/{id} installed the row's labels and concurrency verbatim on a live
agent, so widening a row gave it labels it never claimed until it reconnected. The
handler now only narrows (AgentPresence::narrow_to, via a new
Scheduler::narrow_agent_presence), and the agent's WebSocket session, which is what
knows what Hello claimed, recomputes effective_registration(row, hello) on every
heartbeat from the row it already re-reads. Narrowing is immediate on the replica
that took the edit and within one heartbeat elsewhere; widening lands on the next
heartbeat.

AgentPresence and register_agent are unchanged; the scheduler change is additive.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The boot refusal asked only whether FIBER_ADMIN_USER could log in with the default
password. An instance bootstrapped as admin/fiber whose FIBER_ADMIN_USER was later
pointed at another account still has admin/fiber in its users table, and booted
publicly. Both names are now checked, and the message names the one that matched.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The Makefile's throwaway credentials are skipped when deploy/.env exists, and Compose
now requires FIBER_SECRETS_KEY, so a deploy/.env written before that makes make infra
and make down fail at interpolation. Say so in docs/development.md, with the one-liner.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant