18
18
19
19
import java .net .InetAddress ;
20
20
import java .net .UnknownHostException ;
21
+ import java .util .Scanner ;
21
22
22
23
import jakarta .servlet .http .HttpServletRequest ;
23
24
@@ -47,7 +48,7 @@ public final class IpAddressMatcher implements RequestMatcher {
47
48
* come.
48
49
*/
49
50
public IpAddressMatcher (String ipAddress ) {
50
- assertStartsWithHexa (ipAddress );
51
+ assertNotHostName (ipAddress );
51
52
if (ipAddress .indexOf ('/' ) > 0 ) {
52
53
String [] addressAndMask = StringUtils .split (ipAddress , "/" );
53
54
ipAddress = addressAndMask [0 ];
@@ -68,7 +69,7 @@ public boolean matches(HttpServletRequest request) {
68
69
}
69
70
70
71
public boolean matches (String address ) {
71
- assertStartsWithHexa (address );
72
+ assertNotHostName (address );
72
73
InetAddress remoteAddress = parseAddress (address );
73
74
if (!this .requiredAddress .getClass ().equals (remoteAddress .getClass ())) {
74
75
return false ;
@@ -91,11 +92,17 @@ public boolean matches(String address) {
91
92
return true ;
92
93
}
93
94
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
+ }
99
106
}
100
107
101
108
private InetAddress parseAddress (String address ) {
0 commit comments