Пример #1
0
def test_cvss_class_mixin():
    # Test that an instance of every enum class is present within each of the state classes

    for cls, module, vectors in [(CVSS2State, cvss2, v2_vectors), (CVSS3State, cvss3, v3_vectors)]:
        instance = cls()

        enum_classes_in_module = set([x[1] for x in get_enums(module)])
        enum_classes_in_class = set([e[1].__class__ for e in get_enums(instance, only_classes=False)])

        assert enum_classes_in_class == enum_classes_in_module

        # For each test vector we parse it into an instance, then output a vector from that instance.
        # Then we make a new instance and feed that vector into it, then compare that the resulting calculations
        # are all equal.

        for vector, expected in vectors:
            instance = cls()
            instance.from_vector(vector)

            new_vector = instance.to_vector()

            new_instance = cls()
            new_instance.from_vector(new_vector)

            assert vector == new_vector

            assert new_instance.debug() == instance.debug()

            assert instance.calculate() == expected
            assert new_instance.calculate() == expected
Пример #2
0
def test_cvss_class_mixin(cls, module):
    # Test that an instance of every enum class is present within each of the state classes
    instance = cls()
    enum_classes_in_module = set([x[1] for x in get_enums(module)])
    enum_classes_in_class = set(
        [e[1].__class__ for e in get_enums(instance, only_classes=False)])
    assert enum_classes_in_class == enum_classes_in_module
Пример #3
0
def test_cvss_class_mixin():
    # Test that an instance of every enum class is present within each of the state classes

    for cls, module, vectors in [(CVSS2State, cvss2, v2_vectors),
                                 (CVSS3State, cvss3, v3_vectors)]:
        instance = cls()

        enum_classes_in_module = set([x[1] for x in get_enums(module)])
        enum_classes_in_class = set(
            [e[1].__class__ for e in get_enums(instance, only_classes=False)])

        assert enum_classes_in_class == enum_classes_in_module

        # For each test vector we parse it into an instance, then output a vector from that instance.
        # Then we make a new instance and feed that vector into it, then compare that the resulting calculations
        # are all equal.

        for vector, expected in vectors:
            instance = cls()
            instance.from_vector(vector)

            new_vector = instance.to_vector()

            new_instance = cls()
            new_instance.from_vector(new_vector)

            assert vector == new_vector

            assert new_instance.debug() == instance.debug()

            assert instance.calculate() == expected
            assert new_instance.calculate() == expected
Пример #4
0
def to_vector(module, getter):
    vectors = []

    ordering = getattr(module, 'ORDERING', None)

    for name, enum in get_enums(module):
        enum_attr = getter(enum)
        vector = enum_attr.get_options()["vector"]

        key = enum_attr.get_value_key()

        if key is None:
            continue

        vectors.append((vector, key, ordering.index(enum) if ordering else None))

    if ordering:
        vectors = sorted(vectors, key=operator.itemgetter(2))
        vectors = ("{0}:{1}".format(vector, key) for vector, key, _ in vectors)
    else:
        vectors = ("{0}:{1}".format(vector, key) for vector, key, _ in vectors)
        vectors = sorted(vectors)

    res = "/".join(vectors)
    if module is cvss3:
        res = "CVSS:3.0/" + res

    if module is cvss31:
        res = "CVSS:3.1/" + res

    return res
Пример #5
0
def to_vector(module, getter):
    vectors = []

    ordering = getattr(module, 'ORDERING', None)

    for name, enum in get_enums(module):
        enum_attr = getter(enum)
        vector = enum_attr.get_options()["vector"]

        key = enum_attr.get_value_key()

        if key is None:
            continue

        vectors.append((vector, key, ordering.index(enum) if ordering else None))

    if ordering:
        vectors = sorted(vectors, key=operator.itemgetter(2))
        vectors = ("{0}:{1}".format(vector, key) for vector, key, _ in vectors)
    else:
        vectors = ("{0}:{1}".format(vector, key) for vector, key, _ in vectors)
        vectors = sorted(vectors)

    res = "/".join(vectors)
    if module is cvss3:
        res = "CVSS:3.0/" + res

    return res
Пример #6
0
def parse_vector(vector, module=None, mandatory_error=True):
    if module is None:
        module = detect_vector(vector)

    vector_map, vector_values = {}, {}
    mandatory_keys, given_keys = set(), set()
    enums = dict(get_enums(module))

    for name, enum in enums.items():
        options = enum.get_options()
        vector_name = options["vector"]

        vector_map[vector_name] = enum
        if options.get("mandatory", "") == "yes":
            mandatory_keys.add(vector_name)

    split_vector = vector.split("/")

    for part in split_vector:
        if not part:
            continue

        key, value = part.split(":")

        if key == "CVSS":
            continue  # CVSS3 is prefixed with CVSS:3.0/

        if key == "RVSS":
            continue  # RVSS1 is prefixed with RVSS:1.0/

        if key not in vector_map:
            raise VectorError("Unknown key {0} in {1} vector".format(
                key, module.__name__))

        enum = vector_map[key]
        # print(enum)
        try:
            value_from_key = enum.get_value_from_vector_key(value)
        except RuntimeError as e:
            raise VectorError(*e.args)
        vector_values[enum] = value_from_key
        given_keys.add(key)

    required_diff = mandatory_keys.difference(given_keys)

    if required_diff:
        if mandatory_error:
            raise VectorError(
                "Missing mandatory keys {0}".format(required_diff))

        for enum_key in required_diff:
            enum = vector_map[enum_key]
            default = enum.get_default()
            vector_values[enum] = default

    return vector_values
Пример #7
0
def parse_vector(vector, module=None, mandatory_error=True):
    if module is None:
        module = detect_vector(vector)

    vector_map, vector_values = {}, {}
    mandatory_keys, given_keys = set(), set()
    enums = dict(get_enums(module))

    for name, enum in enums.items():
        options = enum.get_options()
        vector_name = options["vector"]

        vector_map[vector_name] = enum
        if options.get("mandatory", "") == "yes":
            mandatory_keys.add(vector_name)

    split_vector = vector.split("/")

    for part in split_vector:
        if not part:
            continue

        key, value = part.split(":")

        if key == "CVSS":
            continue  # CVSS3 is prefixed with CVSS:3.0/

        if key not in vector_map:
            raise VectorError("Unknown key {0} in {1} vector".format(key, module.__name__))

        enum = vector_map[key]
        try:
            value_from_key = enum.get_value_from_vector_key(value)
        except RuntimeError as e:
            raise VectorError(*e.args)
        vector_values[enum] = value_from_key
        given_keys.add(key)

    required_diff = mandatory_keys.difference(given_keys)

    if required_diff:
        if mandatory_error:
            raise VectorError("Missing mandatory keys {0}".format(required_diff))

        for enum_key in required_diff:
            enum = vector_map[enum_key]
            default = enum.get_default()
            vector_values[enum] = default

    return vector_values