File tree 2 files changed +15
-0
lines changed
main/java/org/springframework/security/web/util/matcher
test/java/org/springframework/security/web/util/matcher
2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ public final class IpAddressMatcher implements RequestMatcher {
47
47
* come.
48
48
*/
49
49
public IpAddressMatcher (String ipAddress ) {
50
+ assertStartsWithHexa (ipAddress );
50
51
if (ipAddress .indexOf ('/' ) > 0 ) {
51
52
String [] addressAndMask = StringUtils .split (ipAddress , "/" );
52
53
ipAddress = addressAndMask [0 ];
@@ -67,6 +68,7 @@ public boolean matches(HttpServletRequest request) {
67
68
}
68
69
69
70
public boolean matches (String address ) {
71
+ assertStartsWithHexa (address );
70
72
InetAddress remoteAddress = parseAddress (address );
71
73
if (!this .requiredAddress .getClass ().equals (remoteAddress .getClass ())) {
72
74
return false ;
@@ -89,6 +91,13 @@ public boolean matches(String address) {
89
91
return true ;
90
92
}
91
93
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" );
99
+ }
100
+
92
101
private InetAddress parseAddress (String address ) {
93
102
try {
94
103
return InetAddress .getByName (address );
Original file line number Diff line number Diff line change @@ -105,4 +105,10 @@ public void ipv6RequiredAddressMaskTooLongThenIllegalArgumentException() {
105
105
"fe80::21f:5bff:fe33:bd68" , 129 ));
106
106
}
107
107
108
+ @ Test
109
+ public void invalidAddressThenIllegalArgumentException () {
110
+ assertThatIllegalArgumentException ().isThrownBy (() -> new IpAddressMatcher ("invalid-ip" ))
111
+ .withMessage ("ipAddress must start with a [, :, or a hexadecimal digit" );
112
+ }
113
+
108
114
}
You can’t perform that action at this time.
0 commit comments