본문 바로가기
프로그래밍/SpringBoot

The request was rejected because the URL contained a potentially malicious String "//" - RequestRejectedException

by 플로어코딩 2024. 4. 7.

 

 

 

"malicious String "//" - RequestRejectedException" 오류는 Spring Security의 HTTP 요청 방화벽이 발견한 악성 문자열 "//"에 대한 예외입니다. 이 예외는 일반적으로 웹 요청에서 발생하며, Spring Security가 보안 상의 이유로 해당 요청을 거부했음을 나타냅니다.

 

일반적으로 이러한 종류의 예외는 URL이나 요청 매개변수 등에서 발견되는 악성 문자열에 의해 트리거됩니다. 이러한 문자열은 시스템을 공격하거나 응용 프로그램의 보안을 침해할 수 있는 보안 취약점을 나타낼 수 있습니다.

 

악성 문자열을 찾아내기 위해 Spring Security는 기본적으로 일부 문자열 패턴을 검사하는 방화벽을 제공합니다.

이런 종류의 보안 기능은 웹 애플리케이션의 보안을 강화하기 위해 사용됩니다.

 

따라서 해당 오류가 발생한 경우, 요청에 악의적인 의도가 있는지를 조사하고, 요청을 안전하게 처리할 수 있도록 보안 정책을 개선해야 합니다.

 
 
 

스프링부트2 적용소스

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.firewall.DefaultHttpFirewall;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.security.web.firewall.RequestRejectedException;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
        DefaultHttpFirewall firewall = new DefaultHttpFirewall();
        firewall.setAllowUrlEncodedSlash(true);
        return firewall;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .httpFirewall(allowUrlEncodedSlashHttpFirewall())
            .exceptionHandling()
                .authenticationEntryPoint((request, response, authException) -> {
                    if (authException instanceof RequestRejectedException && authException.getMessage().contains("malicious String \"//\"")) {
                        response.sendRedirect("/error-page"); // 악성 문자열에 대한 요청이 발견되면 사용자를 에러 페이지로 리다이렉트
                    } else {
                        response.sendRedirect("/login"); // 다른 예외 발생 시 로그인 페이지로 리다이렉트
                    }
                })
            .and()
            .authorizeRequests()
                .antMatchers("/error-page").permitAll() // 에러 페이지는 모든 사용자에게 허용
                .anyRequest().authenticated()
            .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
            .and()
            .logout()
                .permitAll();
    }
}

 

 

스프링부트3 적용 소스

@Bean
public HttpFirewall allowUrlEncodedDoubleSlashHttpFirewall() {
    StrictHttpFirewall firewall = new StrictHttpFirewall();
    firewall.setAllowUrlEncodedDoubleSlash(true);
    return firewall;
}

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
    return (web) -> web.httpFirewall(allowUrlEncodedDoubleSlashHttpFirewall())
        .ignoring()
        .requestMatchers(PathRequest.toStaticResources().atCommonLocations());

댓글