springboot项目漏洞“远端WWW服务支持TRACE请求”解决

该文展示了在低版本和高版本Springboot中如何配置Undertow,包括设置URL字符编码、Cookie值以及允许的HTTP方法,并添加安全约束。主要区别在于使用不同的容器工厂类和定制器接口。

项目中增加config类

  1. 低版本Springboot:

import io.undertow.UndertowOptions;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.WebResourceCollection;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.undertow.UndertowEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;

@Configuration
public class UndertowConfig {
    @Bean
    public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
        UndertowEmbeddedServletContainerFactory factory = new UndertowEmbeddedServletContainerFactory();
        factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ALLOW_UNESCAPED_CHARACTERS_IN_URL, Boolean.TRUE));
        factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ALLOW_EQUALS_IN_COOKIE_VALUE, Boolean.TRUE));
        factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ALLOW_ENCODED_SLASH, Boolean.TRUE));
        factory.addDeploymentInfoCustomizers(deploymentInfo -> {
            WebResourceCollection webResourceCollection = new WebResourceCollection();
            webResourceCollection.addUrlPattern("/*");
            webResourceCollection.addHttpMethod(HttpMethod.HEAD.toString());
            webResourceCollection.addHttpMethod(HttpMethod.PUT.toString());
            webResourceCollection.addHttpMethod(HttpMethod.PATCH.toString());
            webResourceCollection.addHttpMethod(HttpMethod.DELETE.toString());
            webResourceCollection.addHttpMethod(HttpMethod.OPTIONS.toString());
            webResourceCollection.addHttpMethod(HttpMethod.TRACE.toString());

            SecurityConstraint constraint = new SecurityConstraint();
            constraint.addWebResourceCollection(webResourceCollection);

            deploymentInfo.addSecurityConstraint(constraint);
        });
        return factory;
    }
}
  1. 高版本Springboot:

import io.undertow.UndertowOptions;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.WebResourceCollection;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;

/**
 * @author zhanglei
 */
@Configuration
public class UndertowConfig implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
    @Override
    public void customize(UndertowServletWebServerFactory factory) {
        factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ALLOW_UNESCAPED_CHARACTERS_IN_URL, Boolean.TRUE));
        factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ALLOW_EQUALS_IN_COOKIE_VALUE, Boolean.TRUE));
        factory.addBuilderCustomizers(builder -> builder.setServerOption(UndertowOptions.ALLOW_ENCODED_SLASH, Boolean.TRUE));
        factory.addDeploymentInfoCustomizers(deploymentInfo -> {
            WebResourceCollection webResourceCollection = new WebResourceCollection();
            webResourceCollection.addUrlPattern("/*");
            webResourceCollection.addHttpMethod(HttpMethod.HEAD.toString());
            webResourceCollection.addHttpMethod(HttpMethod.PUT.toString());
            webResourceCollection.addHttpMethod(HttpMethod.PATCH.toString());
            webResourceCollection.addHttpMethod(HttpMethod.DELETE.toString());
            webResourceCollection.addHttpMethod(HttpMethod.OPTIONS.toString());
            webResourceCollection.addHttpMethod(HttpMethod.TRACE.toString());

            SecurityConstraint constraint = new SecurityConstraint();
            constraint.addWebResourceCollection(webResourceCollection);

            deploymentInfo.addSecurityConstraint(constraint);
        });
    }
}
<think> 嗯,用户这次的问题很明确:如何在Spring Boot项目中预防TRACK和TRACE请求漏洞。从引用内容看,用户已经收集了相关资料,但需要系统化的解决方案。用户应该是企业的开发或运维人员,正在处理安全漏洞扫描报告,语气中透露出解决问题的紧迫性。 让我先梳理引用中的关键信息: 1. 引用[1]解释了漏洞本质:TRACE/TRACK方法可能被用于跨站追踪攻击窃取cookie 2. 引用[2]和[4]都指向Undertow服务器解决方案,但针对不同组件: - 普通Spring Boot应用可通过配置Undertow禁用这些方法 - Spring Cloud Gateway需要额外的WebFilter 3. 引用[3]的日志追踪插件虽然相关度不高,但提示了链路追踪的上下文 用户真正的需求是全面防御方案。我需要区分普通Web应用和网关场景,因为: - 普通应用用EmbeddedServletContainerCustomizer即可 - Gateway基于WebFlux需要响应式编程的过滤器 - 还需考虑测试验证环节 特别要注意引用[4]中的代码有个潜在问题:只拦截了TRACE没处理TRACK。HTTP规范中TRACK是TRACE的变体,需同时禁用。代码应该改为判断HttpMethod.TRACE或HttpMethod.TRACK。 安全措施必须完整,所以还要补充: 1. 所有方案都要返回403而非404,避免暴露无效端点信息 2. 强调生产环境必须通过curl测试 3. 提醒Spring MVC和WebFlux的拦截器差异 用户可能忽略测试步骤,需要明确给出curl命令示例。另外考虑到企业环境,应当提醒在API网关层统一拦截的方案,这比每个服务单独处理更高效。 </think> 在Spring Boot项目中预防TRACK和TRACE请求漏洞,需根据不同的技术栈(普通Web应用/Spring Cloud Gateway)采用以下方法: --- ### 方案一:普通Spring Boot应用(使用Undertow服务器) **1. 配置Undertow禁用HTTP方法** 在`application.yml`中显式指定允许的HTTP方法: ```yaml server: undertow: options: server: # 只允许GET, POST, PUT, DELETE, HEAD, OPTIONS allowed-methods: GET,POST,PUT,DELETE,HEAD,OPTIONS ``` **2. 验证配置** 执行命令测试(应返回`403 Forbidden`或`405 Method Not Allowed`): ```bash curl -X TRACE http://localhost:端口 curl -X TRACK http://localhost:端口 ``` --- ### 方案二:Spring Cloud Gateway(WebFlux响应式编程) **1. 创建全局过滤器拦截TRACE/TRACK** ```java @Component public class DisableTraceFilter implements WebFilter, Ordered { @Override public int getOrder() { return Ordered.HIGHEST_PRECEDENCE; // 最高优先级 } @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { HttpMethod method = exchange.getRequest().getMethod(); if (HttpMethod.TRACE.equals(method) || HttpMethod.TRACK.equals(method)) { exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN); return exchange.getResponse().setComplete(); // 直接阻断请求 } return chain.filter(exchange); } } ``` **2. 过滤器生效原理** - 通过`ServerWebExchange`获取请求方法类型 - 对`TRACE/TRACK`返回403状态码并终止请求链 - 其他方法正常放行(`chain.filter(exchange)`) --- ### 方案三:Spring MVC应用(传统Servlet) **添加Servlet过滤器** ```java @Bean public FilterRegistrationBean<HttpFilter> disableTraceFilter() { FilterRegistrationBean<HttpFilter> registration = new FilterRegistrationBean<>(); registration.setFilter(new HttpFilter() { @Override protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException { if ("TRACE".equalsIgnoreCase(req.getMethod()) || "TRACK".equalsIgnoreCase(req.getMethod())) { res.sendError(HttpServletResponse.SC_FORBIDDEN); // 返回403 return; } chain.doFilter(req, res); } }); registration.addUrlPatterns("/*"); return registration; } ``` --- ### 关键注意事项 1. **覆盖所有入口** 若项目同时使用Gateway和后端服务,需在**每层**都配置拦截 2. **测试完整性** 使用工具扫描验证(如Nessus, Burp Suite)确保无遗漏[^1][^2] 3. **Spring Boot版本兼容性** - Undertow配置方案适用于Spring Boot 2.0+ - WebFilter方案需Spring WebFlux支持Spring Cloud Gateway默认使用) 4. **生产环境验证** ```bash # 测试命令(预期结果:403) curl -i -X TRACE http://生产环境IP:端口/api ``` > **漏洞原理**:TRACE方法会返回客户端发送的原始请求头,攻击者可利用此反射恶意脚本盗取Cookie(跨站追踪攻击)[^1]。TRACK是TRACE的变种方法,同样存在风险。 --- ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值