Don't translate URLs (#154224)

Co-authored-by: jbouwh <jan@jbsoft.nl>
Co-authored-by: Jan Bouwhuis <jbouwh@users.noreply.github.com>
This commit is contained in:
Joost Lekkerkerker
2026-01-27 17:30:15 +01:00
committed by GitHub
parent 2dc1981932
commit 36b9234f26
2 changed files with 28 additions and 0 deletions

View File

@@ -24,6 +24,11 @@ RE_REFERENCE = r"\[\%key:(.+)\%\]"
RE_TRANSLATION_KEY = re.compile(r"^(?!.+[_-]{2})(?![_-])[a-z0-9-_]+(?<![_-])$")
RE_COMBINED_REFERENCE = re.compile(r"(.+\[%)|(%\].+)")
RE_PLACEHOLDER_IN_SINGLE_QUOTES = re.compile(r"'{\w+}'")
RE_URL = re.compile(
r"(((ftp|ftps|scp|http|https|mqtt|mqtts|socket|socks5):\/\/|www\.)"
r"[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?)",
re.IGNORECASE,
)
# Only allow translation of integration names if they contain non-brand names
ALLOW_NAME_TRANSLATION = {
@@ -143,6 +148,11 @@ def validate_translation_value(value: Any, allow_placeholders=True) -> str:
raise vol.Invalid("the string should not contain combined translations")
if string_value != string_value.strip():
raise vol.Invalid("the string should not contain leading or trailing spaces")
if RE_URL.search(string_value):
raise vol.Invalid(
"the string should not contain URLs, "
"please use description placeholders instead"
)
return string_value

View File

@@ -411,3 +411,21 @@ def test_gen_strings_schema(
validated = schema(SAMPLE_STRINGS)
assert validated == SAMPLE_STRINGS
@pytest.mark.parametrize(
"translation_string",
[
"An example is: https://example.com.",
"www.example.com",
"http://example.com:8080",
"WWW.EXAMPLE.COM",
"HTTPS://www.example.com",
],
)
def test_no_placeholders_used_for_urls(translation_string: str) -> None:
"""Test that translation strings containing URLs are rejected."""
schema = vol.Schema(translations.translation_value_validator)
with pytest.raises(vol.Invalid):
schema(translation_string)