From b3de6abef6db3a6c0137add38ed414e56cdfeb13 Mon Sep 17 00:00:00 2001 From: Gabriel Naughton Date: Thu, 19 Nov 2026 11:33:28 +0000 Subject: [PATCH 1/4] filter files scanneed locally so that exclusions are actually excluded Signed-off-by: BIST pre-commit check --- pyproject.toml | 2 +- src/hooks/trufflehog/scanner.py | 17 +++++++++++--- tests/unit/hooks/trufflehog/test_scanner.py | 25 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 984d4e7e..dc2fa79c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -55,7 +55,7 @@ required-version = "==0.12.15" line-length = 125 indent-width = 4 target-version = "py312" -extend-select = ["T201"] +lint.extend-select = ["T201"] [tool.coverage.run] omit = ['tests/*'] diff --git a/src/hooks/trufflehog/scanner.py b/src/hooks/trufflehog/scanner.py index 64eb731d..8ef3cfe5 100644 --- a/src/hooks/trufflehog/scanner.py +++ b/src/hooks/trufflehog/scanner.py @@ -17,6 +17,7 @@ TRUFFLEHOG_VERBOSE_LOG_LEVEL, ) from src.proxy.plugins import OutgoingRequestInterceptorPlugin +from src.hooks.presidio.path_filter import PathFilter logger = LOGGER @@ -55,6 +56,8 @@ async def _get_args( ) -> List[str]: trufflehog_log_level = TRUFFLEHOG_VERBOSE_LOG_LEVEL if self.verbose else TRUFFLEHOG_INFO_LOG_LEVEL + exclusions_file_exists = await Path(TRUFFLEHOG_EXCLUSIONS_FILE_PATH).exists() + if github_action: # Scan all files in this branch paths_to_scan = [f"file://{paths[0]}"] @@ -64,6 +67,11 @@ async def _get_args( scan_mode = "filesystem" paths_to_scan = paths + if exclusions_file_exists: + path_filter = PathFilter() + exclusions = await path_filter._get_exclusions(TRUFFLEHOG_EXCLUSIONS_FILE_PATH) + paths_to_scan = [path for path in paths_to_scan if not path_filter._is_path_excluded(path, exclusions)] + trufflehog_cmd_args = [ "trufflehog", scan_mode, @@ -76,7 +84,7 @@ async def _get_args( if github_action: trufflehog_cmd_args.append("--since-commit=main") - if await Path(TRUFFLEHOG_EXCLUSIONS_FILE_PATH).exists(): + if exclusions_file_exists: logger.debug("Security scanner exclusions file loaded") trufflehog_cmd_args.append(f"--exclude-paths={TRUFFLEHOG_EXCLUSIONS_FILE_PATH}") @@ -87,9 +95,12 @@ async def _get_args( ) trufflehog_cmd_args.append(f"--include-detectors={trufflehog_detectors}") - trufflehog_cmd_args.extend(paths_to_scan) # type: ignore + trufflehog_cmd_args.extend(paths_to_scan) - logger.debug("Running trufflehog command '%s'", " ".join(trufflehog_cmd_args)) + logger.debug( + "Running trufflehog command '%s'", + " ".join(trufflehog_cmd_args), + ) return trufflehog_cmd_args diff --git a/tests/unit/hooks/trufflehog/test_scanner.py b/tests/unit/hooks/trufflehog/test_scanner.py index 811bbdf0..eac4974b 100644 --- a/tests/unit/hooks/trufflehog/test_scanner.py +++ b/tests/unit/hooks/trufflehog/test_scanner.py @@ -1,3 +1,4 @@ +import re from pathlib import Path from unittest.mock import patch from src.hooks.config import ( @@ -6,6 +7,7 @@ TRUFFLEHOG_INFO_LOG_LEVEL, TRUFFLEHOG_VERBOSE_LOG_LEVEL, ) +from src.hooks.presidio.path_filter import PathFilter from src.hooks.trufflehog.scanner import TrufflehogScanner @@ -105,3 +107,26 @@ async def test_scan_with_trufflehog_success_returns_no_error_response(self): env=mock_env.return_value, ) assert result.detected_keys is None + + async def test_get_args_excludes_explicit_files_matching_security_exclusions(self): + paths = [ + ".github/workflows/org.common-ci.yml", + "some-other-file.yml", + ] + exclusions = [ + re.compile(r"\.github/workflows/org\.common-ci\.yml"), + ] + with ( + patch.object(Path, "exists", return_value=True), + patch.object( + PathFilter, + "_get_exclusions", + return_value=exclusions, + ), + ): + args = await TrufflehogScanner()._get_args( + paths=paths, + github_action=False, + ) + assert ".github/workflows/org.common-ci.yml" not in args + assert "some-other-file.yml" in args From fc327d5463ec57f04198f68e9d8d0b4c8159b4ee Mon Sep 17 00:00:00 2001 From: Gabriel Naughton Date: Thu, 19 Nov 2026 11:43:09 +0000 Subject: [PATCH 2/4] restore ignore Signed-off-by: BIST pre-commit check --- src/hooks/trufflehog/scanner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/trufflehog/scanner.py b/src/hooks/trufflehog/scanner.py index 8ef3cfe5..73da323d 100644 --- a/src/hooks/trufflehog/scanner.py +++ b/src/hooks/trufflehog/scanner.py @@ -95,7 +95,7 @@ async def _get_args( ) trufflehog_cmd_args.append(f"--include-detectors={trufflehog_detectors}") - trufflehog_cmd_args.extend(paths_to_scan) + trufflehog_cmd_args.extend(paths_to_scan) # type: ignore logger.debug( "Running trufflehog command '%s'", From 94c77c4e249a2ece1ef8ee338920749118d1106f Mon Sep 17 00:00:00 2001 From: Gabriel Naughton Date: Thu, 19 Nov 2026 11:44:04 +0000 Subject: [PATCH 3/4] revert formatting Signed-off-by: BIST pre-commit check --- src/hooks/trufflehog/scanner.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/hooks/trufflehog/scanner.py b/src/hooks/trufflehog/scanner.py index 73da323d..10442812 100644 --- a/src/hooks/trufflehog/scanner.py +++ b/src/hooks/trufflehog/scanner.py @@ -97,10 +97,7 @@ async def _get_args( trufflehog_cmd_args.extend(paths_to_scan) # type: ignore - logger.debug( - "Running trufflehog command '%s'", - " ".join(trufflehog_cmd_args), - ) + logger.debug("Running trufflehog command '%s'", " ".join(trufflehog_cmd_args)) return trufflehog_cmd_args From f76164d54a3910d945397b39817851878d8cb9c1 Mon Sep 17 00:00:00 2001 From: Gabe Naughton Date: Thu, 19 Nov 2026 14:04:28 +0000 Subject: [PATCH 4/4] Apply suggestion from @sumau Co-authored-by: Soumaya Mauthoor --- src/hooks/trufflehog/scanner.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hooks/trufflehog/scanner.py b/src/hooks/trufflehog/scanner.py index 10442812..ef3fea10 100644 --- a/src/hooks/trufflehog/scanner.py +++ b/src/hooks/trufflehog/scanner.py @@ -66,7 +66,8 @@ async def _get_args( # Scan the files passed in scan_mode = "filesystem" paths_to_scan = paths - +# trufflehog's --exclude-paths is not applied to files passed in explicitly, +# so filter them here using the same regexes from the exclusions file if exclusions_file_exists: path_filter = PathFilter() exclusions = await path_filter._get_exclusions(TRUFFLEHOG_EXCLUSIONS_FILE_PATH)