diff --git a/vulnerabilities/helpers.py b/vulnerabilities/helpers.py index 8eec354f6..81229abcf 100644 --- a/vulnerabilities/helpers.py +++ b/vulnerabilities/helpers.py @@ -167,10 +167,10 @@ def __lt__(self, other): return affected_package_with_patched_package_objects -def split_markdown_front_matter(lines: str) -> Tuple[str, str]: +def split_markdown_front_matter(text: str) -> Tuple[str, str]: r""" - Split text into markdown front matter and the markdown body - Return ("", text) for text with non existing front matter + Return a tuple of (front matter, markdown body) strings split from ``text``. + Each can be an empty string. >>> text='''--- ... title: DUMMY-SECURITY-2019-001 @@ -180,19 +180,13 @@ def split_markdown_front_matter(lines: str) -> Tuple[str, str]: ... # Markdown starts here ... ''' >>> split_markdown_front_matter(text) - ('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]', '# Markdown starts here\n') + ('title: DUMMY-SECURITY-2019-001\ndescription: Incorrect access control.\ncves: [CVE-2042-1337]', '# Markdown starts here') """ - fmlines = [] - mdlines = [] - splitter = mdlines - - lines = lines.replace("\r\n", "\n") - for index, line in enumerate(lines.split("\n")): - if index == 0 and line.strip().startswith("---"): - splitter = fmlines - elif line.strip().startswith("---"): - splitter = mdlines - else: - splitter.append(line) - - return "\n".join(fmlines), "\n".join(mdlines) + lines = text.splitlines() + if lines[0] == "---": + lines = lines[1:] + text = "\n".join(lines) + frontmatter, _, markdown = text.partition("\n---\n") + return frontmatter, markdown + + return "", text diff --git a/vulnerabilities/importers/istio.py b/vulnerabilities/importers/istio.py index d863aec19..c755b4a3d 100644 --- a/vulnerabilities/importers/istio.py +++ b/vulnerabilities/importers/istio.py @@ -174,10 +174,8 @@ def process_file(self, path): return advisories def get_data_from_md(self, path): - """Return a mapping of vulnerability data from istio. The data is - in the form of yaml objects found inside front matter of the .md file. - """ + """Return a mapping of vulnerability data extracted from an advisory.""" with open(path) as f: - yaml_lines, _ = split_markdown_front_matter(f.read()) - return yaml.safe_load(yaml_lines) + front_matter, _ = split_markdown_front_matter(f.read()) + return yaml.safe_load(front_matter)