Skip to content

Commit 3b84167

Browse files
committed
feat: switch to DRUPAL prefix
1 parent 5348e53 commit 3b84167

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

scripts/generate_osv_advisories.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,12 @@ class DrupalAdvisoryPatch(typing.TypedDict):
342342
patches: dict[str, DrupalAdvisoryPatch] = tomllib.load(fi)
343343

344344

345-
def patch_advisory(sa_id: str, sa_advisory: drupal.Advisory) -> bool:
345+
def patch_advisory(osv_id: str, sa_advisory: drupal.Advisory) -> bool:
346346
"""
347347
Attempts to apply any patches to the advisory that are defined in patches.toml
348348
"""
349+
sa_id = f'SA-{osv_id.removeprefix("DRUPAL-")}'
350+
349351
if sa_id in patches:
350352
before, after = patches[sa_id]['field_affected_versions']
351353

@@ -367,14 +369,14 @@ def unix_timestamp_to_rfc3339(unix: int) -> str:
367369

368370

369371
def build_osv_advisory(
370-
sa_id: str,
372+
osv_id: str,
371373
sa_advisory: drupal.Advisory,
372374
) -> osv.Vulnerability | None:
373375
"""
374376
Builds a representation of the given Drupal SA advisory in OSV format
375377
"""
376378

377-
patched = patch_advisory(sa_id, sa_advisory)
379+
patched = patch_advisory(osv_id, sa_advisory)
378380

379381
# we expect that the downloader has excluded PSA type entries, but
380382
# we still guard against them here just in case one slips through
@@ -391,14 +393,14 @@ def build_osv_advisory(
391393

392394
osv_advisory: osv.Vulnerability = {
393395
'schema_version': '1.7.0',
394-
'id': f'D{sa_id}',
396+
'id': osv_id,
395397
'modified': unix_timestamp_to_rfc3339(int(sa_advisory['changed'])),
396398
'published': unix_timestamp_to_rfc3339(int(sa_advisory['created'])),
397399
'aliases': sa_advisory['field_sa_cve'],
398400
'details': markdownify(sa_advisory['field_sa_description']['value']),
399401
'affected': [
400402
{
401-
# todo: figure out if we need a dedicated ecosystem i.e. Drupal, Drupal8, etc
403+
402404
'package': {
403405
'ecosystem': 'Packagist',
404406
'name': determine_composer_package_name(sa_advisory),
@@ -430,10 +432,10 @@ def fetch_affected_packages(osv_advisory: osv.Vulnerability) -> list[str]:
430432

431433

432434
def is_existing_advisory_ahead(
433-
name: str, sa_id: str, proposed_modified_at: str
435+
name: str, osv_id: str, proposed_modified_at: str
434436
) -> bool:
435437
try:
436-
with open(f'advisories/{name}/D{sa_id}.json') as f:
438+
with open(f'advisories/{name}/{osv_id}.json') as f:
437439
existing_advisory = typing.cast(osv.Vulnerability, json.load(f))
438440
# RFC3339 dates are designed to be comparable as strings, so this is safe
439441
return existing_advisory['modified'] > proposed_modified_at
@@ -449,8 +451,8 @@ def generate_osv_advisories() -> None:
449451
with open(file.path) as f:
450452
sa_advisory: drupal.Advisory = json.load(f)
451453
print(f'processing {sa_advisory["url"]}')
452-
sa_id = file.name.removesuffix('.json')
453-
osv_advisory = build_osv_advisory(sa_id, sa_advisory)
454+
osv_id = f'DRUPAL-{file.name.removeprefix("SA-").removesuffix(".json")}'
455+
osv_advisory = build_osv_advisory(osv_id, sa_advisory)
454456

455457
if osv_advisory is None:
456458
continue
@@ -463,7 +465,7 @@ def generate_osv_advisories() -> None:
463465
for affected_package in affected_packages:
464466
name = affected_package.removeprefix('drupal/')
465467
os.makedirs(f'advisories/{name}', exist_ok=True)
466-
if is_existing_advisory_ahead(name, sa_id, osv_advisory['modified']):
468+
if is_existing_advisory_ahead(name, osv_id, osv_advisory['modified']):
467469
print(
468470
' \\- '
469471
+ text_is.error(
@@ -472,7 +474,7 @@ def generate_osv_advisories() -> None:
472474
)
473475
exit(1)
474476

475-
with open(f'advisories/{name}/D{sa_id}.json', 'w') as f:
477+
with open(f'advisories/{name}/{osv_id}.json', 'w') as f:
476478
json.dump(osv_advisory, f, indent=2)
477479
f.write('\n')
478480

0 commit comments

Comments
 (0)