Skip to content

Commit 650ab17

Browse files
committed
2 parents 54928ba + 6714042 commit 650ab17

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
vendor/
33
composer.lock
44
composer.phar
5+
.DS_Store
6+
.idea/

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ if ($user) {
3939
}
4040
```
4141

42+
You can make api calls by choosing the `HTTP method` and setting optional `parameters`:
43+
```php
44+
$facebook->api('/me/feed/', 'post', array(
45+
'message' => 'I want to display this message on my wall'
46+
));
47+
```
48+
49+
4250
Login or logout url will be needed depending on current user state.
4351
```php
4452
if ($user) {

src/base_facebook.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ abstract class BaseFacebook
123123
/**
124124
* Version.
125125
*/
126-
const VERSION = '3.2.2';
126+
const VERSION = '3.2.3';
127127

128128
/**
129129
* Signed Request Algorithm.
@@ -215,13 +215,23 @@ abstract class BaseFacebook
215215
*/
216216
protected $trustForwarded = false;
217217

218+
/**
219+
* Indicates if signed_request is allowed in query parameters.
220+
*
221+
* @var boolean
222+
*/
223+
protected $allowSignedRequest = true;
224+
218225
/**
219226
* Initialize a Facebook Application.
220227
*
221228
* The configuration:
222229
* - appId: the application ID
223230
* - secret: the application secret
224231
* - fileUpload: (optional) boolean indicating if file uploads are enabled
232+
* - allowSignedRequest: (optional) boolean indicating if signed_request is
233+
* allowed in query parameters or POST body. Should be
234+
* false for non-canvas apps. Defaults to true.
225235
*
226236
* @param array $config The application configuration
227237
*/
@@ -234,6 +244,10 @@ public function __construct($config) {
234244
if (isset($config['trustForwarded']) && $config['trustForwarded']) {
235245
$this->trustForwarded = true;
236246
}
247+
if (isset($config['allowSignedRequest'])
248+
&& !$config['allowSignedRequest']) {
249+
$this->allowSignedRequest = false;
250+
}
237251
$state = $this->getPersistentData('state');
238252
if (!empty($state)) {
239253
$this->state = $state;
@@ -490,9 +504,10 @@ protected function getUserAccessToken() {
490504
*/
491505
public function getSignedRequest() {
492506
if (!$this->signedRequest) {
493-
if (!empty($_REQUEST['signed_request'])) {
507+
if ($this->allowSignedRequest && !empty($_REQUEST['signed_request'])) {
494508
$this->signedRequest = $this->parseSignedRequest(
495-
$_REQUEST['signed_request']);
509+
$_REQUEST['signed_request']
510+
);
496511
} else if (!empty($_COOKIE[$this->getSignedRequestCookieName()])) {
497512
$this->signedRequest = $this->parseSignedRequest(
498513
$_COOKIE[$this->getSignedRequestCookieName()]);
@@ -1025,12 +1040,23 @@ protected function parseSignedRequest($signed_request) {
10251040
// check sig
10261041
$expected_sig = hash_hmac('sha256', $payload,
10271042
$this->getAppSecret(), $raw = true);
1028-
if ($sig !== $expected_sig) {
1043+
1044+
if (strlen($expected_sig) !== strlen($sig)) {
10291045
self::errorLog('Bad Signed JSON signature!');
10301046
return null;
10311047
}
10321048

1033-
return $data;
1049+
$result = 0;
1050+
for ($i = 0; $i < strlen($expected_sig); $i++) {
1051+
$result |= ord($expected_sig[$i]) ^ ord($sig[$i]);
1052+
}
1053+
1054+
if ($result == 0) {
1055+
return $data;
1056+
} else {
1057+
self::errorLog('Bad Signed JSON signature!');
1058+
return null;
1059+
}
10341060
}
10351061

10361062
/**
@@ -1249,7 +1275,8 @@ protected function getCurrentUrl() {
12491275
*/
12501276
protected function shouldRetainParam($param) {
12511277
foreach (self::$DROP_QUERY_PARAMS as $drop_query_param) {
1252-
if (strpos($param, $drop_query_param.'=') === 0) {
1278+
if ($param === $drop_query_param ||
1279+
strpos($param, $drop_query_param.'=') === 0) {
12531280
return false;
12541281
}
12551282
}

tests/tests.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,20 @@ public function testGetUserFromSignedRequest() {
375375
'Failed to get user ID from a valid signed request.');
376376
}
377377

378-
public function testSignedRequestRewrite(){
378+
public function testDisallowSignedRequest() {
379+
$facebook = new TransientFacebook(array(
380+
'appId' => self::APP_ID,
381+
'secret' => self::SECRET,
382+
'allowSignedRequest' => false
383+
));
384+
385+
$_REQUEST['signed_request'] = self::kValidSignedRequest();
386+
$this->assertEquals(0, $facebook->getUser(),
387+
'Should not have received valid user from signed_request.');
388+
}
389+
390+
391+
public function testSignedRequestRewrite(){
379392
$facebook = new FBRewrite(array(
380393
'appId' => self::APP_ID,
381394
'secret' => self::SECRET,

0 commit comments

Comments
 (0)