Skip to content

Commit 308ce55

Browse files
committed
Fix x-forwareded-for header validation using apache commons-validation.
1 parent 5ee9d1e commit 308ce55

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

components/apimgt/org.wso2.carbon.apimgt.gateway/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@
333333
<groupId>org.wso2.carbon</groupId>
334334
<artifactId>org.wso2.carbon.logging.correlation</artifactId>
335335
</dependency>
336+
<dependency>
337+
<groupId>commons-validator</groupId>
338+
<artifactId>commons-validator</artifactId>
339+
</dependency>
336340
</dependencies>
337341

338342
<build>

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/throttling/publisher/DataProcessAndPublishingAgent.java

+4-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.apache.commons.lang3.StringUtils;
77
import org.apache.commons.logging.Log;
88
import org.apache.commons.logging.LogFactory;
9+
import org.apache.commons.validator.routines.InetAddressValidator;
910
import org.apache.synapse.MessageContext;
1011
import org.apache.synapse.core.axis2.Axis2MessageContext;
1112
import org.apache.synapse.transport.passthru.util.RelayUtils;
@@ -22,16 +23,11 @@
2223
import org.wso2.carbon.databridge.agent.DataPublisher;
2324

2425
import java.io.IOException;
25-
import java.net.Inet4Address;
26-
import java.net.Inet6Address;
27-
import java.net.InetAddress;
28-
import java.net.UnknownHostException;
2926
import java.nio.charset.Charset;
3027
import java.util.ArrayList;
3128
import java.util.Collections;
3229
import java.util.HashMap;
3330
import java.util.Map;
34-
import java.util.regex.Pattern;
3531
import javax.xml.stream.XMLStreamException;
3632

3733
import static org.wso2.carbon.apimgt.api.APIConstants.AIAPIConstants.*;
@@ -44,16 +40,10 @@
4440
*/
4541
public class DataProcessAndPublishingAgent implements Runnable {
4642
private static final Log log = LogFactory.getLog(DataProcessAndPublishingAgent.class);
47-
private static final Pattern IPV4_PATTERN = Pattern.compile(
48-
"^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$");
49-
private static final Pattern IPV6_PATTERN = Pattern.compile(
50-
"([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})");
5143
private static String streamID = "org.wso2.throttle.request.stream:1.0.0";
5244
private MessageContext messageContext;
5345
private DataPublisher dataPublisher;
5446

55-
56-
5747
String applicationLevelThrottleKey;
5848
String applicationLevelTier;
5949
String apiLevelThrottleKey;
@@ -243,10 +233,11 @@ public void run() {
243233
log.warn("Client port will be ignored and only the IP address (IPV4) will concern from " + ipAddress);
244234
ipAddress = ipAddress.split(":")[0];
245235
}
246-
if (IPV4_PATTERN.matcher(ipAddress).matches()) {
236+
InetAddressValidator validator = InetAddressValidator.getInstance();
237+
if (validator.isValidInet4Address(ipAddress)) {
247238
jsonObMap.put(APIThrottleConstants.IP, APIUtil.ipToLong(ipAddress));
248239
jsonObMap.put(APIThrottleConstants.IPv6, 0);
249-
} else if (IPV6_PATTERN.matcher(ipAddress).matches()) {
240+
} else if (validator.isValidInet6Address(ipAddress)) {
250241
jsonObMap.put(APIThrottleConstants.IPv6, APIUtil.ipToBigInteger(ipAddress));
251242
jsonObMap.put(APIThrottleConstants.IP, 0);
252243
} else {

0 commit comments

Comments
 (0)