Skip to content

Commit 479ca41

Browse files
LordAizen1anuraaga
andauthored
Exclude the trailing dot from the hostname 253-character limit (#503)
Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
1 parent 81f2962 commit 479ca41

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎protovalidate/_funcs.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ def cel_is_hostname(val: object) -> bool:
210210

211211
def _is_hostname(val: str) -> bool:
212212
"""Internal implementation."""
213-
if len(val) > 253:
214-
return False
213+
string = val.removesuffix(".").lower()
215214

216-
string = val[0 : len(val) - 1].lower() if val.endswith(".") else val.lower()
215+
# The 253-character limit excludes the optional trailing dot (RFC 3339) which
216+
# we just stripped.
217+
if len(string) > 253:
218+
return False
217219

218220
all_digits = False
219221
parts = string.lower().split(sep=".")

‎test/test_funcs.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (c) 2023-2026 Buf Technologies, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from protovalidate._funcs import _is_hostname
18+
19+
20+
def test_hostname_trailing_dot_excluded_from_length() -> None:
21+
name253 = ("a" * 63 + ".") * 3 + "a" * 61
22+
name254 = ("a" * 63 + ".") * 3 + "a" * 62
23+
assert len(name253) == 253
24+
assert len(name254) == 254
25+
# The 253-character limit excludes the optional trailing dot (RFC 3339).
26+
assert _is_hostname(name253)
27+
assert _is_hostname(name253 + ".")
28+
assert not _is_hostname(name254)
29+
assert not _is_hostname(name254 + ".")

0 commit comments

Comments
 (0)