Skip to content
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

Create fix pull request - NuGet & .NET #453

Merged
merged 28 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
510065e
added new package handler for nuget
eranturgeman Aug 21, 2023
7643aa0
added new package handler for nuget
eranturgeman Aug 21, 2023
a69f7fb
Merge branch 'dev' of https://github.com/jfrog/frogbot into nuget-cre…
eranturgeman Aug 28, 2023
05d3960
adding log for debug and tests for nuget
eranturgeman Aug 28, 2023
c047edc
adding test case for nuget
eranturgeman Aug 28, 2023
3b5aa8f
updating go.mod
eranturgeman Aug 28, 2023
99ea680
comment removal
eranturgeman Aug 28, 2023
167f669
removing comment & updating README
eranturgeman Aug 28, 2023
251c5d1
fixed TestPackageTypeFromScan test for nuget
eranturgeman Aug 29, 2023
5a8b2c3
Merge branch 'dev' of https://github.com/jfrog/frogbot into nuget-cre…
eranturgeman Aug 29, 2023
c6002b3
Merge branch 'dev' into nuget-create-fix-pull-request
eranturgeman Aug 29, 2023
70aea44
updated go.mod and added files for nuget testdata
eranturgeman Aug 29, 2023
2854439
Merge branch 'nuget-create-fix-pull-request' of https://github.com/er…
eranturgeman Aug 29, 2023
b0dee58
deleting comment
eranturgeman Aug 29, 2023
43c97c8
fixed PR issues
eranturgeman Aug 30, 2023
ff9d015
Merge branch 'dev' into nuget-create-fix-pull-request
eranturgeman Aug 30, 2023
0b9540b
fixing test
eranturgeman Aug 30, 2023
ec68771
Merge branch 'dev' of https://github.com/jfrog/frogbot into nuget-cre…
eranturgeman Aug 30, 2023
1b53283
Merge branch 'nuget-create-fix-pull-request' of https://github.com/er…
eranturgeman Aug 30, 2023
ad95aa8
removing redundant log.Debug
eranturgeman Aug 30, 2023
3829235
minor fix
eranturgeman Aug 30, 2023
600bab0
updating README
eranturgeman Aug 30, 2023
d320238
Merge branch 'dev' of https://github.com/jfrog/frogbot into nuget-cre…
eranturgeman Aug 30, 2023
46254dc
updating method name
eranturgeman Aug 30, 2023
8f80f62
merging nuget updateDependency test into the general test
eranturgeman Aug 30, 2023
7a6082d
fixing updateDirectDependency to use the common flow + updating commo…
eranturgeman Aug 30, 2023
c5e63af
fixes and improvements
eranturgeman Aug 30, 2023
ec3efec
updating go.mod
eranturgeman Aug 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ Supported package management tools:

- Go
- Maven
- NuGet
- .NET
eranturgeman marked this conversation as resolved.
Show resolved Hide resolved
- npm
- Pip
- Pipenv
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
)

// replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20230824124821-a7f84a425af1
replace github.com/jfrog/jfrog-cli-core/v2 => github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20230830163227-4abdfd45829f

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20230830130057-df2d2a80b555
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -881,10 +881,10 @@ github.com/jfrog/froggit-go v1.13.4 h1:+pHq3iNkKFvojXCJ74sDV+UsV4Thsi03dsu36jkS7
github.com/jfrog/froggit-go v1.13.4/go.mod h1:0jRAaZZusaFFnITosmx6CA60SKryuoaCasJyUrP/c1s=
github.com/jfrog/gofrog v1.3.0 h1:o4zgsBZE4QyDbz2M7D4K6fXPTBJht+8lE87mS9bw7Gk=
github.com/jfrog/gofrog v1.3.0/go.mod h1:IFMc+V/yf7rA5WZ74CSbXe+Lgf0iApEQLxRZVzKRUR0=
github.com/jfrog/jfrog-cli-core/v2 v2.41.4 h1:+V35NN+UaKl6ZFSjAyZFZ4VijCgsORnGsHug02DROdE=
github.com/jfrog/jfrog-cli-core/v2 v2.41.4/go.mod h1:Mi3WFUzG2CU6tlLpGsMNRaKkhH/tIMuci4tjnPZ9S3M=
github.com/jfrog/jfrog-client-go v1.31.6 h1:uWuyT4BDm9s5ES6oDTBny9Gl6yf8iKFjcbmHSHQZrDc=
github.com/jfrog/jfrog-client-go v1.31.6/go.mod h1:icb00ZJN/mMMNkQduHDkzpqsXH9Flwi3f3COYexq3Nc=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20230830163227-4abdfd45829f h1:21oeyFquM/dHQwIr6g/sUk4I66t94/2Os7plYNoLEME=
github.com/jfrog/jfrog-cli-core/v2 v2.31.1-0.20230830163227-4abdfd45829f/go.mod h1:kaFzB3X83/jdzMcuGOYOaqnlz5MVTnDYt/asrOsCv18=
github.com/jfrog/jfrog-client-go v1.28.1-0.20230830130057-df2d2a80b555 h1:yaF5J4LNk+ws5+j+BFMJ43NMqpKuCtF7dUfCeGLttl8=
github.com/jfrog/jfrog-client-go v1.28.1-0.20230830130057-df2d2a80b555/go.mod h1:icb00ZJN/mMMNkQduHDkzpqsXH9Flwi3f3COYexq3Nc=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
Expand Down
23 changes: 17 additions & 6 deletions packagehandlers/commonpackagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func GetCompatiblePackageHandler(vulnDetails *utils.VulnerabilityDetails, detail
handler = &PythonPackageHandler{pipRequirementsFile: details.PipRequirementsFile}
case coreutils.Maven:
handler = &MavenPackageHandler{depsRepo: details.DepsRepo, ServerDetails: details.ServerDetails}
case coreutils.Nuget:
handler = &NugetPackageHandler{}
default:
handler = &UnsupportedPackageHandler{}
}
Expand All @@ -44,18 +46,27 @@ func (cph *CommonPackageHandler) UpdateDependency(vulnDetails *utils.Vulnerabili
impactedPackage := strings.ToLower(vulnDetails.ImpactedDependencyName)
commandArgs := []string{installationCommand}
commandArgs = append(commandArgs, extraArgs...)
operator := vulnDetails.Technology.GetPackageOperator()
fixedPackage := impactedPackage + operator + vulnDetails.SuggestedFixedVersion
commandArgs = append(commandArgs, fixedPackage)
return runPackageMangerCommand(vulnDetails.Technology.GetExecCommandName(), commandArgs)
versionOperator := vulnDetails.Technology.GetPackageVersionOperator()
fixedPackageArgs := getFixedPackage(impactedPackage, versionOperator, vulnDetails.SuggestedFixedVersion)
commandArgs = append(commandArgs, fixedPackageArgs...)
return runPackageMangerCommand(vulnDetails.Technology.GetExecCommandName(), vulnDetails.Technology.ToString(), commandArgs)
}

func runPackageMangerCommand(commandName string, commandArgs []string) error {
func runPackageMangerCommand(commandName string, techName string, commandArgs []string) error {
fullCommand := commandName + " " + strings.Join(commandArgs, " ")
log.Debug(fmt.Sprintf("Running '%s'", fullCommand))
output, err := exec.Command(commandName, commandArgs...).CombinedOutput() // #nosec G204
if err != nil {
return fmt.Errorf("%s command failed: %s\n%s", fullCommand, err.Error(), output)
return fmt.Errorf("failed to update %s dependency: '%s' command failed: %s\n%s", techName, fullCommand, err.Error(), output)
}
return nil
}

// Returns the updated package and version as it should be run in the update command:
// If the package manager expects a single string (example: <packName>@<version>) it returns []string{<packName>@<version>}
// If the command args suppose to be seperated by spaces (example: <packName> -v <version>) it returns []string{<packName>, "-v", <version>}
func getFixedPackage(impactedPackage string, versionOperator string, suggestedFixedVersion string) (fixedPackageArgs []string) {
fixedPackageString := strings.TrimSpace(impactedPackage) + versionOperator + strings.TrimSpace(suggestedFixedVersion)
fixedPackageArgs = strings.Split(fixedPackageString, " ")
return
}
27 changes: 27 additions & 0 deletions packagehandlers/nugetpackagehandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package packagehandlers

import (
"github.com/jfrog/frogbot/utils"
)

const dotnetPackageUpgradeExtraArg = "package"

type NugetPackageHandler struct {
CommonPackageHandler
}

func (nph *NugetPackageHandler) UpdateDependency(vulnDetails *utils.VulnerabilityDetails) error {
if vulnDetails.IsDirectDependency {
return nph.updateDirectDependency(vulnDetails)
}

return &utils.ErrUnsupportedFix{
PackageName: vulnDetails.ImpactedDependencyName,
FixedVersion: vulnDetails.SuggestedFixedVersion,
ErrorType: utils.IndirectDependencyFixNotSupported,
}
}

func (nph *NugetPackageHandler) updateDirectDependency(vulnDetails *utils.VulnerabilityDetails) (err error) {
return nph.CommonPackageHandler.UpdateDependency(vulnDetails, vulnDetails.Technology.GetPackageInstallationCommand(), dotnetPackageUpgradeExtraArg)
}
48 changes: 48 additions & 0 deletions packagehandlers/packagehandlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,27 @@ func TestUpdateDependency(t *testing.T) {
fixSupported: false,
},
},

// NuGet test cases
{
{
// This test case directs to non-existing directory. It only checks if the dependency update is blocked if the vulnerable dependency is not a direct dependency
vulnDetails: &utils.VulnerabilityDetails{
SuggestedFixedVersion: "1.1.1",
IsDirectDependency: false,
VulnerabilityOrViolationRow: formats.VulnerabilityOrViolationRow{Technology: coreutils.Nuget, ImpactedDependencyName: "snappier"},
},
fixSupported: false,
},
{
vulnDetails: &utils.VulnerabilityDetails{
SuggestedFixedVersion: "1.1.1",
IsDirectDependency: true,
VulnerabilityOrViolationRow: formats.VulnerabilityOrViolationRow{Technology: coreutils.Nuget, ImpactedDependencyName: "snappier"},
},
fixSupported: true,
},
},
}

for _, testBatch := range testCases {
Expand Down Expand Up @@ -550,3 +571,30 @@ func uniquePackageManagerChecks(t *testing.T, test dependencyFixTest) {
default:
}
}

func TestGetFixedPackage(t *testing.T) {
var testcases = []struct {
impactedPackage string
versionOperator string
suggestedFixedVersion string
expectedOutput []string
}{
{
impactedPackage: "snappier",
versionOperator: " -v ",
suggestedFixedVersion: "1.1.1",
expectedOutput: []string{"snappier", "-v", "1.1.1"},
},
{
impactedPackage: "json",
versionOperator: "@",
suggestedFixedVersion: "10.0.0",
expectedOutput: []string{"[email protected]"},
},
}

for _, test := range testcases {
fixedPackageArgs := getFixedPackage(test.impactedPackage, test.versionOperator, test.suggestedFixedVersion)
assert.Equal(t, test.expectedOutput, fixedPackageArgs)
}
}
2 changes: 1 addition & 1 deletion packagehandlers/pythonpackagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (py *PythonPackageHandler) handlePoetry(vulnDetails *utils.VulnerabilityDet
return
}
// Update Poetry lock file as well
return runPackageMangerCommand(coreutils.Poetry.GetExecCommandName(), []string{"update"})
return runPackageMangerCommand(coreutils.Poetry.GetExecCommandName(), coreutils.Poetry.ToString(), []string{"update"})
}

func (py *PythonPackageHandler) handlePip(vulnDetails *utils.VulnerabilityDetails) (err error) {
Expand Down
4 changes: 2 additions & 2 deletions scanrepository/scanrepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ var testPackagesData = []struct {
commandArgs: []string{"install"},
},
{
packageType: coreutils.Dotnet.ToString(),
commandName: "dotnet",
packageType: coreutils.Nuget.ToString(),
commandName: "nuget",
commandArgs: []string{"restore"},
},
{
Expand Down
2 changes: 2 additions & 0 deletions testdata/projects/nuget/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
14 changes: 14 additions & 0 deletions testdata/projects/nuget/nuget.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="snappier" Version="1.1.0" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions testdata/projects/nuget/obj/nuget.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"format": 1,
"restore": {
"/Users/erant/Desktop/jfrog/frogbot/testdata/projects/nuget/nuget.csproj": {}
},
"projects": {
"/Users/erant/Desktop/jfrog/frogbot/testdata/projects/nuget/nuget.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/Users/erant/Desktop/jfrog/frogbot/testdata/projects/nuget/nuget.csproj",
"projectName": "nuget",
"projectPath": "/Users/erant/Desktop/jfrog/frogbot/testdata/projects/nuget/nuget.csproj",
"packagesPath": "/Users/erant/.nuget/packages/",
"outputPath": "/Users/erant/Desktop/jfrog/frogbot/testdata/projects/nuget/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/Users/erant/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net6.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"dependencies": {
"snappier": {
"target": "Package",
"version": "[1.1.0, )"
}
},
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.413/RuntimeIdentifierGraph.json"
}
}
}
}
}
15 changes: 15 additions & 0 deletions testdata/projects/nuget/obj/nuget.csproj.nuget.g.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/Users/erant/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/Users/erant/.nuget/packages/</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.3.3</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="/Users/erant/.nuget/packages/" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions testdata/projects/nuget/obj/nuget.csproj.nuget.g.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
107 changes: 107 additions & 0 deletions testdata/projects/nuget/obj/project.assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"version": 3,
"targets": {
"net6.0": {
"Snappier/1.1.0": {
"type": "package",
"compile": {
"lib/net6.0/Snappier.dll": {
"related": ".xml"
}
},
"runtime": {
"lib/net6.0/Snappier.dll": {
"related": ".xml"
}
}
}
}
},
"libraries": {
"Snappier/1.1.0": {
"sha512": "ws78FoXdwY5rTwoSqpdl9HFpgSU4ih0byThG2+s+Bm1VSfyhkQxOdrV0aWQ1R1MptA8EwAcrnn3Mh0GkFSssxA==",
"type": "package",
"path": "snappier/1.1.0",
"files": [
".nupkg.metadata",
".signature.p7s",
"COPYING.txt",
"lib/net6.0/Snappier.dll",
"lib/net6.0/Snappier.xml",
"lib/net7.0/Snappier.dll",
"lib/net7.0/Snappier.xml",
"lib/netstandard2.0/Snappier.dll",
"lib/netstandard2.0/Snappier.xml",
"snappier.1.1.0.nupkg.sha512",
"snappier.nuspec"
]
}
},
"projectFileDependencyGroups": {
"net6.0": [
"snappier >= 1.1.0"
]
},
"packageFolders": {
"/Users/erant/.nuget/packages/": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/Users/erant/Desktop/jfrog/frogbot/testdata/projects/nuget/nuget.csproj",
"projectName": "nuget",
"projectPath": "/Users/erant/Desktop/jfrog/frogbot/testdata/projects/nuget/nuget.csproj",
"packagesPath": "/Users/erant/.nuget/packages/",
"outputPath": "/Users/erant/Desktop/jfrog/frogbot/testdata/projects/nuget/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/Users/erant/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net6.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"dependencies": {
"snappier": {
"target": "Package",
"version": "[1.1.0, )"
}
},
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.413/RuntimeIdentifierGraph.json"
}
}
}
}
Loading