Skip to content

feat(docker): implement exclude paths functionality #4057

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

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

Conversation

tannerjones4075
Copy link

@tannerjones4075 tannerjones4075 commented Apr 15, 2025

Description:

Add support for excluding paths in Docker source scanning:

  • Add ExcludePaths field to Docker protobuf
  • Implement path exclusion logic in docker.go
  • Add comprehensive test coverage for exact and wildcard path matching
  • Update engine to pass exclude paths configuration
  • Add CLI support for --exclude-paths flag

The implementation supports:

  • Exact path matching (e.g., /var/log/test)
  • Wildcard path matching (e.g., /var/log/test/*)
  • Multiple exclude paths

Tests ensure proper handling of:

  • Exact path exclusions
  • Wildcard exclusions
  • Edge cases and similar paths

References:

https://github.com/trufflesecurity/trufflehog/issues/2216?utm_source=chatgpt.com

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

Add support for excluding paths in Docker source scanning:

- Add ExcludePaths field to Docker protobuf

- Implement path exclusion logic in docker.go

- Add comprehensive test coverage for exact and wildcard path matching

- Update engine to pass exclude paths configuration

- Add CLI support for --exclude-paths flag

The implementation supports:

- Exact path matching (e.g., /var/log/test)

- Wildcard path matching (e.g., /var/log/test/*)

- Multiple exclude paths

Tests ensure proper handling of:

- Exact path exclusions

- Wildcard exclusions

- Edge cases and similar paths
@CLAassistant
Copy link

CLAassistant commented Apr 15, 2025

CLA assistant check
All committers have signed the CLA.

@kashifkhan0771
Copy link
Contributor

Thank you for opening this PR @tannerjones4075. Can you sign the CLA?

@tannerjones4075
Copy link
Author

Thank you for opening this PR @tannerjones4075. Can you sign the CLA?

I have signed the CLA :)

Copy link
Collaborator

@rosecodym rosecodym left a comment

Choose a reason for hiding this comment

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

Thanks for this PR! It's a very clear and straightforward improvement. In particular, thank you for the detailed description and the tests. I've found a couple of small improvements I'd like to make before getting this in that I think will substantially reduce our future maintenance burden - please see my inline comments.

@tannerjones4075
Copy link
Author

I appreciate the feedback. I'll make some changes and add another commit.

Copy link
Collaborator

@rosecodym rosecodym left a comment

Choose a reason for hiding this comment

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

I love one of your new tests! But I've added some more commentary to the other one.

pattern = "^" + pattern + "$"
regex, err := regexp.Compile(pattern)
if err != nil {
return fmt.Errorf("error compiling exclude path regex for %q: %w", path, err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

You might also consider adding the pattern being compiled to this error message; if the error is caused by something added during pattern generation, users are likely to be quite confused.

for _, tf := range testFiles {
excluded := false
for _, excludePath := range s.excludePaths {
if tf.path == excludePath {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You're only checking for exact matches here, which means that you're not actually exercising the match logic you've added to the source. This test is effectively adding a single item to a slice and then asserting that a few other items are not in the slice. You don't need a whole source implementation to test that! I think you can just remove this test entirely.

Comment on lines +313 to +330
// Test if the path should be excluded
excluded := false
for _, path := range tt.excludePaths {
if tt.testPath == path {
excluded = true
break
}
// Convert wildcard pattern to regex
pattern := strings.ReplaceAll(path, "*", ".*")
pattern = "^" + pattern + "$"
isMatch, err := regexp.MatchString(pattern, tt.testPath)
assert.NoError(t, err)
if isMatch {
excluded = true
break
}
}
assert.Equal(t, tt.want, excluded)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I love the idea of this new test! But notice how s isn't involved in any of this test code. That means that it's not actually testing the source at all! This is why I suggested creating a method on Source that actually checks for exclusion. If you do that, you can use it in the source and also test it here.

}
}

func TestDockerScanWithExclusions(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test is great!

@rosecodym
Copy link
Collaborator

rosecodym commented Apr 29, 2025

One thing I didn't mention in earlier reviews - that I probably should have - is that the existing codebase has glob inclusion/exclusion implementation logic you could possibly reuse instead of adding this new regex-based technique. I'm not going to insist on it (because I didn't bring it up earlier) but it's probably worth at least giving it a glance to see if it would be helpful.

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.

Add --include-paths and --exclude-paths flags for Docker scan
4 participants