Skip to content

[gateway] 위조된 게이트웨이 헤더 교체와 잘못된 형식 요청 차단 - #13

Open
jyx-07 wants to merge 3 commits into
feat/gateway-securityfrom
feat/gateway-request-sanitize
Open

jyx-07 wants to merge 3 commits into
feat/gateway-securityfrom
feat/gateway-request-sanitize

Conversation

@jyx-07

@jyx-07 jyx-07 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

개요

클라이언트가 게이트웨이 소유 헤더를 위조해도 올바른 값으로 바꿔 넘기고, 형식이 잘못된 요청은 400으로 막습니다.

본문

  • X-User-Id, X-Real-IP, X-Forwarded-*, Forwarded, X-Internal-*를 지우고 게이트웨이가 판별한 값으로 다시 씀
  • 경로 조작(.., %2e, %2f, %5c, //)과 제어문자 헤더는 400, 8KB 넘는 헤더는 431
  • SCG 기본 X-Forwarded/Forwarded 필터를 꺼 값이 덧붙지 않게 함
  • WebSocket ?token=은 검증 후 지우고 서비스에 넘김, /{prefix}/v3/api-docs는 토큰 없이 허용

#10 → #12 → 이 PR 순서로 머지해 주세요.

@jyx-07
jyx-07 force-pushed the feat/gateway-request-sanitize branch from 22e0a48 to de8e991 Compare November 3, 2026 14:19

@cfcromn cfcromn left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쿼리 토큰 제거와 IPv6 호스트 처리에서 확인한 내용 남깁니다.

private fun withoutTokenQuery(uri: URI): URI =
UriComponentsBuilder
.fromUri(uri)
.replaceQueryParam(TOKEN_QUERY)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?%74oken=JWT로 요청해 보니 인증은 통과하는데 토큰이 쿼리에 그대로 남았습니다. 인증할 때는 디코딩된 키를 읽고, 여기서는 원문 키를 기준으로 지워서 생기는 차이 같습니다. 서비스 URL 로그에 JWT가 남을 수 있으니 디코딩한 키가 token인 파라미터도 같이 제거하면 좋지 않을까요?

@jyx-07 jyx-07 Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cfcromn 82cab6e 에서 반영했습니다. (근거: 이 PR의 목적, 쿼리 토큰은 서비스로 넘기지 않음) 쿼리 키를 디코딩한 뒤 token과 비교해 지우도록 바꿨고, ?%74oken= 요청에 대한 테스트를 추가했습니다.

val own =
Forwarded(
proto = proto,
host = headers.getFirst(HttpHeaders.HOST)?.substringBefore(':') ?: uri.host.orEmpty(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPv6 주소는 이 방식으로 자르면 안 될 것 같습니다. Host: [::1]:8080이면 결과가 [가 되고, 이 값이 X-Forwarded-Host로 전달돼 downstream Spring에서 URI를 만들 때 예외가 발생합니다. 여기서는 request.uri.host를 쓰면 IPv6도 처리할 수 있을 것 같아요.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cfcromn 82cab6e 에서 반영했습니다. (근거: java.net.URI가 IPv6 호스트를 대괄호를 포함해 반환함) request.uri.host를 쓰도록 바꿨고, Host: [::1]:port 요청에서 X-Forwarded-Host가 [::1]로 전달되는지 테스트를 추가했습니다.

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

Labels

✨ Feature 신규 기능

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants