Skip to content
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
11 changes: 8 additions & 3 deletions api/services/trigger/webhook_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import math
import mimetypes
import secrets
from collections.abc import Callable, Mapping, Sequence
Expand Down Expand Up @@ -620,6 +621,11 @@ def _convert_form_value(cls, param_name: str, value: str, param_type: SegmentTyp
case SegmentType.NUMBER:
if not cls._can_convert_to_number(value):
raise ValueError(f"Cannot convert '{value}' to number")
try:
# float() rounds integers above 2**53, so parse integers exactly first.
return int(value)
except ValueError:
pass
numeric_value = float(value)
return int(numeric_value) if numeric_value.is_integer() else numeric_value
case SegmentType.BOOLEAN:
Expand Down Expand Up @@ -775,10 +781,9 @@ def _validation_error(cls, error_message: str) -> ValidationResultDict:

@classmethod
def _can_convert_to_number(cls, value: str) -> bool:
"""Check if a string can be converted to a number."""
"""Check if a string can be converted to a finite number."""
try:
float(value)
return True
return math.isfinite(float(value))
except ValueError:
return False

Expand Down
20 changes: 20 additions & 0 deletions api/tests/unit_tests/services/trigger/test_webhook_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,26 @@ def test_convert_form_value_should_convert_supported_types(
result = WebhookService._convert_form_value("param", raw_value, param_type)
assert result == expected

@pytest.mark.parametrize(
("raw_value", "expected"),
[
("1234567890123456789", 1234567890123456789),
("9007199254740993", 9007199254740993),
("-9007199254740993", -9007199254740993),
("1e3", 1000),
],
)
def test_convert_form_value_should_keep_integers_exact(self, raw_value: str, expected: int) -> None:
result = WebhookService._convert_form_value("param", raw_value, SegmentType.NUMBER)

assert result == expected
assert isinstance(result, int)

@pytest.mark.parametrize("raw_value", ["nan", "NaN", "inf", "-Infinity", "1e400"])
def test_convert_form_value_should_reject_non_finite_numbers(self, raw_value: str) -> None:
with pytest.raises(ValueError, match="to number"):
WebhookService._convert_form_value("param", raw_value, SegmentType.NUMBER)

def test_convert_form_value_should_raise_for_unsupported_type(self) -> None:
with pytest.raises(ValueError, match="Unsupported type"):
WebhookService._convert_form_value("p", "x", SegmentType.FILE)
Expand Down
Loading