Skip to content

Commit

Permalink
update $_SERVER[] value check
Browse files Browse the repository at this point in the history
Added error handling if the IP cant be detected in $_SESSION
  • Loading branch information
jasonstockman committed Oct 29, 2014
1 parent 56c02ce commit f32890d
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions csrf-magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ function csrf_get_tokens() {
// any cookies. It may or may not be used, depending on whether or not
// the cookies "stick"
$secret = csrf_get_secret();
if (!$has_cookies && $secret) {
if (!$has_cookies && $secret && (isset($_SERVER['IP_ADDRESS']) || isset($_SERVER['REMOTE_ADDR']) )) {
// :TODO: Harden this against proxy-spoofing attacks
$ip = ';ip:' . csrf_hash($_SERVER['IP_ADDRESS']);
$ip = ';ip:' . csrf_hash( isset($_SERVER['IP_ADDRESS']) ? $_SERVER['IP_ADDRESS'] : $_SERVER['REMOTE_ADDR'] ) ;
} else {
$ip = '';
}
Expand Down Expand Up @@ -327,7 +327,10 @@ function csrf_check_token($token) {
if ($GLOBALS['csrf']['user'] !== false) return false;
if (!empty($_COOKIE)) return false;
if (!$GLOBALS['csrf']['allow-ip']) return false;
return $value === csrf_hash($_SERVER['IP_ADDRESS'], $time);
if (isset($_SERVER['IP_ADDRESS']) || isset($_SERVER['REMOTE_ADDR']) ) {
return $value === csrf_hash(isset($_SERVER['IP_ADDRESS']) ? $_SERVER['IP_ADDRESS'] : $_SERVER['REMOTE_ADDR'], $time);
}
return false;
}
return false;
}
Expand Down

0 comments on commit f32890d

Please sign in to comment.