【问题标题】:Whitelist for redirect URLs in spring bootSpring Boot 中重定向 URL 的白名单
【发布时间】:2018-06-01 10:47:42
【问题描述】:

我们的项目存在安全问题。攻击者可以拦截登录请求并修改其中的“主机”标头。服务器将响应重定向 (303),将用户发送到可能是邪恶的站点。

是否可以为重定向添加白名单?

使用带有嵌入式 tomcat 的 Spring-boot,在生产中这整个事情将在负载均衡器之后。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http  //@formatter:off
        .formLogin()
            .loginProcessingUrl("...")
            .usernameParameter("...")
            .passwordParameter("...")
            .loginPage("/").permitAll()
            .successHandler(authenticationSuccessHandler)
            .failureHandler(authenticationFailureHandler)
        .and().logout().permitAll()
            .logoutSuccessHandler(logoutSuccessHandler)
            .deleteCookies(XSRF_TOKEN, JSESSIONID)
        .and().authorizeRequests()
        .antMatchers("...").permitAll().anyRequest().authenticated()
        .and().csrf().csrfTokenRepository(csrfTokenRepository)
        .and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
        .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);//@formatter:on
}

到目前为止,我已经尝试了以下方法:

  1. 使用TomcatEmbeddedServletContainerFactory 并在那里添加阀门。
  2. 使用FilterRegistrationBeanRemoteAddrFilter.setDeny()

1° 选项根本不会启动。我显然在某个地方犯了错误,但很难找到这些信息,而且我不知道该怎么做。

我在Stackoverflow 上找到的 2° 选项,感觉是正确的做法,但我未能成功。如果setDeny() 存在,它甚至不会让我进入我的网站。如果我将其注释掉,那么看起来根本没有过滤发生。 bean 看起来像这样:

@Bean
    public FilterRegistrationBean remoteAddressFilter() {

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        RemoteAddrFilter filter = new RemoteAddrFilter();

        filter.setDeny("attacker/.com.*");
        filter.setDenyStatus(404);

        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.addUrlPatterns("/*");

        return filterRegistrationBean;
    }

提前感谢您的任何帮助。

【问题讨论】:

  • 攻击者可以拦截登录请求并修改您可以使用 HTTPS 来解决这个问题。
  • 服务器将响应重定向 (303),将用户发送到可能是邪恶的站点。 为什么您的服务器将用户发送到邪恶的站点?重定向在您的处理程序中配置。
  • 是否可以为重定向添加白名单? 在客户端站点上?为什么要更改服务器代码以在客户端站点上实现白名单?
  • RemoteAddrFilter#setDeny 应该如何帮助您?您是否阅读过此方法的文档?顺便说一句:你确定它也适用于 DNS 名称吗?
  • @dur 重定向在您的处理程序中配置。我该如何配置它们? Spring security 只返回 303,仅此而已。 在客户网站上?为什么要更改服务器代码以在客户端站点上实现白名单? 不,它是通过重定向响应的服务器。请求离开前端,被拦截,修改后发送到服务器,返回303。你看过这个方法的文档吗?是的,我觉得它没有描述性。

标签: java spring-boot tomcat spring-security


【解决方案1】:

这是一个老问题。但是添加一个答案,因为 Spring 在 Spring security 4.2.175.2 中添加了对白名单的支持。这可能对其他偶然发现相同事物的人有用
在安全配置中,

@Override
    public void configure(WebSecurity web) throws Exception {
        StrictHttpFirewall firewall = new StrictHttpFirewall();
        firewall.setAllowedHostnames(Arrays.asList("myhostname1","myhostname2"));
        web.httpFirewall(firewall);
    }

它会抛出org.springframework.security.web.firewall.RequestRejectedException 带有类似“请求被拒绝,因为域 www.attackersite.com 不受信任”这样的消息。

如果您不想要 StrictHttpFirewall 的所有功能,您可以扩展 HttpFirewall 并添加自己的实现。

【讨论】:

    猜你喜欢
    • 2017-11-27
    • 2015-08-08
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2018-11-16
    相关资源
    最近更新 更多