Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CVSS v4.0 rounding #61

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cvss/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
from .exceptions import CVSS2Error, CVSS3Error, CVSS4Error, CVSSError
from .interactive import ask_interactively

__version__ = "3.1"
__version__ = "3.2"
16 changes: 4 additions & 12 deletions cvss/cvss4.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from __future__ import unicode_literals

import copy
from decimal import ROUND_CEILING, ROUND_HALF_UP
from decimal import ROUND_HALF_UP
from decimal import Decimal as D

from .constants4 import (
Expand All @@ -52,16 +52,8 @@
from .exceptions import CVSS4MalformedError, CVSS4MandatoryError


def round_away_from_zero(x, dp):
return float(D(x).quantize(D("0." + "0" * dp), rounding=ROUND_HALF_UP))


def round_up(value):
"""
Round up is defined as the smallest number, specified to one decimal place, that is equal to
or higher than its input. For example, Round up (4.02) is 4.1; and Round up (4.00) is 4.0.
"""
return value.quantize(D("0.1"), rounding=ROUND_CEILING)
def round_away_from_zero(x):
return float(D(x * 10).quantize(D("1"), rounding=ROUND_HALF_UP) / 10)


class CVSS4(object):
Expand Down Expand Up @@ -555,7 +547,7 @@ def compute_base_score(self):
value = max(0.0, value)
value = min(10.0, value)

self.base_score = round_away_from_zero(value, 1)
self.base_score = round_away_from_zero(value)

def __hash__(self):
return hash(self.clean_vector())
Expand Down
209,952 changes: 104,976 additions & 104,976 deletions tests/vectors_base4

Large diffs are not rendered by default.

Loading
Loading