diff --git a/CHANGELOG.md b/CHANGELOG.md index 165e42c..e5ec71f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # Changelog +## 2.13.3 + +### 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.2 ### Added diff --git a/pyproject.toml b/pyproject.toml index 5c88c15..82c5bbf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,7 @@ build-backend = "hatchling.build" [project] name = "socketsecurity" -version = "2.13.2" +version = "2.13.3" requires-python = ">= 3.11" license = {"file" = "LICENSE"} dependencies = [ diff --git a/socketsecurity/__init__.py b/socketsecurity/__init__.py index 7ba49e1..0871ade 100644 --- a/socketsecurity/__init__.py +++ b/socketsecurity/__init__.py @@ -1,4 +1,4 @@ __author__ = 'socket.dev' -__version__ = '2.13.2' +__version__ = '2.13.3' 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 33dfa9b..79a5949 100644 --- a/socketsecurity/core/messages.py +++ b/socketsecurity/core/messages.py @@ -1012,7 +1012,7 @@ def license_group_row(alerts: list, diff: Diff, config, show_ignore: bool) -> st row += f"""

From: Manifest File

{Messages.introduced_by_html(first_alert)} -

ℹ️ 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.

@@ -1063,7 +1063,7 @@ def security_alert_row(alert: Issue, diff: Diff, config, show_ignore: bool) -> s

Source: Manifest File

{Messages.introduced_by_html(alert)}

ℹ️ 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">