@@ -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 }
0 commit comments