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

License checker feature #501

Merged
merged 26 commits into from Nov 5, 2023
Merged

License checker feature #501

merged 26 commits into from Nov 5, 2023

Conversation

josieang
Copy link
Collaborator

@josieang josieang commented Aug 21, 2023

TODO:

Name: "experimental-all-packages",
Usage: "when json output is selected, prints all packages",
},
&cli.BoolFlag{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in the json output when the licenses is specified without an allowlist, we want it to print all packages (and their licenses) by default. If that's the case, then I'm not sure how the all-packages and licenses flags should interact.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we do have a all-packages flag, maybe licenses (without allowlist) shouldn't print all packages by default, and just fill in the field for packages that already exists? If someone wants full license info for everything, pass the all-packages flag.

@oliverchang thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure I understand the alternatives here, is it:

  1. --licenses (without --all-packages) will just ignore all Unknown licenses?
  2. --licenses implies --all-packages ?

This is complicated by the behaviour for when an allow-list is passed, because to be consistent with the vulnerability scanning behaviour, it makes more sense for --licenses <ALLOWLIST> to not imply --all-packages.

Thinking about this a bit more, all packages should in fact have a license right? (as opposed to vulnerabilities). We just consider "Unknown" to be a license that's worthwhile reporting too, because from a license compliance perspective it's just as important.

From that perspective, 2. above seems to make more sense to me. Not necessarily because it implies --all-packages for the sake of it, but rather that we do have something worthwhile to report for all packages when it comes to licenses.

Copy link
Collaborator Author

@josieang josieang Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay i see.

This is complicated by the behaviour for when an allow-list is passed, because to be consistent with the vulnerability scanning behaviour it makes more sense for --licenses to not imply --all-packages.

Thinking about this, I think rather than thinking that --licenses implies --all-packages, we can think of how --licenses with an empty allowlist means every license a violation, which means all packages should be printed in the json output.

This is the solution I'm tending towards:

  1. --all-packages has no effect on the table output. It will always be only the vulnerabilities in the vulns table, and if licenses is specified (regardless of allowlist), all the licenses and their counts in addition to the vulns table.
  2. in json output --licenses=<allowlist> prints only packages with license violations or packages with vulns. If allowlist is empty then all packages are violating licenses (the licenses field and license_violations field will be populated for every package)
  3. in json output --licenses=<allowlist> with --all-packages prints all packages whether they have a vuln or license violation or not. The license_violations field is populated for every package that has a violation, license field populated for every package.

thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON output makes sense to me.

Can you clarify what happens in the table output when --licenses is passed in with an allowlist?

Would the packages that are violating the allowlist be shown individually? or only a list of violating licenses in a summary format?

Copy link
Collaborator Author

@josieang josieang Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was imagining that it would be the same as if there was no allowlist specified, it would have a a row per license and a column with count. If we did want to do something, we could add a column in the table when an allowlist is specified like 'violation' column with value either true or false.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and to clarify, all licenses, including the UNKNOWN license would be in that table whether allowlist is specified or not.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would a user know what package violated the allowlist without having to parse the JSON?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they wouldn't.

my thinking was listing all individual licenses, or all license violations here may clutter the view. But you do make a good point as the vulns table shows the individual vulns, but this table would only shows a summary. it doesn't mirror the behaviour of the vulns perfectly.

we did discuss in the meeting adding a summary/all-packages flag to toggle this, but iirc we concluded that we will wait for feedback first and add flags or change the table format in response to user feedback.

did you have a solution in mind?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry! my bad my memory is wrong. For the table view we agreed to list a summary when allowlist is empty, and violations when allowlist isn't empty.

pkg/models/licenses.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@another-rex another-rex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more comments!

internal/output/markdowntable.go Outdated Show resolved Hide resolved
internal/output/table.go Outdated Show resolved Hide resolved
internal/output/table.go Show resolved Hide resolved
pkg/depsdev/license.go Outdated Show resolved Hide resolved
pkg/osvscanner/osvscanner.go Outdated Show resolved Hide resolved
pkg/osvscanner/vulnerability_result.go Outdated Show resolved Hide resolved
@josieang
Copy link
Collaborator Author

I'm getting this error in my tests, the difference seems to be the CVSS severity value of the first vuln. Looking at https://api.osv.dev/v1/vulns/CVE-2022-37434 it looks like the CVSS is reported by the API.

    --- FAIL: TestRun/#03 (2.55s)
        main_test.go:443: stdout
             got:
            Scanning dir ./fixtures/sbom-insecure/
            Scanned /Users/josieang/fork/osv-scanner/cmd/osv-scanner/fixtures/sbom-insecure/alpine.cdx.xml as CycloneDX SBOM and found 15 packages
            Scanned /Users/josieang/fork/osv-scanner/cmd/osv-scanner/fixtures/sbom-insecure/postgres-stretch.cdx.xml as CycloneDX SBOM and found 136 packages
            +-------------------------------------+------+-----------+--------------------------------+------------------------------------+-------------------------------------------------+
            | OSV URL                             | CVSS | ECOSYSTEM | PACKAGE                        | VERSION                            | SOURCE                                          |
            +-------------------------------------+------+-----------+--------------------------------+------------------------------------+-------------------------------------------------+
            | https://osv.dev/CVE-2022-37434      | 9.8  | Alpine    | zlib                           | 1.2.10-r2                          | fixtures/sbom-insecure/alpine.cdx.xml           |
            | https://osv.dev/DLA-3022-1          |      | Debian    | dpkg                           | 1.18.25                            | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-v95c-p5hm-xq8f | 6    | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GO-2022-0274        |      |           |                                |                                    |                                                 |
            | https://osv.dev/GHSA-f3fp-gc8g-vw66 | 5.9  | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-g2j6-57v7-gm8c | 6.1  | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-m8cg-xc2p-r3fc | 2.5  | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-vpvm-3wq2-2wvm | 7    | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-p782-xgp4-8hr8 | 5.3  | Go        | golang.org/x/sys               | v0.0.0-20210817142637-7d9622a276b7 | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GO-2022-0493        |      |           |                                |                                    |                                                 |
            | https://osv.dev/DLA-3012-1          |      | Debian    | libxml2                        | 2.9.4+dfsg1-2.2+deb9u6             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/DLA-3008-1          |      | Debian    | openssl                        | 1.1.0l-1~deb9u5                    | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/DLA-3051-1          |      | Debian    | tzdata                         | 2021a-0+deb9u3                     | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            +-------------------------------------+------+-----------+--------------------------------+------------------------------------+-------------------------------------------------+

             want:
            Scanning dir ./fixtures/sbom-insecure/
            Scanned %%/fixtures/sbom-insecure/alpine.cdx.xml as CycloneDX SBOM and found 15 packages
            Scanned %%/fixtures/sbom-insecure/postgres-stretch.cdx.xml as CycloneDX SBOM and found 136 packages
            +-------------------------------------+------+-----------+--------------------------------+------------------------------------+-------------------------------------------------+
            | OSV URL                             | CVSS | ECOSYSTEM | PACKAGE                        | VERSION                            | SOURCE                                          |
            +-------------------------------------+------+-----------+--------------------------------+------------------------------------+-------------------------------------------------+
            | https://osv.dev/CVE-2022-37434      |      | Alpine    | zlib                           | 1.2.10-r2                          | fixtures/sbom-insecure/alpine.cdx.xml           |
            | https://osv.dev/DLA-3022-1          |      | Debian    | dpkg                           | 1.18.25                            | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-v95c-p5hm-xq8f | 6    | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GO-2022-0274        |      |           |                                |                                    |                                                 |
            | https://osv.dev/GHSA-f3fp-gc8g-vw66 | 5.9  | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-g2j6-57v7-gm8c | 6.1  | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-m8cg-xc2p-r3fc | 2.5  | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-vpvm-3wq2-2wvm | 7    | Go        | github.com/opencontainers/runc | v1.0.1                             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GHSA-p782-xgp4-8hr8 | 5.3  | Go        | golang.org/x/sys               | v0.0.0-20210817142637-7d9622a276b7 | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/GO-2022-0493        |      |           |                                |                                    |                                                 |
            | https://osv.dev/DLA-3012-1          |      | Debian    | libxml2                        | 2.9.4+dfsg1-2.2+deb9u6             | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/DLA-3008-1          |      | Debian    | openssl                        | 1.1.0l-1~deb9u5                    | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            | https://osv.dev/DLA-3051-1          |      | Debian    | tzdata                         | 2021a-0+deb9u3                     | fixtures/sbom-insecure/postgres-stretch.cdx.xml |
            +-------------------------------------+------+-----------+--------------------------------+------------------------------------+-------------------------------------------------+
            ```

@josieang josieang force-pushed the licenses branch 2 times, most recently from 6b582a8 to e741c98 Compare October 26, 2023 20:58
osv.BatchedQuery

We are planning to create deps.dev queries from these packages. Rather
than use the osv.BatchedQuery type to construct deps.dev queries from,
create a new type Package that includes package information and source
information.
another-rex pushed a commit that referenced this pull request Nov 1, 2023
this is in preparation for the license scanning feature. the queries are
structured around making requests to the osv API, we also will want to
make requests to the deps.dev api.
#501
@codecov-commenter
Copy link

codecov-commenter commented Nov 1, 2023

Codecov Report

Merging #501 (288bef9) into main (4aed961) will decrease coverage by 0.04%.
The diff coverage is 78.89%.

@@            Coverage Diff             @@
##             main     #501      +/-   ##
==========================================
- Coverage   78.82%   78.78%   -0.04%     
==========================================
  Files          78       79       +1     
  Lines        5511     5742     +231     
==========================================
+ Hits         4344     4524     +180     
- Misses        990     1031      +41     
- Partials      177      187      +10     
Files Coverage Δ
internal/output/markdowntable.go 100.00% <100.00%> (+27.27%) ⬆️
pkg/reporter/table_reporter.go 100.00% <100.00%> (ø)
pkg/osvscanner/vulnerability_result.go 81.33% <92.50%> (+2.90%) ⬆️
pkg/models/results.go 77.58% <61.53%> (-4.64%) ⬇️
cmd/osv-scanner/main.go 82.35% <79.31%> (+0.09%) ⬆️
internal/output/table.go 81.39% <90.00%> (+5.63%) ⬆️
pkg/osvscanner/osvscanner.go 54.34% <74.57%> (+1.21%) ⬆️
pkg/depsdev/license.go 57.89% <57.89%> (ø)

... and 1 file with indirect coverage changes

Copy link
Collaborator

@another-rex another-rex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good! Added some comments.

cmd/osv-scanner/json Outdated Show resolved Hide resolved
cmd/osv-scanner/main.go Outdated Show resolved Hide resolved
cmd/osv-scanner/main.go Show resolved Hide resolved
cmd/osv-scanner/osv-scanner Outdated Show resolved Hide resolved
cmd/osv-scanner/main.go Outdated Show resolved Hide resolved
cmd/osv-scanner/main_test.go Show resolved Hide resolved
internal/output/table.go Outdated Show resolved Hide resolved
pkg/osvscanner/osvscanner.go Outdated Show resolved Hide resolved
pkg/osvscanner/osvscanner.go Outdated Show resolved Hide resolved
@josieang
Copy link
Collaborator Author

josieang commented Nov 3, 2023

Ah I see, looks like we might need to update the linter and the go version in the checks. errors.Join was added in go 1.20.

josieang added a commit to josieang/osv-scanner that referenced this pull request Nov 3, 2023
I want to use errors.Join in the following PR:
- License checker feature google#501

It is a method added in go 1.20.
josieang added a commit that referenced this pull request Nov 3, 2023
I want to use errors.Join in the following PR:
- License checker feature #501

It is a method added in go 1.20.
cmd/osv-scanner/main.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@another-rex another-rex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@josieang josieang merged commit a85d675 into google:main Nov 5, 2023
8 checks passed
@josieang josieang mentioned this pull request Nov 21, 2023
case errors.Is(err, osvscanner.OnlyUncalledVulnerabilitiesFoundErr):
// TODO: Discuss whether to have a different exit code
// now that running call analysis is not default.
return 0b0010 // 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we're complicating the return codes here by having so many cases and different error types. If users do need to distinguish these, this should be done through another mechanism (e.g. the JSON output or models.VulnerabilityResults output).

I suggest we just return 1 if there is any error at all.

@G-Rath @another-rex

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yyeaahhh I'd say for now best to keep it simple and find out if people have a strong need for them - as you've eluded to @oliverchang, I think as well the scanner will grow to a point that fine-grain errors should be handled via e.g. JSON just because of all the different facets

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to return 0 or 1 here and sync with @another-rex so that these changes can be done alongside his changes to make call analysis return 0 or 1 as well.

I may not get to implementing error details in the models.VulnerabilityResults output soon but I'll file an issue for this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Note that I don't think #675 is necessary -- all the data is there in models.VulnerabilityResults already.


return vulnerabilityResults, nil
switch {
case !vuln && !onlyUncalledVuln && !licenseViolation:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning to this :) Sorry I didn't get a chance to review this earlier.

Similar to my other comment, this is getting very complicated. I suggest we just fold this into a single VulnerabilitiesFoundErr if there are any errors (including license errors).

If a user wants to distinguish between these errors more, they can just look at the models.VulnerabilityResults result.

@another-rex

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.

None yet

6 participants