Skip to content

Commit b560955

Browse files
authored
Add avid for gitlab (#1952)
* Add AVID for gitlab advisories without package Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Add tests Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> --------- Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent f423cb7 commit b560955

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

vulnerabilities/pipelines/v2_importers/gitlab_importer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def get_purl(package_slug, purl_type_by_gitlab_scheme, logger):
156156
"""
157157
parts = [p for p in package_slug.strip("/").split("/") if p]
158158
gitlab_scheme = parts[0]
159-
purl_type = purl_type_by_gitlab_scheme[gitlab_scheme]
159+
purl_type = purl_type_by_gitlab_scheme.get(gitlab_scheme)
160+
if not purl_type:
161+
return
160162
if gitlab_scheme == "go":
161163
name = "/".join(parts[1:])
162164
return PackageURL(type=purl_type, namespace=None, name=name)
@@ -262,11 +264,13 @@ def parse_gitlab_advisory(
262264
f"parse_yaml_file: purl is not valid: {file!r} {package_slug!r}", level=logging.ERROR
263265
)
264266
return AdvisoryData(
267+
advisory_id=advisory_id,
265268
aliases=aliases,
266269
summary=summary,
267270
references_v2=references,
268271
date_published=date_published,
269272
url=advisory_url,
273+
original_advisory_text=json.dumps(gitlab_advisory, indent=2, ensure_ascii=False),
270274
)
271275
affected_version_range = None
272276
fixed_versions = gitlab_advisory.get("fixed_versions") or []

vulnerabilities/tests/pipelines/test_gitlab_v2_importer.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
66
#
77

8+
from datetime import datetime
89
from pathlib import Path
910
from unittest.mock import MagicMock
1011
from unittest.mock import patch
1112

1213
import pytest
14+
import saneyaml
1315

1416
from vulnerabilities.importer import AdvisoryData
17+
from vulnerabilities.pipelines.v2_importers.gitlab_importer import parse_gitlab_advisory
1518

1619

1720
@pytest.fixture
@@ -151,3 +154,55 @@ def test_advisories_count_empty(mock_vcs_response, mock_fetch_via_vcs, tmp_path)
151154

152155
count = pipeline.advisories_count()
153156
assert count == 0
157+
158+
159+
@pytest.fixture
160+
def gitlab_advisory_yaml(tmp_path):
161+
content = {
162+
"identifier": "GMS-2018-26",
163+
"package_slug": "pypi/django",
164+
"title": "Incorrect header injection check",
165+
"description": "django isn't properly protected against HTTP header injection.",
166+
"pubdate": "2018-03-15",
167+
"affected_range": "<2.0.1",
168+
"fixed_versions": ["v2.0.1"],
169+
"urls": ["https://github.com/django/django/pull/123"],
170+
"cwe_ids": ["CWE-1035", "CWE-937"],
171+
"identifiers": ["GMS-2018-26"],
172+
}
173+
174+
advisory_path = tmp_path / "GMS-2018-26.yaml"
175+
advisory_path.write_text(saneyaml.dump(content))
176+
return advisory_path, content
177+
178+
179+
def test_parse_gitlab_advisory_with_no_purl(monkeypatch, gitlab_advisory_yaml):
180+
file_path, advisory_data = gitlab_advisory_yaml
181+
182+
# Mock get_purl to always return None
183+
def mock_get_purl(package_slug, purl_type_by_gitlab_scheme, logger):
184+
return None
185+
186+
# Patch the dependencies
187+
import vulnerabilities.pipelines.v2_importers.gitlab_importer as gitlab_module
188+
189+
monkeypatch.setattr(gitlab_module, "get_purl", mock_get_purl)
190+
191+
dummy_logger = lambda *args, **kwargs: None # Ignore logging in test
192+
193+
result = parse_gitlab_advisory(
194+
file=file_path,
195+
base_path=file_path.parent,
196+
gitlab_scheme_by_purl_type={},
197+
purl_type_by_gitlab_scheme={},
198+
logger=dummy_logger,
199+
)
200+
201+
assert isinstance(result, AdvisoryData)
202+
assert result.advisory_id == "pypi/django/GMS-2018-26"
203+
assert result.aliases == ["GMS-2018-26"]
204+
assert result.summary.startswith("Incorrect header")
205+
assert result.url.startswith("https://gitlab.com/gitlab-org/advisories-community")
206+
assert isinstance(result.date_published, datetime)
207+
assert result.date_published.year == 2018
208+
assert result.affected_packages == [] # Because get_purl was mocked to return None

0 commit comments

Comments
 (0)