From 8340471041b146cc38a60efbc1deb1821ea75644 Mon Sep 17 00:00:00 2001 From: David Larsen Date: Fri, 20 Nov 2026 17:09:14 -0500 Subject: [PATCH 1/2] 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. --- CHANGELOG.md | 9 ++ pyproject.toml | 2 +- socketsecurity/__init__.py | 2 +- socketsecurity/core/messages.py | 4 +- tests/unit/test_comment_package_links.py | 153 +++++++++++++++++++++++ uv.lock | 2 +- 6 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 tests/unit/test_comment_package_links.py diff --git a/CHANGELOG.md b/CHANGELOG.md index ff28e72..1ed8976 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 2.13.1 + +### Fixed + +- The "This package" link on each PR/MR security comment row (new alerts, + existing alerts and license violations) now opens the package's page on + socket.dev. It pointed at the bare package coordinate, such as + `handlebars@4.0.11`, which GitHub and GitLab resolve as a broken relative link. + ## 2.13.0 ### Upgrade notes diff --git a/pyproject.toml b/pyproject.toml index 3604a71..355df9c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "hatchling.build" [project] name = "socketsecurity" -version = "2.13.0" +version = "2.13.1" requires-python = ">= 3.11" license = {"file" = "LICENSE"} dependencies = [ diff --git a/socketsecurity/__init__.py b/socketsecurity/__init__.py index 08835ec..a3c8f69 100644 --- a/socketsecurity/__init__.py +++ b/socketsecurity/__init__.py @@ -1,4 +1,4 @@ __author__ = 'socket.dev' -__version__ = '2.13.0' +__version__ = '2.13.1' USER_AGENT_NAME = 'socket-python-cli' USER_AGENT = f'{USER_AGENT_NAME}/{__version__}' diff --git a/socketsecurity/core/messages.py b/socketsecurity/core/messages.py index 2c09ed2..907087c 100644 --- a/socketsecurity/core/messages.py +++ b/socketsecurity/core/messages.py @@ -990,7 +990,7 @@ def license_group_row(alerts: list, diff: Diff, config, show_ignore: bool) -> st ) if show_ignore else "" row += f"""

From: Manifest File

-

ℹ️ Read more on: This package | What is a license policy violation?

+

ℹ️ Read more on: This package | What is a license policy violation?

Next steps: 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 support@socket.dev.

Suggestion: Find a package that does not violate your license policy or adjust your policy to allow this package's license.

@@ -1040,7 +1040,7 @@ def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> s {patched_version_html}

Source: Manifest File

ℹ️ Read more on: - This package | + This package | This alert | What is known malware?

diff --git a/tests/unit/test_comment_package_links.py b/tests/unit/test_comment_package_links.py new file mode 100644 index 0000000..aad39e2 --- /dev/null +++ b/tests/unit/test_comment_package_links.py @@ -0,0 +1,153 @@ +"""The "This package" link in PR/MR security comments must be an absolute URL. + +It used to be the bare package coordinate (``handlebars@4.0.11``), which GitHub +and GitLab resolve as a relative link to a page that does not exist. Each case +here goes from a diff artifact, as the API returns it, through the same Package +and Issue construction a real run uses, to the rendered comment. +""" + +import re +from dataclasses import dataclass +from types import SimpleNamespace + +import pytest + +from socketsecurity.core import Core +from socketsecurity.core.classes import Diff, Package +from socketsecurity.core.messages import Messages + + +@dataclass +class _FakeConfig: + disable_ignore: bool = False + scm: str = "github" + strict_blocking: bool = True + + +# (artifact fields, expected package page URL) +ECOSYSTEMS = { + "npm": ( + dict(type="npm", name="handlebars", version="4.0.11"), + "https://socket.dev/npm/package/handlebars/overview/4.0.11", + ), + "npm-scoped": ( + dict(type="npm", namespace="@babel", name="traverse", version="7.22.5"), + "https://socket.dev/npm/package/@babel/traverse/overview/7.22.5", + ), + "pypi": ( + dict(type="pypi", name="django", version="2.2"), + "https://socket.dev/pypi/package/django/overview/2.2", + ), + "maven": ( + dict(type="maven", namespace="org.apache.logging.log4j", name="log4j-core", version="2.14.1"), + "https://socket.dev/maven/package/org.apache.logging.log4j/log4j-core/overview/2.14.1", + ), + "golang": ( + dict(type="golang", namespace="github.com/gin-gonic", name="gin", version="v1.6.0"), + "https://socket.dev/golang/package/github.com/gin-gonic/gin/overview/v1.6.0", + ), + "nuget": ( + dict(type="nuget", name="Newtonsoft.Json", version="12.0.1"), + "https://socket.dev/nuget/package/Newtonsoft.Json/overview/12.0.1", + ), + "gem": ( + dict(type="gem", name="rails", version="5.2.0"), + "https://socket.dev/gem/package/rails/overview/5.2.0", + ), + "cargo": ( + dict(type="cargo", name="smallvec", version="0.6.9"), + "https://socket.dev/cargo/package/smallvec/overview/0.6.9", + ), +} + +CVE_ALERT = {"type": "criticalCVE", "key": "cve-key", "severity": "critical", "action": "error"} +LICENSE_ALERT = {"type": "licenseSpdxDisj", "key": "license-key", "severity": "low", "action": "error"} + + +def _issues(artifact: dict, alert: dict) -> list: + """Build Issues the way a scan does: diff artifact -> Package -> Issue.""" + package = Package.from_diff_artifact({ + "id": f"{artifact['type']}-{artifact['name']}", + "score": {}, + "topLevelAncestors": [], + "direct": True, + "manifestFiles": [{"file": "manifest"}], + "alerts": [alert], + **artifact, + }) + package = Core.update_package_values(package) + core = Core.__new__(Core) + core.config = SimpleNamespace(all_issues=SimpleNamespace()) + collection = core.add_package_alerts_to_collection(package, {}, {package.id: package}) + return collection[alert["key"]] + + +def _this_package_hrefs(body: str) -> list: + return re.findall(r'This package', body) + + +def _diff(alerts: list) -> Diff: + diff = Diff() + diff.id = "scan" + diff.diff_url = "https://socket.dev/dashboard/org/acme/diff/abc" + diff.new_alerts = alerts + diff.unchanged_alerts = [] + return diff + + +@pytest.mark.parametrize("ecosystem", ECOSYSTEMS) +def test_alert_row_links_the_package_page(ecosystem): + artifact, expected = ECOSYSTEMS[ecosystem] + alerts = _issues(artifact, CVE_ALERT) + + body = Messages.security_comment_template(_diff(alerts), _FakeConfig()) + + assert _this_package_hrefs(body) == [expected] + + +@pytest.mark.parametrize("ecosystem", ECOSYSTEMS) +def test_license_row_links_the_package_page(ecosystem): + artifact, expected = ECOSYSTEMS[ecosystem] + alerts = _issues(artifact, LICENSE_ALERT) + + body = Messages.security_comment_template(_diff(alerts), _FakeConfig()) + + assert _this_package_hrefs(body) == [expected] + + +@pytest.mark.parametrize("alert", [CVE_ALERT, LICENSE_ALERT], ids=["alert", "license"]) +def test_existing_alerts_table_links_the_package_page(alert): + """The --strict-blocking existing-alerts table reuses the same row templates.""" + artifact, expected = ECOSYSTEMS["npm-scoped"] + diff = _diff([]) + diff.unchanged_alerts = _issues(artifact, alert) + + body = Messages.security_comment_template(diff, _FakeConfig()) + + assert _this_package_hrefs(body) == [expected] + + +@pytest.mark.parametrize("scm", ["github", "gitlab"]) +def test_no_relative_package_links_on_either_scm(scm): + alerts = [ + alert + for artifact, _ in ECOSYSTEMS.values() + for alert in _issues(artifact, CVE_ALERT) + _issues(artifact, LICENSE_ALERT) + ] + + body = Messages.security_comment_template(_diff(alerts), _FakeConfig(scm=scm)) + + hrefs = _this_package_hrefs(body) + assert len(hrefs) == 2 * len(ECOSYSTEMS) + assert all(href.startswith("https://socket.dev/") for href in hrefs) + + +def test_package_url_is_attribute_escaped(): + artifact = dict(type="npm", name='evil">