github-advanced-security[bot] commented on code in PR #7485: URL: https://github.com/apache/incubator-seata/pull/7485#discussion_r2173623134
########## core/src/main/java/org/apache/seata/core/rpc/netty/http/filter/impl/XSSHttpRequestFilter.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.seata.core.rpc.netty.http.filter.impl; + +import org.apache.seata.config.ConfigurationFactory; +import org.apache.seata.config.ConfigurationKeys; +import org.apache.seata.core.exception.HttpRequestFilterException; +import org.apache.seata.core.rpc.netty.http.filter.HttpFilterContext; +import org.apache.seata.core.rpc.netty.http.filter.HttpRequestFilter; + +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +/** + * Filter to detect and block potential XSS attack vectors in HTTP request parameters. + */ +public class XSSHttpRequestFilter implements HttpRequestFilter { + + private static final String[] XSS_KEYWORDS = { + "<script>", + "</script>", + "javascript:", + "vbscript:", + "data:", + "expression(", + "onerror", + "onload", + "onclick", + "onmouseover", + "onfocus", + "onblur", + "onmouseenter", + "onmouseleave", + "onkeydown", + "onkeyup", + "onchange", + "<iframe>", + "<img>", + "<svg>", + "<embed>", + "<object>", + "<style>", + "<link>" + }; + + private static final Pattern EVENT_HANDLER_PATTERN = + Pattern.compile("on\\w+\\s*=\\s*['\"].*?['\"]", Pattern.CASE_INSENSITIVE); + + @Override + public int getOrder() { + return 1; + } + + /** + * Checks all request parameters for XSS risks and throws if found. + */ + @Override + public void doFilter(HttpFilterContext context) throws HttpRequestFilterException { + Map<String, List<String>> allParams = context.getParamWrapper().getAllParamsAsMultiMap(); + for (Map.Entry<String, List<String>> entry : allParams.entrySet()) { + for (String value : entry.getValue()) { + if (value != null && containsXssRisk(value)) { + throw new HttpRequestFilterException( + "XSS risk detected in param: " + entry.getKey() + ", value: " + value); + } + } + } + } + + /** + * Returns whether this XSS filter is enabled via configuration. + */ + @Override + public boolean shouldApply() { + return ConfigurationFactory.getInstance() + .getBoolean(ConfigurationKeys.SERVER_HTTP_FILTER_XSS_FILTER_ENABLE, true); + } + + /** + * Basic check for common XSS patterns in a string value. + */ + private boolean containsXssRisk(String value) { + if (value == null) { + return false; + } + + String normalized = value.toLowerCase().replaceAll("\\s+", ""); + + for (String keyword : XSS_KEYWORDS) { + if (normalized.contains(keyword)) { + return true; + } + } + + if (EVENT_HANDLER_PATTERN.matcher(value).find()) { Review Comment: ## Polynomial regular expression used on uncontrolled data This [regular expression](1) that depends on a [user-provided value](2) may run slow on strings starting with 'on' and with many repetitions of 'on'. [Show more details](https://github.com/apache/incubator-seata/security/code-scanning/46) ########## core/src/main/java/org/apache/seata/core/rpc/netty/http/filter/HttpRequestParamWrapper.java: ########## @@ -0,0 +1,150 @@ +/* + * 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.seata.core.rpc.netty.http.filter; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.handler.codec.http.DefaultFullHttpRequest; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.QueryStringDecoder; +import io.netty.handler.codec.http.multipart.Attribute; +import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder; +import io.netty.handler.codec.http.multipart.InterfaceHttpData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Wrapper for HTTP request parameters from multiple sources: query, form, header, JSON body. + */ +public class HttpRequestParamWrapper { + + private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequestParamWrapper.class); + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + private final Map<String, List<String>> queryParams = new HashMap<>(); + private final Map<String, List<String>> formParams = new HashMap<>(); + private final Map<String, List<String>> headerParams = new HashMap<>(); + private final Map<String, List<String>> jsonParams = new HashMap<>(); + + public HttpRequestParamWrapper(HttpRequest httpRequest) { + if (!(httpRequest instanceof FullHttpRequest)) { + throw new IllegalArgumentException("HttpRequest must be FullHttpRequest to read body."); + } + FullHttpRequest fullRequest = (FullHttpRequest) httpRequest; + parseQueryParams(fullRequest); + parseHeaders(fullRequest); + parseBody(fullRequest); + } + + private void parseQueryParams(FullHttpRequest request) { + QueryStringDecoder decoder = new QueryStringDecoder(request.uri()); + queryParams.putAll(decoder.parameters()); + } + + private void parseHeaders(FullHttpRequest request) { + for (Map.Entry<String, String> entry : request.headers()) { + headerParams.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).add(entry.getValue()); + } + } + + private void parseBody(FullHttpRequest request) { + String contentType = request.headers().get(HttpHeaderNames.CONTENT_TYPE); + if (contentType == null) { + return; + } + + ByteBuf originalContent = request.content(); + ByteBuf copiedBuf = Unpooled.copiedBuffer(originalContent); + + String bodyStr = copiedBuf.toString(StandardCharsets.UTF_8); + + try { + if (contentType.contains("application/json")) { + parseJsonBody(bodyStr); + } else if (contentType.contains("application/x-www-form-urlencoded") + || contentType.contains("multipart/form-data")) { + FullHttpRequest copiedRequest = new DefaultFullHttpRequest( + request.protocolVersion(), request.method(), request.uri(), copiedBuf); Review Comment: ## Server-side request forgery Potential server-side request forgery due to a [user-provided value](1). [Show more details](https://github.com/apache/incubator-seata/security/code-scanning/47) -- 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...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org