-
Notifications
You must be signed in to change notification settings - Fork 32
Detect dependency locations at files #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f42f928
Detect dependency locations at files
attiasas 7d74b78
start impl npm dep location detection
attiasas 4be911e
Merge remote-tracking branch 'upstream/dev' into tech_detect_dep_loca…
attiasas 141a593
done npm impl
attiasas 56c0e19
fix tests
attiasas 1cbe7aa
add tests
attiasas 142a7ef
fix static tests
attiasas d44ce0c
fix tests
attiasas 9e7087b
done implement npm tech
attiasas ba605bb
CR view
attiasas 689d0d5
Merge remote-tracking branch 'upstream/dev' into tech_detect_dep_loca…
attiasas 2702438
fix static tests
attiasas 0f1f2ea
Merge remote-tracking branch 'upstream/dev' into tech_detect_dep_loca…
attiasas 8811062
Merge remote-tracking branch 'upstream/dev' into tech_detect_dep_loca…
attiasas e405f32
fix and add tests
attiasas fc87112
Merge remote-tracking branch 'upstream/dev' into tech_detect_dep_loca…
attiasas ac2b58b
Merge remote-tracking branch 'upstream/dev' into tech_detect_dep_loca…
attiasas 4f95fd6
Merge remote-tracking branch 'upstream/dev' into tech_detect_dep_loca…
attiasas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,93 @@ | ||
package npm | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/jfrog/jfrog-cli-security/utils/formats/sarifutils" | ||
"github.com/jfrog/jfrog-client-go/utils/io/fileutils" | ||
"github.com/owenrumney/go-sarif/v2/sarif" | ||
) | ||
|
||
const ( | ||
PackageJson = "package.json" | ||
) | ||
|
||
type NpmHandler struct{} | ||
|
||
func (nh *NpmHandler) GetTechDependencyLocations(directDependencyName, directDependencyVersion string, filesToSearch ...string) (locations []*sarif.Location, err error) { | ||
for _, file := range getFilesToSearch(filesToSearch...) { | ||
fileLocations, err := getDependencyLocations(file, directDependencyName, directDependencyVersion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
locations = append(locations, fileLocations...) | ||
} | ||
return | ||
} | ||
|
||
// getFilesToSearch returns the npm related files to search for the dependency | ||
// If no files are provided, the default is to search for package.json at the current directory | ||
func getFilesToSearch(filesToSearch ...string) (out []string) { | ||
if len(filesToSearch) == 0 { | ||
return []string{PackageJson} | ||
} | ||
for _, file := range filesToSearch { | ||
if strings.HasSuffix(strings.TrimSuffix(file, "/"), PackageJson) { | ||
out = append(out, file) | ||
} | ||
} | ||
return out | ||
} | ||
|
||
// getDependencyLocations returns the locations of the dependency in the file | ||
func getDependencyLocations(file, directDependencyName, dependencyVersion string) (locations []*sarif.Location, err error) { | ||
content, err := fileutils.ReadFile(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read file '%s': %v", file, err) | ||
} | ||
|
||
// Prepare regular expression to match all possible ways to specify a version. | ||
pattern := fmt.Sprintf(`"%s"\s*:\s*"([~^]?\d+(?:\.\d+)?(?:\.\d+)?)"`, regexp.QuoteMeta(directDependencyName)) | ||
|
||
// Compile the regex. | ||
re := regexp.MustCompile(pattern) | ||
|
||
// Split the contents into lines for processing. | ||
lines := strings.Split(string(content), "\n") | ||
|
||
for lineNumber, line := range lines { | ||
// Find all match locations in the line. | ||
matches := re.FindStringSubmatch(line) | ||
if matches != nil { | ||
detectedVersion := matches[1] // Extract detected version from match | ||
attiasas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Normalize the given dependency version by allowing optional ~ or ^ prefix | ||
if dependencyVersion != "" { | ||
allowedPrefixes := []string{"", "~", "^"} | ||
matchFound := false | ||
for _, prefix := range allowedPrefixes { | ||
if strings.HasPrefix(prefix+dependencyVersion, detectedVersion) { | ||
matchFound = true | ||
break | ||
} | ||
} | ||
if !matchFound { | ||
continue // Skip if the provided version does not match the detected version | ||
attiasas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
// Get the matched snippet | ||
matchIndex := re.FindStringIndex(line) | ||
matchedSnippet := line[matchIndex[0]:matchIndex[1]] | ||
|
||
// Rows and Cols are 1-indexed | ||
attiasas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
row := lineNumber + 1 | ||
startCol := matchIndex[0] + 1 | ||
locations = append(locations, sarifutils.CreateLocation(file, row, startCol, row, startCol+len(matchedSnippet), matchedSnippet)) | ||
} | ||
} | ||
|
||
return locations, nil | ||
} |
This file contains hidden or 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,78 @@ | ||
package npm | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/owenrumney/go-sarif/v2/sarif" | ||
"github.com/stretchr/testify/assert" | ||
|
||
securityTestUtils "github.com/jfrog/jfrog-cli-security/tests/utils" | ||
"github.com/jfrog/jfrog-cli-security/utils/formats/sarifutils" | ||
) | ||
|
||
var ( | ||
testDataDir = filepath.Join("..", "..", "tests", "testdata", "projects", "package-managers", "npm") | ||
) | ||
|
||
func TestNpmGetTechDependencyLocations(t *testing.T) { | ||
cleanUp := securityTestUtils.ChangeWDWithCallback(t, filepath.Join(testDataDir, "npm")) | ||
defer cleanUp() | ||
|
||
testCases := []struct { | ||
name string | ||
directDependencyName string | ||
directDependencyVersion string | ||
filesToSearch []string | ||
expectedLocations []*sarif.Location | ||
expectedError error | ||
}{ | ||
{ | ||
name: "dependency not found", | ||
directDependencyName: "json", | ||
filesToSearch: []string{filepath.Join("..", "npm-scripts", "package.json")}, | ||
}, | ||
{ | ||
name: "dependency all versions", | ||
directDependencyName: "json", | ||
filesToSearch: []string{ | ||
"package.json", | ||
filepath.Join("..", "npm-big-tree", "package.json"), | ||
filepath.Join("..", "npm-no-lock", "package.json"), | ||
}, | ||
expectedLocations: []*sarif.Location{ | ||
sarifutils.CreateLocation("package.json", 15, 5, 15, 20, "\"json\": \"9.0.6\""), | ||
sarifutils.CreateLocation(filepath.Join("..", "npm-no-lock", "package.json"), 12, 5, 12, 20, "\"json\": \"9.0.3\""), | ||
}, | ||
}, | ||
{ | ||
name: "dependency specific version", | ||
directDependencyName: "json", | ||
directDependencyVersion: "9.0.6", | ||
filesToSearch: []string{ | ||
"package.json", | ||
filepath.Join("..", "npm-scripts", "package.json"), | ||
filepath.Join("..", "npm-no-lock", "package.json"), | ||
}, | ||
expectedLocations: []*sarif.Location{ | ||
sarifutils.CreateLocation("package.json", 15, 5, 15, 20, "\"json\": \"9.0.6\""), | ||
}, | ||
}, | ||
{ | ||
name: "search at cwd (no files to search)", | ||
directDependencyName: "xml", | ||
expectedLocations: []*sarif.Location{ | ||
sarifutils.CreateLocation("package.json", 12, 5, 12, 19, "\"xml\": \"1.0.1\""), | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
nh := NpmHandler{} | ||
locations, err := nh.GetTechDependencyLocations(tc.directDependencyName, tc.directDependencyVersion, tc.filesToSearch...) | ||
assert.ElementsMatch(t, tc.expectedLocations, locations) | ||
assert.Equal(t, tc.expectedError, err) | ||
}) | ||
} | ||
} |
This file contains hidden or 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,17 @@ | ||
package sca | ||
|
||
import ( | ||
"github.com/owenrumney/go-sarif/v2/sarif" | ||
|
||
"github.com/jfrog/jfrog-cli-security/sca/npm" | ||
"github.com/jfrog/jfrog-cli-security/utils/techutils" | ||
) | ||
|
||
func GetTechDependencyLocations(tech techutils.Technology, directDependencyName, directDependencyVersion string, filesToSearch ...string) ([]*sarif.Location, error) { | ||
switch tech { | ||
case techutils.Npm: | ||
nh := npm.NpmHandler{} | ||
return nh.GetTechDependencyLocations(directDependencyName, directDependencyVersion, filesToSearch...) | ||
} | ||
return nil, nil | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.