훈훈훈

Spring Security :: CSRF protection disable option 대한 생각 정리 본문

Spring Framework/개념

Spring Security :: CSRF protection disable option 대한 생각 정리

훈훈훈 2020. 10. 20. 00:10

Spring Security에서 제공하는 CSRF protection 기능에 대해 공부 중 읽어 보았던 블로그 & 스터디 자료로 선택한 강의에서 CSRF protection 기능을 무조건 Enable 하는 걸 권장하였다.

 

하지만 문득 CSRF에 취약하지 않은 환경이라면 무조건 Enable 해야하는가에 대한 의문이 갑자기 들어 해당 내용을 정리하게 되었다.

 

해당 글은 먼저 CSRF에 대한 내용과 Spring Security에서 제공하는 CSRF protection 기능을 간단히 살펴보고

CSRF protection 기능을 무조건 Enable 해야하는가에 대해 필자의 생각을 적으려고 한다.

 

CSRF (Cross Script Resourse Forgery)  

출처 : https://portswigger.net/web-security/csrf

 

CSRF 취약점은 공격자가 사용자가 의도하지 않는 요청을 수행하게 하는 취약점이다.

예를 들어 위 그림과 같이 공격자는 로그인한 사용자의 권한을 사용하여 다른 페이지에서도 패스워드를 변경할 수 있다.

 

즉 공격자는 사용자가 가지고 있는 권한 범위 내에서 악의적인 행위를 할 수 있다. 

 

위 예시를 좀 더 자세히 살펴보자.

 

아래와 같이 사용자는 페이지에 요청을 보낼 수 있다고 가정하자.

POST /email/change HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 30
Cookie: session=yvthwsztyeQkAPzeQ5gHgTvlyxHfsAAA

email=test@normal-user.com 

 

위 예시를 살펴보면 해당 애플리케이션은 쿠키와 세션을 사용해서 인증을 하는 것을 알 수 있다.

따라서 공격자는 이미 인증이 된 사용자의 브라우저에서 권한 범위 내에서 원하는 행위를 할 수 있다. 

 

즉 아래와 같이 공격자는 악의적인 HTML 파일을 만들 수 있다.

<html>
  <body>
    <form action = http/hostname/email/change method=POST>
        <input type="hidden" name="email" value="pwned@evil-user.net" />
    </form>
    <script>
      document.forms[0].submit();
    </script>
  </body>
</html>

 

Spring Security CSRF protection  

Spring securiry 에서는 CSRF 취약점 발생을 예방하기 위한 기능을 제공한다.

( Spring 이외의 프레임워크들도 해당 기능을 제공한다. )

 

해당 기능은 CSRF Token을 발급 후, Client로 부터 Request 받을 때마다 해당 토큰을 검증하는 방식이다.

CSRF Token에 대한 자세한 내용은 해당 문서를 참고 바란다.

 

Spring Securiry에서는 CSRF protection 기능은 디폴트로 Enable 상태이며, 원한다면 해당 기능을 Disable 할 수 있다.

 

공식 문서를 살펴보면 언제 Spring Security에서 제공하는 CSRF protection 기능을 사용하는지 명시되어 있다.

 

위 내용을 살펴보면 CSRF protecton 기능은 브라우저를 통해 Request를 받을 때 사용하라고 적혀 있다. 

 

그렇다면 해당 기능은 브라우저를 통해 Request를 받지 않는다면 사용하지 않아도 된다고 생각할 수 있다.

즉, SPA(Single Page Application) 방식으로 개발된 (REST) API 서버는 해당 기능을 disable 해도 된다고 생각할 수 있다.

 

이제 왜 REST (API) 서버는 해당기능을 disable 해도 되는지 생각해보자.

 

REST API 서버는 stateless 하게 개발하기 때문에 사용자의 정보를 세션에 저장하지 않는다.

일반적으로 JWT 같은 토큰을 사용하여 인증하기 때문에 해당 토큰을 Cookie에 저장하지 않는다면 CSRF 취약점에 대해서는 어느 정도 안전하다고 말할 수 있다. 

 

하지만 Local Storage 같은 곳에 토큰에 저장된다면 XSS 취약점이 발생할 수 있다 ....

( XSS에 대해서는 다음에 정리 해보려고 한다. )

 

정리하자면 세션을 사용할 때 발생하는 취약점인 CSRF에 대해서 100%라고 장담할 수는 없지만 취약점 발생 요소가 제거 되었기 때문에 안전하다고 말할 수 있다고 생각한다.

 

 

# 참고 자료

portswigger.net/web-security/csrf

 

What is CSRF (Cross-site request forgery)? Tutorial & Examples | Web Security Academy

In this section, we'll explain what cross-site request forgery is, describe some examples of common CSRF vulnerabilities, and explain how to prevent CSRF ...

portswigger.net

portswigger.net/web-security/csrf/tokens

 

CSRF tokens | Web Security Academy

In this section, we'll explain what CSRF tokens are, how they protect against CSRF attacks, and how CSRF tokens should be generated and validated. What are ...

portswigger.net

docs.spring.io/spring-security/site/docs/5.0.x/reference/html/csrf.html

 

19. Cross Site Request Forgery (CSRF)

So what are the steps necessary to use Spring Security’s to protect our site against CSRF attacks? The steps to using Spring Security’s CSRF protection are outlined below: 19.4.1 Use proper HTTP verbs The first step to protecting against CSRF attacks

docs.spring.io

medium.com/@mena.meseha/how-to-defend-against-csrf-using-jwt-8adebe64824b

 

How to defend against CSRF using JWT

CSRF (Cross Site Request Forgery)

medium.com

security.stackexchange.com/questions/166724/should-i-use-csrf-protection-on-rest-api-endpoints/166798#166798

 

Should I use CSRF protection on Rest API endpoints?

Quick note: this is not a duplicate of CSRF protection with custom headers (and without validating token) despite some overlap. That post discusses how to perform CSRF protection on Rest endpoints

security.stackexchange.com

 

 

Comments