Skip to content

Commit

Permalink
Merge pull request #116 from pusher/kill-the-spare
Browse files Browse the repository at this point in the history
Kill notification payload validation
  • Loading branch information
jpatel531 authored Aug 15, 2016
2 parents 40f4cab + 6b09e9f commit 04535a4
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 159 deletions.
63 changes: 0 additions & 63 deletions lib/Pusher.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ public static function get_pusher()
class Pusher
{
public static $VERSION = '2.5.0-rc3';
private static $RESTRICTED_GCM_PAYLOAD_KEYS = array('to', 'registration_ids');
private static $GCM_TTL = 241920;
private static $WEBHOOK_LEVELS = array('INFO', 'DEBUG');

private $settings = array(
'scheme' => 'http',
Expand Down Expand Up @@ -353,63 +350,6 @@ private function exec_curl($ch)
return $response;
}

/**
* Validates the push notification payload.
*
* @param array $payload
*
* @throws PusherException if validation fails
*/
private function validate_notification_payload($payload)
{
if (!(isset($payload['gcm']) || isset($payload['apns']))) {
throw new PusherException('GCM or APNS data must be provided');
}

if (isset($payload['gcm'])) {
$gcm_payload = $payload['gcm'];

// Delete restricted keys
foreach (self::$RESTRICTED_GCM_PAYLOAD_KEYS as $key) {
unset($gcm_payload[$key]);
}

if (isset($gcm_payload['time_to_live'])) {
$ttl = (int) ($payload['gcm']['time_to_live']);

if ($ttl < 0 || $ttl > self::$GCM_TTL) {
throw new PusherException('Time to live must be between 0 and 241920 (4 weeks)');
}
}

if (isset($gcm_payload['notification'])) {
if (!isset($gcm_payload['notification']['title'])) {
throw new PusherException('Notification title is a required field');
}

if (!isset($gcm_payload['notification']['icon'])) {
throw new PusherException('Notification icon is a required field');
}
}
}

if (isset($payload['webhook_url'])) {
if (!filter_var($payload['webhook_url'], FILTER_VALIDATE_URL)) {
throw new PusherException('Webhook url is invalid');
}
}

if (isset($payload['webhook_level'])) {
if (!isset($payload['webhook_url'])) {
throw new PusherException('Webhook level cannot be used without a webhook url');
}

if (!in_array(strtoupper($payload['webhook_level']), self::$WEBHOOK_LEVELS)) {
throw new PusherException('Webhook level must either be INFO or DEBUG');
}
}
}

/**
* Build the notification domain.
*
Expand Down Expand Up @@ -745,9 +685,6 @@ public function notify($interests, $data = array(), $debug = false)
throw new PusherException('Multiple interests provided. Please provide a single interest');
}

// Validate notification payload
$this->validate_notification_payload($data);

$data['interests'] = $interests;

$post_value = json_encode($data);
Expand Down
96 changes: 0 additions & 96 deletions test/unit/notificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,100 +14,4 @@ public function testInvalidInterestLength()
{
$this->pusher->notify(array('a', 'b'), array('foo' => 'bar'));
}

/**
* @expectedException PusherException
*/
public function testMissingGcmApnsKeys()
{
$this->pusher->notify(array('test'), array('foo' => 'bar'));
}

/**
* @expectedException PusherException
*/
public function testInvalidTimeToLive()
{
$payload = array(
'gcm' => array(
'time_to_live' => 21273871,
'notification' => array(
'title' => 'title',
'icon' => 'icon',
),
),
);
$this->pusher->notify(array('test'), $payload);
}

/**
* @expectedException PusherException
*/
public function testMissingNotificationTitle()
{
$payload = array(
'gcm' => array(
'notification' => array(
'icon' => 'icon',
),
),
);
$this->pusher->notify(array('test'), $payload);
}

/**
* @expectedException PusherException
*/
public function testMissingNotificationIcon()
{
$payload = array(
'gcm' => array(
'notification' => array(
'title' => 'title',
),
),
);
$this->pusher->notify(array('test'), $payload);
}

/**
* @expectedException PusherException
*/
public function testInvalidWebhookUrl()
{
$payload = array(
'gcm' => array(
'foo' => 'bar',
),
'webhook_url' => 'aksdasd',
);
$this->pusher->notify(array('test'), $payload);
}

/**
* @expectedException PusherException
*/
public function testInvalidWebhookLevel()
{
$payload = array(
'gcm' => array(
'foo' => 'bar',
),
'webhook_url' => 'https://test.com/wh',
'webhook_level' => 'WARN',
);
$this->pusher->notify(array('test'), $payload);
}

/**
* @expectedException PusherException
*/
public function testWebhookLevelWithoutUrl()
{
$payload = array(
'gcm' => array('foo' => 'bar'),
'webhook_level' => 'INFO',
);
$this->pusher->notify(array('test'), $payload);
}
}

0 comments on commit 04535a4

Please sign in to comment.