11import { toast } from "@/lib/zustand/useToastStore" ;
22import type { appleOAuth2CodeResponse } from "@/types/auth" ;
33
4- // 오픈 리다이렉트 공격 방지를 위한 redirect 파라미터 검증
5- // 단일 "/"로 시작하고 "//"나 "://"를 포함하지 않는 내부 경로만 허용
6- export const validateSafeRedirect = ( redirectParam : string | null ) : string => {
7- if ( ! redirectParam || typeof redirectParam !== "string" ) {
8- return "/" ;
9- }
10-
11- if ( redirectParam . startsWith ( "/" ) && ! redirectParam . startsWith ( "//" ) && ! redirectParam . includes ( "://" ) ) {
12- return redirectParam ;
13- }
14-
15- return "/" ;
16- } ;
17-
184export const authProviderName = ( provider : "KAKAO" | "APPLE" | "EMAIL" ) : string => {
195 if ( provider === "KAKAO" ) {
206 return "카카오" ;
@@ -29,20 +15,8 @@ export const authProviderName = (provider: "KAKAO" | "APPLE" | "EMAIL"): string
2915
3016export const kakaoLogin = ( ) => {
3117 if ( window . Kakao ?. Auth ) {
32- // 현재 URL에서 redirect 파라미터 추출 및 검증
33- const urlParams = new URLSearchParams ( window . location . search ) ;
34- const redirectParam = urlParams . get ( "redirect" ) ;
35- const safeRedirect = validateSafeRedirect ( redirectParam ) ;
36-
37- // 검증된 redirect 파라미터를 callback URL에 전달
38- let redirectUri = `${ process . env . NEXT_PUBLIC_WEB_URL } /login/kakao/callback` ;
39- // 기본값 "/"가 아닌 경우에만 redirect 파라미터 추가 (기본값이면 생략 가능)
40- if ( safeRedirect !== "/" ) {
41- redirectUri += `?redirect=${ encodeURIComponent ( safeRedirect ) } ` ;
42- }
43-
4418 window . Kakao . Auth . authorize ( {
45- redirectUri,
19+ redirectUri : ` ${ process . env . NEXT_PUBLIC_WEB_URL } /login/kakao/callback` ,
4620 } ) ;
4721 } else {
4822 toast . error ( "Kakao SDK를 불러오는 중입니다. 잠시 후 다시 시도해주세요." ) ;
@@ -55,34 +29,17 @@ export const appleLogin = async () => {
5529 return ;
5630 }
5731
58- // 현재 URL에서 redirect 파라미터 추출 및 검증
59- const urlParams = new URLSearchParams ( window . location . search ) ;
60- const redirectParam = urlParams . get ( "redirect" ) ;
61- const safeRedirect = validateSafeRedirect ( redirectParam ) ;
62-
63- // 검증된 redirect 파라미터를 callback URL에 전달
64- let redirectURI = `${ process . env . NEXT_PUBLIC_WEB_URL } /login/apple/callback` ;
65- // 기본값 "/"가 아닌 경우에만 redirect 파라미터 추가 (기본값이면 생략 가능)
66- if ( safeRedirect !== "/" ) {
67- redirectURI += `?redirect=${ encodeURIComponent ( safeRedirect ) } ` ;
68- }
69-
7032 window . AppleID . auth . init ( {
7133 clientId : process . env . NEXT_PUBLIC_APPLE_CLIENT_ID ,
7234 scope : process . env . NEXT_PUBLIC_APPLE_SCOPE ,
73- redirectURI,
35+ redirectURI : ` ${ process . env . NEXT_PUBLIC_WEB_URL } /login/apple/callback` ,
7436 usePopup : true ,
7537 } ) ;
7638
7739 try {
7840 const res = ( await window . AppleID . auth . signIn ( ) ) as appleOAuth2CodeResponse ;
7941 if ( res . authorization ) {
80- // 검증된 redirect 파라미터를 callback URL에 전달
81- let callbackUrl = `/login/apple/callback?code=${ encodeURIComponent ( res . authorization . code ) } ` ;
82- if ( safeRedirect !== "/" ) {
83- callbackUrl += `&redirect=${ encodeURIComponent ( safeRedirect ) } ` ;
84- }
85- window . location . href = callbackUrl ;
42+ window . location . href = `/login/apple/callback?code=${ encodeURIComponent ( res . authorization . code ) } ` ;
8643 }
8744 } catch ( error ) {
8845 // Log error for developers
0 commit comments