Skip to content

fix: refresh 토큰과 탈퇴 사용자 토큰의 인증을 막는다 - #833

Open
Whale0928 wants to merge 4 commits into
mainfrom
fix/jwt-token-type-and-deleted-user
Open

Whale0928 wants to merge 4 commits into
mainfrom
fix/jwt-token-type-and-deleted-user

Conversation

@Whale0928

@Whale0928 Whale0928 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

배경

보안 검토에서 인증 토큰 처리의 두 가지 문제를 확인했다.

  1. access 토큰과 refresh 토큰은 claim 구성과 서명 키가 같고 만료 시간만 다르다. 인증 필터는 서명과 만료만 검사하므로 refresh 토큰(14일)을 Authorization: Bearer에 넣으면 인증된다. admin 토큰도 같다.
  2. 탈퇴는 User.status만 DELETED로 바꾸고 email과 행을 유지한다. CustomUserDetailsService는 findByEmail만 하고 isAlive()를 확인하지 않으므로, 탈퇴한 사용자의 토큰이 만료 전까지 인증을 통과한다.

변경 동작

커밋 1: refresh 토큰으로 API 인증을 할 수 없게 한다

  • 사용자·admin의 access/refresh 발급 경로 4개에 tokenType claim(access 또는 refresh)을 넣는다.
  • 사용자 체인과 admin 체인의 인증 필터는 tokenType이 refresh인 토큰을 거부한다. 응답은 기존의 유효하지 않은 토큰과 같다(사용자 체인 401 MALFORMED_TOKEN, admin 체인 403).
  • 재발급(AuthService.reissue, AdminAuthService.refresh)은 tokenType이 access인 토큰을 INVALID_REFRESH_TOKEN으로 거부한다.
  • 응답 스키마, 만료 시간, 서명 알고리즘, DB 스키마는 바꾸지 않았다.

커밋 2: 탈퇴한 사용자의 토큰으로 인증할 수 없게 한다

  • CustomUserDetailsService가 살아 있지 않은 사용자에 대해 UsernameNotFoundException을 던진다.
  • JwtAuthenticationFilter가 UsernameNotFoundException을 잡아 401(JWT_TOKEN_EXCEPTION)로 응답한다. 기존에는 존재하지 않는 사용자의 토큰이 이 예외로 필터 밖까지 나가 500이 됐고, 이 경우도 함께 401이 된다.

호환성

  • 기존 토큰: tokenType claim이 없는 배포 전 토큰은 access로 계속 받는다. 배포 즉시 전원이 로그아웃되는 것을 피하기 위해서다. 따라서 배포 전에 발급된 refresh 토큰은 만료(최대 14일)까지 Bearer 인증에 쓸 수 있고, 그 뒤에는 claim 없는 토큰 허용을 제거할 수 있다.
  • 탈퇴 재요청: 탈퇴한 사용자가 같은 토큰으로 다시 탈퇴를 요청하면 200이 아니라 401이 된다. 기존 UserCommandIntegrationTest.test_2("탈퇴한 회원이 다시 탈퇴하는 경우 성공")의 기대값을 401로 바꿨다. 탈퇴 재시도에 200을 기대하는 클라이언트가 있는지 확인이 필요하다.
  • 선택 인증 경로: 탈퇴 사용자의 토큰이나 refresh 토큰을 붙이면 익명으로 처리하지 않고 401로 응답한다. 기존의 유효하지 않은 토큰과 같은 동작이다. 토큰 없는 요청과 공개 경로는 바뀌지 않았다.
  • AdminApiSecurityIntegrationTest의 "admin 토큰으로 product API 호출 시 UsernameNotFoundException" 단언은 "401 응답" 단언으로 바꿨다.
  • AdminApiHttpBoundaryIntegrationTest는 admin 토큰으로 product 인증 필수 API를 호출할 때의 500을 기존 동작으로 고정하고 있었다. 이 기대값을 401로 바꿨다(커밋 3).

범위 밖

  • admin 체인에서 비활성 admin의 토큰은 CustomAdminUserDetailsService가 던지는 UsernameNotFoundException을 AdminJwtAuthenticationFilter가 잡지 않는다. 500이 될 가능성이 있다.
  • 사용자 필터에서 roles claim이 없을 때의 UserException(INVALID_TOKEN)도 잡지 않는다.

검증

  • 로컬 컴파일과 테스트는 실행하지 못했다. 작업 환경에서 Gradle 배포본과 플러그인을 받을 수 없었다.
  • head a3722c1의 ci pipeline(pull_request)이 전체 성공했다: https://github.com/bottle-note/bottle-note-api-server/actions/runs/46381279095
  • 직전 head e18f0c7의 실행은 AdminApiHttpBoundaryIntegrationTest 1건이 실패했고, 커밋 3에서 기대값을 맞췄다.

추가·수정한 테스트:

  • 신규: JwtTokenValidatorTest, AdminJwtAuthenticationFilterTest, JwtAuthenticationFilterTest, JwtTokenTypeIntegrationTest
  • 케이스 추가: AuthServiceTest, AdminAuthServiceTest, AdminApiSecurityIntegrationTest, UserCommandIntegrationTest
  • 확인 내용: refresh 토큰으로 보호 API 호출 시 거부, claim 없는 기존 토큰 인증 유지, access 토큰으로 재발급 거부, 정상 발급·재발급 흐름 유지, 탈퇴·미존재 사용자 토큰 401

https://claude.ai/code/session_01DNr3PM1DZCQLC8RiGJwD3a


Generated by Claude Code

claude and others added 4 commits November 30, 2026 17:14
access/refresh 토큰에 tokenType claim(access, refresh)을 넣고,
사용자·admin JWT 필터는 refresh로 명시된 토큰을 유효하지 않은 토큰으로 처리한다.
재발급(AuthService.reissue, AdminAuthService.refresh)은 access로 명시된 토큰을
INVALID_REFRESH_TOKEN으로 거부한다.

배포 전에 발급된 claim 없는 토큰은 access·refresh 양쪽에서 기존처럼 허용한다.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DNr3PM1DZCQLC8RiGJwD3a
탈퇴는 users.status만 DELETED로 바꾸고 email과 행을 유지하므로
CustomUserDetailsService가 findByEmail만으로 탈퇴 사용자를 인증하고 있었다.
살아 있지 않은 사용자는 UsernameNotFoundException으로 거부한다.

JwtAuthenticationFilter가 UsernameNotFoundException을 잡지 않아
존재하지 않는 사용자의 토큰이 필터 밖으로 예외를 던져 500이 되던 문제도
요청 attribute exception 설정 후 401로 처리하도록 함께 바로잡는다.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DNr3PM1DZCQLC8RiGJwD3a
존재하지 않는 사용자의 토큰이 500 대신 401로 응답하게 된 변경을 HTTP 경계 테스트에 반영한다.

Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DNr3PM1DZCQLC8RiGJwD3a

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants