mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-25 06:35:21 +00:00
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.
100 lines
2.9 KiB
Go
100 lines
2.9 KiB
Go
package npm
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestScopedTarballFilename verifies that scoped packages produce the correct
|
|
// upstream tarball filename. NPM registry expects:
|
|
//
|
|
// @scope/pkg v1.0.0 -> https://registry.npmjs.org/@scope/pkg/-/pkg-1.0.0.tgz
|
|
//
|
|
// The filename portion must NOT include the leading "@scope-" prefix.
|
|
func TestScopedTarballFilename(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
packageName string
|
|
version string
|
|
wantURL string
|
|
}{
|
|
{
|
|
name: "unscoped package",
|
|
packageName: "express",
|
|
version: "4.18.2",
|
|
wantURL: "https://registry.npmjs.org/express/-/express-4.18.2.tgz",
|
|
},
|
|
{
|
|
name: "scoped package",
|
|
packageName: "@types/node",
|
|
version: "20.0.0",
|
|
wantURL: "https://registry.npmjs.org/@types/node/-/node-20.0.0.tgz",
|
|
},
|
|
{
|
|
name: "scoped package with hyphen in name",
|
|
packageName: "@babel/preset-env",
|
|
version: "7.22.0",
|
|
wantURL: "https://registry.npmjs.org/@babel/preset-env/-/preset-env-7.22.0.tgz",
|
|
},
|
|
}
|
|
|
|
upstream := "https://registry.npmjs.org"
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
bareName := tt.packageName
|
|
if strings.HasPrefix(bareName, "@") {
|
|
if idx := strings.Index(bareName, "/"); idx >= 0 {
|
|
bareName = bareName[idx+1:]
|
|
}
|
|
}
|
|
tarballFilename := bareName + "-" + tt.version + ".tgz"
|
|
gotURL := fmt.Sprintf("%s/%s/-/%s", upstream, tt.packageName, tarballFilename)
|
|
if gotURL != tt.wantURL {
|
|
t.Errorf("got %q, want %q", gotURL, tt.wantURL)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExtractTarballInfo_Scoped(t *testing.T) {
|
|
// Sanity check that extractTarballInfo correctly parses scoped paths so
|
|
// the construction pipeline above is consistent end-to-end.
|
|
tests := []struct {
|
|
path string
|
|
wantPackage string
|
|
wantVersion string
|
|
}{
|
|
{"/@types/node/-/node-20.0.0.tgz", "@types/node", "20.0.0"},
|
|
{"/@babel/preset-env/-/preset-env-7.22.0.tgz", "@babel/preset-env", "7.22.0"},
|
|
{"/express/-/express-4.18.2.tgz", "express", "4.18.2"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.path, func(t *testing.T) {
|
|
gotPkg, gotVer := extractTarballInfo(tt.path)
|
|
if gotPkg != tt.wantPackage || gotVer != tt.wantVersion {
|
|
t.Errorf("extractTarballInfo(%q) = (%q,%q), want (%q,%q)",
|
|
tt.path, gotPkg, gotVer, tt.wantPackage, tt.wantVersion)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|