Skip to content

Commit 366d3a1

Browse files
authored
Merge pull request #443 from Hritik14/front_matter_splitter
helper: split_markdown_front_matter
2 parents 753dd72 + 9f3e1ef commit 366d3a1

2 files changed

Lines changed: 32 additions & 45 deletions

File tree

vulnerabilities/helpers.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
import dataclasses
2525
import json
2626
import re
27-
from typing import Optional
2827
from typing import List
28+
from typing import Optional
29+
from typing import Tuple
2930

3031
import requests
3132
import saneyaml
@@ -164,3 +165,29 @@ def __lt__(self, other):
164165
)
165166

166167
return affected_package_with_patched_package_objects
168+
169+
170+
def split_markdown_front_matter(text: str) -> Tuple[str, str]:
171+
r"""
172+
Return a tuple of (front matter, markdown body) strings split from ``text``.
173+
Each can be an empty string.
174+
175+
>>> text='''---
176+
... title: DUMMY-SECURITY-2019-001
177+
... description: Incorrect access control.
178+
... cves: [CVE-2042-1337]
179+
... ---
180+
... # Markdown starts here
181+
... '''
182+
>>> split_markdown_front_matter(text)
183+
('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]', '# Markdown starts here')
184+
"""
185+
# The doctest contains \n and for the sake of clarity I chose raw strings than escaping those.
186+
lines = text.splitlines()
187+
if lines[0] == "---":
188+
lines = lines[1:]
189+
text = "\n".join(lines)
190+
frontmatter, _, markdown = text.partition("\n---\n")
191+
return frontmatter, markdown
192+
193+
return "", text

vulnerabilities/importers/istio.py

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from vulnerabilities.data_source import GitDataSource
3434
from vulnerabilities.data_source import Reference
3535
from vulnerabilities.helpers import nearest_patched_package
36+
from vulnerabilities.helpers import split_markdown_front_matter
3637
from vulnerabilities.package_managers import GitHubTagsAPI
3738

3839
is_release = re.compile(r"^[\d.]+$", re.IGNORECASE).match
@@ -80,45 +81,6 @@ def get_pkg_versions_from_ranges(self, version_range_list):
8081
safe_pkg_versions = set(all_version) - set(vuln_pkg_versions)
8182
return safe_pkg_versions, vuln_pkg_versions
8283

83-
def get_data_from_yaml_lines(self, yaml_lines):
84-
"""Return a mapping of data from a iterable of yaml_lines
85-
for example :
86-
['title: ISTIO-SECURITY-2019-001',
87-
'description: Incorrect access control.','cves: [CVE-2019-12243]']
88-
89-
would give {'title':'ISTIO-SECURITY-2019-001',
90-
'description': 'Incorrect access control.',
91-
'cves': '[CVE-2019-12243]'}
92-
"""
93-
94-
return saneyaml.load("\n".join(yaml_lines))
95-
96-
def get_yaml_lines(self, lines):
97-
"""The istio advisory file contains lines similar to yaml format .
98-
This function extracts those lines and return an iterable of lines
99-
100-
for example :
101-
lines =
102-
---
103-
title: ISTIO-SECURITY-2019-001
104-
description: Incorrect access control.
105-
cves: [CVE-2019-12243]
106-
---
107-
108-
get_yaml_lines(lines) would return
109-
['title: ISTIO-SECURITY-2019-001','description: Incorrect access control.'
110-
,'cves: [CVE-2019-12243]']
111-
"""
112-
113-
for index, line in enumerate(lines):
114-
line = line.strip()
115-
if line.startswith("---") and index == 0:
116-
continue
117-
elif line.endswith("---"):
118-
break
119-
else:
120-
yield line
121-
12284
def process_file(self, path):
12385

12486
advisories = []
@@ -212,10 +174,8 @@ def process_file(self, path):
212174
return advisories
213175

214176
def get_data_from_md(self, path):
215-
"""Return a mapping of vulnerability data from istio . The data is
216-
in the form of yaml_lines inside a .md file.
217-
"""
177+
"""Return a mapping of vulnerability data extracted from an advisory."""
218178

219179
with open(path) as f:
220-
yaml_lines = self.get_yaml_lines(f)
221-
return self.get_data_from_yaml_lines(yaml_lines)
180+
front_matter, _ = split_markdown_front_matter(f.read())
181+
return saneyaml.load(front_matter)

0 commit comments

Comments
 (0)