Skip to content
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

Optimize AzureStorageKeyDetector performance #214

Merged
merged 4 commits into from
May 12, 2024
Merged
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
18 changes: 14 additions & 4 deletions detect_secrets/plugins/azure_storage_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
from detect_secrets.plugins.base import RegexBasedDetector
from detect_secrets.util.code_snippet import CodeSnippet


class AzureStorageKeyDetector(RegexBasedDetector):
"""Scans for Azure Storage Account access keys."""
secret_type = 'Azure Storage Account access key'

account_key = 'AccountKey'
azure = 'azure'

denylist = [
# Account Key (AccountKey=xxxxxxxxx)
re.compile(
Expand All @@ -25,14 +29,15 @@ class AzureStorageKeyDetector(RegexBasedDetector):
]

context_keys = [
r'AccountKey=\s*{secret}',
r'{account_key}=\s*{secret}',

# maximum 2 lines secret distance under azure mention (case-insensitive)
r'(?i)azure.*\n?.*\n?.*{secret}',
r'(?i){azure}.*\n?.*\n?.*{secret}',

# maximum 2 lines secret distance above azure mention (case-insensitive)
r'(?i){secret}.*\n?.*\n?.*azure',
r'(?i){secret}.*\n?.*\n?.*{azure}',
]

def analyze_line(
self,
filename: str,
Expand Down Expand Up @@ -65,9 +70,14 @@ def context_keys_exists(self, result: PotentialSecret, string: str) -> bool:
for secret_regex in self.context_keys:
regex = re.compile(
secret_regex.format(
secret=re.escape(result.secret_value),
secret=re.escape(result.secret_value), account_key=self.account_key,
azure=self.azure,
), re.MULTILINE,
)
if regex.pattern.startswith(self.account_key) and self.account_key not in string:
continue
if self.azure in regex.pattern.lower() and self.azure not in string.lower():
continue
if regex.search(string) is not None:
return True
return False
Loading