fix: remediate severity-ordered review findings across cache, scanners, and proxies

- H2: gormstore soft-delete + storage_key UNIQUE collision; re-save after eviction succeeds
- H1: cache getOrFetch cache-hit branch no longer runs CheckVulnerabilities on metadata entries
- M2: cache store() streams upstream body to temp file + SHA256 instead of double-buffering
- H3/L3: FetchResult/FetchFunc plumbing (+RequiresAuth/AuthProvider); proxy closures adapted;
        goproxy strips git+ prefix from UpstreamURL
- H4: scanner normalizes bypass package names; CheckVulnerabilities builds normalized packageKey
- M4: requireAdmin routes through extractAPIKey -> ValidateAPIKey -> admin role check
- M5: network client keeps only final RecordFailure (one count per attempt cycle)
- M3: normalizeOriginHost + buildCheckOrigin rewrite; origin_test.go added
- M1: app lifecycle cancel func; Run -> Shutdown on listen error (sole cache owner); JoinHostPort
- M6: VulnerabilityThresholds defaults all -1 so BlockOnSeverity governs
- L4: metadata.NormalizeSeverity at 4 severity-count sites (MEDIUM->MODERATE consistency)
- L1: handleDeleteBypass uses structured *errors.Error + ErrCodeNotFound
- L2: pypi extractVersionFromFilename (wheel/2nd segment, non-wheel/last digit segment);
      cacheVersion uses full unique basename; regression tests added
- L5: document per-instance ValidationCache key scoping; rename packageURL param to resource

All: go build ./... && go vet ./... && go test ./... pass; golangci-lint clean.
This commit is contained in:
2026-09-04 14:24:13 +01:00
parent edeb2eca0c
commit 171386f7a7
20 changed files with 790 additions and 239 deletions
-7
View File
@@ -229,19 +229,12 @@ func (c *Client) do(ctx context.Context, req *http.Request) (*http.Response, err
resp, err := c.client.Do(req)
if err != nil {
lastErr = err
if c.circuitBreaker != nil {
c.circuitBreaker.RecordFailure()
}
continue
}
// Check if response is retryable
if c.isRetryable(resp.StatusCode) {
_ = resp.Body.Close() // #nosec G104 -- Cleanup, error not critical
lastErr = fmt.Errorf("received retryable status code: %d", resp.StatusCode)
if c.circuitBreaker != nil {
c.circuitBreaker.RecordFailure()
}
continue
}
+35
View File
@@ -405,3 +405,38 @@ func TestConcurrentRequests(t *testing.T) {
assert.NoError(t, err)
}
}
// TestCircuitBreakerOpens verifies that a configured number of consecutive
// failed requests opens the circuit breaker and that subsequent requests are
// rejected with the circuit-open error until the breaker half-open window
// elapses and a request succeeds again.
func TestCircuitBreakerOpens(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close() // #nosec G104 -- Cleanup, error not critical
client := network.NewClient(network.Config{
Timeout: 5 * time.Second,
MaxRetries: 0,
CircuitBreaker: network.CircuitBreakerConfig{
Enabled: true,
FailureThreshold: 3,
SuccessThreshold: 1,
Timeout: time.Hour,
},
})
ctx := context.Background()
// Three consecutive failed requests open the breaker.
for i := 0; i < 3; i++ {
_, _, err := client.Get(ctx, server.URL, nil)
assert.Error(t, err)
}
// The next request is rejected because the breaker is open.
_, _, err := client.Get(ctx, server.URL, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "circuit breaker is open")
}