File tree Expand file tree Collapse file tree
main/java/dev/aikido/agent_api/helpers/net Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,13 +18,20 @@ public void add(String ipOrCIDR) {
1818 return ; // Don't add if IP is null
1919 }
2020 IPAddress ip = new IPAddressString (ipOrCIDR ).getAddress ();
21+ if (ip == null ) {
22+ return ;
23+ }
24+ // Normalize IPv4-mapped IPv6 addresses to their IPv4 form so matching is symmetric.
25+ if (ip .isIPv6 () && ip .toIPv6 ().isIPv4Convertible ()) {
26+ IPAddress ipv4 = ip .toIPv6 ().toIPv4 ();
27+ if (ipv4 != null ) {
28+ ip = ipv4 ;
29+ }
30+ }
2131 if (ipOrCIDR .contains ("/" )) {
22- // CIDR :
2332 ip = ip .toPrefixBlock ();
2433 }
25- if (ip != null ) {
26- ipAddresses .add (ip );
27- }
34+ ipAddresses .add (ip );
2835 }
2936
3037 public boolean matches (String ip ) {
Original file line number Diff line number Diff line change @@ -140,4 +140,32 @@ public void testBlocklistStoredIPv4MappedMatchesIPv4Input() {
140140 blocklist .add ("::ffff:23.45.67.89" );
141141 assertTrue (blocklist .matches ("::ffff:23.45.67.89" ));
142142 }
143+
144+ @ Test
145+ public void testBlocklistAddInvalidIpIgnored () {
146+ blocklist .add ("notanip" );
147+ assertEquals (0 , blocklist .length ());
148+ assertFalse (blocklist .matches ("192.168.1.1" ));
149+ }
150+
151+ @ Test
152+ public void testBlocklistLengthEmpty () {
153+ assertEquals (0 , blocklist .length ());
154+ }
155+
156+ @ Test
157+ public void testBlocklistStoredIPv4MappedMatchesPlainIPv4 () {
158+ blocklist .add ("::ffff:23.45.67.89" );
159+ assertTrue (blocklist .matches ("23.45.67.89" ));
160+ assertTrue (blocklist .matches ("::ffff:23.45.67.89" ));
161+ assertFalse (blocklist .matches ("23.45.67.90" ));
162+ }
163+
164+ @ Test
165+ public void testBlocklistStoredIPv4MappedCidrMatchesPlainIPv4 () {
166+ blocklist .add ("::ffff:10.0.0.0/104" );
167+ assertTrue (blocklist .matches ("10.1.2.3" ));
168+ assertTrue (blocklist .matches ("::ffff:10.1.2.3" ));
169+ assertFalse (blocklist .matches ("11.1.2.3" ));
170+ }
143171}
You can’t perform that action at this time.
0 commit comments