Skip to content

Commit 64dd98d

Browse files
authored
Filter out ARNs (#252)
1 parent 107c151 commit 64dd98d

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

detect_secrets/filters/heuristic.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,18 @@ def is_swagger_file(filename: str) -> bool:
258258
@lru_cache(maxsize=1)
259259
def _get_swagger_regex() -> Pattern:
260260
return re.compile(r'.*swagger.*')
261+
262+
263+
def is_aws_arn(secret: str) -> bool:
264+
"""
265+
Filters AWS ARN strings that match the pattern:
266+
arn:aws:<service>:<region>:<account-id>:<resource-type>:<resource-name>
267+
"""
268+
return bool(_get_arn_regex().search(secret))
269+
270+
271+
@lru_cache(maxsize=1)
272+
def _get_arn_regex() -> Pattern:
273+
return re.compile(
274+
r'^arn:aws:[a-z0-9\-]+:([a-z0-9\-]*:[0-9]{12}|:[0-9]{12}|\*)?:.*$',
275+
)

tests/filters/heuristic_filter_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,16 @@ def test_is_not_alphanumeric_string(secret, result):
172172
)
173173
def test_is_swagger_file(filename, result):
174174
assert filters.heuristic.is_swagger_file(filename.format(sep=os.path.sep)) is result
175+
176+
@pytest.mark.parametrize(
177+
'secret, result',
178+
(
179+
('arn:aws:lambda:us-west-2:123456789012:function:my-function', True),
180+
('arn:aws:s3:::my-bucket', True),
181+
('arn:aws:iam::123456789012:role/service-role/my-role', True),
182+
('not:an:arn:123456789012', False),
183+
('randomstring', False),
184+
),
185+
)
186+
def test_is_aws_arn(secret, result):
187+
assert filters.heuristic.is_aws_arn(secret) is result

tests/plugins/high_entropy_strings_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class TestHighEntropyString:
4343
# Non-quoted string from Yaml
4444
('some_key: {secret}', True),
4545
('some_key:{secret}', True),
46+
47+
# Don't flag ARNs
48+
('ServiceToken: "arn:aws:lambda:us-west-2:123456789012:function:dummy-token"', False),
4649
),
4750
)
4851
def test_basic(plugin, non_secret, secret, format, should_be_caught):

0 commit comments

Comments
 (0)