Skip to content

fix: security & data-safety hardening across server and CLI - #13

Open
aoruLola wants to merge 4 commits into
fix/revert-confirmation-v2from
fix/security-hardening-batch
Open

fix: security & data-safety hardening across server and CLI#13
aoruLola wants to merge 4 commits into
fix/revert-confirmation-v2from
fix/security-hardening-batch

Conversation

@aoruLola

Copy link
Copy Markdown
Collaborator

Summary

A focused security- and correctness-hardening batch surfaced by a multi-subsystem review (auth/rate-limiting, storage/versioning/locks, trust/audit chain, and the CLI). Each change is grounded in a concrete failure scenario. All findings verified against the code; cargo clippy, cargo fmt --check, and the full test suite pass (server 85 incl. a new regression test, CLI 42).

Stacked on #12. Base is fix/revert-confirmation-v2 so the diff shows only this batch. GitHub will re-target this PR to main once #12 merges.

Server — auth & request path

  • High-risk guard fails closed: no longer falls back to a hardcoded signing secret shipped in source when enforcement is on; signature compared in constant time (blake3::Hash).
  • API key cache TTL (API_KEY_CACHE_TTL_SECS, default 60s): DB-side revocation / permission changes are honored within the TTL instead of forever; stale entries dropped when the DB reports invalid.
  • Atomic refresh-token rotation: single conditional UPDATE; rows_affected == 0 ⇒ replay ⇒ family revoked. Closes the concurrent-refresh replay window.
  • list_keys no longer leaks raw secret prefixes (returns DB-hashed keys when a DB is configured).

Server — locks, versioning, storage

  • Draft-first changeset can be promoted (ROOT accepted against an empty head in promote/gate). +regression test.
  • Snapshots persisted under the changeset's real branch (was always the default branch).
  • Optimistic state_version guard (migration 202602260018): concurrent-writer clobbering becomes a detectable conflict; single-instance behavior unchanged.
  • Owner-scoped lock release/renew: a stale cache can't delete or steal a lock another owner re-acquired; force-release stays admin/owner-blind.
  • Dedup no longer reads the whole existing object back into memory (size check via metadata; integrity still verified on retrieve).
  • Opt-in separation of duties (HYPERTIDE_REQUIRE_SEPARATE_APPROVER) rejecting self-approve/self-promote.

Server — trust / audit / replay

  • Witness quorum requires verified signatures: summary() recomputes each receipt's HMAC over checkpoint material and counts only configured, verified witnesses (was a raw row count → forgeable).
  • Consistent checkpoint snapshot: generate_checkpoint runs in one REPEATABLE READ transaction under the audit advisory lock (removes a TOCTOU).
  • Replay correctness: lock set keyed by (repo_id, file_path); always full-scan (the checkpoint marker holds no state snapshot, so suffix replay reported spurious mismatches).
  • Accurate audit checked counter (no longer derived from gap-prone seq).

CLI — data & credential safety

  • Atomic state writes (temp+rename) with 0600/0700 perms and auto .hypertide/.gitignore — plaintext credentials can't be world-readable or accidentally committed.
  • checkpoint restore/branch no longer overwrite local files unconditionally (checkout-style pre-flight + --force).
  • sync materializes content and updates tracked hashes when advancing the base pointer, refusing on divergence (+--force) — fixes a silent lost update.
  • confirm_dangerous returns non-zero on cancel and hard-fails in a non-TTY without --yes (was exit 0 no-op).

Deliberately deferred (need a threat-model / product decision)

  • Audit-chain tamper-evidence vs a DB-write attacker (asymmetric signing + external anchoring; Merkle state_root).
  • Canonical JSON hashing for audit payloads (no practical trigger with current string/int payloads).
  • End-to-end streaming for uploads/downloads (currently bounded by the 256 MB request cap).

🤖 Generated with Claude Code

aoruLola and others added 4 commits July 26, 2026 22:15
- high_risk: fail closed when HIGH_RISK_SIGNATURE_REQUIRED is set but no
  signing secret is configured, instead of falling back to a hardcoded
  secret shipped in source; verify signatures in constant time via
  blake3::Hash equality rather than hex string comparison.
- auth: give the in-memory API key cache a TTL (API_KEY_CACHE_TTL_SECS,
  default 60s) so a DB-side revocation or permission change is honored
  within the TTL instead of being trusted forever; drop stale entries when
  the DB reports a key invalid.
- auth: make refresh-token rotation atomic via a single conditional UPDATE
  (rotate_refresh_token); rows_affected == 0 is treated as replay and burns
  the token family, closing the concurrent-refresh replay window.
- auth: list_keys_persistent returns only DB-backed hashed keys when a DB
  is configured, so raw secret prefixes are never echoed by list_keys.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- versioning: allow a draft-first changeset (base=ROOT, head never
  advanced) to be promoted by mirroring submit's ROOT acceptance rule in
  promote_changeset and changeset_gate (head_accepts_base). Adds regression
  test.
- versioning: persist each snapshot under its changeset's actual branch
  instead of always the default branch (snapshots are keyed by branch_name).
- versioning: add an optimistic state_version guard (migration
  202602260018) so a concurrent writer's update is detected as a conflict
  rather than silently clobbered. Single-instance behavior is unchanged.
- versioning: opt-in separation of duties
  (HYPERTIDE_REQUIRE_SEPARATE_APPROVER) rejecting self-approve/self-promote.
- lock: unlock/expired-renew use owner-scoped DB deletes and an ownership
  guard on renew upsert, so a stale cached view cannot delete or steal a
  lock that another owner re-acquired. Force-release stays owner-blind.
- storage: on a dedup hit, compare object size via metadata instead of
  reading the whole existing object back into memory and re-hashing it;
  integrity is still verified on retrieve.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- witness: summary() recomputes each receipt's HMAC over the referenced
  checkpoint material (constant-time verify_slice) and counts only
  configured, signature-verified witnesses toward quorum. Previously quorum
  was a raw row count, so forged receipts could fake it.
- checkpoint: build generate_checkpoint inside one REPEATABLE READ
  transaction holding the audit advisory lock, so log head/size and the
  table counts describe a single consistent moment (removes a TOCTOU).
- replay: key the replayed lock set by (repo_id, file_path) to match DB
  uniqueness; always full-scan events (the checkpoint marker has no state
  snapshot, so a suffix replay produced spurious mismatches).
- audit_chain: report `checked` from a running verified counter instead of
  deriving it from seq, which has gaps from rolled-back appends.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- workspace: write state atomically (temp file + rename) with 0600 file /
  0700 dir permissions on Unix, and auto-create .hypertide/.gitignore so
  plaintext credentials can't be world-readable or accidentally committed.
- checkpoint restore/branch: run the same pre-flight as checkout (staged /
  local-modification / untracked-collision checks) and add --force, instead
  of overwriting local files unconditionally.
- sync: materialize snapshot content and update checked_out_assets when
  advancing the base pointer, and refuse (or --force) on local divergence,
  fixing a silent lost update where a later submit discarded intervening
  changes.
- confirm_dangerous: return a non-zero error on cancel and hard-fail in a
  non-TTY without --yes, instead of exiting 0 as a silent no-op.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

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