fix(trivy): unpack npm tarballs before scanning

Trivy does not read archives, so a bare npm tarball always scanned clean.
The scanner now extracts the tarball into node_modules/<name> in a temp
dir and runs trivy rootfs on it, so the node-pkg analyzer sees the
package.json. Extraction rejects absolute paths, parent segments and
links that leave the tree, and caps the unpacked size at 512 MiB. The
temp dir is removed after the scan.

PyPI wheels and Go module zips share the archive problem and need their
own extractors. This commit does not change them.
This commit is contained in:
2026-11-24 00:16:07 +00:00
parent 4870f243fc
commit 22f2a8dfb9
3 changed files with 304 additions and 5 deletions
+12 -4
View File
@@ -110,15 +110,23 @@ func (s *Scanner) Scan(ctx context.Context, registry, packageName, version strin
defer cancel()
}
// Determine scan type based on registry
scanType := s.determineScanType(registry, filePath)
target := filePath
if registry == "npm" {
// trivy does not unpack archives; the node-pkg analyzer needs an installed tree.
root, err := unpackNPM(filePath, packageName)
if err != nil {
return nil, fmt.Errorf("prepare npm package for trivy: %w", err)
}
defer func() { _ = os.RemoveAll(root) }()
target = root
}
// Build Trivy command
args := []string{
scanType,
"--format", "json",
"--quiet",
filePath,
target,
}
cmd := exec.CommandContext(ctx, "trivy", args...) // #nosec G204 -- trivy command with controlled arguments
@@ -166,7 +174,7 @@ func (s *Scanner) determineScanType(registry, filePath string) string {
switch registry {
case "npm":
return "fs" // Filesystem scan for npm packages
return "rootfs" // rootfs enables the node-pkg analyzer; fs reads lockfiles only
case "pypi":
return "fs" // Filesystem scan for Python packages
case "go":