mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
chore(lean): model redirects, key lookup, upserts, retries, CORS, TTL
This commit is contained in:
@@ -5,7 +5,7 @@ import Mathlib
|
||||
|
||||
Models `pkg/app/app.go` `buildCheckOrigin` / `normalizeOriginHost`, `pkg/app/handlers_admin.go`
|
||||
`requireAdmin`, `pkg/auth/auth.go` (`getPermissionsForRole`, `ValidateAPIKey` expiry),
|
||||
`pkg/proxy/pypi/pypi.go` (`isAllowedPyPIHost`, the `original_url` guards), `Dockerfile.frontend`.
|
||||
`pkg/proxy/pypi/pypi.go` (`isAllowedPyPIHost`, the `original_url` guards, `get`), `pkg/network/client.go` (`send`), `pkg/auth/auth.go` (`ValidateAPIKey` cost), `pkg/app/cors.go` (`apiOriginGuard`), `Dockerfile.frontend`.
|
||||
Omits: the TLS layer, bcrypt internals, DNS.
|
||||
-/
|
||||
|
||||
@@ -125,15 +125,64 @@ theorem internal_hosts_rejected :
|
||||
hostAllowed "169.254.169.254" = false ∧ hostAllowed "localhost" = false ∧
|
||||
hostAllowed "metadata.google.internal" = false := by decide
|
||||
|
||||
/-- The allowlist is checked once, on the first hop. `network.Client` follows redirects without
|
||||
re-checking, so an allowed host that answers 302 reaches any target. Acceptable only while no
|
||||
allowed host redirects (pypi.org and files.pythonhosted.org do not). -/
|
||||
def hopAllowed (hops : List String) : Bool := hops.head?.any hostAllowed
|
||||
/-- Before the fix the allowlist was checked once, on the first hop. `network.Client` followed
|
||||
redirects without re-checking, so an allowed host that answers 302 reached any target. -/
|
||||
def hopAllowedOld (hops : List String) : Bool := hops.head?.any hostAllowed
|
||||
|
||||
/-- After the fix (`network.Client.send`, `pypi.Handler.get`) every hop must pass the allowlist. -/
|
||||
def hopAllowed (hops : List String) : Bool := hops.all hostAllowed
|
||||
|
||||
theorem redirect_escapes_allowlist :
|
||||
hopAllowed ["files.pythonhosted.org", "169.254.169.254"] = true ∧
|
||||
hopAllowedOld ["files.pythonhosted.org", "169.254.169.254"] = true ∧
|
||||
hostAllowed "169.254.169.254" = false := by decide
|
||||
|
||||
theorem fixed_redirect_stays_allowlisted :
|
||||
hopAllowed ["pypi.org", "files.pythonhosted.org"] = true ∧
|
||||
hopAllowed ["files.pythonhosted.org", "169.254.169.254"] = false := by decide
|
||||
|
||||
/-- A hop is accepted only if every hop is on the allowlist. -/
|
||||
theorem hopAllowed_implies_each (hops : List String) (h : hopAllowed hops = true) :
|
||||
∀ x ∈ hops, hostAllowed x = true := by
|
||||
simpa [hopAllowed] using h
|
||||
|
||||
/-! ### API key validation cost (`pkg/auth/auth.go` `ValidateAPIKey`) -/
|
||||
|
||||
/-- bcrypt compares for one request when every stored key is a candidate (before the fix). -/
|
||||
def compareCostOld (storedKeys : ℕ) : ℕ := storedKeys
|
||||
|
||||
/-- A key carrying its id selects one stored key: one compare, hit or miss (a miss compares a dummy hash). -/
|
||||
def compareCostNew (_storedKeys : ℕ) : ℕ := 1
|
||||
|
||||
theorem old_cost_grows_with_keys : compareCostOld 1000 = 1000 := rfl
|
||||
|
||||
theorem new_cost_independent_of_keys (n m : ℕ) : compareCostNew n = compareCostNew m := rfl
|
||||
|
||||
/-! ### Cross-origin writes on `/api` (`pkg/app/cors.go` `apiOriginGuard`) -/
|
||||
|
||||
/-- Before the fix every handler answered `Access-Control-Allow-Origin: *`, so any page could call them. -/
|
||||
def corsOld (_ : Origin) : Bool := true
|
||||
|
||||
/-- After the fix a state-changing request passes iff it names no origin (non-browser client) or an
|
||||
allowed one; the same rule as the WebSocket origin check. -/
|
||||
def writeAllowed (rules : List Origin) : Option Origin → Bool
|
||||
| none => true
|
||||
| some o => rules.any (fun r => matchesExact r o)
|
||||
|
||||
def dashboard : Origin := ⟨Scheme.https, "gohoarder.example.com".toList, none⟩
|
||||
def attacker : Origin := ⟨Scheme.https, "evil.example.net".toList, none⟩
|
||||
|
||||
theorem old_cors_admits_any_page : corsOld attacker = true := rfl
|
||||
|
||||
theorem fixed_refuses_foreign_origin : writeAllowed [dashboard] (some attacker) = false := by decide
|
||||
|
||||
theorem fixed_admits_dashboard_and_cli :
|
||||
writeAllowed [dashboard] (some dashboard) = true ∧ writeAllowed [dashboard] none = true := by decide
|
||||
|
||||
/-- With nothing configured only a request with no origin passes the allowlist step; browsers fall to the
|
||||
same-origin host comparison in the handler. -/
|
||||
theorem empty_allowlist_refuses_any_origin (o : Origin) : writeAllowed [] (some o) = false := by
|
||||
simp [writeAllowed]
|
||||
|
||||
/-! ### Frontend blocklist (`Dockerfile.frontend`) -/
|
||||
|
||||
/-- `location ~ ^/(npm|pypi|go|metrics)(/|$)` on the normalised URI. -/
|
||||
|
||||
@@ -47,12 +47,12 @@ theorem stale_hides_only_gated (gate ttl now s t : ℤ) (hstale : now - s ≤ tt
|
||||
(hnew : s < t) : ¬ gateAccepts gate now t := by
|
||||
unfold gateAccepts; omega
|
||||
|
||||
/-- Production: `default_ttl: 24h` against a 72h gate. -/
|
||||
theorem prod_ttl_within_gate : (24 : ℤ) ≤ 72 := by decide
|
||||
/-- Production and, since the fix, the compiled default (`config.DefaultMetadataTTL`): 24h against a 72h gate. -/
|
||||
theorem compiled_ttl_within_gate : (24 : ℤ) ≤ 72 := by decide
|
||||
|
||||
/-- The compiled default is 7 days. Against a 72h gate a version aged 84h is eligible but absent
|
||||
from a 7-day-old snapshot: the proxy withholds it, which is stricter than the gate, never looser. -/
|
||||
theorem default_ttl_exceeds_gate :
|
||||
/-- The compiled default used to be 7 days. Against a 72h gate a version aged 84h is eligible but
|
||||
absent from a 7-day-old snapshot: the proxy withheld it, stricter than the gate, never looser. -/
|
||||
theorem old_default_ttl_exceeded_gate :
|
||||
∃ gate ttl now s t : ℤ, now - s ≤ ttl ∧ s < t ∧ gateAccepts gate now t ∧ ttl = 168 ∧ gate = 72 := by
|
||||
exact ⟨72, 168, 200, 100, 116, by decide, by decide, by unfold gateAccepts; decide, rfl, rfl⟩
|
||||
|
||||
|
||||
@@ -137,6 +137,20 @@ theorem infra_error_served_forever (policy : Option ℕ) (now : ℤ) (bs : List
|
||||
/-- Scanner disabled: `CheckVulnerabilities` returns `(false, "", nil)` and `cache.New` drops the scanner. -/
|
||||
def servesDisabled : Bool := true
|
||||
|
||||
/-! ### Retry input (`pkg/cache/cache.go` `store`, `ScanFile`; `scheduleRetry`) -/
|
||||
|
||||
/-- Before the fix a retry reused the first scan's path. Remote storage scans a temp file removed
|
||||
after the first attempt, so only local backends kept it. -/
|
||||
def retryReadableOld (localBackend : Bool) : Bool := localBackend
|
||||
|
||||
/-- After the fix a retry whose file is gone re-reads the blob through the file source. -/
|
||||
def retryReadableNew (localBackend fileSourceSet : Bool) : Bool := localBackend || fileSourceSet
|
||||
|
||||
theorem old_remote_retry_unreadable : retryReadableOld false = false := rfl
|
||||
|
||||
theorem fixed_retry_always_readable : retryReadableNew false true = true ∧ retryReadableNew true false = true := by
|
||||
decide
|
||||
|
||||
/-! ### Retry budget -/
|
||||
|
||||
def maxRetries : ℕ := 6
|
||||
|
||||
@@ -87,4 +87,24 @@ theorem chartNew_never_drops (rollup live : ℕ) : chartOld rollup live ≤ char
|
||||
/-- A download aggregated after the rollup ran: the old chart shows 1 of 2. -/
|
||||
theorem chartOld_drops_late_download : chartOld 1 2 < chartNew 1 2 := by decide
|
||||
|
||||
/-! ### SQLite upsert (`aggregation_worker.go`, `sqlite_stats.go`) -/
|
||||
|
||||
/-- A bucket is a list of stored rows; the chart sums them. Before the fix SQLite had no unique index,
|
||||
so `INSERT OR REPLACE` appended a row per pass. -/
|
||||
def chartTotal (rows : List ℕ) : ℕ := rows.sum
|
||||
|
||||
/-- Three passes over a bucket whose registry total is 3: three copies. -/
|
||||
theorem old_sqlite_duplicates_rows : chartTotal [3, 3, 3] ≠ 3 := by decide
|
||||
|
||||
/-- `ON CONFLICT ... DO UPDATE` over the unique index keeps one row per bucket: a derived row is set,
|
||||
a package row adds the new events, as on PostgreSQL. -/
|
||||
def upsertDerived (_old new : ℕ) : List ℕ := [new]
|
||||
def upsertAdditive (old new : ℕ) : List ℕ := [old + new]
|
||||
|
||||
theorem fixed_sqlite_registry_row_idempotent (old new : ℕ) :
|
||||
chartTotal (upsertDerived old new) = new := by simp [chartTotal, upsertDerived]
|
||||
|
||||
theorem fixed_sqlite_package_row_keeps_late_events (old new : ℕ) :
|
||||
chartTotal (upsertAdditive old new) = old + new := by simp [chartTotal, upsertAdditive]
|
||||
|
||||
end Gohoarder.Stats
|
||||
|
||||
Reference in New Issue
Block a user