github-advanced-security[bot] commented on code in PR #5544: URL: https://github.com/apache/shenyu/pull/5544#discussion_r1587244489
########## shenyu-admin/src/main/java/org/apache/shenyu/admin/filter/ClusterForwardFilter.java: ########## @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.admin.filter; + +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.config.properties.ClusterProperties; +import org.apache.shenyu.admin.model.dto.ClusterMasterDTO; +import org.apache.shenyu.admin.service.ClusterMasterService; +import org.jetbrains.annotations.NotNull; +import org.owasp.encoder.Encode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.server.ServletServerHttpRequest; +import org.springframework.stereotype.Component; +import org.springframework.util.AntPathMatcher; +import org.springframework.util.PathMatcher; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.filter.OncePerRequestFilter; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.annotation.Resource; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Writer; +import java.util.Collections; +import java.util.Objects; + +/** + * Cluster forward filter. + */ +@Component +public class ClusterForwardFilter extends OncePerRequestFilter { + + private static final Logger LOG = LoggerFactory.getLogger(ClusterForwardFilter.class); + + private static final PathMatcher PATH_MATCHER = new AntPathMatcher(); + + @Resource + private RestTemplate restTemplate; + + @Resource + private ClusterMasterService clusterMasterService; + + @Resource + private ClusterProperties clusterProperties; + + @Override + protected void doFilterInternal(@NotNull final HttpServletRequest request, + @NotNull final HttpServletResponse response, + @NotNull final FilterChain filterChain) throws ServletException, IOException { + String method = request.getMethod(); + if (StringUtils.equals(HttpMethod.OPTIONS.name(), method)) { + filterChain.doFilter(request, response); + return; + } + + if (clusterMasterService.isMaster()) { + filterChain.doFilter(request, response); + return; + } + + String uri = request.getRequestURI(); + String requestContextPath = request.getContextPath(); + String replaced = uri.replaceAll(requestContextPath, ""); + boolean anyMatch = clusterProperties.getForwardList().stream().anyMatch(x -> PATH_MATCHER.match(x, replaced)); + if (!anyMatch) { + filterChain.doFilter(request, response); + return; + } + + // cluster forward request to master + forwardRequest(request, response, filterChain); + } + + private void forwardRequest(final HttpServletRequest request, + final HttpServletResponse response, + final FilterChain filterChain) throws IOException, ServletException { + ClusterMasterDTO master = clusterMasterService.getMaster(); + String host = master.getMasterHost(); + String port = master.getMasterPort(); + // String contextPath = master.getContextPath(); + + // new URL + // String uri = request.getRequestURI(); + // String requestContextPath = request.getContextPath(); + // uri = uri.replaceAll(requestContextPath, ""); + // if (StringUtils.isNotEmpty(contextPath)) { + // uri = contextPath + "/" + uri; + // } + UriComponentsBuilder builder = UriComponentsBuilder + .fromHttpRequest(new ServletServerHttpRequest(request)) + .host(host) + .port(port); + + // 添加查询参数 + if (StringUtils.isNotEmpty(request.getQueryString())) { + builder.query(request.getQueryString()); + } + + LOG.debug("forwarding request to url: {}", builder.toUriString()); + // Create request entity + HttpHeaders headers = new HttpHeaders(); + + copyHeaders(request, headers); + HttpEntity<byte[]> requestEntity = new HttpEntity<>(getBody(request), headers); + // Send request + ResponseEntity<String> responseEntity = restTemplate.exchange(builder.toUriString(), HttpMethod.valueOf(request.getMethod()), requestEntity, String.class); + + // Set response status and headers + response.setStatus(responseEntity.getStatusCodeValue()); + copyHeaders(responseEntity.getHeaders(), response); + // fix cors error + response.addHeader("Access-Control-Allow-Origin", "*"); + response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); + + try (Writer writer = response.getWriter()) { +// writer.write(URLEncoder.encode(Objects.requireNonNull(responseEntity.getBody()), "UTF-8")); + writer.write(Encode.forJava(Objects.requireNonNull(responseEntity.getBody()))); + writer.flush(); + } + + } + + private void copyHeaders(final HttpServletRequest request, final HttpHeaders headers) { + Collections.list(request.getHeaderNames()) + .forEach(headerName -> headers.add(headerName, request.getHeader(headerName).replaceAll("\r", "").replaceAll("\n", ""))); + } + + private void copyHeaders(final HttpHeaders sourceHeaders, final HttpServletResponse response) { + sourceHeaders.forEach((headerName, headerValues) -> { +// String name = headerName.replaceAll("[^a-zA-Z ]", ""); + if (!response.containsHeader(headerName)) { + headerValues.forEach(headerValue -> { + response.addHeader(headerName, headerValue.replaceAll("\r", "").replaceAll("\n", "")); Review Comment: ## HTTP response splitting This header depends on a [user-provided value](1), which may cause a response-splitting vulnerability. [Show more details](https://github.com/apache/shenyu/security/code-scanning/73) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@shenyu.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org