|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +SSVC objects use namespaces to distinguish between objects that arise from different |
| 4 | +stakeholders or analytical category sources. This module defines the official namespaces |
| 5 | +for SSVC and provides a method to validate namespace values. |
| 6 | +""" |
| 7 | +# Copyright (c) 2025 Carnegie Mellon University and Contributors. |
| 8 | +# - see Contributors.md for a full list of Contributors |
| 9 | +# - see ContributionInstructions.md for information on how you can Contribute to this project |
| 10 | +# Stakeholder Specific Vulnerability Categorization (SSVC) is |
| 11 | +# licensed under a MIT (SEI)-style license, please see LICENSE.md distributed |
| 12 | +# with this Software or contact permission@sei.cmu.edu for full terms. |
| 13 | +# Created, in part, with funding and support from the United States Government |
| 14 | +# (see Acknowledgments file). This program may include and/or can make use of |
| 15 | +# certain third party source code, object code, documentation and other files |
| 16 | +# (“Third Party Software”). See LICENSE.md for more details. |
| 17 | +# Carnegie Mellon®, CERT® and CERT Coordination Center® are registered in the |
| 18 | +# U.S. Patent and Trademark Office by Carnegie Mellon University |
| 19 | + |
| 20 | +import re |
| 21 | +from enum import StrEnum, auto |
| 22 | + |
| 23 | +X_PFX = "x_" |
| 24 | +"""The prefix for extension namespaces. Extension namespaces must start with this prefix.""" |
| 25 | + |
| 26 | +# pattern to match |
| 27 | +# `(?=.{3,25}$)`: 3-25 characters long |
| 28 | +# `^(x_)`: `x_` prefix is optional |
| 29 | +# `[a-z0-9]{3,4}`: must start with 3-4 alphanumeric characters |
| 30 | +# `[/.-]?`: only one punctuation character is allowed between alphanumeric characters |
| 31 | +# `[a-z0-9]+`: at least one alphanumeric character is required after the punctuation character |
| 32 | +# `([/.-]?[a-z0-9]+){0,22}`: zero to 22 occurrences of the punctuation character followed by at least one alphanumeric character |
| 33 | +# (note that the total limit will kick in at or before this point) |
| 34 | +# `$`: end of the string |
| 35 | +NS_PATTERN = re.compile(r"^(?=.{3,25}$)(x_)?[a-z0-9]{3}([/.-]?[a-z0-9]+){0,22}$") |
| 36 | +"""The regular expression pattern for validating namespaces. |
| 37 | +
|
| 38 | +Note: |
| 39 | + Namespace values must |
| 40 | + |
| 41 | + - be 3-25 characters long |
| 42 | + - contain only lowercase alphanumeric characters and limited punctuation characters (`/`,`.` and `-`) |
| 43 | + - have only one punctuation character in a row |
| 44 | + - start with 3-4 alphanumeric characters after the optional extension prefix |
| 45 | + - end with an alphanumeric character |
| 46 | + |
| 47 | + See examples in the `NameSpace` enum. |
| 48 | +""" |
| 49 | + |
| 50 | + |
| 51 | +class NameSpace(StrEnum): |
| 52 | + """ |
| 53 | + Defines the official namespaces for SSVC. |
| 54 | +
|
| 55 | + The namespace value must be one of the members of this enum or start with the prefix specified in X_PFX. |
| 56 | + Namespaces must be 3-25 lowercase characters long and must start with 3-4 alphanumeric characters after the optional prefix. |
| 57 | + Limited punctuation characters (/.-) are allowed between alphanumeric characters, but only one at a time. |
| 58 | +
|
| 59 | + Example: |
| 60 | + Following are examples of valid and invalid namespace values: |
| 61 | +
|
| 62 | + - `ssvc` is *valid* because it is present in the enum |
| 63 | + - `custom` is *invalid* because it does not start with the experimental prefix and is not in the enum |
| 64 | + - `x_custom` is *valid* because it starts with the experimental prefix and meets the pattern requirements |
| 65 | + - `x_custom/extension` is *valid* because it starts with the experimental prefix and meets the pattern requirements |
| 66 | + - `x_custom/extension/with/multiple/segments` is *invalid* because it exceeds the maximum length |
| 67 | + - `x_custom//extension` is *invalid* because it has multiple punctuation characters in a row |
| 68 | + - `x_custom.extension.` is *invalid* because it does not end with an alphanumeric character |
| 69 | + - `x_custom.extension.9` is *valid* because it meets the pattern requirements |
| 70 | + """ |
| 71 | + |
| 72 | + # auto() is used to automatically assign values to the members. |
| 73 | + # when used in a StrEnum, auto() assigns the lowercase name of the member as the value |
| 74 | + SSVC = auto() |
| 75 | + CVSS = auto() |
| 76 | + |
| 77 | + @classmethod |
| 78 | + def validate(cls, value: str) -> str: |
| 79 | + """ |
| 80 | + Validate the namespace value. Valid values are members of the enum or start with the experimental prefix and |
| 81 | + meet the specified pattern requirements. |
| 82 | +
|
| 83 | + Args: |
| 84 | + value: the namespace value to validate |
| 85 | +
|
| 86 | + Returns: |
| 87 | + the validated namespace value |
| 88 | +
|
| 89 | + Raises: |
| 90 | + ValueError: if the value is not a valid namespace |
| 91 | +
|
| 92 | + """ |
| 93 | + if value in cls.__members__.values(): |
| 94 | + return value |
| 95 | + if value.startswith(X_PFX) and NS_PATTERN.match(value): |
| 96 | + return value |
| 97 | + raise ValueError( |
| 98 | + f"Invalid namespace: {value}. Must be one of {[ns.value for ns in cls]} or start with '{X_PFX}'." |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def main(): |
| 103 | + for ns in NameSpace: |
| 104 | + print(ns) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + main() |
0 commit comments