File tree Expand file tree Collapse 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 Expand file tree Collapse file tree 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 ];
@@ -66,6 +67,7 @@ public boolean matches(HttpServletRequest request) {
66
67
}
67
68
68
69
public boolean matches (String address ) {
70
+ assertStartsWithHexa (address );
69
71
InetAddress remoteAddress = parseAddress (address );
70
72
if (!this .requiredAddress .getClass ().equals (remoteAddress .getClass ())) {
71
73
return false ;
@@ -88,6 +90,13 @@ public boolean matches(String address) {
88
90
return true ;
89
91
}
90
92
93
+ private void assertStartsWithHexa (String ipAddress ) {
94
+ Assert .isTrue (
95
+ ipAddress .charAt (0 ) == '[' || ipAddress .charAt (0 ) == ':'
96
+ || Character .digit (ipAddress .charAt (0 ), 16 ) != -1 ,
97
+ "ipAddress must start with a [, :, or a hexadecimal digit" );
98
+ }
99
+
91
100
private InetAddress parseAddress (String address ) {
92
101
try {
93
102
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