Skip to content

Commit

Permalink
Merge pull request facebookarchive#372 from facebook/gFosco.csrf2
Browse files Browse the repository at this point in the history
Updated FacebookRedirectLoginHelper and SignedRequest ...
  • Loading branch information
gfosco committed Mar 2, 2015
2 parents e3f5645 + 2ed50e1 commit 8f65adc
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 = strlen($state);
$givenLen = 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 8f65adc

Please sign in to comment.