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
+29
View File
@@ -19,6 +19,9 @@ import (
"github.com/rs/zerolog/log"
)
// routePrefix is where the handler is mounted.
const routePrefix = "/npm/"
// Handler implements the NPM registry protocol
type Handler struct {
cache *cache.Manager
@@ -75,6 +78,32 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
// CanonicalPath turns the escaped scope separator npm and pnpm send
// (/npm/@scope%2fname) into the literal form (/npm/@scope/name). Fiber's
// net/http adaptor answers 404 to any escaped slash, so the rewrite must
// happen before the adaptor. Only the first escape in the first segment is
// rewritten, so a name cannot smuggle extra path segments.
func CanonicalPath(path string) string {
rest, ok := strings.CutPrefix(path, routePrefix)
if !ok {
return path
}
segment, tail, _ := strings.Cut(rest, "/")
if !strings.HasPrefix(segment, "@") {
return path
}
for _, escaped := range []string{"%2f", "%2F"} {
if scope, name, found := strings.Cut(segment, escaped); found {
canonical := routePrefix + scope + "/" + name
if rest != segment {
canonical += "/" + tail
}
return canonical
}
}
return path
}
// handleMetadata handles package metadata requests
func (h *Handler) handleMetadata(ctx context.Context, w http.ResponseWriter, r *http.Request, path string) {
url := h.upstream + path
+18
View File
@@ -79,3 +79,21 @@ func TestExtractTarballInfo_Scoped(t *testing.T) {
})
}
}
func TestCanonicalPath(t *testing.T) {
tests := []struct{ in, want string }{
{"/npm/@types%2fnode", "/npm/@types/node"},
{"/npm/@types%2Fnode", "/npm/@types/node"},
{"/npm/@types%2fnode/-/node-1.0.0.tgz", "/npm/@types/node/-/node-1.0.0.tgz"},
{"/npm/@types/node", "/npm/@types/node"},
{"/npm/express", "/npm/express"},
{"/npm/express/-/express-4.0.0.tgz", "/npm/express/-/express-4.0.0.tgz"},
{"/npm/@a%2f..%2fb", "/npm/@a/..%2fb"},
{"/go/@a%2fb", "/go/@a%2fb"},
}
for _, tt := range tests {
if got := CanonicalPath(tt.in); got != tt.want {
t.Errorf("CanonicalPath(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}