forked from Yelp/detect-secrets
-
Notifications
You must be signed in to change notification settings - Fork 54
feat(ip_public): Add IPPublicDetector plugin #186
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
jfagoagas
wants to merge
3
commits into
IBM:master
Choose a base branch
from
jfagoagas:feat/ip-public-detector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−0
Open
Changes from all commits
Commits
Show all changes
3 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import re | ||
|
|
||
| from .base import RegexBasedDetector | ||
|
|
||
|
|
||
| class IPPublicDetector(RegexBasedDetector): | ||
| """Scans for public ip address (ipv4) | ||
|
|
||
| Some non-public ipv4 addresses are ignored, such as: | ||
| - 127. | ||
| - 10. | ||
| - 172.(16-31) | ||
| - 192.168. | ||
| - 169.254. - Link Local Address IPv4 | ||
|
|
||
| Reference: | ||
| https://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xhtml | ||
| https://en.wikipedia.org/wiki/Private_network | ||
| """ | ||
| secret_type = 'Public IP (ipv4)' | ||
|
|
||
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| denylist_ipv4_address = r""" | ||
| (?<![\w.]) # Negative lookbehind: Ensures no preceding word character or dot | ||
| ( # Start of the main capturing group | ||
| (?! # Negative lookahead: Ensures the following pattern doesn't match | ||
| 192\.168\. # Exclude "192.168." | ||
| |127\. # Exclude "127." | ||
| |10\. # Exclude "10." | ||
| |169\.254\. # Exclude IPv4 Link Local Address (169.254.0.0/16) | ||
| |172\.(?:1[6-9]|2[0-9]|3[01])\. # Exclude "172.16-31." with specific ranges | ||
| ) | ||
| (?: # Non-capturing group for octets | ||
| # Match numbers 0-255 followed by dot, properly handle leading zeros | ||
| (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\. | ||
| ){3} # Repeat for three octets | ||
| # Match final octet (0-255), properly handle leading zeros | ||
| (?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9]) | ||
| (?: # Optional non-capturing group for port number | ||
| :\d{1,5} # Match colon followed by 1 to 5 digits | ||
| )? | ||
| ) # End of the main capturing group | ||
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (?![\w.]) # Negative lookahead: Ensures no following word character or dot | ||
| """ | ||
|
|
||
| denylist = [ | ||
| re.compile(denylist_ipv4_address, flags=re.IGNORECASE | re.VERBOSE), | ||
| ] | ||
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,59 @@ | ||
| import pytest | ||
|
|
||
| from detect_secrets.plugins.ip_public import IPPublicDetector | ||
|
|
||
|
|
||
| class TestIPPublicDetector: | ||
|
|
||
| class TestIPv4: | ||
| """ | ||
| Testing strategy | ||
|
|
||
| Cover the cartesian product of these partitions: | ||
|
|
||
| 1. Partition on ip address format: | ||
| a. Valid ipv4 address | ||
|
|
||
| 2. Partition on ip address type: | ||
| a. Public | ||
| b. Non-public | ||
|
|
||
| And cover this case: | ||
| 1. Partition on ip address format: | ||
| a. Invalid ipv4 address | ||
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| @pytest.mark.parametrize( | ||
| 'payload, should_flag', | ||
| [ | ||
| # Valid IPv4 addresses, Public | ||
| ('133.133.133.133', True), | ||
| ('This line has an IP address 133.133.133.133@something else', True), | ||
| ('133.133.133.133:8080', True), | ||
| ('This line has an IP address: 133.133.133.133:8080@something else', True), | ||
| ('1.1.1.1', True), | ||
| # Valid IPv4 addresses, Non-public | ||
| ('127.0.0.1', False), | ||
| ('10.0.0.1', False), | ||
| ('172.16.0.1', False), | ||
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ('192.168.0.1', False), | ||
| ('169.254.169.254', False), | ||
| # 172.x boundary: 172.15 and 172.32 are public, 172.16-31 are private | ||
| ('172.15.0.1', True), | ||
| ('172.32.0.1', True), | ||
| ('172.160.0.1', True), | ||
| # Invalid IPv4 addresses | ||
| ('256.256.256.256', False), | ||
| ('1.2.3', False), | ||
| ('1.2.3.4.5.6', False), | ||
| ('1.2.3.4.5.6.7.8', False), | ||
| ('1.2.3.04', False), | ||
| ('noreply@github.com', False), | ||
| ('github.com', False), | ||
| ], | ||
| ) | ||
| def test_analyze_line(self, payload, should_flag): | ||
| logic = IPPublicDetector() | ||
|
|
||
| output = logic.analyze_line(payload, 1, 'mock_filename') | ||
| assert len(output) == int(should_flag) | ||
jfagoagas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.