Skip to content

Commit d14e7c2

Browse files
committed
Break load_toml_from_md into smaller methods
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent 23d7095 commit d14e7c2

2 files changed

Lines changed: 56 additions & 15 deletions

File tree

vulnerabilities/importers/rust.py

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ def _load_advisories(self, files) -> Set[Advisory]:
8383
def collect_packages(self, paths):
8484
packages = set()
8585
for path in paths:
86-
record = load_toml_from_md(path)
86+
record = get_advisory_data(path)
8787
packages.add(record["advisory"]["package"])
8888

8989
return packages
9090

9191
def _load_advisory(self, path: str) -> Optional[Advisory]:
92-
record = load_toml_from_md(path)
92+
record = get_advisory_data(path)
9393
advisory = record.get("advisory", {})
9494
crate_name = advisory["package"]
9595
references = []
@@ -174,7 +174,54 @@ def categorize_versions(
174174
return unaffected, affected
175175

176176

177-
def load_toml_from_md(md_path):
177+
def get_toml_lines(lines):
178+
"""
179+
Yield lines of TOML extracted from an iterable of text ``lines``.
180+
The lines are expected to be from a RustSec Markdown advisory file with
181+
embedded TOML metadata.
182+
183+
For example::
184+
185+
>>> text = '''
186+
... ```toml
187+
... [advisory]
188+
... id = "RUST-001"
189+
...
190+
... [versions]
191+
... patch = [">= 1.2.1"]
192+
... ```
193+
... # Use-after-free with objects returned by `Stream`'s `get_format_info`
194+
...
195+
... Affected versions contained a pair of use-after-free issues with the objects.
196+
... '''
197+
>>> list(get_toml_lines(text.splitlines()))
198+
['', '[advisory]', 'id = "RUST-001"', '', '[versions]', 'patch = [">= 1.2.1"]']
199+
"""
200+
201+
for line in lines:
202+
line = line.strip()
203+
if line.startswith("```toml"):
204+
continue
205+
elif line.endswith("```"):
206+
break
207+
else:
208+
yield line
209+
210+
211+
def data_from_toml_lines(lines):
212+
"""
213+
Return a mapping of data from an iterable of TOML text ``lines``.
214+
215+
For example::
216+
217+
>>> lines = ['[advisory]', 'id = "RUST1"', '', '[versions]', 'patch = [">= 1"]']
218+
>>> data_from_toml_lines(lines)
219+
{'advisory': {'id': 'RUST1'}, 'versions': {'patch': ['>= 1']}}
220+
"""
221+
return toml.loads("\n".join(lines))
222+
223+
224+
def get_advisory_data(location):
178225
"""
179226
Return a mapping of vulnerability data from a RustSec advisory file at
180227
``location``.
@@ -183,11 +230,7 @@ def load_toml_from_md(md_path):
183230
https://github.com/RustSec/advisory-db#advisory-format:
184231
Advisories are formatted in Markdown with TOML "front matter".
185232
"""
186-
parsed_data = {}
187-
with open(md_path) as f:
188-
result = re.findall("^```toml(.*?)^```", f.read(), re.DOTALL | re.MULTILINE)
189-
# This regex matches everything that's between ```toml and ```
190-
for i in result:
191-
parsed_toml = toml.loads("".join(i))
192-
parsed_data.update(parsed_toml)
193-
return parsed_data
233+
234+
with open(location) as lines:
235+
toml_lines = get_toml_lines(lines)
236+
return data_from_toml_lines(toml_lines)

vulnerabilities/tests/test_rust.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from vulnerabilities.importers.rust import categorize_versions
3535
from vulnerabilities.import_runner import ImportRunner
3636
from vulnerabilities.package_managers import VersionAPI
37-
from vulnerabilities.importers.rust import load_toml_from_md
37+
from vulnerabilities.importers.rust import get_advisory_data
3838

3939
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
4040
TEST_DATA = os.path.join(BASE_DIR, "test_data/rust")
@@ -187,7 +187,7 @@ def test_import(self, _):
187187
def test_load_toml_from_md(self, _):
188188
md_path = os.path.join(TEST_DATA, "CVE-2019-16760.md")
189189
# print(list(os.walk(self.tempdir)))
190-
loaded_data = load_toml_from_md(md_path)
190+
loaded_data = get_advisory_data(md_path)
191191
expected_data = {
192192
"advisory": {
193193
"aliases": ["GHSA-phjm-8x66-qw4r"],
@@ -196,8 +196,6 @@ def test_load_toml_from_md(self, _):
196196
"package": "cargo",
197197
"url": "https://groups.google.com/forum/#!topic/rustlang-security-announcements/rVQ5e3TDnpQ", # nopep8
198198
},
199-
"serde": {"features": ["derive"], "version": "1.0"},
200-
"serde1": {"features": ["derive"], "version": "1.0"},
201199
"versions": {"patched": [">= 1.26.0"]},
202200
}
203201

0 commit comments

Comments
 (0)