fix(proxy): serve escaped npm scopes and unscanned packages

npm and pnpm request scoped packages as @scope%2fname. Fiber's adaptor
answers 404 to an escaped slash, so a route middleware now rewrites the
scope separator before the adaptor runs.

With scanning off, the cache still waited 30 s for a scan result and then
refused the package. A scanner that is off is now treated as absent.

A scan that fails on infrastructure (timeout, network error, upstream 5xx)
no longer blocks the package until its entry expires. The package is
served and the scan is retried every scan_retry_interval. Set
security.fail_closed to refuse such packages.

The OSV api_url default and the docs named the host, which answers 404 to a
query. They now name https://api.osv.dev/v1/query, and a bare host is
completed with the query path.
This commit is contained in:
2026-11-23 00:31:47 +00:00
parent 65b2f23486
commit a313ac1096
17 changed files with 862 additions and 18 deletions
+15 -5
View File
@@ -13,6 +13,7 @@ import (
"github.com/lukaszraczylo/gohoarder/pkg/config"
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
"github.com/lukaszraczylo/gohoarder/pkg/scanner/scanerr"
"github.com/lukaszraczylo/gohoarder/pkg/uuid"
"github.com/rs/zerolog/log"
)
@@ -94,10 +95,7 @@ type OSVEvent struct {
// New creates a new OSV scanner
func New(cfg config.OSVConfig) *Scanner {
apiURL := cfg.APIURL
if apiURL == "" {
apiURL = defaultOSVAPIURL
}
cfg.APIURL = queryURL(cfg.APIURL)
timeout := cfg.Timeout
if timeout == 0 {
@@ -113,6 +111,18 @@ func New(cfg config.OSVConfig) *Scanner {
}
// Name returns the scanner name
// queryURL returns the OSV query endpoint. A bare host is what older docs
// listed as api_url, and it answers 404 to a POST, so the path is added.
func queryURL(apiURL string) string {
if apiURL == "" {
return defaultOSVAPIURL
}
if strings.TrimRight(apiURL, "/") == "" || !strings.Contains(strings.TrimPrefix(strings.TrimPrefix(apiURL, "https://"), "http://"), "/") {
return strings.TrimRight(apiURL, "/") + "/v1/query"
}
return apiURL
}
func (s *Scanner) Name() string {
return ScannerName
}
@@ -169,7 +179,7 @@ func (s *Scanner) Scan(ctx context.Context, registry, packageName, version strin
// Check status code
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("OSV API returned status %d: %s", resp.StatusCode, string(body))
return nil, &scanerr.StatusError{Service: "OSV API", Code: resp.StatusCode, Body: string(body)}
}
// Parse response
+46
View File
@@ -0,0 +1,46 @@
package osv
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/lukaszraczylo/gohoarder/pkg/config"
)
// A host-only api_url (the previously documented value) must still reach the
// query endpoint.
func TestScan_HostOnlyAPIURLUsesQueryEndpoint(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
_, _ = w.Write([]byte(`{}`))
}))
t.Cleanup(srv.Close)
s := New(config.OSVConfig{APIURL: srv.URL})
if _, err := s.Scan(context.Background(), "npm", "left-pad", "1.0.0", ""); err != nil {
t.Fatalf("Scan: %v", err)
}
if gotPath != "/v1/query" {
t.Errorf("request path = %q, want /v1/query", gotPath)
}
}
func TestScan_FullQueryURLIsKept(t *testing.T) {
var gotPath string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
_, _ = w.Write([]byte(`{}`))
}))
t.Cleanup(srv.Close)
s := New(config.OSVConfig{APIURL: srv.URL + "/v1/query"})
if _, err := s.Scan(context.Background(), "npm", "left-pad", "1.0.0", ""); err != nil {
t.Fatalf("Scan: %v", err)
}
if gotPath != "/v1/query" {
t.Errorf("request path = %q, want /v1/query", gotPath)
}
}