|
| 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"><script>/overview/1.0.0" |
| 153 | + ] |
0 commit comments