1818
1919import java .net .InetAddress ;
2020import java .net .UnknownHostException ;
21+ import java .util .Scanner ;
2122
2223import jakarta .servlet .http .HttpServletRequest ;
2324
@@ -47,7 +48,7 @@ public final class IpAddressMatcher implements RequestMatcher {
4748 * come.
4849 */
4950 public IpAddressMatcher (String ipAddress ) {
50- assertStartsWithHexa (ipAddress );
51+ assertNotHostName (ipAddress );
5152 if (ipAddress .indexOf ('/' ) > 0 ) {
5253 String [] addressAndMask = StringUtils .split (ipAddress , "/" );
5354 ipAddress = addressAndMask [0 ];
@@ -68,7 +69,7 @@ public boolean matches(HttpServletRequest request) {
6869 }
6970
7071 public boolean matches (String address ) {
71- assertStartsWithHexa (address );
72+ assertNotHostName (address );
7273 InetAddress remoteAddress = parseAddress (address );
7374 if (!this .requiredAddress .getClass ().equals (remoteAddress .getClass ())) {
7475 return false ;
@@ -91,11 +92,17 @@ public boolean matches(String address) {
9192 return true ;
9293 }
9394
94- private void assertStartsWithHexa (String ipAddress ) {
95- Assert .isTrue (
96- ipAddress .charAt (0 ) == '[' || ipAddress .charAt (0 ) == ':'
97- || Character .digit (ipAddress .charAt (0 ), 16 ) != -1 ,
98- "ipAddress must start with a [, :, or a hexadecimal digit" );
95+ private void assertNotHostName (String ipAddress ) {
96+ String error = "ipAddress " + ipAddress + " doesn't look like an IP Address. Is it a host name?" ;
97+ Assert .isTrue (ipAddress .charAt (0 ) == '[' || ipAddress .charAt (0 ) == ':'
98+ || Character .digit (ipAddress .charAt (0 ), 16 ) != -1 , error );
99+ if (!ipAddress .contains (":" )) {
100+ Scanner parts = new Scanner (ipAddress );
101+ parts .useDelimiter ("[./]" );
102+ while (parts .hasNext ()) {
103+ Assert .isTrue (parts .hasNextInt () && parts .nextInt () >> 8 == 0 , error );
104+ }
105+ }
99106 }
100107
101108 private InetAddress parseAddress (String address ) {
0 commit comments