Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Protect the Decimal scalar from non-normal numbers (#16520)
Patryk Zawadzki692c8bbc083e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b20494d0fc..c7648144e3 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -19,3 +19,4 @@ All notable, unreleased changes to this project will be documented in this file.- Added support for numeric and lower-case boolean environment variables - #16313 by @NyanKiyoshi- Fixed a potential crash when Checkout metadata is accessed with high concurrency - #16411 by @patrys+- Fixed a crash when the Decimal scalar is passed a non-normal value - #16520 by @patrysdiff --git a/saleor/graphql/core/scalars.py b/saleor/graphql/core/scalars.pyindex 1f5c43e1bd..6ec801de6c 100644--- a/saleor/graphql/core/scalars.py+++ b/saleor/graphql/core/scalars.py@@ -33,7 +33,14 @@ class Decimal(graphene.Float):# Converting the float to str before parsing it to Decimal is# necessary to keep the decimal places as typedvalue = str(value)- return decimal.Decimal(value)+ value = decimal.Decimal(value)+ if value.is_infinite():+ return None+ if value.is_nan():+ return None+ if value.is_subnormal():+ return None+ return valueexcept decimal.DecimalException:return None@@ -46,7 +53,7 @@ class PositiveDecimal(Decimal):@staticmethoddef parse_value(value):- value = super(PositiveDecimal, PositiveDecimal).parse_value(value)+ value = Decimal.parse_value(value)if value and value < 0:return Nonereturn value@@ -125,19 +132,19 @@ class WeightScalar(graphene.Scalar):class UUID(graphene.UUID):@staticmethoddef serialize(uuid):- return super(UUID, UUID).serialize(uuid)+ return graphene.UUID.serialize(uuid)@staticmethoddef parse_literal(node):try:- return super(UUID, UUID).parse_literal(node)+ return graphene.UUID.parse_literal(node)except ValueError:return None@staticmethoddef parse_value(value):try:- return super(UUID, UUID).parse_value(value)+ return graphene.UUID.parse_value(value)except ValueError:return None@@ -153,7 +160,7 @@ class DateTime(graphene.DateTime):@staticmethoddef parse_value(value):- parsed_value = super(DateTime, DateTime).parse_value(value)+ parsed_value = graphene.DateTime.parse_value(value)if parsed_value is not None and isinstance(parsed_value, datetime):if parsed_value.year in [MINYEAR, MAXYEAR]:try:@@ -177,7 +184,7 @@ class Date(graphene.Date):# The current graphene version returning unhandled `IndexError`.if isinstance(value, str) and not value:return None- return super(Date, Date).parse_value(value)+ return graphene.Date.parse_value(value)class Minute(graphene.Int):diff --git a/saleor/graphql/core/tests/test_scalars.py b/saleor/graphql/core/tests/test_scalars.pyindex cd0c7c90b8..0b02813988 100644--- a/saleor/graphql/core/tests/test_scalars.py+++ b/saleor/graphql/core/tests/test_scalars.py@@ -5,6 +5,7 @@ import pytestfrom ....order.models import Orderfrom ....payment.interface import PaymentGatewayDatafrom ...tests.utils import get_graphql_content, get_graphql_content_from_response+from ..scalars import Decimal, PositiveDecimalfrom ..utils import to_global_id_or_noneQUERY_CHECKOUT = """@@ -419,3 +420,15 @@ def test_correct_date_time_as_input(# thenget_graphql_content(response)++[email protected]("invalid_value", ["NaN", "-Infinity", "1e-9999999", "-", "x"])+def test_decimal_scalar_invalid_value(invalid_value):+ result = Decimal.parse_value(invalid_value)+ assert result is None++[email protected]("invalid_value", ["NaN", "-Infinity", "1e-9999999", "-1"])+def test_positive_decimal_scalar_invalid_value(invalid_value):+ result = PositiveDecimal.parse_value(invalid_value)+ assert result is None