Skip to content

Commit 6d6276e

Browse files
committed
fix: Reject leading zeros in ipv4 format octets
1 parent 0a23331 commit 6d6276e

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

src/main/java/com/retailsvc/http/validate/DefaultValidator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ private record FormatCheck(Predicate<String> isValid, String message) {}
5050
private static final Pattern EMAIL = Pattern.compile("^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$");
5151

5252
private static final Pattern IPV4 =
53-
Pattern.compile("^((25[0-5]|2[0-4]\\d|1?\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1?\\d?\\d)$");
53+
Pattern.compile(
54+
"^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)$");
5455

5556
private static final Pattern IPV6 =
5657
Pattern.compile(

src/test/java/com/retailsvc/http/validate/StringIntegerNumberTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ void stringFormatIpv4() {
128128
assertThatThrownBy(() -> v.validate("1.2.3", s, "/v"))
129129
.extracting(t -> ((ValidationException) t).error().keyword())
130130
.isEqualTo("format");
131+
assertThatThrownBy(() -> v.validate("01.02.03.04", s, "/v"))
132+
.extracting(t -> ((ValidationException) t).error().keyword())
133+
.isEqualTo("format");
131134
}
132135

133136
@Test

0 commit comments

Comments
 (0)