Repository navigation
Description
Description
ParseWorkspaces converts the decoded workspaces field with unchecked type assertions, so a package.json whose workspaces has an unexpected shape panics and takes the whole scan down.
The field is decoded into any (pkg/dependency/parser/nodejs/packagejson/types.go:16), so whatever the scanned repository contains reaches the parser. Two places assume the shape:
// pkg/dependency/parser/nodejs/packagejson/parse.go:66
if pkgsWorkspaces, ok := ws["packages"]; ok {
return xslices.Map(pkgsWorkspaces.([]any), func(workspace any) string {
return workspace.(string) // <- panics if an element is not a string
})
}
case []any:
return xslices.Map(ws, func(workspace any) string {
return workspace.(string) // <- same
})The first assertion (pkgsWorkspaces.([]any)) is also unchecked: "workspaces": {"packages": "packages/*"} panics before any element is looked at.
Reproduction
Four shapes, all of which a package.json can contain:
map[string]any{"packages": "packages/*"} // packages is a string
map[string]any{"packages": map[string]any{"a": "b"}} // packages is an object
[]any{"packages/*", 1.0} // array holds a number
[]any{"packages/*", map[string]any{"a": "b"}} // array holds an objectpanic: interface conversion: interface {} is string, not []interface {}
ParseWorkspaces(...) parse.go:67
(*Parser).Parse(...) parse.go:50
The last frame matters: this is reached from Parser.Parse, i.e. scanning a single package.json is enough — no lock file, no monorepo layout needed.
Expected behaviour
The rest of the file already treats malformed input as "ignore it, keep parsing": License.UnmarshalJSON falls back to an empty list for an unknown shape (types.go:70), and the code already tolerates a workspaces object without a packages key. ParseWorkspaces should behave the same way: keep the string members it can use and ignore the rest, rather than panicking.
trivy fs / trivy repo on a repository that contains such a package.json should report the dependencies it can read.
Environment
- Trivy
mainatf94c2ef(also present in v0.76.x) - Go 1.23.4 / 1.27.0, Windows and Linux
- Found by reading the parser; reproduced with a unit test that calls
NewParser().Parseon the document above
Suggested fix
Replace both assertions with a checked assertion that skips non-string elements (and return nil when packages is not an array). A test per shape plus one that runs Parser.Parse end to end is enough to keep it from regressing.
Activity
github-actions commentedon Nov 26, 2026
Please see https://trivy.dev/docs/latest/community/contribute/issue/