Skip to content

Commit 9b6e25b

Browse files
committed
2 parents da69afc + f466705 commit 9b6e25b

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ The <action> type attribute can be add,update,fix,remove.
9191
<action type="fix" dev="ggregory" due-to="sahvx655-wq, Gary Gregory">Preserve exact value in BigDecimal and BigInteger validate (#406).</action>
9292
<action type="fix" dev="ggregory" due-to="sahvx655-wq, Gary Gregory">Preserve fractional bound in BigIntegerValidator range checks (#407).</action>
9393
<action type="fix" dev="sebb" due-to="sahvx655-wq">DomainValidatorTest: ensure CC is in correct list (#409).</action>
94+
<action type="fix" dev="sebb" due-to="sahvx655-wq">range-check the port for IPv6 hosts in isValidAuthority (#412).</action>
9495
<action type="fix" dev="sebb">Fix deprecations in test classes.</action>
9596
<!-- ADD -->
9697
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use CheckDigitException.CheckDigitException(String, Object...) (#389).</action>

src/main/java/org/apache/commons/validator/routines/UrlValidator.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,19 @@ protected boolean isValidAuthority(final String authority) {
443443
return false;
444444
}
445445
}
446-
final String port = authorityMatcher.group(PARSE_AUTHORITY_PORT);
447-
if (!GenericValidator.isBlankOrNull(port)) {
448-
try {
449-
final int iPort = Integer.parseInt(port);
450-
if (iPort < 0 || iPort > MAX_UNSIGNED_16_BIT_INT) {
451-
return false;
452-
}
453-
} catch (final NumberFormatException nfe) {
454-
return false; // this can happen for big numbers
446+
}
447+
448+
// the port is captured in the same group regardless of host form, so it must be
449+
// range checked for a bracketed IPv6 host too, not just the hostname/IPv4 branch
450+
final String port = authorityMatcher.group(PARSE_AUTHORITY_PORT);
451+
if (!GenericValidator.isBlankOrNull(port)) {
452+
try {
453+
final int iPort = Integer.parseInt(port);
454+
if (iPort < 0 || iPort > MAX_UNSIGNED_16_BIT_INT) {
455+
return false;
455456
}
457+
} catch (final NumberFormatException nfe) {
458+
return false; // this can happen for big numbers
456459
}
457460
}
458461

src/test/java/org/apache/commons/validator/routines/UrlValidatorTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ void testIpv6EmbeddedIpv4() {
173173
assertFalse(urlValidator.isValid("http://[::ffff:129.144.52.999]/"));
174174
}
175175

176+
@Test
177+
void testIpv6Port() {
178+
final UrlValidator urlValidator = new UrlValidator();
179+
// a port on a bracketed IPv6 host must be range checked just like a hostname/IPv4 host
180+
assertTrue(urlValidator.isValid("http://[::1]:65535/index.html"));
181+
assertFalse(urlValidator.isValid("http://[::1]:65536/index.html"));
182+
assertFalse(urlValidator.isValid("http://[::1]:99999/index.html"));
183+
assertTrue(urlValidator.isValidAuthority("[::1]:65535"));
184+
assertFalse(urlValidator.isValidAuthority("[::1]:65536"));
185+
}
186+
176187
@ParameterizedTest
177188
// @formatter:off
178189
@ValueSource(strings = {

0 commit comments

Comments
 (0)