Skip to content

Conversation

@Ankitsinghsisodya
Copy link
Contributor

Overview

Issue: #2324

This PR adds a --exclude CLI flag that allows users to specify glob patterns to exclude files and directories from vulnerability scanning.

Fixes #2324

Details

Problem

Users scanning large repositories often want to exclude test files, documentation, and other non-production code from vulnerability scans. While .gitignore is respected by default and osv-scanner.toml can be used, there was no quick CLI option to exclude paths.

Solution

Added --exclude / -e flag that accepts glob patterns:

osv-scanner scan source -r --exclude="**/test/**" --exclude="**/docs/**" ./path

Add --exclude/-e CLI flag that accepts glob patterns to exclude
files and directories from vulnerability scanning. This addresses
user requests to easily skip test files and documentation during scans.

Changes:
- Add ExcludePatterns field to ScannerActions struct
- Add --exclude flag to scan source command
- Wire ExcludePatterns to scalibr's SkipDirGlob option
- Document the new flag in scan-source.md

Fixes google#2324
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.

This is definitely something we want, though might need a bit more design before including it. It is really hard for us to justify removing a flag once it's added.

I want a way for people to specify regex patterns without having to add another separate flag. Maybe something like if it is wrapped /<regex>/ like in javascript?

- Replace regexp import with cachedregexp to satisfy depguard rules
- Add Regexp type alias and Compile function to cachedregexp package
- Remove duplicate package doc comment (godoclint)
- Add blank line before return (nlreturn)
- Add t.Parallel() calls to test functions (paralleltest)
Copy link
Collaborator

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

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

Appreciate your work on this!

Could you please:

  • move the flag to be --experimental- (this is mainly because right now scalibr only supports skipping directories, not files which'll hopefully be able to change but in the meantime this will mean we can do breaking changes if we go another direction instead)
  • update the docs to reflect that this only applies to directories, not files
  • add a couple of cmd tests
  • update the syntax to use :, matching our --lockfile flag, which'll also let us use DirsToSkip too
    • if the string is prefixed with : or nothing, it should be applied as a directory (DirsToSkip)
    • if the string is prefixed with g:, it should be applied as a glob (SkipDirGlob)
    • if the string is prefixed with r:, it should be applied as a regex (SkipDirRegex)

@another-rex
Copy link
Collaborator

another-rex commented Jan 20, 2026

if the string is prefixed with :, it should be applied as a directory (DirsToSkip)

to clarify, also if it is not prefixed, use DirsToSkip, the prefixing with : is just an escape hatch incase your file path itself has a : inside.

@Ankitsinghsisodya
Copy link
Contributor Author

@another-rex sir, I have done the changes suggested you.

@Ankitsinghsisodya
Copy link
Contributor Author

I initially removed the type alias as you suggested, but CI failed with a depguard error:

import 'regexp' is not allowed from list 'regexp':
Use github.com/google/osv-scanner/v2/internal/cachedregexp instead (depguard)

The exclude.go file needs the *regexp.Regexp type for the struct field, but can't import regexp directly due to the lint rule. Adding the type alias back in cachedregexp allows other packages to use *cachedregexp.Regexp without violating depguard.

Should I look into adding an exception to the depguard config for this file, or is the type alias acceptable given this constraint?

@G-Rath
Copy link
Collaborator

G-Rath commented Jan 20, 2026

right yeah of course - I think for now let's go with adding the exception as that mirrors what we're doing elsewhere for this situation, though I think I'll revisit that after this as it'll probably be better to just re-export stuff from cachedregexp and/or try to use the forbidigo linter instead

(I had forgotten we pretty much don't use regexp outside of cachedregexp.MustCompile 😅)

Copy link
Collaborator

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

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

looking good, we just also do want to change the cmd flag to use exclude too

@G-Rath G-Rath assigned G-Rath and unassigned G-Rath Jan 20, 2026
Ankitsinghsisodya and others added 4 commits January 21, 2026 01:59
Co-authored-by: Gareth Jones <3151613+G-Rath@users.noreply.github.com>
Co-authored-by: Gareth Jones <3151613+G-Rath@users.noreply.github.com>
…e` and update its usage and documentation.
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 good!

@another-rex
Copy link
Collaborator

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new experimental flag --experimental-exclude to allow excluding paths from scanning. The implementation is well-structured, with new logic for parsing exclusion patterns, comprehensive tests, and clear documentation. I've found one issue with how absolute paths are handled, which could lead to silent failures. My detailed feedback is in the review comments. Also, note that the PR description and title mention --exclude, but the implementation uses --experimental-exclude, which is correctly reflected in the documentation.

@another-rex
Copy link
Collaborator

Hmm the requested interactions not found errors are a bit odd in the CI, because it looks like you have added the new interactions to the cassettes. @G-Rath Can you take a look at why that could be happening?

@G-Rath
Copy link
Collaborator

G-Rath commented Jan 21, 2026

I think they might just need to be regenerated, as they're still using the old X-Test-Name and it doesn't look like there are interactions for the exclude_with_multiple_exact_directories and exclude_with_multiple_pattern_types tests.

First I'd do is just revert all changes to cmd/osv-scanner/scan/source/testdata/cassettes/TestCommand.yaml, re-run the suite, and commit the results

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.

Option to skip or ignore files and folder while scanning for Vulnerabilities

3 participants