-
-
Notifications
You must be signed in to change notification settings - Fork 297
Add commit collection support to Apache Tomcat V2 importer #2195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
5316e68
d93a7c8
1ce5df0
23eb77f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| from collections import defaultdict | ||
| from collections import namedtuple | ||
| from typing import Iterable | ||
| from typing import List | ||
|
|
||
| import requests | ||
| from bs4 import BeautifulSoup | ||
|
|
@@ -25,8 +26,18 @@ | |
|
|
||
| from vulnerabilities.importer import AdvisoryDataV2 | ||
| from vulnerabilities.importer import AffectedPackageV2 | ||
| from vulnerabilities.importer import PackageCommitPatchData | ||
| from vulnerabilities.importer import ReferenceV2 | ||
| from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 | ||
|
|
||
| GITHUB_COMMIT_URL_RE = re.compile( | ||
| r"https?://github\.com/apache/tomcat/commit/(?P<commit_hash>[0-9a-f]{5,40})" | ||
| ) | ||
| GITBOX_COMMIT_URL_RE = re.compile( | ||
| r"https?://gitbox\.apache\.org/repos/asf\?p=tomcat\.git;a=commit;h=(?P<commit_hash>[0-9a-f]{5,40})" | ||
| ) | ||
| TOMCAT_VCS_URL = "https://github.com/apache/tomcat" | ||
|
|
||
|
|
||
| class ApacheTomcatImporterPipeline(VulnerableCodeBaseImporterPipelineV2): | ||
| """ | ||
|
|
@@ -75,6 +86,9 @@ def collect_advisories(self) -> Iterable[AdvisoryDataV2]: | |
| grouped[advisory.cve].append(advisory) | ||
| for cve, advisory_list in grouped.items(): | ||
| affected_packages = [] | ||
| references = [] | ||
| all_commit_patches = [] | ||
|
|
||
| for advisory in advisory_list: | ||
| self.log(f"Processing advisory {advisory.cve}") | ||
| apache_range = to_version_ranges_apache( | ||
|
|
@@ -88,10 +102,19 @@ def collect_advisories(self) -> Iterable[AdvisoryDataV2]: | |
| advisory.fixed_in, | ||
| ) | ||
|
|
||
| commit_patches = get_commit_patches( | ||
| advisory.commit_urls, | ||
| ) | ||
| all_commit_patches.extend(commit_patches) | ||
|
|
||
| for ref_url in advisory.reference_urls: | ||
| references.append(ReferenceV2(url=ref_url)) | ||
|
|
||
| affected_packages.append( | ||
| AffectedPackageV2( | ||
| package=PackageURL(type="apache", name="tomcat"), | ||
| affected_version_range=apache_range, | ||
| fixed_by_commit_patches=commit_patches, | ||
| ) | ||
| ) | ||
|
|
||
|
|
@@ -103,13 +126,15 @@ def collect_advisories(self) -> Iterable[AdvisoryDataV2]: | |
| name="tomcat", | ||
| ), | ||
| affected_version_range=maven_range, | ||
| fixed_by_commit_patches=commit_patches, | ||
| ) | ||
| ) | ||
| page_id = page_url.split("/")[-1].replace(".html", "") | ||
| yield AdvisoryDataV2( | ||
| advisory_id=f"{page_id}/{cve}", | ||
| summary=advisory_list[0].summary, | ||
| affected_packages=affected_packages, | ||
| references=references, | ||
| url=page_url, | ||
| ) | ||
|
|
||
|
|
@@ -260,6 +285,8 @@ class TomcatAdvisoryData: | |
| summary: str | ||
| fixed_in: str | ||
| affected_versions: str | ||
| commit_urls: List[str] = dataclasses.field(default_factory=list) | ||
| reference_urls: List[str] = dataclasses.field(default_factory=list) | ||
|
|
||
|
|
||
| def parse_tomcat_security(html_content): | ||
|
|
@@ -285,36 +312,60 @@ def parse_tomcat_security(html_content): | |
|
|
||
| if strong and cve_link: | ||
| if current: | ||
| results.append(current) | ||
| results.append(_finalize_advisory(current)) | ||
|
|
||
| current = { | ||
| "cve": cve_link.get_text(strip=True), | ||
| "summary": strong.get_text(" ", strip=True), | ||
| "affected_versions": None, | ||
| "fixed_in": fixed_in, | ||
| "commit_urls": [], | ||
| "reference_urls": [], | ||
| } | ||
| continue | ||
|
|
||
| if current: | ||
| text = p.get_text(" ", strip=True) | ||
|
|
||
| if "was fixed" in text.lower(): | ||
| for link in p.find_all("a", href=True): | ||
| href = link["href"] | ||
| if GITHUB_COMMIT_URL_RE.match(href) or GITBOX_COMMIT_URL_RE.match(href): | ||
| current["commit_urls"].append(href) | ||
| current["reference_urls"].append(href) | ||
|
|
||
| if text.startswith("Affects:"): | ||
| current["affected_versions"] = text.replace("Affects:", "").strip() | ||
| current = TomcatAdvisoryData( | ||
| cve=current["cve"], | ||
| summary=current["summary"], | ||
| affected_versions=current["affected_versions"], | ||
| fixed_in=current["fixed_in"], | ||
| ) | ||
| results.append(current) | ||
| results.append(_finalize_advisory(current)) | ||
| current = None | ||
|
|
||
| if current: | ||
| current = TomcatAdvisoryData( | ||
| cve=current["cve"], | ||
| summary=current["summary"], | ||
| affected_versions=current["affected_versions"], | ||
| fixed_in=current["fixed_in"], | ||
| ) | ||
| results.append(current) | ||
| results.append(_finalize_advisory(current)) | ||
|
|
||
| return results | ||
|
|
||
|
|
||
| def _finalize_advisory(current): | ||
| return TomcatAdvisoryData( | ||
| cve=current["cve"], | ||
| summary=current["summary"], | ||
| affected_versions=current["affected_versions"], | ||
| fixed_in=current["fixed_in"], | ||
| commit_urls=current.get("commit_urls", []), | ||
| reference_urls=current.get("reference_urls", []), | ||
| ) | ||
|
|
||
|
|
||
| def get_commit_patches(commit_urls): | ||
| commit_patches = [] | ||
| for url in commit_urls: | ||
| match = GITHUB_COMMIT_URL_RE.match(url) or GITBOX_COMMIT_URL_RE.match(url) | ||
| if match: | ||
| commit_hash = match.group("commit_hash") | ||
| commit_patches.append( | ||
| PackageCommitPatchData( | ||
| vcs_url=TOMCAT_VCS_URL, | ||
| commit_hash=commit_hash, | ||
| ) | ||
| ) | ||
| return commit_patches | ||
|
Comment on lines
+366
to
+386
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use see other importers like:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. append_patch_classifications used in my changes |
||
Uh oh!
There was an error while loading. Please reload this page.