Skip to content

Commit 48d034c

Browse files
committed
Merge pull request #49 from pusher/validate_socket_id
Added socket_id validation
2 parents 4f9ca33 + def64d2 commit 48d034c

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

pusher/pusher.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pusher.http import GET, POST, Request, request_method
66
from pusher.signature import sign, verify
77
from pusher.requests import RequestsBackend
8-
from pusher.util import ensure_text, validate_channel, app_id_re, pusher_url_re, channel_name_re
8+
from pusher.util import ensure_text, validate_channel, validate_socket_id, app_id_re, pusher_url_re, channel_name_re
99

1010
import collections
1111
import hashlib
@@ -153,7 +153,7 @@ def trigger(self, channels, event_name, data, socket_id=None):
153153
'data': data
154154
}
155155
if socket_id:
156-
params['socket_id'] = ensure_text(socket_id, "socket_id")
156+
params['socket_id'] = validate_socket_id(socket_id)
157157

158158
return Request(self, POST, "/apps/%s/events" % self.app_id, params)
159159

@@ -208,7 +208,7 @@ def authenticate(self, channel, socket_id, custom_data=None):
208208
if not channel_name_re.match(channel):
209209
raise ValueError('Channel should be a valid channel, got: %s' % channel)
210210

211-
socket_id = ensure_text(socket_id, "socket_id")
211+
socket_id = validate_socket_id(socket_id)
212212

213213
if custom_data:
214214
custom_data = json.dumps(custom_data, cls=self._json_encoder)

pusher/util.py

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
channel_name_re = re.compile('^[-a-zA-Z0-9_=@,.;]+$')
1212
app_id_re = re.compile('^[0-9]+$')
1313
pusher_url_re = re.compile('(http|https)://(.*):(.*)@(.*)/apps/([0-9]+)')
14+
socket_id_re = re.compile('\d+\.\d+')
1415

1516
if sys.version_info < (3,):
1617
text = 'a unicode string'
@@ -34,3 +35,11 @@ def validate_channel(channel):
3435
raise ValueError("Invalid Channel: %s" % channel)
3536

3637
return channel
38+
39+
def validate_socket_id(socket_id):
40+
socket_id = ensure_text(socket_id, "socket_id")
41+
42+
if not socket_id_re.match(socket_id):
43+
raise ValueError("Invalid socket ID: %s" % socket_id)
44+
45+
return socket_id

pusher_tests/test_pusher.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ def test_authenticate_for_private_channels(self):
105105
pusher = Pusher.from_url(u'http://foo:bar@host/apps/4')
106106

107107
expected = {
108-
u'auth': u"foo:076740bd063f0299742a73bc5aac88900e5f35cb0185a1facbf45d326b5b204b"
108+
u'auth': u"foo:89955e77e1b40e33df6d515a5ecbba86a01dc816a5b720da18a06fd26f7d92ff"
109109
}
110110

111-
self.assertEqual(pusher.authenticate(u'private-channel', u'34523'), expected)
111+
self.assertEqual(pusher.authenticate(u'private-channel', u'345.23'), expected)
112112

113113
def test_authenticate_for_presence_channels(self):
114114
pusher = Pusher.from_url(u'http://foo:bar@host/apps/4')
@@ -121,12 +121,12 @@ def test_authenticate_for_presence_channels(self):
121121
}
122122

123123
expected = {
124-
u'auth': u"foo:fbbc6d8acc85fc807bba060e2df45aba33deb8ad44cbee1633675b3ce73f4817",
124+
u'auth': u"foo:e80ba6439492c2113022c39297a87a948de14061cc67b5788e045645a68b8ccd",
125125
u'channel_data': u"{\"user_id\":\"fred\",\"user_info\":{\"key\":\"value\"}}"
126126
}
127127

128128
with mock.patch('json.dumps', return_value=expected[u'channel_data']) as dumps_mock:
129-
actual = pusher.authenticate(u'presence-channel', u'34543245', custom_data)
129+
actual = pusher.authenticate(u'presence-channel', u'345.43245', custom_data)
130130

131131
self.assertEqual(actual, expected)
132132
dumps_mock.assert_called_once_with(custom_data, cls=None)
@@ -257,9 +257,9 @@ def test_custom_json_decoder(self):
257257
def test_custom_json_encoder(self):
258258
expected = {
259259
u'channel_data': '{"money": "1.32"}',
260-
u'auth': u'key:75c6044a30f2ccd9952c48cfcf149cb0a4843bf38bab47545fb953acd62bd0c9'
260+
u'auth': u'key:7f2ae5922800a20b9615543ce7c8e7d1c97115d108939410825ea690f308a05f'
261261
}
262-
data = self.pusher.authenticate("presence-c1", "1", {"money": Decimal("1.32")})
262+
data = self.pusher.authenticate("presence-c1", "1.1", {"money": Decimal("1.32")})
263263
self.assertEqual(expected, data)
264264

265265
if __name__ == '__main__':

0 commit comments

Comments
 (0)