Skip to content

Commit

Permalink
Updated FacebookRedirectLoginHelper and SignedRequest to constant tim…
Browse files Browse the repository at this point in the history
…e CSRF check.
  • Loading branch information
Fosco Marotto committed Mar 2, 2015
1 parent e3f5645 commit 507c182
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/Facebook/Entities/SignedRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,18 @@ public static function validateSignature($hashedSig, $sig)
*/
public static function validateCsrf(array $data, $state)
{
if (isset($data['state']) && $data['state'] === $state) {
return;
if (isset($data['state'])) {
$savedLen = mb_strlen($state);
$givenLen = mb_strlen($data['state']);
if ($savedLen == $givenLen) {
$result = 0;
for ($i = 0; $i < $savedLen; $i++) {
$result |= ord($state[$i]) ^ ord($data['state'][$i]);
}
if ($result === 0) {
return;
}
}
}

throw new FacebookSDKException(
Expand Down
17 changes: 15 additions & 2 deletions src/Facebook/FacebookRedirectLoginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,21 @@ public function getSessionFromRedirect()
*/
protected function isValidRedirect()
{
return $this->getCode() && isset($_GET['state'])
&& $_GET['state'] == $this->state;
$savedState = $this->getCode();
if (!$this->getCode() || !isset($_GET['state'])) {
return false;
}
$givenState = $_GET['state'];
$savedLen = mb_strlen($savedState);
$givenLen = mb_strlen($givenState);
if ($savedLen !== $givenLen) {
return false;
}
$result = 0;
for ($i = 0; $i < $savedLen; $i++) {
$result |= ord($savedState[$i]) ^ ord($givenState[$i]);
}
return $result === 0;
}

/**
Expand Down

0 comments on commit 507c182

Please sign in to comment.