Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#9323]1.x http接口服务名校验问题 #9324

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.CommonParams;
import com.alibaba.nacos.common.utils.ExceptionUtil;
import com.alibaba.nacos.core.utils.OverrideParameterRequestWrapper;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.core.utils.OverrideParameterRequestWrapper;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -31,6 +31,8 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static org.apache.http.HttpStatus.SC_BAD_REQUEST;

/**
* Service name filter. This class is created for adapting 1.x. client and old openAPI.
* <p>
Expand Down Expand Up @@ -63,6 +65,12 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
if (StringUtils.isNotBlank(serviceName) && !serviceName.contains(Constants.SERVICE_INFO_SPLITER)) {
groupedServiceName = groupName + Constants.SERVICE_INFO_SPLITER + serviceName;
}
if (StringUtils.isNotBlank(groupedServiceName)) {
boolean checkServiceNameFormat = checkServiceNameFormat(groupedServiceName, resp);
if (!checkServiceNameFormat) {
return;
}
}
OverrideParameterRequestWrapper requestWrapper = OverrideParameterRequestWrapper.buildRequest(request);
requestWrapper.addParameter(CommonParams.SERVICE_NAME, groupedServiceName);
filterChain.doFilter(requestWrapper, servletResponse);
Expand All @@ -71,4 +79,18 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
"Service name filter error," + ExceptionUtil.getAllExceptionMsg(e));
}
}

private boolean checkServiceNameFormat(String combineServiceName, HttpServletResponse resp) throws IOException {
hujun-w-2 marked this conversation as resolved.
Show resolved Hide resolved
String[] split = combineServiceName.split(Constants.SERVICE_INFO_SPLITER);
if (split.length <= 1) {
resp.sendError(SC_BAD_REQUEST,
"Param 'serviceName' is illegal, it should be format as 'groupName@@serviceName'");
return false;
}
if (split[0].isEmpty()) {
resp.sendError(SC_BAD_REQUEST, "Param 'serviceName' is illegal, groupName can't be empty");
return false;
}
return true;
}
}