Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(trigger): keep webhook form/query integers exact and reject non-finite numbers (#44706)

Temitope Kadri2a99ac015e8f
diff --git a/api/services/trigger/webhook_service.py b/api/services/trigger/webhook_service.py
index 78a67caa3a3..0c9f6f8473f 100644
--- a/api/services/trigger/webhook_service.py
+++ b/api/services/trigger/webhook_service.py
@@ -1,5 +1,6 @@
import json
import logging
+import math
import mimetypes
import secrets
from collections.abc import Callable, Mapping, Sequence
@@ -620,6 +621,11 @@ class WebhookService:
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:
@@ -775,10 +781,9 @@ class WebhookService:
@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
diff --git a/api/tests/unit_tests/services/trigger/test_webhook_service.py b/api/tests/unit_tests/services/trigger/test_webhook_service.py
index 9aa4af9dcad..7e5ac49b18e 100644
--- a/api/tests/unit_tests/services/trigger/test_webhook_service.py
+++ b/api/tests/unit_tests/services/trigger/test_webhook_service.py
@@ -1139,6 +1139,26 @@ class TestWebhookServiceAdditionalCases:
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)