Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*']
Expand Down
11 changes: 10 additions & 1 deletion src/hooks/trufflehog/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TRUFFLEHOG_VERBOSE_LOG_LEVEL,
)
from src.proxy.plugins import OutgoingRequestInterceptorPlugin
from src.hooks.presidio.path_filter import PathFilter

logger = LOGGER

Expand Down Expand Up @@ -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]}"]
Expand All @@ -63,6 +66,12 @@ 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)
paths_to_scan = [path for path in paths_to_scan if not path_filter._is_path_excluded(path, exclusions)]

trufflehog_cmd_args = [
"trufflehog",
Expand All @@ -76,7 +85,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}")

Expand Down
25 changes: 25 additions & 0 deletions tests/unit/hooks/trufflehog/test_scanner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from pathlib import Path
from unittest.mock import patch
from src.hooks.config import (
Expand All @@ -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


Expand Down Expand Up @@ -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
Loading