Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Commit 3858c7e

Browse files
committed
Update CSRF comparison to use hash_equals()
1 parent 5202853 commit 3858c7e

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/Facebook/Helpers/FacebookRedirectLoginHelper.php

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,27 +235,19 @@ public function getAccessToken($redirectUrl = null)
235235
protected function validateCsrf()
236236
{
237237
$state = $this->getState();
238-
$savedState = $this->persistentDataHandler->get('state');
239-
240-
if (!$state || !$savedState) {
241-
throw new FacebookSDKException('Cross-site request forgery validation failed. Required param "state" missing.');
238+
if (!$state) {
239+
throw new FacebookSDKException('Cross-site request forgery validation failed. Required GET param "state" missing.');
242240
}
243-
244-
$savedLen = strlen($savedState);
245-
$givenLen = strlen($state);
246-
247-
if ($savedLen !== $givenLen) {
248-
throw new FacebookSDKException('Cross-site request forgery validation failed. The "state" param from the URL and session do not match.');
241+
$savedState = $this->persistentDataHandler->get('state');
242+
if (!$savedState) {
243+
throw new FacebookSDKException('Cross-site request forgery validation failed. Required param "state" missing from persistent data.');
249244
}
250245

251-
$result = 0;
252-
for ($i = 0; $i < $savedLen; $i++) {
253-
$result |= ord($state[$i]) ^ ord($savedState[$i]);
246+
if (\hash_equals($savedState, $state)) {
247+
return;
254248
}
255249

256-
if ($result !== 0) {
257-
throw new FacebookSDKException('Cross-site request forgery validation failed. The "state" param from the URL and session do not match.');
258-
}
250+
throw new FacebookSDKException('Cross-site request forgery validation failed. The "state" param from the URL and session do not match.');
259251
}
260252

261253
/**

0 commit comments

Comments
 (0)