설명
로그인이나 결제 후 요청에 담긴 URL을 그대로 sendRedirect에 전달하면 사용자가 반환 위치를 임의로 지정할 수 있습니다.
잠재적 영향
서비스 링크가 피싱 사이트나 악성 다운로드로 이어질 수 있습니다. OAuth나 SSO가 리다이렉트 대상을 신뢰하면 인증 정보가 노출될 수 있습니다.
해결 방법
next=profile처럼 고정된 키를 서버의 내부 경로와 연결하세요. 알 수 없는 값은 거부하거나 기본 경로로 보내세요.- 외부 URL을 받아야 한다면 파싱하고 정규화한 뒤
https만 허용하고 호스트와 포트를 허용 목록과 정확히 비교하세요.startsWith나contains로는 호스트가 일치하는지 알 수 없습니다.
예시
Spring 컨트롤러의 일부이며, 필요한 import와 애플리케이션의 내부 경로 정의는 생략했습니다.
변경 전
java
@GetMapping("/login/success")
public void loginSuccess(
@RequestParam("next") String next,
HttpServletResponse response
) throws IOException {
response.sendRedirect(next);
}
변경 후
java
private static final Map<String, String> ALLOWED_REDIRECTS = Map.of(
"profile", "/account/profile",
"orders", "/account/orders"
);
@GetMapping("/login/success")
public void loginSuccess(
@RequestParam(value = "next", defaultValue = "profile") String next,
HttpServletResponse response
) throws IOException {
String redirectPath = ALLOWED_REDIRECTS.getOrDefault(next, "/account/profile");
response.sendRedirect(redirectPath);
}
profile과 orders는 서버에 등록한 경로를 선택합니다. next가 없거나 등록된 키가 아니면 /account/profile로 이동합니다.