Skip to content

Commit b94248c

Browse files
committed
Strictly validations
1 parent 33e31ef commit b94248c

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/ssl/DefaultHostnameVerifier.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,21 @@ static List<SubjectName> getSubjectAltNames(final X509Certificate cert, final in
357357
} else if (o instanceof byte[]) {
358358
final byte[] bytes = (byte[]) o;
359359
final String decodedValue;
360-
if (type == SubjectName.IP && bytes.length == 4) {
361-
decodedValue = byteArrayToIp(bytes);
362-
} else if (type == SubjectName.IP && bytes.length == 16) {
363-
decodedValue = byteArrayToIPv6(bytes);
360+
if (type == SubjectName.IP) {
361+
if (bytes.length == 4) {
362+
decodedValue = byteArrayToIp(bytes); // IPv4
363+
} else if (bytes.length == 16) {
364+
decodedValue = byteArrayToIPv6(bytes); // IPv6
365+
} else {
366+
throw new IllegalArgumentException("Invalid byte length for IP address: " + bytes.length);
367+
}
368+
} else if (type == SubjectName.DNS) {
369+
throw new IllegalArgumentException("Unexpected byte[] for DNS SAN type");
364370
} else {
365-
decodedValue = TextUtils.toHexString(bytes);
371+
if (LOG.isWarnEnabled()) {
372+
LOG.warn("Unrecognized SAN type: {}, data: {}", type, TextUtils.toHexString(bytes));
373+
}
374+
decodedValue = TextUtils.toHexString(bytes); // Fallback to hex string
366375
}
367376

368377
result.add(new SubjectName(decodedValue, type));

httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,8 @@ void testSimulatedBytePropertiesRawHex() throws Exception {
624624

625625
// Mocking the certificate behavior
626626
final X509Certificate mockCert = generateX509Certificate(entries);
627+
Assertions.assertThrows(IllegalArgumentException.class, () -> DefaultHostnameVerifier.getSubjectAltNames(mockCert, -1));
627628

628-
final List<SubjectName> result = DefaultHostnameVerifier.getSubjectAltNames(mockCert, -1);
629-
Assertions.assertEquals(1, result.size(), "Should have one SubjectAltName");
630-
631-
final SubjectName sn = result.get(0);
632-
Assertions.assertEquals(SubjectName.IP, sn.getType(), "Should be an IP type");
633-
// Here, you'll need logic to convert byte array to string for assertion
634-
Assertions.assertEquals("0a1b2c3d4e5f", sn.getValue(), "IP address should match after conversion");
635629
}
636630

637631

0 commit comments

Comments
 (0)