Skip to content

Commit 07b7c42

Browse files
committed
Error webhook url validation
1 parent 4894718 commit 07b7c42

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

lib/pusher/native_notification/client.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Client
77
API_VERSION = "v1"
88
GCM_TTL = 241920
99
RESTRICTED_GCM_PAYLOAD_KEYS = [:to, :registration_ids]
10+
WEBHOOK_LEVELS = ["DEBUG", "INFO"]
1011

1112
def initialize(app_id, host, pusher_client)
1213
@app_id = app_id
@@ -74,21 +75,18 @@ def validate_payload(payload)
7475
raise Pusher::Error, "GCM or APNS data must be provided"
7576
end
7677

77-
if payload.has_key?(:gcm)
78-
gcm_payload = payload[:gcm]
78+
if (gcm_payload = payload[:gcm])
79+
if (ttl = gcm_payload[:time_to_live])
7980

80-
if gcm_payload.has_key?(:time_to_live)
81-
ttl = gcm_payload[:time_to_live].to_i
82-
83-
if ttl < 0 || ttl > GCM_TTL
84-
raise Pusher::Error, "time_to_live key must have a value between 0 and 241920 (4 weeks)"
81+
if ttl.to_i < 0 || ttl.to_i > GCM_TTL
82+
raise Pusher::Error, "Time to live must be between 0 and 241920 (4 weeks)"
8583
end
8684
end
8785

8886
# If the notification key is provided
8987
# validate the `icon` and `title`keys
90-
if gcm_payload.has_key?(:notification)
91-
notification_title, notification_icon = gcm_payload[:notification].values_at(:title, :icon)
88+
if (notification = gcm_payload[:notification])
89+
notification_title, notification_icon = notification.values_at(:title, :icon)
9290

9391
if (!notification_title || notification_title.empty?)
9492
raise Pusher::Error, "Notification title is a required field"
@@ -99,6 +97,18 @@ def validate_payload(payload)
9997
end
10098
end
10199
end
100+
101+
if (webhook_url = payload[:webhook_url])
102+
raise Pusher::Error, "Webhook url is invalid" unless webhook_url =~ /\A#{URI::regexp(['http', 'https'])}\z/
103+
end
104+
105+
if (webhook_level = payload[:webhook_level])
106+
raise Pusher::Error, "Webhook level cannot be used without a webhook url" if !payload.has_key?(:webhook_url)
107+
108+
unless WEBHOOK_LEVELS.includes?(webhook_level.upcase)
109+
raise Pusher::Error, "Webhook level must either be INFO or DEBUG"
110+
end
111+
end
102112
end
103113

104114
# Symbolize all keys in the hash recursively

0 commit comments

Comments
 (0)