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

Feature: filter returned vulnerabilites and warnings to subtree of a … #862

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

dkcumming
Copy link

…target package

Passing -t followed by the name of a target package omits any vulnerabilities or warnings found that are not from packages in the subgraph formed from the target node. This is achieved by a depth first search for the vulnerable packages, from the target node.

Currently misses for the target name, and multiple targets returned for the target name are handled naively by ignoring flag and continuing with the full graph.

If -t is not provided, no change to complexity.
Providing -t increases complexity by $O(P \times (V' + E'))$ where $V'$ is the vertices in the subgraph (packages), $E'$ is the edges in the subgraph (dependencies), $P$ is the sum of all vulnerabilites and warnings found from the audit.

@Shnatsel
Copy link
Member

Thank you for the PR!

Could you clarify why such a feature is desirable? This introduces additional complexity to the codebase, and I'm not sure what are the use cases for it.

If we do decide to merge this, I'd also expect all TODOs to be done and tests to be added.

@tarcieri
Copy link
Member

We'd previously discussed this a bit over email, although I thought the intent was to enable it at the library-level, not necessarily something that would be in cargo-audit

/// Generate a report for the given advisory database and lockfile
pub fn generate(db: &Database, lockfile: &Lockfile, settings: &Settings) -> Self {
/// Generate a report for the given advisory database and lockfile, optional narrowing of subgraph from target package
pub fn generate(db: &Database, lockfile: &Lockfile, settings: &Settings, tree:&dependency::Tree, target:Option<&Package>) -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

There are already two types for carrying additional state so you don't have to add parameters to this method: Settings and its Settings::query field which contains a database::Query.

I would think you could access the dependency tree via the Lockfile, and this feature could potentially set some option on the database::Query.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, this makes more sense. I will do a refactor using Query

@dkcumming
Copy link
Author

Hi @Shnatsel, the motivation for the feature that auditing large packages can result in a large amount of vulnerabilities being output. Sometimes it is beneficial to isolate a particular part and see what vulnerabilities exist for that component of the package. Previously when auditing a large code base I was using cargo audit in combination with cargo tree to determine if the returned vulnerabilities were dependent in the subtree of a target package, but it felt appropriate to add the feature to cargo audit directly.

…target package

Passing `-t` followed by the name of a target package omits any vulnerabilities or warnings found that are not from packages in the subgraph formed from the target node. This is achieved by a depth first search for the vulnerable packages, from the target node.

Currently misses for the target name, and multiple targets returned for the target name are handled naively by ignoring flag and continuing with the full graph.

If `-t` is not provided, no change to complexity.
Providing `-t` increases complexity by $O(P \times (V' + E'))$ where $V'$ is the vertices in the subgraph (packages), $E'$ is the edges in the subgraph (dependencies), $P$ is the sum of all vulnerabilites and warnings found from the audit.
…s` occurs in `query_vulnerabilities`.

`Warnings` that are `Yanked` are still occuring in `check_for_yanked`.
- Accepts target package identifier, and validates correct name, version, source.
- Filters packages in lockfile by the name, version, source looking for unique package matching target.
- If no package matches, the identifier is retuned with the error.
- If multiple packages could match the identifier, then the provided identifier and the possible targets are returned with the error.
- If a unique target is found the package is returned.
@dkcumming
Copy link
Author

dkcumming commented May 5, 2023

Hi @Shnatsel @tarcieri,
Sorry for the long time between commits. I have had other work. I implemented some solutions to the requested changes, however this push is to get some feedback from you. Some details of this push are:

Features:

  • Refactored the parameters to make use of query.
  • Added feedback to user if target package doesn't exist, or multiple packages match the identifier. I had a look at cargo tree -p package for this as they had nice feedback for this. I pulled and edited some of the code (credited), extending the functionality useful for this case and stripping what wasn't relevant. But as it is now, The user provides a package identifier as a combination of name, version, url.
    If no package matches the identifier then the user receives
    error: No target package found matching identifier: not-there
    And if multiple packages match the identifier then the user receives
    error: Multiple packages found matching identifier: hydra-dx-math
    These packages were found that could match:
        https://crates.io/foo#package-name@1.2.3
        https://crates.io/foo#package-name@1.2.5
    

These strings that are returned are able to be copy pasted for the next cargo audit -t command and will parse correctly.

Known Problems:

  • The location of the files is not in final place. As I was moving them I thought it better to get some feedback from you.
  • Formatting of error messages can be improved, just wanted to see if you had anything to say on this.
  • I expect there is some sub optimal rust code in here, I am not an expert rust developer by any means, so feedback is appreciated.
  • The cargo tree -p method does not support if people screw up the versioning. For example if name, version, url are the same but the commit at url is different. I haven't looked into it deeper if that is intended or not.
  • The tree does not print from the target package node. I missed this as I only use the json implementation, but I will implement it.

Let me know what you think when you get some time. Thank you.

Comment on lines 32 to 40

// ! Package identifiers.
// !
// ! Adapted from Cargo's `package_id_spec.rs`:
// !
// ! <https://github.com/rust-lang/cargo/blob/master/src/cargo/core/package_id_spec.rs>
// !
// ! Copyright (c) 2014 The Rust Project Developers
// ! Licensed under the same terms as the `cargo-lock` crate: Apache 2.0 + MIT
Copy link
Author

Choose a reason for hiding this comment

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

This does not belong here, but I was wondering what structure you would recommend.

Running `cargo audit` alone, or with `-t multi-vulns` will show both vulnerabilities.
Running `cargo audit` with `-t base64` will show the `base64` vulnerability, and omit the `chrono` branch with `time` vulnerability.
Running `cargo audit` with `-t chrono`, or `-t time` will show the `time` vulnerability, and omit the `base64` vulnerability.
@dkcumming
Copy link
Author

Further progression. I refactored and added test cases to show the restriction of output for different branches. These can be run in the from the test directory like so:

  • No filtering
    multi_vulns$ ../../../../target/debug/cargo-audit audit -t multi_vuln or
    multi_vulns$ ../../../../target/debug/cargo-audit audit
    -Filter base64 branch
    multi_vulns$ ../../../../target/debug/cargo-audit audit -t base64
    -Filter chrono branch
    multi_vulns$ ../../../../target/debug/cargo-audit audit -t chrono or
    multi_vulns$ ../../../../target/debug/cargo-audit audit -t time (for the time dependency vuln directly)

Previously I thought there was an issue with the layout when printing the tree "The tree does not print from the target package node. I missed this as I only use the json implementation, but I will implement it.". After using this a few times I realised the current output is correct as is.

@Shnatsel @tarcieri
Are you able to provide feedback, or what you would need to see for a merge?
This change has been very useful for RV with audits, and we believe is likely useful to others who use cargo audit, it would be great to make it available.

@tarcieri
Copy link
Member

I worked through reviewing a rather large backlog of PRs this weekend and didn't quite get to this one. Maybe next weekend.

@dkcumming
Copy link
Author

No problem, I see that some CI tests failed. Looks like related to the lockfile. Is that related to anything on my end that I need to address?

@tarcieri
Copy link
Member

You can try rebasing

@dkcumming
Copy link
Author

I synced the main branch with upstream/main. The feature branch was already merged with the upstream/main. Is this what you meant?

@ACassimiro
Copy link

@Shnatsel @tarcieri I would just like to leave a ping here and say that this was a really helpful feature for going through specific packages in a huge repository. It saved me quite a bit of effort.

@tarcieri
Copy link
Member

I should have some time this weekend to take a look

@dkcumming
Copy link
Author

@tarcieri If there would be any benefit from a meeting to go over anything, I'm available pretty much anytime weekend or weekday for a meeting.

@tarcieri
Copy link
Member

tarcieri commented Jul 1, 2023

@dkcumming can you rebase? that should fix the test failures

@dkcumming
Copy link
Author

@tarcieri Should be updated now

@tarcieri
Copy link
Member

tarcieri commented Jul 1, 2023

@dkcumming looks like there's a test failure

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

4 participants