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

more explicit build dependency handling #736

Merged
merged 4 commits into from
Jul 17, 2024

Conversation

thillux
Copy link
Contributor

@thillux thillux commented Jun 28, 2024

I was wondering, why pkg-config is listed in my SBOM.

SBOMs should IMHO only contain dependencies which are not only used during build or development. It would also be possible to check against DependencyKind::Normal.

@thillux thillux requested a review from a team as a code owner June 28, 2024 13:24
@Shnatsel
Copy link
Contributor

I don't think there's a one-size-fits-all solution to this. So I don't think excluding them unconditionally is a good idea.

However, what we should do is clearly indicate that these are build-time dependencies. We can set scope = "excluded" for build dependencies, which would indicate that they are not reachable at runtime. I'd be happy to accept a PR with that.

We could also consider adding a command-line option to exclude build dependencies, if them being explicitly marked as such is not sufficient. But we would need a really strong motivation to add yet another configuration knob.

Signed-off-by: Markus Theil <theil.markus@gmail.com>
Signed-off-by: Markus Theil <theil.markus@gmail.com>
@thillux
Copy link
Contributor Author

thillux commented Jul 9, 2024

I don't think there's a one-size-fits-all solution to this. So I don't think excluding them unconditionally is a good idea.

However, what we should do is clearly indicate that these are build-time dependencies. We can set scope = "excluded" for build dependencies, which would indicate that they are not reachable at runtime. I'd be happy to accept a PR with that.

We could also consider adding a command-line option to exclude build dependencies, if them being explicitly marked as such is not sufficient. But we would need a really strong motivation to add yet another configuration knob.

Good suggestions. I'd like to have both and I'm currently working on it.

@thillux thillux changed the title also strip build dependencies from SBOM [WIP/Draft] more explicit build dependency handling Jul 9, 2024
@thillux
Copy link
Contributor Author

thillux commented Jul 9, 2024

@Shnatsel I hacked a first draft together to familarize myself with the code base.

Is the basic idea correct, to check if a package was used as a normal dependency somewhere in the dependency graph and set it to DependencyKind::Required and DependencyKind::Excluded otherwise?

Is there a easier/faster path to check this?

@thillux
Copy link
Contributor Author

thillux commented Jul 9, 2024

My current simple test-case looks like this:

[package]
name = "cargo-meta-tests"
version = "0.1.0"
edition = "2021"

[build-dependencies]
rand = "0.8.5"
rand-esdm = "0.1.4"

[dependencies]
rand = "0.8.5"

[dev-dependencies]
rand = "0.8.5"

@thillux
Copy link
Contributor Author

thillux commented Jul 9, 2024

After looking through my current SBOM, I realized, that my idea for exclusion is probably too simple. Dependencies, which are only used by build dependencies are not marked as excluded.

Shall we perform some additional pruning/indexing or just live with the simple CLI option to drop build dependencies on demand?

If we can settle on some common idea after my tests, I'll cleanup/rewrite the PR.

@Shnatsel
Copy link
Contributor

Shnatsel commented Jul 9, 2024

Shall we perform some additional pruning/indexing

This. I've already written this logic for cargo auditable, you can simply crib it from here: https://github.com/rust-secure-code/cargo-auditable/blob/38b37332dfa3601f90608c1565fbd6e63bd6b08a/auditable-serde/src/lib.rs#L230-L366

Signed-off-by: Markus Theil <theil.markus@gmail.com>
@thillux
Copy link
Contributor Author

thillux commented Jul 10, 2024

@Shnatsel after cribbing from cargo auditable and adding another graph traversal for indexing dependency kinds, this is ready now from my side.

@thillux thillux changed the title [WIP/Draft] more explicit build dependency handling more explicit build dependency handling Jul 10, 2024
@Shnatsel
Copy link
Contributor

Could you also add tests, to make sure this functionality doesn't break in the future?

@thillux
Copy link
Contributor Author

thillux commented Jul 11, 2024

Sure. I'll try tomorrow.

@thillux
Copy link
Contributor Author

thillux commented Jul 12, 2024

Two simple tests were added. Please have a look again.

@Shnatsel
Copy link
Contributor

Yep, that looks good. Thanks a lot!

Nix flake CI is failing, but none of the current maintainers know or use Nix, and it's been effectively unmaintained for a while. I think we should just go ahead and drop it.

@Shnatsel
Copy link
Contributor

That's a pretty large Cargo.lock for the test crate. It would be pretty difficult to debug when something fails, and presents a rather large supply chain attack surface.

Could you make a minimal Cargo.lock file for the tests? It's likely that cargo auditable has some kind of test fixtures for this already. That should also fix the Nix flake CI.

Everything else looks good. Thanks a lot!

@thillux
Copy link
Contributor Author

thillux commented Jul 14, 2024

Ok. I'll try tomorrow.

@thillux thillux force-pushed the strip-build-deps branch 2 times, most recently from eda8764 to c1f0c32 Compare July 15, 2024 12:39
Signed-off-by: Markus Theil <theil.markus@gmail.com>
@thillux
Copy link
Contributor Author

thillux commented Jul 15, 2024

Done. Thanks for the hint to cargo auditable.

@Shnatsel Shnatsel merged commit 14af585 into CycloneDX:main Jul 17, 2024
14 checks passed
@Shnatsel
Copy link
Contributor

Unfortunately I have to revert this because it causes an infinite loop when there is a dependency cycle. A reproducing example can be found in #750

@Shnatsel
Copy link
Contributor

FWIW cargo auditable doesn't enter an infinite loop here.

@thillux
Copy link
Contributor Author

thillux commented Jul 17, 2024

Ok & sorry for this. I had faulty assumptions. My assumption was, that the metadata I get is already acyclic. I just have to add a cycle check to the graph search. I'll fix this in a new PR. I'll leave the command line argument as is, therefore your other PR will still apply.

@Shnatsel
Copy link
Contributor

No, there may be cycles through dev-dependencies. cargo auditable has correct handling for it, so if you just copy over its traversal algorithm it should work fine.
And please add a test for the cyclic graph too; IIRC cargo auditable has one already, so that could be a good starting point.

Thanks you for sticking with this! It is kind of a hard problem.

I'll leave the command line argument as is, therefore your other PR will still apply.

My other PR is not going to be mergeable. Git will make sure of it. So if you think that name is a good idea, I'd be happy if you just integrated that directly.

@thillux
Copy link
Contributor Author

thillux commented Jul 17, 2024

@Shnatsel I was able to reproduce the infinite loop and the fix was a one-liner in the end. I have to rebase/cherry-pick now and open a new PR tomorrow.

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.

2 participants