-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(nodejs): detect direct dependencies when using
latest
version f…
…or files `yarn.lock` + `package.json` (#7110)
- Loading branch information
1 parent
4e54a7e
commit 54bb8bd
Showing
6 changed files
with
132 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"bytes" | ||
"io" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/samber/lo" | ||
|
@@ -127,7 +128,7 @@ func ignoreProtocol(protocol string) bool { | |
return false | ||
} | ||
|
||
func parseResults(patternIDs map[string]string, dependsOn map[string][]string) (deps []ftypes.Dependency) { | ||
func parseResults(patternIDs map[string]string, dependsOn map[string][]string) (deps ftypes.Dependencies) { | ||
// find dependencies by patterns | ||
for pkgID, depPatterns := range dependsOn { | ||
depIDs := lo.Map(depPatterns, func(pattern string, index int) string { | ||
|
@@ -269,14 +270,20 @@ func parseDependency(line string) (string, error) { | |
} | ||
} | ||
|
||
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependency, error) { | ||
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependency, map[string][]string, error) { | ||
lineNumber := 1 | ||
var pkgs []ftypes.Package | ||
var pkgs ftypes.Packages | ||
|
||
// patternIDs holds mapping between patterns and library IDs | ||
// patternIDs holds mapping between patterns and package IDs | ||
// e.g. ajv@^6.5.5 => [email protected] | ||
// This is needed to update dependencies from `DependsOn`. | ||
patternIDs := make(map[string]string) | ||
|
||
// patternIDs holds mapping between package ID and patterns | ||
// e.g. `@babel/[email protected]` => [`@babel/helper-regex@^7.0.0`, `@babel/helper-regex@^7.4.4`] | ||
// This is needed to compare package patterns with patterns from package.json files in `fanal` package. | ||
pkgIDPatterns := make(map[string][]string) | ||
|
||
scanner := bufio.NewScanner(r) | ||
scanner.Split(p.scanBlocks) | ||
dependsOn := make(map[string][]string) | ||
|
@@ -285,7 +292,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc | |
lib, deps, newLine, err := p.parseBlock(block, lineNumber) | ||
lineNumber = newLine + 2 | ||
if err != nil { | ||
return nil, nil, err | ||
return nil, nil, nil, err | ||
} else if lib.Name == "" { | ||
continue | ||
} | ||
|
@@ -298,6 +305,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc | |
Locations: []ftypes.Location{lib.Location}, | ||
}) | ||
|
||
pkgIDPatterns[pkgID] = lib.Patterns | ||
for _, pattern := range lib.Patterns { | ||
// e.g. | ||
// combined-stream@^1.0.6 => [email protected] | ||
|
@@ -310,13 +318,16 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc | |
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
return nil, nil, xerrors.Errorf("failed to scan yarn.lock, got scanner error: %s", err.Error()) | ||
return nil, nil, nil, xerrors.Errorf("failed to scan yarn.lock, got scanner error: %s", err.Error()) | ||
} | ||
|
||
// Replace dependency patterns with library IDs | ||
// Replace dependency patterns with package IDs | ||
// e.g. ajv@^6.5.5 => [email protected] | ||
deps := parseResults(patternIDs, dependsOn) | ||
return pkgs, deps, nil | ||
|
||
sort.Sort(pkgs) | ||
sort.Sort(deps) | ||
return pkgs, deps, pkgIDPatterns, nil | ||
} | ||
|
||
func packageID(name, version string) string { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
pkg/fanal/analyzer/language/nodejs/yarn/testdata/latest-version/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"dependencies": { | ||
"debug": "latest" | ||
}, | ||
"devDependencies" : { | ||
"js-tokens": "^9.0.0" | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
pkg/fanal/analyzer/language/nodejs/yarn/testdata/latest-version/yarn.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|
||
debug@latest: | ||
version "4.3.5" | ||
resolved "https://registry.npmjs.org/debug/-/debug-4.3.5.tgz" | ||
integrity sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg== | ||
dependencies: | ||
ms "2.1.2" | ||
|
||
js-tokens@^9.0.0: | ||
version "9.0.0" | ||
resolved "https://registry.npmjs.org/js-tokens/-/js-tokens-9.0.0.tgz" | ||
integrity sha512-WriZw1luRMlmV3LGJaR6QOJjWwgLUTf89OwT2lUOyjX2dJGBwgmIkbcz+7WFZjrZM635JOIR517++e/67CP9dQ== | ||
|
||
[email protected]: | ||
version "2.1.2" | ||
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz" | ||
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -354,6 +354,61 @@ func Test_yarnLibraryAnalyzer_Analyze(t *testing.T) { | |
}, | ||
}, | ||
}, | ||
{ | ||
name: "package uses `latest` version", | ||
dir: "testdata/latest-version", | ||
want: &analyzer.AnalysisResult{ | ||
Applications: []types.Application{ | ||
{ | ||
Type: types.Yarn, | ||
FilePath: "yarn.lock", | ||
Packages: types.Packages{ | ||
{ | ||
ID: "[email protected]", | ||
Name: "debug", | ||
Version: "4.3.5", | ||
Relationship: types.RelationshipDirect, | ||
Locations: []types.Location{ | ||
{ | ||
StartLine: 5, | ||
EndLine: 10, | ||
}, | ||
}, | ||
DependsOn: []string{ | ||
"[email protected]", | ||
}, | ||
}, | ||
{ | ||
ID: "[email protected]", | ||
Name: "js-tokens", | ||
Version: "9.0.0", | ||
Relationship: types.RelationshipDirect, | ||
Dev: true, | ||
Locations: []types.Location{ | ||
{ | ||
StartLine: 12, | ||
EndLine: 15, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ID: "[email protected]", | ||
Name: "ms", | ||
Version: "2.1.2", | ||
Indirect: true, | ||
Relationship: types.RelationshipIndirect, | ||
Locations: []types.Location{ | ||
{ | ||
StartLine: 17, | ||
EndLine: 20, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "happy path with alias rewrite", | ||
dir: "testdata/alias", | ||
|