-
Notifications
You must be signed in to change notification settings - Fork 201
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
Add Apache HTTPD advisory importer #261
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# Copyright (c) nexB Inc. and others. All rights reserved. | ||
# http://nexb.com and https://github.com/nexB/vulnerablecode/ | ||
# The VulnerableCode software is licensed under the Apache License version 2.0. | ||
# Data generated with VulnerableCode require an acknowledgment. | ||
# | ||
# You may not use this software except in compliance with the License. | ||
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software distributed | ||
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
# CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations under the License. | ||
# | ||
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode | ||
# derivative work, you must accompany this data with the following acknowledgment: | ||
# | ||
# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
# VulnerableCode should be considered or used as legal advice. Consult an Attorney | ||
# for any legal advice. | ||
# VulnerableCode is a free software tool from nexB Inc. and others. | ||
# Visit https://github.com/nexB/vulnerablecode/ for support and download. | ||
|
||
from dataclasses import dataclass | ||
from xml.etree import ElementTree | ||
|
||
import requests | ||
from packageurl import PackageURL | ||
|
||
from vulnerabilities.data_source import Advisory | ||
from vulnerabilities.data_source import DataSource | ||
from vulnerabilities.data_source import DataSourceConfiguration | ||
|
||
|
||
@dataclass | ||
class ApacheHTTPDDataSourceConfiguration(DataSourceConfiguration): | ||
etags: dict | ||
|
||
|
||
class ApacheHTTPDDataSource(DataSource): | ||
|
||
CONFIG_CLASS = ApacheHTTPDDataSourceConfiguration | ||
url = "https://httpd.apache.org/security/vulnerabilities-httpd.xml" | ||
|
||
def updated_advisories(self): | ||
# Etags are like hashes of web responses. We maintain | ||
# (url, etag) mappings in the DB. `create_etag` creates | ||
# (url, etag) pair. If a (url, etag) already exists then the code | ||
# skips processing the response further to avoid duplicate work | ||
|
||
if self.create_etag(self.url): | ||
data = fetch_xml(self.url) | ||
advisories = to_advisories(data) | ||
return self.batch_advisories(advisories) | ||
|
||
return [] | ||
|
||
def create_etag(self, url): | ||
etag = requests.head(url).headers.get("ETag") | ||
if not etag: | ||
return True | ||
|
||
elif url in self.config.etags: | ||
if self.config.etags[url] == etag: | ||
return False | ||
|
||
self.config.etags[url] = etag | ||
return True | ||
|
||
|
||
def to_advisories(data): | ||
advisories = [] | ||
for issue in data: | ||
resolved_packages = [] | ||
impacted_packages = [] | ||
for info in issue: | ||
if info.tag == "cve": | ||
cve = info.attrib["name"] | ||
|
||
if info.tag == "title": | ||
summary = info.text | ||
|
||
if info.tag == "fixed": | ||
resolved_packages.append( | ||
PackageURL(type="apache", name="httpd", version=info.attrib["version"]) | ||
) | ||
|
||
if info.tag == "affects" or info.tag == "maybeaffects": | ||
impacted_packages.append( | ||
PackageURL(type="apache", name="httpd", version=info.attrib["version"]) | ||
) | ||
|
||
advisories.append( | ||
Advisory( | ||
cve_id=cve, | ||
summary=summary, | ||
impacted_package_urls=impacted_packages, | ||
resolved_package_urls=resolved_packages, | ||
) | ||
) | ||
|
||
return advisories | ||
|
||
|
||
def fetch_xml(url): | ||
resp = requests.get(url).content | ||
return ElementTree.fromstring(resp) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just FYI, at some point of time, we should review together the etag story and why this is used in parallel of https://github.com/nexB/vulnerablecode/pull/261/files#diff-941e9f048d96b3af4998962020f96430R105 and not with it?
I would like to make sure that I grok the caching approach alright.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing.
The gist of it is:
create_tag
uses aHEADER
request to just fetch the etag. If this etag matches with old etag we stop the process because the data hasn't changed. Otherwise use https://github.com/nexB/vulnerablecode/pull/261/files#diff-941e9f048d96b3af4998962020f96430R105 which is just normal download.