Skip to content

fix(nodejs): ignore a malformed workspaces field instead of panicking - #11766

Open
lxcxjxhx wants to merge 1 commit into
aquasecurity:mainfrom
lxcxjxhx:fix/nodejs-workspaces-malformed
Open

lxcxjxhx wants to merge 1 commit into
aquasecurity:mainfrom
lxcxjxhx:fix/nodejs-workspaces-malformed

Conversation

@lxcxjxhx

Copy link
Copy Markdown
Contributor

Description

ParseWorkspaces converted the decoded workspaces field with unchecked type assertions. The field is decoded into any (types.go:16), so whatever a scanned repository's package.json contains reaches the parser, and a malformed one panicked from (*Parser).Parse — one file was enough to fail the whole scan.

// before
if pkgsWorkspaces, ok := ws["packages"]; ok {
    return xslices.Map(pkgsWorkspaces.([]any), func(workspace any) string {
        return workspace.(string)
    })
}
case []any:
    return xslices.Map(ws, func(workspace any) string {
        return workspace.(string)
    })

Four shapes triggered it:

"workspaces": {"packages": "packages/*"}      interface conversion: interface {} is string, not []interface {}
"workspaces": {"packages": {"a": "b"}}        interface conversion: interface {} is map[string]interface {}, not []interface {}
"workspaces": ["packages/*", 1]               interface conversion: interface {} is float64, not string
"workspaces": ["packages/*", {"a": "b"}]      interface conversion: interface {} is map[string]interface {}, not string

After

Both assertions are checked. A packages value that is not an array yields no workspaces, and an array keeps its string members and ignores the rest — the same treatment the file already gives an unknown shape: License.UnmarshalJSON falls back to an empty list (types.go:70), and a workspaces object without a packages key already returned nil.

The documented shapes are unchanged: "workspaces": ["packages/*", "backend"] and {"packages": ["packages/*", "plugins/*"]} produce exactly what they did before.

Tests

workspaces_test.go:

  • TestParseWorkspacesMalformed — the five malformed shapes above plus a nil element; each must not panic.
  • TestParseWorkspacesValid — both documented shapes, an object without packages, a bare string, and nil.
  • TestParserWorkspacesNeverPanics — runs NewParser().Parse on the package.json from the issue and checks the package is still read.

Red/green: with parse.go reverted, TestParseWorkspacesMalformed/packages_is_a_string fails with func (assert.PanicTestFunc) should not panic. With the fix, go test ./pkg/dependency/parser/nodejs/... passes for all five packages (bun, npm, packagejson, pnpm, yarn); gofmt -l and go vet are clean.

Fixes #11765.

`ParseWorkspaces` converted the decoded `workspaces` value with unchecked type
assertions, and the field is decoded into `any`, so whatever a scanned
repository's package.json contains reaches it:

    "workspaces": {"packages": "packages/*"}
    "workspaces": {"packages": {"a": "b"}}
    "workspaces": ["packages/*", 1]
    "workspaces": ["packages/*", {"a": "b"}]

Each of those panicked with an interface conversion error, from
`(*Parser).Parse`, so a single package.json was enough to fail the scan.

Both assertions are now checked. `packages` that is not an array yields no
workspaces, and an array keeps its string members and drops the rest, matching
how the rest of the file treats an unknown shape (the License field already
falls back to an empty list).

Test: one case per malformed shape, the documented shapes unchanged, and an end
to end Parse of the document from the issue.
@lxcxjxhx
lxcxjxhx force-pushed the fix/nodejs-workspaces-malformed branch from f13161f to 481b841 Compare November 26, 2026 18:04

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(nodejs): a package.json with a malformed workspaces field panics the scan

1 participant