go-dep-parser icon indicating copy to clipboard operation
go-dep-parser copied to clipboard

feat(nuget): Parse deps specified as PackageReference in .csproj files

Open Sjord opened this issue 3 years ago • 15 comments

C# project files, with the extension .csproj, are XML files that can specify project dependencies in <PackageReference> tags.

See also:

  • https://github.com/aquasecurity/trivy/issues/2668
  • https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files

Sjord avatar Aug 08 '22 09:08 Sjord

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Aug 08 '22 09:08 CLAassistant

From the CLA:

You represent that each of Your Contributions is Your original creation

Well, I copied pkg/nuget/config to pkg/nuget/csproj and modified things to get it to work on .csproj files. I guess that's OK, right?

Sjord avatar Aug 08 '22 09:08 Sjord

Well, I copied pkg/nuget/config to pkg/nuget/csproj and modified things to get it to work on .csproj files. I guess that's OK, right?

Yes, thanks for signing.

@DmitriyLewen Could you review this PR?

knqyf263 avatar Aug 08 '22 12:08 knqyf263

I now put this in the nuget directory, but I later saw that there is also a dotnet directory. Is csproj correct in the nuget dir, or does dotnet make more sense?

Sjord avatar Aug 12 '22 08:08 Sjord

LGTM.

Is csproj correct in the nuget dir, or does dotnet make more sense?

I think nuget is okay

DmitriyLewen avatar Aug 18 '22 09:08 DmitriyLewen

@knqyf263 This has been reviewed, could you merge this?

Sjord avatar Aug 29 '22 07:08 Sjord

The result can be incomplete since it extracts only packages having exact versions, right? We're currently discussing whether we should display these incomplete results or encourage users to generate a lock file. If we show any result, it looks like we properly determined installed packages, but it is not the case actually. It can confuse users. Any thoughts? Please correct me if my above assumption is wrong.

knqyf263 avatar Sep 01 '22 15:09 knqyf263

Yes, this is an opportunistic approach where we can verify some dependencies, but in some cases determining the exact version is not possible. I think this is preferable to not scanning dependencies at all, but I agree this may give false confidence. If a lock file is present, that should be scanned instead. If no lock file is present, it would be a good idea to report that to the user.

As a security consultant, I use trivy to scan my client's projects. Finding any vulnerable dependency allows me to point the client to the importance of keeping packages up to date, and advice them to use a lock file. So completeness is not very important to me, as I use this to indicate a general problem instead of finding which packages need updating.

Sjord avatar Sep 06 '22 08:09 Sjord

Any chance this gets merged? As it is, it does have some limitations, but i would split the implementation to 2 phases, Optimistic and Deterministic. At the moment we have nothing in the SBOM, and with this implementation, we can at least get most of the package names.

bjuraga avatar Nov 17 '22 21:11 bjuraga

Any update on this?

afrischk avatar Mar 15 '23 15:03 afrischk

Any update on this?

Might be better to use the *.deps.json from your bin folder. We have had a great success in doing so, and that file contains the actual dependencies instead of the requested ones.

bjuraga avatar Mar 15 '23 15:03 bjuraga

This holds true for .NET Core and .NET 5+. I was hoping to be able to use Trivy on an older .NET project with no .deps.json available but PackageReference in .csproj files.

afrischk avatar Mar 15 '23 15:03 afrischk

Ok, I found a solution. After reading the comment from @knqyf263 carefully I noticed 'encourage users to generate a lock file'. I read through microsofts docs about packages.lock.json and added a Directory.Build.props file with the following contents to the root directory of my solution.

<Project>
 <PropertyGroup>
   <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
 </PropertyGroup>
</Project>

All subprojects are now generating a packages.lock.json after a NuGet restore and Trivy happily parses them :-) I hope this is helpful for anybody having the same issue.

afrischk avatar Mar 16 '23 08:03 afrischk

Dear Team,

Any news about this ? Are you planning to merge it ?

Thank you in advance

itrf98 avatar Dec 21 '23 12:12 itrf98

@itrf98 have you read @bjuraga , @knqyf263 and my comment about the json files? Is this a solution for you?

afrischk avatar Jan 01 '24 08:01 afrischk