Recent Posts
Recent Comments
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
관리 메뉴

ㅇ.ㅇ

[Security] HttpFirewall 본문

Today I Learned

[Security] HttpFirewall

dbs_ 2025. 7. 11. 16:33
반응형

 

 

최근 URL에서 // 와 같은 이중 슬래시를 사용한 것과 관련하여 핸들링 요청이 들어왔었다. 이중 슬래시 막는 방법을 찾아봤는데 다음과 같이 여러 방식이 있었다.

  • 400 에러로 직접 응답하는 방식
  • 슬래시를 하나로 치환(replace)하는 방식
  • Spring Security 설정으로 제어하는 방식 등등..

그러던 중, HttpFirewall이라는 Spring Security 속성을 알게 되었고, 그 내용을 정리해보게 되었다.

 

HttpFirewall이란?

  • Spring Security가 제공하는 URL 검사기
  • FilterChain에 들어가기 전에 요청을 점검해 위험한 요청을 선제적으로 차단
  • 대표적으로 차단되는 경우
    • 이중 슬래시 / 디렉토리 이동 / 잘못된 인코딩
    • 이러한 요청들은 디렉토리 우회 공격이나 URL 인코딩 우회 등 보안 취약점으로 악용될 가능성이 있음

 

HttpFirewall 기본 설정

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        .authorizeHttpRequests().anyRequest().permitAll();
    return http.build();
}

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
    return web -> web.httpFirewall(strictHttpFirewall());
}

@Bean
public HttpFirewall strictHttpFirewall() {
    return new StrictHttpFirewall(); 
}
  • Spring Security의 기본 설정은 StrictHttpFirewall
    • //, ../, % 등 의심스러운 경로 전부 차단

 

HttpFirewall 커스터마이징

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

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
    return (web) -> web.httpFirewall(looseHttpFirewall());
}
  • 예 : 이중 슬래시 허용하고 싶을 때
    • allowUrlEncodedDoubleSlash 설정을 true로 변경하여 허용 가능
    • 단, 보안상의 이유로 꼭 필요한 경우에만 사용하는 것이 좋다

 

HttpFirewall 예외처리

  • HttpFirewall에 걸리면 RequestRejectedException 발생하며 Spring Security가 자동으로 400 에러로 응답
  • RequestRejectedException을 핸들링하고 싶다면 RequestRejectedHandler를 직접 등록하여 사용자 정의 처리 가능

 

HttpFirewall은 Spring Security에서 잘 드러나지 않지만, 요청 보안의 가장 앞단에서 동작하는 중요한 부분이라고 한다. 이후에도 갑자기 400 에러가 뜬다면, 여기에서 걸리고 있는지 확인해봐도 좋을 것 같다.

 

반응형