diff --git a/blurb_it/__main__.py b/blurb_it/__main__.py index 47cf321..da130f0 100644 --- a/blurb_it/__main__.py +++ b/blurb_it/__main__.py @@ -157,7 +157,19 @@ async def handle_add_blurb_post(request: Request) -> Response: issue_number = data.get("issue_number", "").strip() section = data.get("section", "").strip() news_entry = data.get("news_entry", "").strip() + "\n" + cve = data.get("cve", "").strip() + if cve: + if section != util.CVE_SECTION: + raise web.HTTPBadRequest( + reason=f"A CVE may only be given for the {util.CVE_SECTION} section." + ) + cve = util.normalize_cve(cve) + if cve is None: + raise web.HTTPBadRequest( + reason="Invalid CVE identifier (expected CVE-YYYY-NNNN)." + ) path = await util.get_misc_news_filename(issue_number, section, news_entry) + news_content = util.get_misc_news_content(news_entry, cve=cve) pr_number = data.get("pr_number", "").strip() context = {} @@ -190,7 +202,7 @@ async def handle_add_blurb_post(request: Request) -> Response: ) pr = await gh.getitem(f"/repos/python/cpython/pulls/{pr_number}") pr_repo_full_name = pr["head"]["repo"]["full_name"] - encoded = base64.b64encode(str.encode(news_entry)) + encoded = base64.b64encode(str.encode(news_content)) decoded = encoded.decode("utf-8") put_data = { "branch": pr["head"]["ref"], diff --git a/blurb_it/util.py b/blurb_it/util.py index e38e780..52193f1 100644 --- a/blurb_it/util.py +++ b/blurb_it/util.py @@ -1,6 +1,7 @@ import base64 import hashlib import os +import re import time import secrets @@ -8,6 +9,27 @@ from blurb_it import error +# A CVE may only be attached to a Security entry, and is stored as a ".. cve:" +# metadata line at the top of the news file. +CVE_SECTION = "Security" +_cve_re = re.compile(r"(?:CVE-)?(\d{4})-(\d{4,})", re.IGNORECASE | re.ASCII) + + +def normalize_cve(value): + """Return a CVE identifier in canonical "CVE-YYYY-NNNN" form.""" + match = _cve_re.fullmatch(value.strip()) + if match is None: + return None + year, number = match.groups() + return f"CVE-{year}-{number}" + + +def get_misc_news_content(body, cve=None): + """Return the news file contents, with ".. cve:" metadata when given.""" + if not cve: + return body + return f".. cve: {cve}\n\n{body}" + async def get_misc_news_filename(issue_number, section, body): if " " in section: diff --git a/templates/add_blurb.html b/templates/add_blurb.html index 67fda2e..c6da37e 100644 --- a/templates/add_blurb.html +++ b/templates/add_blurb.html @@ -51,6 +51,14 @@

📜🤖 Blurb it again?

+
+
+ +
+
+ Optional. The CVE identifier (CVE-YYYY-NNNN) for a Security entry only. +
+