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

Fix check bypass via square brackets Issue #857 #860

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion detect_secrets/filters/heuristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,15 @@ def _get_indirect_reference_regex() -> Pattern:
# [^\v]* -> Something except line breaks
# [\]\)] -> End of indirect reference: ] or )
# )
return re.compile(r'([^\v=!:]*)\s*(:=?|[!=]{1,3})\s*([\w.-]+[\[\(][^\v]*[\]\)])')
return re.compile(
r'([^\v=!:"<%>]*)\s*(:=?|[!=]{1,3}|\|\|)\s*('
r'[\w.-]+[\[\(][^\v]*[\]\)]' # Matches ENV[...] or similar references
r'|'
r'\'[^\']*\'' # Matches single-quoted strings
r'|'
r'"[^"]*"' # Matches double-quoted strings
r')'
)


def is_lock_file(filename: str) -> bool:
Expand Down
9 changes: 9 additions & 0 deletions tests/filters/heuristic_filter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ def test_is_prefixed_with_dollar_sign(secret, result):
('secret = get_secret_key()', True),
('secret = request.headers["apikey"]', True),
('secret = hunter2', False),
("<%= ENV['CLIENT_ACCESS_KEY_ID'].presence || 'AKIA123456789ABCDEF1' %>", True), # Erb template with intermediate method
("<%= ENV['CLIENT_ACCESS_KEY_ID'] || 'AKIA123456789ABCDEF1' %>", True), # Erb template without intermediate method
("ENV['CLIENT_ACCESS_KEY_ID'].presence || 'AKIA123456789ABCDEF1'", True), # Ruby with intermediate method
("ENV['CLIENT_ACCESS_KEY_ID'] || 'AKIA123456789ABCDEF1'", True), # Ruby without intermediate method
('not_a_secret ||= something_else', False), # Ruby assignment
('not_a_secret || something_else', False), # Ruby truthy validation
('api_key = ENV["API_KEY"].get() || "default_key"', True), # Ruby with intermediate method with assignment
('token = ENV["TOKEN"] || default_token', True), # Ruby without intermediate method with assignment
('api_key ||= fetch_api_key()', True), # Ruby without intermediate method with assignment
),
)
def test_is_indirect_reference(line, result):
Expand Down