summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Helmert III <ajak@gentoo.org>2023-12-26 15:45:08 -0800
committerJohn Helmert III <ajak@gentoo.org>2023-12-26 15:45:08 -0800
commita97403b5fbb95a0847f9486f1e88401559361995 (patch)
tree789e8bc7bbc3bc4077c03319328928f64636e766
parenttest_reference: test both comparison directions on Reference objects (diff)
downloadglsamaker-a97403b5fbb95a0847f9486f1e88401559361995.tar.gz
glsamaker-a97403b5fbb95a0847f9486f1e88401559361995.tar.bz2
glsamaker-a97403b5fbb95a0847f9486f1e88401559361995.zip
models/reference: add reference format validation
Signed-off-by: John Helmert III <ajak@gentoo.org>
-rw-r--r--glsamaker/models/reference.py41
-rw-r--r--test/models/test_reference.py10
2 files changed, 51 insertions, 0 deletions
diff --git a/glsamaker/models/reference.py b/glsamaker/models/reference.py
index 7e3b3b5..328cb08 100644
--- a/glsamaker/models/reference.py
+++ b/glsamaker/models/reference.py
@@ -1,14 +1,43 @@
+from typing import TypeGuard
+
from glsamaker.extensions import base, db
+class InvalidReferenceFormatException(Exception):
+ pass
+
+
class Reference(base):
__tablename__ = "reference"
ref_text = db.Column(db.String(), primary_key=True)
url = db.Column(db.String())
+ PREFIXES = [
+ "CVE",
+ "GHSA",
+ "GStreamer",
+ "MFSA",
+ "TALOS",
+ "TROVE",
+ "VMSA",
+ "WNPA-SEC",
+ "WSA",
+ "XSA",
+ "YSA",
+ "ZDI-CAN",
+ ]
+
def __init__(self, ref_text, url=None):
+ # note that we can't actually raise exception on a validation
+ # failure here yet because there are lots of old references
+ # that wouldn't pass the validation, and this would block the
+ # ingestion of old GLSAs
+ # if not self.valid_reference(ref_text):
+ # raise InvalidReferenceFormatException
+
self.ref_text = ref_text
+
if url:
self.url = url
else:
@@ -31,6 +60,18 @@ class Reference(base):
return row
return Reference(ref, url)
+ @classmethod
+ def valid_reference(cls, ref_text: str) -> bool:
+ # not using a lambda here and returning a type of
+ # TypeGuard[object] seemingly for:
+ # https://github.com/python/mypy/issues/12682
+ def _ref_startswith_prefix(prefix: str) -> TypeGuard[object]:
+ return ref_text.startswith(prefix)
+
+ if not any(filter(_ref_startswith_prefix, cls.PREFIXES)):
+ return False
+ return True
+
def __lt__(self, other) -> bool:
parts = self.ref_text.split("-")
other_parts = other.ref_text.split("-")
diff --git a/test/models/test_reference.py b/test/models/test_reference.py
index b7daa3e..851c3fb 100644
--- a/test/models/test_reference.py
+++ b/test/models/test_reference.py
@@ -37,3 +37,13 @@ class TestReference:
)
def test_reference_url(self, identifier, url):
assert Reference(identifier).url == url
+
+ def test_valid_reference(self):
+ invalid_references = ["-fno-common", "VE-2023-0001", "2023-1234"]
+ valid_references = ["CVE-2023-1234", "TALOS-2023-1234"]
+
+ for reference in valid_references:
+ assert Reference.valid_reference(reference)
+
+ for reference in invalid_references:
+ assert not Reference.valid_reference(reference)