Browse Source

Fix the empty value matching bug in GatewayParamParser of API gateway adapter common module

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao cdfive 5 years ago
parent
commit
fb31bb8d4d
2 changed files with 34 additions and 5 deletions
  1. +5
    -5
      sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/param/GatewayParamParser.java
  2. +29
    -0
      sentinel-adapter/sentinel-api-gateway-adapter-common/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/common/param/GatewayParamParserTest.java

+ 5
- 5
sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/param/GatewayParamParser.java View File

@@ -103,7 +103,7 @@ public class GatewayParamParser<T> {
private String parseClientIp(/*@Valid*/ GatewayParamFlowItem item, T request) {
String clientIp = requestItemParser.getRemoteAddress(request);
String pattern = item.getPattern();
if (pattern == null) {
if (StringUtil.isEmpty(pattern)) {
return clientIp;
}
return parseWithMatchStrategyInternal(item.getMatchStrategy(), clientIp, pattern);
@@ -114,7 +114,7 @@ public class GatewayParamParser<T> {
String pattern = item.getPattern();
// TODO: what if the header has multiple values?
String headerValue = requestItemParser.getHeader(request, headerKey);
if (pattern == null) {
if (StringUtil.isEmpty(pattern)) {
return headerValue;
}
// Match value according to regex pattern or exact mode.
@@ -124,7 +124,7 @@ public class GatewayParamParser<T> {
private String parseHost(/*@Valid*/ GatewayParamFlowItem item, T request) {
String pattern = item.getPattern();
String host = requestItemParser.getHeader(request, "Host");
if (pattern == null) {
if (StringUtil.isEmpty(pattern)) {
return host;
}
// Match value according to regex pattern or exact mode.
@@ -135,7 +135,7 @@ public class GatewayParamParser<T> {
String paramName = item.getFieldName();
String pattern = item.getPattern();
String param = requestItemParser.getUrlParam(request, paramName);
if (pattern == null) {
if (StringUtil.isEmpty(pattern)) {
return param;
}
// Match value according to regex pattern or exact mode.
@@ -146,7 +146,7 @@ public class GatewayParamParser<T> {
String cookieName = item.getFieldName();
String pattern = item.getPattern();
String param = requestItemParser.getCookieValue(request, cookieName);
if (pattern == null) {
if (StringUtil.isEmpty(pattern)) {
return param;
}
// Match value according to regex pattern or exact mode.


+ 29
- 0
sentinel-adapter/sentinel-api-gateway-adapter-common/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/common/param/GatewayParamParserTest.java View File

@@ -186,6 +186,35 @@ public class GatewayParamParserTest {
assertThat(params[apiRule1.getParamItem().getIndex()]).isEqualTo(expectedUrlParamValue2);
}

@Test
public void testParseParametersWithEmptyItemPattern() {
RequestItemParser<Object> itemParser = mock(RequestItemParser.class);
GatewayParamParser<Object> paramParser = new GatewayParamParser<>(itemParser);
// Create a fake request.
Object request = new Object();
// Prepare gateway rules.
Set<GatewayFlowRule> rules = new HashSet<>();
final String routeId = "my_test_route_DS(*H";
final String headerName = "X-Sentinel-Flag";
GatewayFlowRule routeRule1 = new GatewayFlowRule(routeId)
.setCount(10)
.setIntervalSec(2)
.setParamItem(new GatewayParamFlowItem()
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER)
.setFieldName(headerName)
.setPattern("")
.setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_EXACT)
);
rules.add(routeRule1);
GatewayRuleManager.loadRules(rules);

mockSingleHeader(itemParser, headerName, "Sent1nel");
Object[] params = paramParser.parseParameterFor(routeId, request, routeIdPredicate);
assertThat(params.length).isEqualTo(1);
// Empty pattern should not take effect.
assertThat(params[routeRule1.getParamItem().getIndex()]).isEqualTo("Sent1nel");
}

@Test
public void testParseParametersWithItemPatternMatching() {
RequestItemParser<Object> itemParser = mock(RequestItemParser.class);


Loading…
Cancel
Save