Skip to content

Commit fbad307

Browse files
committed
Add support for composer vulnerabilities
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent 3dcad7d commit fbad307

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

vulnerabilities/importers/github.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ def process_name(ecosystem, pkg_name):
151151
return None, pkg_name
152152

153153
if ecosystem == "COMPOSER":
154-
raise NotImplementedError
154+
vendor, name = pkg_name.split("/")
155+
return vendor, name
155156

156157
def process_response(self) -> List[Advisory]:
157158
adv_list = []
@@ -306,4 +307,35 @@ def extract_versions(json_resp):
306307

307308
class ComposerVersionAPI:
308309
def __init__(self):
309-
raise NotImplementedError
310+
self.cache = {}
311+
312+
def get(self, pkg_name):
313+
return self.cache.get(pkg_name.lower(), set())
314+
315+
def load_to_api(self, pkg_name):
316+
if pkg_name in self.cache:
317+
return
318+
endpoint = self.composer_url(pkg_name)
319+
json_resp = requests.get(endpoint).json()
320+
self.cache[pkg_name] = self.extract_versions(json_resp, pkg_name)
321+
322+
@staticmethod
323+
def composer_url(pkg_name):
324+
vendor, name = pkg_name.split("/")
325+
return f"https://repo.packagist.org/p/{vendor}/{name}.json"
326+
327+
@staticmethod
328+
def extract_versions(json_resp, pkg_name):
329+
all_versions = json_resp["packages"][pkg_name].keys()
330+
# This filter ensures, that all_versions contains only released versions
331+
all_versions = set(filter(lambda x: "dev" not in x, all_versions))
332+
# more_versions ensures that we have a version with and without version tag for
333+
# each version present in all_versions
334+
more_versions = set()
335+
for version in all_versions:
336+
if version.startswith("v"):
337+
more_versions.add(version[1:])
338+
else:
339+
more_versions.add("v" + version)
340+
341+
return all_versions.union(more_versions)

vulnerabilities/migrations/0015_github_importer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def add_github_importer(apps, _):
3232
last_run=None,
3333
data_source='GitHubAPIDataSource',
3434
data_source_cfg={'endpoint':'https://api.github.com/graphql',
35-
'ecosystems':['MAVEN','NUGET']
35+
'ecosystems':['MAVEN','NUGET','COMPOSER']
3636
},
3737
)
3838

0 commit comments

Comments
 (0)