Skip to content

Commit 60ddc10

Browse files
committed
Keep each CVE apart in GitLab reports, and group existing license alerts
GitLab finding IDs were built from package, alert type and severity, so two CVEs of one severity on a package shared an ID and the report kept only one. The ID now includes the advisory ID when the alert has one; other findings keep the IDs GitLab already tracks them by. A repeated ID is skipped, since GitLab expects each finding once per report. In the PR comment's existing-alerts table, license policy violations are grouped per package with the license row, as in the new-alerts table, instead of rendering as security findings with a malware link. The 2.13.0 changelog entry is cut to short bullets of observable behavior.
1 parent 879e40b commit 60ddc10

4 files changed

Lines changed: 152 additions & 83 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,33 @@
77
Exit codes change in this release. Check any CI step that matches on a specific
88
code; see [Exit codes](README.md#exit-codes).
99

10-
- Invalid flags and config values exit `2` instead of `1`, so `1` always means
11-
blocking findings. `--disable-blocking` doesn't apply to them.
10+
- Invalid flags exit `2` instead of `1`. `--disable-blocking` doesn't apply to
11+
them.
1212
- Ctrl+C exits `130` instead of `2`.
13-
- A missing API or SCM token, missing reachability dependencies, a plan without
14-
reachability, and a repository that can't be created now honor
15-
`--disable-blocking` and `--exit-code-on-api-error`.
16-
- `--strict-blocking` now fails on existing blocking alerts, as documented, so
17-
runs that passed with them will fail.
13+
- Missing tokens and other setup failures now honor `--disable-blocking` and
14+
`--exit-code-on-api-error`.
15+
- `--strict-blocking` now fails on existing blocking alerts.
1816

1917
### Added
2018

2119
- Under `--strict-blocking`, PR/MR comments list existing alerts in their own
22-
table after the new ones. `@SocketSecurity ignore` applies to them.
20+
table.
2321

2422
### Changed
2523

2624
- An incomplete scan comparison fails the run with the infrastructure-error
27-
code instead of reporting a result that may be missing alerts.
25+
code.
26+
- GitLab security report IDs for CVE findings include the advisory ID, so
27+
existing CVE findings get new IDs once.
2828

2929
### Fixed
3030

31-
- `--strict-blocking` passed with existing blocking alerts, because the scan
32-
comparison returns unchanged packages without their alerts. The CLI now reads
33-
them from the full scan, at the cost of one more API request. The GitLab
34-
security report and FOSSA output get the same alerts.
35-
- `--strict-blocking` listed each finding on an added package twice.
36-
- A package with several CVEs of the same severity reported only one, so a
37-
blocking advisory could be dropped behind a warning.
38-
- A token without the `repo:list` scope was reported as a missing repository,
39-
ending in a `repo:create` error.
31+
- `--strict-blocking`, the GitLab security report and FOSSA output include
32+
alerts on unchanged packages.
33+
- `--strict-blocking` no longer lists findings on added packages twice.
34+
- Every CVE on a package is reported, not one per severity.
35+
- A token without `repo:list` is reported as a missing scope, not a missing
36+
repository.
4037

4138
## 2.12.1
4239

‎socketsecurity/core/messages.py‎

Lines changed: 103 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,12 @@ def generate_uuid_from_alert_gitlab(alert: Issue) -> str:
622622
"""
623623
# Create unique string from alert key properties
624624
unique_str = f"{alert.pkg_name}:{alert.pkg_version}:{alert.type}:{alert.severity}"
625+
# Every CVE of one severity on a package shares a type, so the advisory ID
626+
# keeps them apart. It's appended only when present, so other findings
627+
# keep the IDs GitLab already tracks them by.
628+
advisory = alert.advisory_id() if hasattr(alert, "advisory_id") else ""
629+
if advisory:
630+
unique_str += f":{advisory}"
625631

626632
# Generate UUID5 (deterministic) from namespace and unique string
627633
namespace = uuid.UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8') # DNS namespace
@@ -772,9 +778,15 @@ def create_security_comment_gitlab(diff: Diff) -> dict:
772778
dep_files_map: dict = {}
773779

774780
all_alerts = list(diff.new_alerts) + list(getattr(diff, 'unchanged_alerts', []))
781+
seen_ids = set()
775782
for alert in all_alerts:
783+
vulnerability_id = Messages.generate_uuid_from_alert_gitlab(alert)
784+
# GitLab expects each finding ID once per report.
785+
if vulnerability_id in seen_ids:
786+
continue
787+
seen_ids.add(vulnerability_id)
776788
vulnerability = {
777-
"id": Messages.generate_uuid_from_alert_gitlab(alert),
789+
"id": vulnerability_id,
778790
"category": "dependency_scanning",
779791
"name": alert.title if hasattr(alert, 'title') else f"{alert.type} in {alert.pkg_name}",
780792
"message": f"{alert.pkg_name}@{alert.pkg_version}: {alert.title if hasattr(alert, 'title') else alert.type}",
@@ -925,6 +937,67 @@ def get_view_report_url(diff: Diff) -> str:
925937
return diff.report_url
926938
return ""
927939

940+
@staticmethod
941+
def license_group_row(alerts: list, diff: Diff, config, show_ignore: bool) -> str:
942+
"""One row of the security comment table for a package's license policy violations."""
943+
row = ""
944+
action = "Block" if any(alert.error for alert in alerts) else "Warn"
945+
first_alert = alerts[0]
946+
947+
# Use orange diamond for license policy violations
948+
license_icon = "🔶"
949+
950+
license_label = Messages.html_text(
951+
f"{first_alert.pkg_name}@{first_alert.pkg_version}"
952+
)
953+
license_marker = Messages.comment_marker_text(
954+
f"{first_alert.pkg_name}@{first_alert.pkg_version}"
955+
)
956+
957+
# Build license findings list
958+
license_findings = []
959+
for alert in alerts:
960+
license_findings.append(alert.title)
961+
962+
row += f"""
963+
<!-- start-socket-alert-{license_marker} -->
964+
<tr>
965+
<td><strong>{action}</strong></td>
966+
<td align="center">{license_icon}</td>
967+
<td>
968+
<details>
969+
<summary>{license_label} has a License Policy Violation.</summary>
970+
<p><strong>License findings:</strong></p>
971+
<ul>
972+
"""
973+
for finding in license_findings:
974+
row += f" <li>{Messages.html_text(finding)}</li>\n"
975+
976+
977+
# Generate proper manifest URL for license violations
978+
license_manifest_url = Messages.get_manifest_file_url(diff, first_alert.manifests, config)
979+
980+
license_ignore_html = (
981+
f"<p><em>Mark the package as acceptable risk:</em> To ignore this alert only in this pull request, reply with the comment "
982+
f"<code>@SocketSecurity ignore {Messages.html_text(first_alert.pkg_type)}/{license_label}</code>. "
983+
f"You can also ignore all packages with <code>@SocketSecurity ignore-all</code>. "
984+
f"To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.</p>"
985+
) if show_ignore else ""
986+
row += f""" </ul>
987+
<p><strong>From:</strong> <a href="{Messages.html_attr(license_manifest_url)}">Manifest File</a></p>
988+
<p>ℹ️ Read more on: <a href="{Messages.html_attr(first_alert.purl)}">This package</a> | <a href="https://socket.dev/alerts/license">What is a license policy violation?</a></p>
989+
<blockquote>
990+
<p><em>Next steps:</em> Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at <strong>support@socket.dev</strong>.</p>
991+
<p><em>Suggestion:</em> Find a package that does not violate your license policy or adjust your policy to allow this package's license.</p>
992+
{license_ignore_html}
993+
</blockquote>
994+
</details>
995+
</td>
996+
</tr>
997+
<!-- end-socket-alert-{license_marker} -->
998+
"""
999+
return row
1000+
9281001
@staticmethod
9291002
def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> str:
9301003
"""One row of the security comment table for a non-license alert."""
@@ -1048,61 +1121,7 @@ def security_comment_template(diff: Diff, config=None) -> str:
10481121

10491122
# Add license policy violation entries grouped by PURL
10501123
for purl_key, alerts in license_groups.items():
1051-
action = "Block" if any(alert.error for alert in alerts) else "Warn"
1052-
first_alert = alerts[0]
1053-
1054-
# Use orange diamond for license policy violations
1055-
license_icon = "🔶"
1056-
1057-
license_label = Messages.html_text(
1058-
f"{first_alert.pkg_name}@{first_alert.pkg_version}"
1059-
)
1060-
license_marker = Messages.comment_marker_text(
1061-
f"{first_alert.pkg_name}@{first_alert.pkg_version}"
1062-
)
1063-
1064-
# Build license findings list
1065-
license_findings = []
1066-
for alert in alerts:
1067-
license_findings.append(alert.title)
1068-
1069-
comment += f"""
1070-
<!-- start-socket-alert-{license_marker} -->
1071-
<tr>
1072-
<td><strong>{action}</strong></td>
1073-
<td align="center">{license_icon}</td>
1074-
<td>
1075-
<details>
1076-
<summary>{license_label} has a License Policy Violation.</summary>
1077-
<p><strong>License findings:</strong></p>
1078-
<ul>
1079-
"""
1080-
for finding in license_findings:
1081-
comment += f" <li>{Messages.html_text(finding)}</li>\n"
1082-
1083-
1084-
# Generate proper manifest URL for license violations
1085-
license_manifest_url = Messages.get_manifest_file_url(diff, first_alert.manifests, config)
1086-
1087-
license_ignore_html = (
1088-
f"<p><em>Mark the package as acceptable risk:</em> To ignore this alert only in this pull request, reply with the comment "
1089-
f"<code>@SocketSecurity ignore {Messages.html_text(first_alert.pkg_type)}/{license_label}</code>. "
1090-
f"You can also ignore all packages with <code>@SocketSecurity ignore-all</code>. "
1091-
f"To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.</p>"
1092-
) if show_ignore else ""
1093-
comment += f""" </ul>
1094-
<p><strong>From:</strong> <a href="{Messages.html_attr(license_manifest_url)}">Manifest File</a></p>
1095-
<p>ℹ️ Read more on: <a href="{Messages.html_attr(first_alert.purl)}">This package</a> | <a href="https://socket.dev/alerts/license">What is a license policy violation?</a></p>
1096-
<blockquote>
1097-
<p><em>Next steps:</em> Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at <strong>support@socket.dev</strong>.</p>
1098-
<p><em>Suggestion:</em> Find a package that does not violate your license policy or adjust your policy to allow this package's license.</p>
1099-
{license_ignore_html}
1100-
</blockquote>
1101-
</details>
1102-
</td>
1103-
</tr>
1104-
<!-- end-socket-alert-{license_marker} -->
1105-
"""
1124+
comment += Messages.license_group_row(alerts, diff, config, show_ignore)
11061125

11071126
if not security_alerts and not license_groups:
11081127
comment += """
@@ -1152,12 +1171,28 @@ def existing_comment_alerts(diff: Diff, config) -> list:
11521171
def existing_alerts_section(
11531172
alerts: list, diff: Diff, config, show_ignore: bool, view_report_url: str, size_so_far: int
11541173
) -> str:
1155-
"""Table of alerts on packages this change didn't touch, blocking ones first."""
1156-
ordered = sorted(alerts, key=lambda alert: not alert.error)
1157-
blocking = sum(1 for alert in ordered if alert.error)
1174+
"""Table of alerts on packages this change didn't touch, blocking ones first.
1175+
1176+
License policy violations are grouped per package, as in the table of new
1177+
alerts.
1178+
"""
1179+
entries = []
1180+
license_groups: dict = {}
1181+
for alert in alerts:
1182+
if alert.type == "licenseSpdxDisj":
1183+
purl_key = f"{alert.pkg_type}/{alert.pkg_name}@{alert.pkg_version}"
1184+
if purl_key not in license_groups:
1185+
license_groups[purl_key] = []
1186+
entries.append(license_groups[purl_key])
1187+
license_groups[purl_key].append(alert)
1188+
else:
1189+
entries.append([alert])
1190+
entries.sort(key=lambda group: not any(alert.error for alert in group))
1191+
1192+
blocking = sum(1 for alert in alerts if alert.error)
11581193
section = f"""
11591194
<!-- start-socket-existing-alerts -->
1160-
**Existing alerts ({len(ordered)}).** These are on dependencies this change didn't add or update. `--strict-blocking` fails the run on the {blocking} with a **Block** action.
1195+
**Existing alerts ({len(alerts)}).** These are on dependencies this change didn't add or update. `--strict-blocking` fails the run on the {blocking} with a **Block** action.
11611196
11621197
<table>
11631198
<thead>
@@ -1174,16 +1209,19 @@ def existing_alerts_section(
11741209
</table>
11751210
"""
11761211
shown = 0
1177-
for alert in ordered:
1178-
row = Messages.security_alert_row(alert, diff, config, show_ignore)
1212+
for group in entries:
1213+
if group[0].type == "licenseSpdxDisj":
1214+
row = Messages.license_group_row(group, diff, config, show_ignore)
1215+
else:
1216+
row = Messages.security_alert_row(group[0], diff, config, show_ignore)
11791217
if size_so_far + len(section) + len(row) + len(closing) > Messages.SECURITY_COMMENT_SIZE_BUDGET:
11801218
break
11811219
section += row
1182-
shown += 1
1220+
shown += len(group)
11831221
section += closing
1184-
if shown < len(ordered):
1222+
if shown < len(alerts):
11851223
section += (
1186-
f"\n{len(ordered) - shown} more existing alert(s) are not shown here to keep "
1224+
f"\n{len(alerts) - shown} more existing alert(s) are not shown here to keep "
11871225
f"this comment within GitHub's size limit. See the [full report]({view_report_url}?action=error%2Cwarn).\n"
11881226
)
11891227
return section + "<!-- end-socket-existing-alerts -->\n"

‎tests/core/test_alert_consolidation.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,24 @@ def test_title_already_naming_the_advisory_is_left_alone():
113113
alert.title = "GHSA-aaaa-aaaa-aaaa in lodash"
114114

115115
assert Messages.alert_title_with_advisory(alert) == "GHSA-aaaa-aaaa-aaaa in lodash"
116+
117+
118+
def test_gitlab_report_keeps_each_advisory_as_its_own_finding():
119+
diff = Diff()
120+
diff.new_alerts = [_cve("GHSA-aaaa-aaaa-aaaa", error=True), _cve("GHSA-bbbb-bbbb-bbbb", error=True)]
121+
122+
report = Messages.create_security_comment_gitlab(diff)
123+
124+
ids = [v["id"] for v in report["vulnerabilities"]]
125+
assert len(ids) == 2
126+
assert len(set(ids)) == 2
127+
128+
129+
def test_gitlab_ids_for_findings_without_an_advisory_are_unchanged():
130+
"""GitLab tracks findings by ID across pipelines, so these must not move."""
131+
import uuid
132+
133+
alert = Issue(pkg_name="a", pkg_version="1.0.0", type="malware", severity="critical", props={})
134+
expected = uuid.uuid5(uuid.UUID("6ba7b810-9dad-11d1-80b4-00c04fd430c8"), "a:1.0.0:malware:critical")
135+
136+
assert Messages.generate_uuid_from_alert_gitlab(alert) == str(expected)

‎tests/core/test_existing_alerts.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,16 @@ def test_pr_comment_stays_within_the_size_budget(monkeypatch):
291291
assert len(comment) < 6500
292292
assert "more existing alert(s) are not shown" in comment
293293
assert "Existing alerts (20)" in comment
294+
295+
296+
def test_existing_license_violations_use_the_license_row():
297+
license_alert = _issue("gamma")
298+
license_alert.type = "licenseSpdxDisj"
299+
license_alert.title = "License policy violation: GPL-3.0"
300+
diff = _comment_diff(new=[_issue("alpha")], existing=[license_alert])
301+
302+
comment = Messages.security_comment_template(diff, _config())
303+
existing = comment[comment.index("Existing alerts"):]
304+
305+
assert "gamma@1.0.0 has a License Policy Violation." in existing
306+
assert "What is known malware?" not in existing

0 commit comments

Comments
 (0)