|
| 1 | +# Copyright (c) 2017 nexB Inc. and others. All rights reserved. |
| 2 | +# http://nexb.com and https://github.com/nexB/vulnerablecode/ |
| 3 | +# The VulnerableCode software is licensed under the Apache License version 2.0. |
| 4 | +# Data generated with VulnerableCode require an acknowledgment. |
| 5 | +# |
| 6 | +# You may not use this software except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +# specific language governing permissions and limitations under the License. |
| 12 | +# |
| 13 | +# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode |
| 14 | +# derivative work, you must accompany this data with the following acknowledgment: |
| 15 | +# |
| 16 | +# Generated with VulnerableCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 17 | +# OR CONDITIONS OF ANY KIND, either express or implied. No content created from |
| 18 | +# VulnerableCode should be considered or used as legal advice. Consult an Attorney |
| 19 | +# for any legal advice. |
| 20 | +# VulnerableCode is a free software code scanning tool from nexB Inc. and others. |
| 21 | +# Visit https://github.com/nexB/vulnerablecode/ for support and download. |
| 22 | + |
| 23 | + |
| 24 | +import asyncio |
| 25 | +import dataclasses |
| 26 | +from typing import Iterable |
| 27 | +from typing import List |
| 28 | +from typing import Mapping |
| 29 | +from typing import Set |
| 30 | +import xml.etree.ElementTree as ET |
| 31 | + |
| 32 | + |
| 33 | +from aiohttp import ClientSession |
| 34 | +from aiohttp.client_exceptions import ClientResponseError, ServerDisconnectedError |
| 35 | +import requests |
| 36 | + |
| 37 | + |
| 38 | +from vulnerabilities.data_source import OvalDataSource, DataSourceConfiguration |
| 39 | + |
| 40 | + |
| 41 | +@dataclasses.dataclass |
| 42 | +class DebianOvalConfiguration(DataSourceConfiguration): |
| 43 | + releases: list |
| 44 | + etags: dict |
| 45 | + |
| 46 | + |
| 47 | +class DebianOvalDataSource(OvalDataSource): |
| 48 | + |
| 49 | + CONFIG_CLASS = DebianOvalConfiguration |
| 50 | + |
| 51 | + def __init__(self, *args, **kwargs): |
| 52 | + super().__init__(*args, **kwargs) |
| 53 | + # we could avoid setting translations, and have it |
| 54 | + # set by default in the OvalParser, but we don't yet know |
| 55 | + # whether all OVAL providers use the same format |
| 56 | + self.translations = {'less than': '<'} |
| 57 | + self.pkg_manager_api = VersionAPI() |
| 58 | + |
| 59 | + def _fetch(self): |
| 60 | + base_url = 'https://www.debian.org/security/oval/' |
| 61 | + file_name = 'oval-definitions-{}.xml' |
| 62 | + releases = self.config.releases |
| 63 | + for release in releases: |
| 64 | + file_url = base_url + file_name.format(release) |
| 65 | + if not self.create_etag(file_url): |
| 66 | + continue |
| 67 | + resp = requests.get(file_url).content |
| 68 | + yield ( |
| 69 | + {'type': 'deb', 'namespace': 'debian', |
| 70 | + 'qualifiers': {'distro': release} |
| 71 | + }, |
| 72 | + ET.ElementTree(ET.fromstring(resp.decode('utf-8'))) |
| 73 | + ) |
| 74 | + return [] |
| 75 | + |
| 76 | + def set_api(self, packages): |
| 77 | + asyncio.run(self.pkg_manager_api.load_api(packages)) |
| 78 | + |
| 79 | + def create_etag(self, url): |
| 80 | + etag = requests.head(url).headers.get('ETag') |
| 81 | + if not etag: |
| 82 | + return True |
| 83 | + elif url in self.config.etags: |
| 84 | + if self.config.etags[url] == etag: |
| 85 | + return False |
| 86 | + self.config.etags[url] = etag |
| 87 | + return True |
| 88 | + |
| 89 | + |
| 90 | +class VersionAPI: |
| 91 | + def __init__(self, cache: Mapping[str, Set[str]] = None): |
| 92 | + self.cache = cache or {} |
| 93 | + |
| 94 | + def get(self, package_name: str) -> Set[str]: |
| 95 | + return self.cache[package_name] |
| 96 | + |
| 97 | + async def load_api(self, pkg_set): |
| 98 | + # Need to set the headers, because the Debian API upgrades |
| 99 | + # the connection to HTTP 2.0 |
| 100 | + async with ClientSession( |
| 101 | + raise_for_status=True, |
| 102 | + headers={'Connection': 'keep-alive'} |
| 103 | + ) as session: |
| 104 | + await asyncio.gather(*[self.set_api(pkg, session) |
| 105 | + for pkg in pkg_set if pkg not in self.cache]) |
| 106 | + |
| 107 | + async def set_api(self, pkg, session, retry_count=5): |
| 108 | + if pkg in self.cache: |
| 109 | + return |
| 110 | + url = ('https://sources.debian.org/api/src/{}'.format(pkg)) |
| 111 | + try: |
| 112 | + all_versions = set() |
| 113 | + response = await session.request(method='GET', url=url) |
| 114 | + resp_json = await response.json() |
| 115 | + |
| 116 | + if resp_json.get('error') or not resp_json.get('versions'): |
| 117 | + self.cache[pkg] = {} |
| 118 | + return |
| 119 | + for release in resp_json['versions']: |
| 120 | + all_versions.add(release['version']) |
| 121 | + |
| 122 | + self.cache[pkg] = all_versions |
| 123 | + # TODO : Handle ServerDisconnectedError by using some sort of |
| 124 | + # retry mechanism |
| 125 | + except (ClientResponseError, asyncio.exceptions.TimeoutError, ServerDisconnectedError): |
| 126 | + self.cache[pkg] = {} |
0 commit comments