Skip to content

Commit 3a3f1eb

Browse files
committed
use InetAddresses.forString instead of InetAddress.getByName because of DNS resolving
1 parent daf84be commit 3a3f1eb

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

captchaservice-backend/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@
318318
<artifactId>commons-net</artifactId>
319319
<version>3.11.1</version>
320320
</dependency>
321+
<dependency>
322+
<groupId>com.google.guava</groupId>
323+
<artifactId>guava</artifactId>
324+
<version>33.4.8-jre</version>
325+
</dependency>
321326
</dependencies>
322327

323328
<scm>

captchaservice-backend/src/main/java/de/muenchen/captchaservice/service/sourceaddress/SourceAddressService.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.muenchen.captchaservice.service.sourceaddress;
22

3+
import com.google.common.net.InetAddresses;
34
import de.muenchen.captchaservice.configuration.captcha.CaptchaProperties;
45
import de.muenchen.captchaservice.configuration.captcha.CaptchaSite;
56
import de.muenchen.captchaservice.data.SourceAddress;
@@ -8,7 +9,6 @@
89
import org.springframework.stereotype.Service;
910

1011
import java.net.InetAddress;
11-
import java.net.UnknownHostException;
1212

1313
@Service
1414
@AllArgsConstructor
@@ -18,17 +18,13 @@ public class SourceAddressService {
1818
public SourceAddress parse(final String siteKey, final String sourceAddress) {
1919
CaptchaSite site = captchaProperties.sites().get(siteKey);
2020
String networkAddressString;
21-
try {
22-
InetAddress addr = InetAddress.getByName(sourceAddress);
23-
if (addr instanceof java.net.Inet4Address) {
24-
networkAddressString = NetworkAddressCalculator.getNetworkAddress(sourceAddress, site.sourceAddressIpv4Cidr());
25-
} else if (addr instanceof java.net.Inet6Address) {
26-
networkAddressString = NetworkAddressCalculator.getNetworkAddress(sourceAddress, site.sourceAddressIpv6Cidr());
27-
} else {
28-
throw new IllegalArgumentException("Unsupported IP address type: " + addr.getClass().getName());
29-
}
30-
} catch (UnknownHostException e) {
31-
throw new IllegalArgumentException("Invalid IP address provided: " + sourceAddress, e);
21+
InetAddress addr = InetAddresses.forString(sourceAddress);
22+
if (addr instanceof java.net.Inet4Address) {
23+
networkAddressString = NetworkAddressCalculator.getNetworkAddress(sourceAddress, site.sourceAddressIpv4Cidr());
24+
} else if (addr instanceof java.net.Inet6Address) {
25+
networkAddressString = NetworkAddressCalculator.getNetworkAddress(sourceAddress, site.sourceAddressIpv6Cidr());
26+
} else {
27+
throw new IllegalArgumentException("Unsupported IP address type: " + addr.getClass().getName());
3228
}
3329
return new SourceAddress(networkAddressString);
3430
}

captchaservice-backend/src/main/java/de/muenchen/captchaservice/util/networkaddresscalculator/NetworkAddressCalculator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.muenchen.captchaservice.util.networkaddresscalculator;
22

3+
import com.google.common.net.InetAddresses;
4+
35
import java.net.InetAddress;
46
import java.net.UnknownHostException;
57

@@ -17,7 +19,7 @@ public class NetworkAddressCalculator {
1719
*/
1820
public static String getNetworkAddress(String address, int netSize) {
1921
try {
20-
InetAddress inetAddress = InetAddress.getByName(address);
22+
InetAddress inetAddress = InetAddresses.forString(address);
2123
byte[] addressBytes = inetAddress.getAddress();
2224
int addressLengthBits = addressBytes.length * 8;
2325

@@ -45,8 +47,7 @@ public static String getNetworkAddress(String address, int netSize) {
4547

4648
InetAddress netInetAddress = InetAddress.getByAddress(netAddressBytes);
4749
return netInetAddress.getHostAddress();
48-
49-
} catch (UnknownHostException e) {
50+
} catch (IllegalArgumentException | UnknownHostException e) {
5051
throw new InvalidAddressException("Invalid address: " + address, address, e);
5152
}
5253
}

captchaservice-backend/src/main/java/de/muenchen/captchaservice/validation/SourceAddressConstraintValidator.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package de.muenchen.captchaservice.validation;
22

3+
import com.google.common.net.InetAddresses;
34
import jakarta.validation.ConstraintValidator;
45
import jakarta.validation.ConstraintValidatorContext;
56

6-
import java.net.InetAddress;
7-
import java.net.UnknownHostException;
8-
97
public class SourceAddressConstraintValidator implements ConstraintValidator<ValidSourceAddress, String> {
108
@Override
119
public boolean isValid(final String value, final ConstraintValidatorContext constraintValidatorContext) {
1210
try {
13-
InetAddress.getByName(value);
14-
} catch (UnknownHostException e) {
11+
InetAddresses.forString(value);
12+
} catch (IllegalArgumentException e) {
1513
return false;
1614
}
1715
return true;

0 commit comments

Comments
 (0)