Skip to content

Commit 8340471

Browse files
committed
Link "This package" in PR comments to the package page
Each security comment row linked "This package" to the bare package coordinate (alert.purl, e.g. handlebars@4.0.11), so GitHub and GitLab resolved it as a relative link to a page that does not exist. The alert rows have done this since the HTML comment template was introduced, and license rows since they were added; the --strict-blocking existing-alerts table reuses both templates. Both rows now use alert.url, the package overview URL that Package.socket_url builds with the namespace (npm scope, Maven groupId, Go module path). The Markdown, Slack and Jira outputs already link the package with that URL.
1 parent 4250b5c commit 8340471

6 files changed

Lines changed: 167 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## 2.13.1
4+
5+
### Fixed
6+
7+
- The "This package" link on each PR/MR security comment row (new alerts,
8+
existing alerts and license violations) now opens the package's page on
9+
socket.dev. It pointed at the bare package coordinate, such as
10+
`handlebars@4.0.11`, which GitHub and GitLab resolve as a broken relative link.
11+
312
## 2.13.0
413

514
### Upgrade notes

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.13.0"
9+
version = "2.13.1"
1010
requires-python = ">= 3.11"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [

‎socketsecurity/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.13.0'
2+
__version__ = '2.13.1'
33
USER_AGENT_NAME = 'socket-python-cli'
44
USER_AGENT = f'{USER_AGENT_NAME}/{__version__}'

‎socketsecurity/core/messages.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ def license_group_row(alerts: list, diff: Diff, config, show_ignore: bool) -> st
990990
) if show_ignore else ""
991991
row += f""" </ul>
992992
<p><strong>From:</strong> <a href="{Messages.html_attr(license_manifest_url)}">Manifest File</a></p>
993-
<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>
993+
<p>ℹ️ Read more on: <a href="{Messages.html_attr(first_alert.url)}">This package</a> | <a href="https://socket.dev/alerts/license">What is a license policy violation?</a></p>
994994
<blockquote>
995995
<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>
996996
<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>
@@ -1040,7 +1040,7 @@ def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> s
10401040
{patched_version_html}
10411041
<p><strong>Source:</strong> <a href="{Messages.html_attr(manifest_url)}">Manifest File</a></p>
10421042
<p>ℹ️ Read more on:
1043-
<a href="{Messages.html_attr(alert.purl)}">This package</a> |
1043+
<a href="{Messages.html_attr(alert.url)}">This package</a> |
10441044
<a href="{Messages.html_attr(alert.url)}">This alert</a> |
10451045
<a href="https://socket.dev/alerts/malware">What is known malware?</a></p>
10461046
<blockquote>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
"""The "This package" link in PR/MR security comments must be an absolute URL.
2+
3+
It used to be the bare package coordinate (``handlebars@4.0.11``), which GitHub
4+
and GitLab resolve as a relative link to a page that does not exist. Each case
5+
here goes from a diff artifact, as the API returns it, through the same Package
6+
and Issue construction a real run uses, to the rendered comment.
7+
"""
8+
9+
import re
10+
from dataclasses import dataclass
11+
from types import SimpleNamespace
12+
13+
import pytest
14+
15+
from socketsecurity.core import Core
16+
from socketsecurity.core.classes import Diff, Package
17+
from socketsecurity.core.messages import Messages
18+
19+
20+
@dataclass
21+
class _FakeConfig:
22+
disable_ignore: bool = False
23+
scm: str = "github"
24+
strict_blocking: bool = True
25+
26+
27+
# (artifact fields, expected package page URL)
28+
ECOSYSTEMS = {
29+
"npm": (
30+
dict(type="npm", name="handlebars", version="4.0.11"),
31+
"https://socket.dev/npm/package/handlebars/overview/4.0.11",
32+
),
33+
"npm-scoped": (
34+
dict(type="npm", namespace="@babel", name="traverse", version="7.22.5"),
35+
"https://socket.dev/npm/package/@babel/traverse/overview/7.22.5",
36+
),
37+
"pypi": (
38+
dict(type="pypi", name="django", version="2.2"),
39+
"https://socket.dev/pypi/package/django/overview/2.2",
40+
),
41+
"maven": (
42+
dict(type="maven", namespace="org.apache.logging.log4j", name="log4j-core", version="2.14.1"),
43+
"https://socket.dev/maven/package/org.apache.logging.log4j/log4j-core/overview/2.14.1",
44+
),
45+
"golang": (
46+
dict(type="golang", namespace="github.com/gin-gonic", name="gin", version="v1.6.0"),
47+
"https://socket.dev/golang/package/github.com/gin-gonic/gin/overview/v1.6.0",
48+
),
49+
"nuget": (
50+
dict(type="nuget", name="Newtonsoft.Json", version="12.0.1"),
51+
"https://socket.dev/nuget/package/Newtonsoft.Json/overview/12.0.1",
52+
),
53+
"gem": (
54+
dict(type="gem", name="rails", version="5.2.0"),
55+
"https://socket.dev/gem/package/rails/overview/5.2.0",
56+
),
57+
"cargo": (
58+
dict(type="cargo", name="smallvec", version="0.6.9"),
59+
"https://socket.dev/cargo/package/smallvec/overview/0.6.9",
60+
),
61+
}
62+
63+
CVE_ALERT = {"type": "criticalCVE", "key": "cve-key", "severity": "critical", "action": "error"}
64+
LICENSE_ALERT = {"type": "licenseSpdxDisj", "key": "license-key", "severity": "low", "action": "error"}
65+
66+
67+
def _issues(artifact: dict, alert: dict) -> list:
68+
"""Build Issues the way a scan does: diff artifact -> Package -> Issue."""
69+
package = Package.from_diff_artifact({
70+
"id": f"{artifact['type']}-{artifact['name']}",
71+
"score": {},
72+
"topLevelAncestors": [],
73+
"direct": True,
74+
"manifestFiles": [{"file": "manifest"}],
75+
"alerts": [alert],
76+
**artifact,
77+
})
78+
package = Core.update_package_values(package)
79+
core = Core.__new__(Core)
80+
core.config = SimpleNamespace(all_issues=SimpleNamespace())
81+
collection = core.add_package_alerts_to_collection(package, {}, {package.id: package})
82+
return collection[alert["key"]]
83+
84+
85+
def _this_package_hrefs(body: str) -> list:
86+
return re.findall(r'<a href="([^"]*)">This package</a>', body)
87+
88+
89+
def _diff(alerts: list) -> Diff:
90+
diff = Diff()
91+
diff.id = "scan"
92+
diff.diff_url = "https://socket.dev/dashboard/org/acme/diff/abc"
93+
diff.new_alerts = alerts
94+
diff.unchanged_alerts = []
95+
return diff
96+
97+
98+
@pytest.mark.parametrize("ecosystem", ECOSYSTEMS)
99+
def test_alert_row_links_the_package_page(ecosystem):
100+
artifact, expected = ECOSYSTEMS[ecosystem]
101+
alerts = _issues(artifact, CVE_ALERT)
102+
103+
body = Messages.security_comment_template(_diff(alerts), _FakeConfig())
104+
105+
assert _this_package_hrefs(body) == [expected]
106+
107+
108+
@pytest.mark.parametrize("ecosystem", ECOSYSTEMS)
109+
def test_license_row_links_the_package_page(ecosystem):
110+
artifact, expected = ECOSYSTEMS[ecosystem]
111+
alerts = _issues(artifact, LICENSE_ALERT)
112+
113+
body = Messages.security_comment_template(_diff(alerts), _FakeConfig())
114+
115+
assert _this_package_hrefs(body) == [expected]
116+
117+
118+
@pytest.mark.parametrize("alert", [CVE_ALERT, LICENSE_ALERT], ids=["alert", "license"])
119+
def test_existing_alerts_table_links_the_package_page(alert):
120+
"""The --strict-blocking existing-alerts table reuses the same row templates."""
121+
artifact, expected = ECOSYSTEMS["npm-scoped"]
122+
diff = _diff([])
123+
diff.unchanged_alerts = _issues(artifact, alert)
124+
125+
body = Messages.security_comment_template(diff, _FakeConfig())
126+
127+
assert _this_package_hrefs(body) == [expected]
128+
129+
130+
@pytest.mark.parametrize("scm", ["github", "gitlab"])
131+
def test_no_relative_package_links_on_either_scm(scm):
132+
alerts = [
133+
alert
134+
for artifact, _ in ECOSYSTEMS.values()
135+
for alert in _issues(artifact, CVE_ALERT) + _issues(artifact, LICENSE_ALERT)
136+
]
137+
138+
body = Messages.security_comment_template(_diff(alerts), _FakeConfig(scm=scm))
139+
140+
hrefs = _this_package_hrefs(body)
141+
assert len(hrefs) == 2 * len(ECOSYSTEMS)
142+
assert all(href.startswith("https://socket.dev/") for href in hrefs)
143+
144+
145+
def test_package_url_is_attribute_escaped():
146+
artifact = dict(type="npm", name='evil"><script>', version="1.0.0")
147+
alerts = _issues(artifact, CVE_ALERT)
148+
149+
body = Messages.security_comment_template(_diff(alerts), _FakeConfig())
150+
151+
assert _this_package_hrefs(body) == [
152+
"https://socket.dev/npm/package/evil&quot;&gt;&lt;script&gt;/overview/1.0.0"
153+
]

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)