-
Notifications
You must be signed in to change notification settings - Fork 0
feat(scanner): Issue 3 - False-positive noise filter 구현 #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| """Noise filter implementation for Gitleaks findings.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import math | ||
| import re | ||
| from collections import Counter | ||
|
|
||
| # Template placeholder patterns (e.g., ${VAR}, {{secret}}, <VAR>, [VAR], %VAR%, __VAR__) | ||
| TEMPLATE_PATTERN = re.compile( | ||
| r"^(" | ||
| r"\$\{[a-zA-Z0-9_-]+\}" | ||
| r"|\{\{[a-zA-Z0-9_-]+\}\}" | ||
| r"|<[a-zA-Z0-9_-]+>" | ||
| r"|\[[a-zA-Z0-9_-]+\]" | ||
| r"|%[a-zA-Z0-9_-]+%" | ||
| r"|__[a-zA-Z0-9_-]+__" | ||
| r")$" | ||
| ) | ||
|
|
||
| # Known dummy values (case-insensitive) | ||
| KNOWN_DUMMY_VALUES = { | ||
| "your_api_key", | ||
| "changeme", | ||
| "insert-token-here", | ||
| } | ||
|
|
||
| # False-negative prevention patterns (synthetic AWS/GitHub token shapes). | ||
| FALSE_NEGATIVE_PATTERN = re.compile(r"^(AKIA[A-Z0-9]{16}|ghp_[a-zA-Z0-9]{36,})$") | ||
|
|
||
|
|
||
| def calculate_entropy(s: str) -> float: | ||
| """Calculate the Shannon Entropy of a string.""" | ||
| if not s: | ||
| return 0.0 | ||
| total_len = len(s) | ||
| counts = Counter(s) | ||
| entropy = 0.0 | ||
| for count in counts.values(): | ||
| p = count / total_len | ||
| entropy -= p * math.log2(p) | ||
| return entropy | ||
|
|
||
|
|
||
| def noise_reason(item: dict) -> str | None: | ||
| """Return the noise reason for a Gitleaks item, or None when it should pass. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| item : dict | ||
| A single Gitleaks JSON finding item (containing 'Secret', 'Match', etc.) | ||
|
|
||
| Returns | ||
| ------- | ||
| str | None | ||
| A non-sensitive reason string when the item is classified as noise. | ||
| None when the item should not be filtered. | ||
| """ | ||
| secret = item.get("Secret", "") | ||
| if not isinstance(secret, str) or not secret: | ||
| return "empty-secret" | ||
|
|
||
| # 1. False-Negative Prevention | ||
| if FALSE_NEGATIVE_PATTERN.match(secret): | ||
| return None | ||
|
|
||
| # 2. Template placeholders | ||
| if TEMPLATE_PATTERN.match(secret): | ||
| return "template-placeholder" | ||
|
|
||
| # 3. Known dummy values (case-insensitive) | ||
| if secret.lower() in KNOWN_DUMMY_VALUES: | ||
| return "known-dummy-value" | ||
|
|
||
| # 4. Repeated characters | ||
| if len(secret) >= 1 and len(set(secret)) == 1: | ||
| return "repeated-character" | ||
|
|
||
| # 5. Low entropy & short strings | ||
| if len(secret) <= 5: | ||
| return "short-secret" | ||
|
|
||
| entropy = calculate_entropy(secret) | ||
| if len(secret) < 10 and entropy < 1.8: | ||
| return "low-entropy-short-secret" | ||
| if len(secret) >= 10 and entropy < 2.5: | ||
| return "low-entropy-secret" | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def should_filter_item(item: dict) -> bool: | ||
| """Determine if a Gitleaks finding item should be filtered out as noise.""" | ||
| return noise_reason(item) is not None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Unit tests for Gitleaks noise filter.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from security_scanner.scanners.gitleaks.filter import ( | ||
| calculate_entropy, | ||
| noise_reason, | ||
| should_filter_item, | ||
| ) | ||
|
|
||
|
|
||
| FAKE_AWS_ACCESS_KEY_ID = "AKIAFAKEEXAMPLE00000" | ||
| FAKE_GITHUB_TOKEN = "ghp_FAKEtoken123456789012345678901234567" | ||
|
|
||
|
|
||
| def test_template_placeholders(): | ||
| placeholders = [ | ||
| "${VAR}", | ||
| "{{secret}}", | ||
| "<VAR>", | ||
| "[VAR]", | ||
| "%VAR%", | ||
| "__VAR__", | ||
| "${SOME_ENV_VARIABLE}", | ||
| "{{database_password}}", | ||
| ] | ||
| for ph in placeholders: | ||
| assert should_filter_item({"Secret": ph}) is True | ||
|
|
||
|
|
||
| def test_known_dummy_values(): | ||
| dummies = [ | ||
| "your_api_key", | ||
| "YOUR_API_KEY", | ||
| "CHANGEME", | ||
| "changeme", | ||
| "insert-token-here", | ||
| "Insert-Token-Here", | ||
| ] | ||
| for dummy in dummies: | ||
| assert should_filter_item({"Secret": dummy}) is True | ||
|
|
||
|
|
||
| def test_repeated_characters(): | ||
| repeated = [ | ||
| "xxxxxx", | ||
| "aaaaaa", | ||
| "11111", | ||
| "ZZZZZZZZ", | ||
| ] | ||
| for rep in repeated: | ||
| assert should_filter_item({"Secret": rep}) is True | ||
|
|
||
|
|
||
| def test_low_entropy_and_short_strings(): | ||
| # Length <= 5: always filtered | ||
| assert should_filter_item({"Secret": ""}) is True | ||
| assert should_filter_item({"Secret": "abcd"}) is True | ||
| assert should_filter_item({"Secret": "12345"}) is True | ||
|
|
||
| # Length < 10 and entropy < 1.8: filtered | ||
| # "1231231" has length 7, entropy is 1.556 < 1.8 | ||
| assert should_filter_item({"Secret": "1231231"}) is True | ||
|
|
||
| # Length < 10 and entropy >= 1.8: NOT filtered | ||
| # "abcdefg" has length 7, entropy is 2.807 >= 1.8 | ||
| assert should_filter_item({"Secret": "abcdefg"}) is False | ||
|
|
||
| # Length >= 10 but low entropy: filtered | ||
| assert should_filter_item({"Secret": "aaaaabbbbb"}) is True | ||
|
|
||
| # Length >= 10 with enough entropy: NOT filtered | ||
| assert should_filter_item({"Secret": "abcdefghi0"}) is False | ||
|
|
||
|
|
||
| def test_false_negatives_prevention(): | ||
| # AWS Access Key format (typically 20 chars, starting with AKIA) | ||
| assert should_filter_item({"Secret": FAKE_AWS_ACCESS_KEY_ID}) is False | ||
| # GitHub Token format (typically 40 chars, starting with ghp_) | ||
| assert should_filter_item({"Secret": FAKE_GITHUB_TOKEN}) is False | ||
|
|
||
|
|
||
| def test_noise_reason_does_not_include_secret_value(): | ||
| secret = "${DATABASE_PASSWORD}" | ||
| reason = noise_reason({"Secret": secret}) | ||
|
|
||
| assert reason == "template-placeholder" | ||
| assert secret not in reason | ||
|
|
||
|
|
||
| def test_noise_reason_handles_non_string_secret_values(): | ||
| for secret in (None, 123, True, [], {}): | ||
| assert noise_reason({"Secret": secret}) == "empty-secret" | ||
| assert should_filter_item({"Secret": secret}) is True | ||
|
|
||
|
|
||
| def test_calculate_entropy(): | ||
| assert calculate_entropy("") == 0.0 | ||
| assert calculate_entropy("a") == 0.0 | ||
| assert abs(calculate_entropy("ab") - 1.0) < 1e-9 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.