mirror of
https://github.com/langgenius/dify.git
synced 2026-08-28 17:32:13 +08:00
Co-authored-by: Harsh Kashyap <Harsh23Kashyap@users.noreply.github.com>
This commit is contained in:
parent
63f072ebfb
commit
59fb603ec6
@ -289,7 +289,11 @@ UUIDStr = Annotated[str, AfterValidator(_strict_uuid)]
|
|||||||
|
|
||||||
def alphanumeric(value: str):
|
def alphanumeric(value: str):
|
||||||
# check if the value is alphanumeric and underlined
|
# check if the value is alphanumeric and underlined
|
||||||
if re.match(r"^[a-zA-Z0-9_]+$", value):
|
# Use re.fullmatch instead of re.match to reject trailing newlines.
|
||||||
|
# In Python, '$' matches at end-of-string OR just before a trailing newline,
|
||||||
|
# so re.match accepts "tool_name\n". re.fullmatch requires the entire
|
||||||
|
# string to match. Regression for #39666 (sibling of #39234 / #39548).
|
||||||
|
if re.fullmatch(r"^[a-zA-Z0-9_]+$", value):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
raise ValueError(f"{value} is not a valid alphanumeric value")
|
raise ValueError(f"{value} is not a valid alphanumeric value")
|
||||||
|
|||||||
@ -2,7 +2,7 @@ from datetime import datetime
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from libs.helper import OptionalTimestampField, email, escape_like_pattern, extract_tenant_id
|
from libs.helper import OptionalTimestampField, alphanumeric, email, escape_like_pattern, extract_tenant_id
|
||||||
from models.account import Account
|
from models.account import Account
|
||||||
from models.model import EndUser
|
from models.model import EndUser
|
||||||
|
|
||||||
@ -153,3 +153,47 @@ class TestEmailValidator:
|
|||||||
def test_invalid_email_rejected(self):
|
def test_invalid_email_rejected(self):
|
||||||
with pytest.raises(ValueError, match="not a valid email"):
|
with pytest.raises(ValueError, match="not a valid email"):
|
||||||
email("not-an-email")
|
email("not-an-email")
|
||||||
|
|
||||||
|
|
||||||
|
class TestAlphanumericValidator:
|
||||||
|
"""Tests for the alphanumeric() validator — regression for #39666."""
|
||||||
|
|
||||||
|
def test_valid_alphanumeric_accepted(self):
|
||||||
|
assert alphanumeric("tool_name") == "tool_name"
|
||||||
|
assert alphanumeric("Tool123") == "Tool123"
|
||||||
|
assert alphanumeric("_underscore_start") == "_underscore_start"
|
||||||
|
assert alphanumeric("a") == "a"
|
||||||
|
|
||||||
|
def test_trailing_newline_rejected(self):
|
||||||
|
# re.match with $ accepts a trailing \n in Python; re.fullmatch does not.
|
||||||
|
# This was the pre-fix behaviour: alphanumeric("tool\n") returned "tool\n".
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("tool_name\n")
|
||||||
|
|
||||||
|
def test_trailing_carriage_return_rejected(self):
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("tool_name\r")
|
||||||
|
|
||||||
|
def test_trailing_crlf_rejected(self):
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("tool_name\r\n")
|
||||||
|
|
||||||
|
def test_leading_newline_rejected(self):
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("\ntool_name")
|
||||||
|
|
||||||
|
def test_embedded_whitespace_rejected(self):
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("tool name")
|
||||||
|
|
||||||
|
def test_empty_string_rejected(self):
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("")
|
||||||
|
|
||||||
|
def test_special_characters_rejected(self):
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("tool-name")
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("tool.name")
|
||||||
|
with pytest.raises(ValueError, match="not a valid alphanumeric value"):
|
||||||
|
alphanumeric("tool/name")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user