|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Sync MidnightBSD GitHub Security Advisories into OSV YAML files. |
| 3 | +
|
| 4 | +Fetches published security advisories from a GitHub repository (default |
| 5 | +MidnightBSD/src) via the `gh` CLI, maps each one to its MNBSD-YYYY-ID identifier |
| 6 | +(parsed from the advisory summary), and writes an OSV-format YAML file for every |
| 7 | +advisory that is not already present in vulns/midnightbsd/. |
| 8 | +
|
| 9 | +Usage: |
| 10 | + python3 sync_advisories.py [options] |
| 11 | +
|
| 12 | +Options: |
| 13 | + --repo OWNER/NAME GitHub repo to read advisories from (default MidnightBSD/src) |
| 14 | + --out DIR Output directory (default vulns/midnightbsd relative to CWD) |
| 15 | + --latest-id FILE latest-id.txt path to update (default ./latest-id.txt) |
| 16 | + --year YYYY Only process MNBSD-YYYY-* advisories (default: all years) |
| 17 | + --from N Lowest numeric ID to (re)generate (default: all) |
| 18 | + --to M Highest numeric ID to (re)generate (default: all) |
| 19 | + --force Overwrite YAML files that already exist |
| 20 | + --dry-run Print what would be written without writing |
| 21 | +
|
| 22 | +After running, regenerate HTML with: |
| 23 | + python3 scripts/osvtohtml.py vulns/midnightbsd |
| 24 | +""" |
| 25 | + |
| 26 | +import argparse, json, os, re, subprocess, sys |
| 27 | + |
| 28 | +EXCLUDE = ('patch', 'reference', 'solution', 'credit', 'affected version') |
| 29 | + |
| 30 | + |
| 31 | +def fetch_advisories(repo): |
| 32 | + out = subprocess.check_output( |
| 33 | + ['gh', 'api', '/repos/%s/security-advisories' % repo, '--paginate']) |
| 34 | + return json.loads(out) |
| 35 | + |
| 36 | + |
| 37 | +def split_sections(desc): |
| 38 | + lines = desc.replace('\r\n', '\n').replace('\r', '\n').split('\n') |
| 39 | + sections, head, body = [], None, [] |
| 40 | + for ln in lines: |
| 41 | + hm = re.match(r'^\s*#{1,6}\s*(.+?)\s*$', ln) |
| 42 | + if hm: |
| 43 | + if head is not None or body: |
| 44 | + sections.append((head, body)) |
| 45 | + head, body = hm.group(1), [] |
| 46 | + else: |
| 47 | + body.append(ln) |
| 48 | + if head is not None or body: |
| 49 | + sections.append((head, body)) |
| 50 | + return sections |
| 51 | + |
| 52 | + |
| 53 | +def clean_inline(t): |
| 54 | + t = t.replace('**', '') |
| 55 | + return re.sub(r'`([^`]*)`', r'\1', t) |
| 56 | + |
| 57 | + |
| 58 | +def build_details(desc): |
| 59 | + parts = [] |
| 60 | + for head, body in split_sections(desc): |
| 61 | + if head and any(x in head.lower() for x in EXCLUDE): |
| 62 | + continue |
| 63 | + text = clean_inline('\n'.join(body)).strip() |
| 64 | + if text: |
| 65 | + parts.append(text) |
| 66 | + return re.sub(r'\n{3,}', '\n\n', '\n\n'.join(parts)).strip() |
| 67 | + |
| 68 | + |
| 69 | +def extract_refs(desc): |
| 70 | + urls = [] |
| 71 | + for head, body in split_sections(desc): |
| 72 | + if head and 'reference' in head.lower(): |
| 73 | + for ln in body: |
| 74 | + for u in re.findall(r'https?://\S+', ln): |
| 75 | + urls.append(u.rstrip('.,);')) |
| 76 | + return urls |
| 77 | + |
| 78 | + |
| 79 | +def parse_range(vr, patched): |
| 80 | + vr = (vr or '').strip() |
| 81 | + introduced = '0' |
| 82 | + m = re.match(r'>=\s*([0-9][\w.\-]*)', vr) |
| 83 | + if m: |
| 84 | + introduced = m.group(1) |
| 85 | + fixed = None |
| 86 | + if patched: |
| 87 | + fm = re.search(r'([0-9]+(?:\.[0-9]+)+)', patched) |
| 88 | + if fm: |
| 89 | + fixed = fm.group(1) |
| 90 | + return introduced, fixed |
| 91 | + |
| 92 | + |
| 93 | +def q(v): |
| 94 | + return '"%s"' % v |
| 95 | + |
| 96 | + |
| 97 | +def summary_line(summary): |
| 98 | + if re.search(r'^["\'\[\]{}#&*!|>%@`]|:\s|\s#', summary) or summary != summary.strip(): |
| 99 | + return 'summary: %s' % json.dumps(summary) |
| 100 | + return 'summary: %s' % summary |
| 101 | + |
| 102 | + |
| 103 | +def render_yaml(aid, num, adv): |
| 104 | + summary = re.sub(r'^%s\s*' % re.escape(aid), '', adv['summary']).strip() |
| 105 | + details = build_details(adv['description']) |
| 106 | + |
| 107 | + cves = [] |
| 108 | + for c in re.findall(r'CVE-\d{4}-\d+', adv['summary'] + ' ' + adv['description']): |
| 109 | + if c not in cves: |
| 110 | + cves.append(c) |
| 111 | + |
| 112 | + refs = extract_refs(adv['description']) |
| 113 | + if not refs: |
| 114 | + refs = (['https://www.cve.org/CVERecord?id=%s' % c for c in cves] |
| 115 | + if cves else [adv['html_url']]) |
| 116 | + |
| 117 | + date = adv.get('published_at') or adv.get('updated_at') or adv.get('created_at') |
| 118 | + |
| 119 | + lines = ['id: %s' % aid, summary_line(summary), 'details: |'] |
| 120 | + for dl in details.split('\n'): |
| 121 | + lines.append('' if dl == '' else ' ' + dl) |
| 122 | + |
| 123 | + lines.append('affected:') |
| 124 | + for v in adv['vulnerabilities']: |
| 125 | + introduced, fixed = parse_range(v.get('vulnerable_version_range', ''), |
| 126 | + v.get('patched_versions', '')) |
| 127 | + lines += [' - package:', |
| 128 | + ' name: %s' % v['package']['name'], |
| 129 | + ' ecosystem: MidnightBSD', |
| 130 | + ' ranges:', |
| 131 | + ' - type: ECOSYSTEM', |
| 132 | + ' events:', |
| 133 | + ' - introduced: %s' % q(introduced)] |
| 134 | + if fixed: |
| 135 | + lines.append(' - fixed: %s' % q(fixed)) |
| 136 | + |
| 137 | + lines.append('references:') |
| 138 | + for u in refs: |
| 139 | + lines += [' - type: WEB', ' url: %s' % u] |
| 140 | + |
| 141 | + if cves: |
| 142 | + lines.append('aliases:') |
| 143 | + lines += [' - %s' % c for c in cves] |
| 144 | + |
| 145 | + lines += ['modified: %s' % q(date), 'published: %s' % q(date)] |
| 146 | + return '\n'.join(lines) + '\n' |
| 147 | + |
| 148 | + |
| 149 | +def main(): |
| 150 | + ap = argparse.ArgumentParser() |
| 151 | + ap.add_argument('--repo', default='MidnightBSD/src') |
| 152 | + ap.add_argument('--out', default='vulns/midnightbsd') |
| 153 | + ap.add_argument('--latest-id', default='latest-id.txt') |
| 154 | + ap.add_argument('--year', type=int) |
| 155 | + ap.add_argument('--from', dest='lo', type=int) |
| 156 | + ap.add_argument('--to', dest='hi', type=int) |
| 157 | + ap.add_argument('--force', action='store_true') |
| 158 | + ap.add_argument('--dry-run', action='store_true') |
| 159 | + args = ap.parse_args() |
| 160 | + |
| 161 | + data = fetch_advisories(args.repo) |
| 162 | + |
| 163 | + parsed = {} # (year, num) -> adv |
| 164 | + for a in data: |
| 165 | + if a.get('state') != 'published': |
| 166 | + continue |
| 167 | + m = re.search(r'MNBSD-(\d{4})-(\d+)', a.get('summary', '')) |
| 168 | + if m: |
| 169 | + parsed[(int(m.group(1)), int(m.group(2)))] = a |
| 170 | + |
| 171 | + written, skipped = [], [] |
| 172 | + max_by_year = {} |
| 173 | + for (year, num), adv in sorted(parsed.items()): |
| 174 | + if args.year and year != args.year: |
| 175 | + continue |
| 176 | + if args.lo is not None and num < args.lo: |
| 177 | + continue |
| 178 | + if args.hi is not None and num > args.hi: |
| 179 | + continue |
| 180 | + aid = 'MNBSD-%d-%d' % (year, num) |
| 181 | + path = os.path.join(args.out, '%s.yaml' % aid) |
| 182 | + max_by_year[year] = max(max_by_year.get(year, 0), num) |
| 183 | + if os.path.exists(path) and not args.force: |
| 184 | + skipped.append(aid) |
| 185 | + continue |
| 186 | + content = render_yaml(aid, num, adv) |
| 187 | + if args.dry_run: |
| 188 | + print('--- would write %s ---' % path) |
| 189 | + print(content) |
| 190 | + else: |
| 191 | + with open(path, 'w') as f: |
| 192 | + f.write(content) |
| 193 | + written.append(aid) |
| 194 | + |
| 195 | + print('Wrote %d file(s): %s' % (len(written), ', '.join(written) or '(none)')) |
| 196 | + if skipped: |
| 197 | + print('Skipped %d existing (use --force to overwrite): %s' |
| 198 | + % (len(skipped), ', '.join(skipped))) |
| 199 | + |
| 200 | + # Update latest-id.txt to the highest ID for the most recent year seen. |
| 201 | + if max_by_year and not args.dry_run: |
| 202 | + year = max(max_by_year) |
| 203 | + newest = '%d-%d' % (year, max_by_year[year]) |
| 204 | + cur = '' |
| 205 | + if os.path.exists(args.latest_id): |
| 206 | + cur = open(args.latest_id).read().strip() |
| 207 | + if cur != newest: |
| 208 | + with open(args.latest_id, 'w') as f: |
| 209 | + f.write(newest + '\n') |
| 210 | + print('Updated %s: %s -> %s' % (args.latest_id, cur or '(empty)', newest)) |
| 211 | + |
| 212 | + if written and not args.dry_run: |
| 213 | + print('\nNext: python3 scripts/osvtohtml.py %s' % args.out) |
| 214 | + |
| 215 | + |
| 216 | +if __name__ == '__main__': |
| 217 | + sys.exit(main()) |
0 commit comments