Skip to content

Commit e1ab5c9

Browse files
Copilotfelickz
andauthored
Add support for SECRET_TYPE_FILTER environment variable to filter secret scanning alerts (#77)
* Initial plan for issue * Add SECRET_TYPE_FILTER support for secret scanning alerts Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> * Redesign secret scanning to use two API calls by default Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> * Apply Black formatting to ensure code style consistency Co-authored-by: felickz <1760475+felickz@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent acafa81 commit e1ab5c9

File tree

2 files changed

+117
-16
lines changed

2 files changed

+117
-16
lines changed

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Outputs:
1414
- CSV file of secret scanning alerts
1515
- CSV file of code scanning alerts
16-
- CSV file of Dependabot alerts
16+
- CSV file of Dependabot alerts
1717
"""
1818

1919
# Import modules

src/secret_scanning.py

Lines changed: 116 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,45 @@ def get_repo_ss_alerts(api_endpoint, github_pat, repo_name):
1515
- Repository name
1616
1717
Outputs:
18-
- List of _all_ secret scanning alerts on the repository
18+
- List of _all_ secret scanning alerts on the repository (both default and generic secret types)
1919
"""
20-
url = f"{api_endpoint}/repos/{repo_name}/secret-scanning/alerts?per_page=100&page=1"
21-
ss_alerts = api_helpers.make_api_call(url, github_pat)
22-
print(f"Found {len(ss_alerts)} secret scanning alerts in {repo_name}")
23-
return ss_alerts
20+
# First call: get default secret types (without any filters)
21+
url_default = f"{api_endpoint}/repos/{repo_name}/secret-scanning/alerts?per_page=100&page=1"
22+
ss_alerts_default = api_helpers.make_api_call(url_default, github_pat)
23+
24+
# Second call: get generic secret types with hardcoded list
25+
generic_secret_types = "password,http_basic_authentication_header,http_bearer_authentication_header,mongodb_connection_string,mysql_connection_string,openssh_private_key,pgp_private_key,postgres_connection_string,rsa_private_key"
26+
url_generic = f"{api_endpoint}/repos/{repo_name}/secret-scanning/alerts?per_page=100&page=1&secret_type={generic_secret_types}"
27+
ss_alerts_generic = api_helpers.make_api_call(url_generic, github_pat)
28+
29+
# Combine results and deduplicate
30+
combined_alerts = []
31+
alert_numbers_seen = set()
32+
duplicates_found = False
33+
34+
# Add default alerts
35+
for alert in ss_alerts_default:
36+
alert_numbers_seen.add(alert["number"])
37+
combined_alerts.append(alert)
38+
39+
# Add generic alerts, checking for duplicates
40+
for alert in ss_alerts_generic:
41+
if alert["number"] in alert_numbers_seen:
42+
duplicates_found = True
43+
else:
44+
alert_numbers_seen.add(alert["number"])
45+
combined_alerts.append(alert)
46+
47+
# Warn if duplicates were found
48+
if duplicates_found:
49+
print(
50+
f"::warning::Duplicate secret scanning alerts detected in {repo_name}. Please report this behavior via an issue to the repository owners as the API behavior may have changed."
51+
)
52+
53+
print(
54+
f"Found {len(combined_alerts)} secret scanning alerts in {repo_name} ({len(ss_alerts_default)} default, {len(ss_alerts_generic)} generic)"
55+
)
56+
return combined_alerts
2457

2558

2659
def write_repo_ss_list(secrets_list):
@@ -79,12 +112,47 @@ def get_org_ss_alerts(api_endpoint, github_pat, org_name):
79112
- Organization name
80113
81114
Outputs:
82-
- List of _all_ secret scanning alerts on the organization
115+
- List of _all_ secret scanning alerts on the organization (both default and generic secret types)
83116
"""
84-
url = f"{api_endpoint}/orgs/{org_name}/secret-scanning/alerts?per_page=100&page=1"
85-
ss_alerts = api_helpers.make_api_call(url, github_pat)
86-
print(f"Found {len(ss_alerts)} secret scanning alerts in {org_name}")
87-
return ss_alerts
117+
# First call: get default secret types (without any filters)
118+
url_default = f"{api_endpoint}/orgs/{org_name}/secret-scanning/alerts?per_page=100&page=1"
119+
ss_alerts_default = api_helpers.make_api_call(url_default, github_pat)
120+
121+
# Second call: get generic secret types with hardcoded list
122+
generic_secret_types = "password,http_basic_authentication_header,http_bearer_authentication_header,mongodb_connection_string,mysql_connection_string,openssh_private_key,pgp_private_key,postgres_connection_string,rsa_private_key"
123+
url_generic = (
124+
f"{api_endpoint}/orgs/{org_name}/secret-scanning/alerts?per_page=100&page=1&secret_type={generic_secret_types}"
125+
)
126+
ss_alerts_generic = api_helpers.make_api_call(url_generic, github_pat)
127+
128+
# Combine results and deduplicate
129+
combined_alerts = []
130+
alert_numbers_seen = set()
131+
duplicates_found = False
132+
133+
# Add default alerts
134+
for alert in ss_alerts_default:
135+
alert_numbers_seen.add(alert["number"])
136+
combined_alerts.append(alert)
137+
138+
# Add generic alerts, checking for duplicates
139+
for alert in ss_alerts_generic:
140+
if alert["number"] in alert_numbers_seen:
141+
duplicates_found = True
142+
else:
143+
alert_numbers_seen.add(alert["number"])
144+
combined_alerts.append(alert)
145+
146+
# Warn if duplicates were found
147+
if duplicates_found:
148+
print(
149+
f"::warning::Duplicate secret scanning alerts detected in {org_name}. Please report this behavior via an issue to the repository owners as the API behavior may have changed."
150+
)
151+
152+
print(
153+
f"Found {len(combined_alerts)} secret scanning alerts in {org_name} ({len(ss_alerts_default)} default, {len(ss_alerts_generic)} generic)"
154+
)
155+
return combined_alerts
88156

89157

90158
def write_org_ss_list(secrets_list):
@@ -158,12 +226,45 @@ def get_enterprise_ss_alerts(api_endpoint, github_pat, enterprise_slug):
158226
- https://docs.github.com/en/rest/reference/enterprise-admin
159227
160228
Outputs:
161-
- List of _all_ secret scanning alerts on the enterprise
229+
- List of _all_ secret scanning alerts on the enterprise (both default and generic secret types)
162230
"""
163-
url = f"{api_endpoint}/enterprises/{enterprise_slug}/secret-scanning/alerts?per_page=100&page=1"
164-
ss_alerts = api_helpers.make_api_call(url, github_pat)
165-
print(f"Found {len(ss_alerts)} secret scanning alerts in {enterprise_slug}")
166-
return ss_alerts
231+
# First call: get default secret types (without any filters)
232+
url_default = f"{api_endpoint}/enterprises/{enterprise_slug}/secret-scanning/alerts?per_page=100&page=1"
233+
ss_alerts_default = api_helpers.make_api_call(url_default, github_pat)
234+
235+
# Second call: get generic secret types with hardcoded list
236+
generic_secret_types = "password,http_basic_authentication_header,http_bearer_authentication_header,mongodb_connection_string,mysql_connection_string,openssh_private_key,pgp_private_key,postgres_connection_string,rsa_private_key"
237+
url_generic = f"{api_endpoint}/enterprises/{enterprise_slug}/secret-scanning/alerts?per_page=100&page=1&secret_type={generic_secret_types}"
238+
ss_alerts_generic = api_helpers.make_api_call(url_generic, github_pat)
239+
240+
# Combine results and deduplicate
241+
combined_alerts = []
242+
alert_numbers_seen = set()
243+
duplicates_found = False
244+
245+
# Add default alerts
246+
for alert in ss_alerts_default:
247+
alert_numbers_seen.add(alert["number"])
248+
combined_alerts.append(alert)
249+
250+
# Add generic alerts, checking for duplicates
251+
for alert in ss_alerts_generic:
252+
if alert["number"] in alert_numbers_seen:
253+
duplicates_found = True
254+
else:
255+
alert_numbers_seen.add(alert["number"])
256+
combined_alerts.append(alert)
257+
258+
# Warn if duplicates were found
259+
if duplicates_found:
260+
print(
261+
f"::warning::Duplicate secret scanning alerts detected in {enterprise_slug}. Please report this behavior via an issue to the repository owners as the API behavior may have changed."
262+
)
263+
264+
print(
265+
f"Found {len(combined_alerts)} secret scanning alerts in {enterprise_slug} ({len(ss_alerts_default)} default, {len(ss_alerts_generic)} generic)"
266+
)
267+
return combined_alerts
167268

168269

169270
def write_enterprise_ss_list(secrets_list):

0 commit comments

Comments
 (0)