diff --git a/lean4/FINDINGS.md b/lean4/FINDINGS.md index 498a740..e190dc8 100644 --- a/lean4/FINDINGS.md +++ b/lean4/FINDINGS.md @@ -30,6 +30,7 @@ Currency: `sh lean4/scripts/check_currency.sh origin/main`. | GH-23 | PyPI sidecar | pip's PEP 658 request (`.metadata`, suffix on `original_url`) is served as its wheel's sidecar; another file's sidecar is still refused | correctness (pip install failed with 400) | medium (every pip install through the proxy) | `Access.pip_form_keeps_key_binding`, `pip_sidecar_of_other_file_rejected`, `pip_sidecar_of_same_file_accepted` / `TestPipSidecarFormIsStreamed`, `TestHandlePackageFile_OriginalURLFilenameBound` | 501bf1a | | GH-24 | rescan | with `security.rescan_interval` set, artifacts whose scan never completed are scanned and old results refreshed | correctness (scans lost to a restart stayed unscanned; new CVEs never picked up) | medium | not modelled (worker wiring) / `TestWorkersStartTheRescanWorker` | 378b873 | | GH-25 | scanners | an error result from one scanner is left out of the merge, so the row reads error only when every scanner failed; grype, npm-audit, govulncheck and pip-audit give correct results with the scanner image tool versions | correctness (false error, false clean) | high (grype, npm-audit and pip-audit failed on every package; govulncheck and a bare-archive grype scan read vulnerable packages clean) | `Security.old_error_poisons_clean`, `error_and_clean_reads_clean`, `all_error_reads_error`, `merged_not_error_when_one_ran` / `TestScanPackage_ErrorResultPlusCleanReadsClean`, `TestScanPackage_AllErrorResultsReadError`, `TestScanPackage_InfrastructureErrorResultsCountAsInfrastructure`, `TestScanMapsRealOutput`, `TestParseKeepsBothViaShapes`, `TestScanReadsPrettyPrintedFindings`, `TestScanAuditsTheReleaseFromARequirement` | 3731ca0 | +| GH-26 | rescan | the rescan worker skips the entries the serve path skips, so Go `.mod` and `.info` rows are no longer scanned | cost (1316 of about 2780 rescans on production were these rows, about 3.3 s each) | low | `Cache.old_rescan_scanned_go_metadata`, `rescan_skips_what_serve_skips` / `TestRescanSkipsMetadataEntries` | d6044cd | ### Exploit paths and reach @@ -84,6 +85,11 @@ Currency: `sh lean4/scripts/check_currency.sh origin/main`. as clean the same way grype did. - **GH-17 cleanup.** The per-handler `Access-Control-*` lines in `pkg/app/handlers*.go` are removed; `apiOriginGuard` is the only CORS source. `TestHandlersSetNoCORSHeaders` and `TestAPIResponsesNeverCarryWildcardOrigin` guard it. +- **GH-26.** `rescanPackages` kept its own list of metadata versions and never called `IsMetadataEntry`, so Go `.mod` and `.info` + rows were scanned every cycle. Related options, whose defaults keep the old behaviour: `rescan_active_window` skips packages idle longer than the + window, `rescan_concurrency` runs scans in parallel, and `rescan_on_access` rescans a stale cache hit before serving it. + Model of the last: `Security.finished_rescan_verdict_younger_than_interval`, `old_verdict_only_on_timeout`, + `stale_clean_fresh_vulnerable_blocked`, `timeout_keeps_previous`, `timeout_without_previous_is_miss_rule`, `off_is_old_hit_path`. ## Not fixed diff --git a/lean4/Gohoarder/Cache.lean b/lean4/Gohoarder/Cache.lean index f3f2331..d2d1f19 100644 --- a/lean4/Gohoarder/Cache.lean +++ b/lean4/Gohoarder/Cache.lean @@ -208,4 +208,16 @@ theorem old_exempted_npm_package : scanExemptOld true "1.0.0" = true := by decid theorem go_metadata_still_exempt : scanExempt "go" true "v1.0.0" = true := by decide +/-- `pkg/scanner/rescanner.go` before the fix: the worker skipped only the version names, not the Go suffixes. -/ +def rescanSkipOld (version : String) : Bool := decide (version ∈ metaVersions) + +/-- After the fix the worker asks `IsMetadataEntry`, the same predicate as the serve path. -/ +def rescanSkip : String → Bool → String → Bool := scanExempt + +theorem old_rescan_scanned_go_metadata : + rescanSkipOld "v1.0.0" = false ∧ scanExempt "go" true "v1.0.0" = true := by decide + +theorem rescan_skips_what_serve_skips (registry : String) (suffixMeta : Bool) (version : String) : + rescanSkip registry suffixMeta version = scanExempt registry suffixMeta version := rfl + end Gohoarder.Cache diff --git a/lean4/Gohoarder/Security.lean b/lean4/Gohoarder/Security.lean index 8d4f3ca..ee4836e 100644 --- a/lean4/Gohoarder/Security.lean +++ b/lean4/Gohoarder/Security.lean @@ -271,4 +271,90 @@ theorem one_package_failure_is_not_infra : infraOnly [true, false] = false := by theorem all_infra_failures_are_infra : infraOnly [true, true] = true := by decide +/-! ### Rescan on access (`pkg/cache/rescan_on_access.go` `refreshStaleScan`) + +Ages are seconds since `ScannedAt`; `interval` is `Config.RescanAfter` (0 = off). A rescan that timed out +or failed is `none`: the previous verdict stands, and with no previous verdict the first-download rule applies. -/ + +structure Verdict where + scan : Scan + age : ℕ + +/-- The hit is rescanned when the feature is on and its result is missing or at least `interval` old. -/ +def rescans (interval : ℕ) (prev : Option Verdict) : Bool := + decide (0 < interval) && (match prev with + | none => true + | some v => decide (interval ≤ v.age)) + +/-- The verdict the gate decides on; a finished rescan is brand new (age 0). -/ +def hitVerdict (interval : ℕ) (prev : Option Verdict) (rescan : Option Scan) : Option Verdict := + if rescans interval prev then + (match rescan with + | some s => some ⟨s, 0⟩ + | none => prev) + else prev + +/-- No verdict at all is a scan still in flight: `fail_closed` refuses, otherwise the package is served. -/ +def servesOnAccess (failClosed pkgBypass : Bool) (policy : Option ℕ) (now : ℤ) (bs : List Bypass) + (interval : ℕ) (prev : Option Verdict) (rescan : Option Scan) : Bool := + match hitVerdict interval prev rescan with + | some v => serves failClosed pkgBypass policy now bs v.scan + | none => serves failClosed pkgBypass policy now bs (Scan.missing true) + +/-- Key property: with the feature on, a rescan that finished leaves no verdict as old as `interval`. -/ +theorem finished_rescan_verdict_younger_than_interval (interval : ℕ) (hi : 0 < interval) + (prev : Option Verdict) (s : Scan) (v : Verdict) + (h : hitVerdict interval prev (some s) = some v) : v.age < interval := by + unfold hitVerdict at h + by_cases hr : rescans interval prev = true + · simp only [hr, ite_true, Option.some.injEq] at h + subst h + simpa using hi + · simp only [hr] at h + subst h + simp only [rescans, hi, decide_true, Bool.true_and, Bool.not_eq_true] at hr + simpa using hr + +/-- A verdict older than `interval` survives only when the rescan timed out or failed. -/ +theorem old_verdict_only_on_timeout (interval : ℕ) (hi : 0 < interval) (prev : Option Verdict) + (rescan : Option Scan) (v : Verdict) (h : hitVerdict interval prev rescan = some v) + (hold : interval ≤ v.age) : rescan = none := by + cases rescan with + | none => rfl + | some s => exact absurd (finished_rescan_verdict_younger_than_interval interval hi prev s v h) (not_lt.mpr hold) + +/-- A stale hit is judged on the fresh scan: a clean result that now holds a blocked vulnerability is refused. -/ +theorem stale_clean_fresh_vulnerable_blocked (policy : Option ℕ) (t : ℕ) (hp : policy = some t) + (failClosed : Bool) (now : ℤ) (bs : List Bypass) (interval age : ℕ) (hi : 0 < interval) + (hage : interval ≤ age) (vs : List Vuln) (v : Vuln) (hv : v ∈ vs) (hsev : t ≤ v.sev) + (hun : v.id ∉ bypassedIds now bs) : + servesOnAccess failClosed false policy now bs interval (some ⟨Scan.ok [], age⟩) (some (Scan.ok vs)) = false := by + have hr : rescans interval (some ⟨Scan.ok [], age⟩) = true := by simp [rescans, hi, hage] + simp only [servesOnAccess, hitVerdict, hr, ite_true] + exact vulnerable_not_served policy t hp failClosed now bs vs v hv hsev hun + +/-- A fresh result is served as is: no rescan, whatever the rescan outcome. -/ +theorem fresh_hit_not_rescanned (interval : ℕ) (v : Verdict) (hfresh : v.age < interval) + (rescan : Option Scan) : hitVerdict interval (some v) rescan = some v := by + have : rescans interval (some v) = false := by simp [rescans, not_le.mpr hfresh] + simp [hitVerdict, this] + +/-- A timeout keeps the previous verdict. -/ +theorem timeout_keeps_previous (interval : ℕ) (prev : Option Verdict) : + hitVerdict interval prev none = prev := by + unfold hitVerdict + split <;> rfl + +/-- A timeout with no previous verdict follows the first-download rule. -/ +theorem timeout_without_previous_is_miss_rule (failClosed pkgBypass : Bool) (policy : Option ℕ) (now : ℤ) + (bs : List Bypass) (interval : ℕ) : + servesOnAccess failClosed pkgBypass policy now bs interval none none = (pkgBypass || !failClosed) := by + simp only [servesOnAccess, timeout_keeps_previous] + cases failClosed <;> cases pkgBypass <;> simp [serves] + +/-- `rescan_on_access` off (`RescanAfter` 0) is the old hit path: the stored verdict decides. -/ +theorem off_is_old_hit_path (prev : Option Verdict) (rescan : Option Scan) : + hitVerdict 0 prev rescan = prev := by + simp [hitVerdict, rescans] + end Gohoarder.Security