@@ -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 )
0 commit comments