Skip to content

Commit c9fb93a

Browse files
committed
Merge pull request #72 from pusher/validate_socket_id
Validate socket IDs
2 parents 3295cd7 + be2e2a7 commit c9fb93a

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

lib/pusher/channel.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def initialize(base_url, name, client = Pusher)
3434
#
3535
def trigger_async(event_name, data, socket_id = nil)
3636
params = {}
37-
params[:socket_id] = socket_id if socket_id
37+
if socket_id
38+
validate_socket_id(socket_id)
39+
params[:socket_id] = socket_id
40+
end
3841
@client.trigger_async(name, event_name, data, params)
3942
end
4043

@@ -60,7 +63,10 @@ def trigger_async(event_name, data, socket_id = nil)
6063
#
6164
def trigger!(event_name, data, socket_id = nil)
6265
params = {}
63-
params[:socket_id] = socket_id if socket_id
66+
if socket_id
67+
validate_socket_id(socket_id)
68+
params[:socket_id] = socket_id
69+
end
6470
@client.trigger(name, event_name, data, params)
6571
end
6672

@@ -115,9 +121,7 @@ def users
115121
# @return [String]
116122
#
117123
def authentication_string(socket_id, custom_string = nil)
118-
if socket_id.nil? || socket_id.empty?
119-
raise Error, "Invalid socket_id #{socket_id}"
120-
end
124+
validate_socket_id(socket_id)
121125

122126
unless custom_string.nil? || custom_string.kind_of?(String)
123127
raise Error, 'Custom argument must be a string'
@@ -162,5 +166,13 @@ def authenticate(socket_id, custom_data = nil)
162166
r[:channel_data] = custom_data if custom_data
163167
r
164168
end
169+
170+
private
171+
172+
def validate_socket_id(socket_id)
173+
unless socket_id && /\A\d+\.\d+\z/.match(socket_id)
174+
raise Pusher::Error, "Invalid socket ID #{socket_id.inspect}"
175+
end
176+
end
165177
end
166178
end

spec/channel_spec.rb

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def authentication_string(*data)
9999
end
100100

101101
it "should return an authentication string given a socket id" do
102-
auth = @channel.authentication_string('socketid')
102+
auth = @channel.authentication_string('1.1')
103103

104-
auth.should == '12345678900000001:827076f551e22451357939e4c7bb1200de29f921d5bf80b40d71668f9cd61c40'
104+
auth.should == '12345678900000001:02259dff9a2a3f71ea8ab29ac0c0c0ef7996c8f3fd3702be5533f30da7d7fed4'
105105
end
106106

107107
it "should raise error if authentication is invalid" do
@@ -112,17 +112,17 @@ def authentication_string(*data)
112112

113113
describe 'with extra string argument' do
114114
it 'should be a string or nil' do
115-
authentication_string('socketid', 123).should raise_error Pusher::Error
116-
authentication_string('socketid', {}).should raise_error Pusher::Error
115+
authentication_string('1.1', 123).should raise_error Pusher::Error
116+
authentication_string('1.1', {}).should raise_error Pusher::Error
117117

118-
authentication_string('socketid', 'boom').should_not raise_error
119-
authentication_string('socketid', nil).should_not raise_error
118+
authentication_string('1.1', 'boom').should_not raise_error
119+
authentication_string('1.1', nil).should_not raise_error
120120
end
121121

122122
it "should return an authentication string given a socket id and custom args" do
123-
auth = @channel.authentication_string('socketid', 'foobar')
123+
auth = @channel.authentication_string('1.1', 'foobar')
124124

125-
auth.should == "12345678900000001:#{hmac(@client.secret, "socketid:test_channel:foobar")}"
125+
auth.should == "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:foobar")}"
126126
end
127127
end
128128
end
@@ -135,12 +135,34 @@ def authentication_string(*data)
135135
it 'should return a hash with signature including custom data and data as json string' do
136136
MultiJson.stub(:encode).with(@custom_data).and_return 'a json string'
137137

138-
response = @channel.authenticate('socketid', @custom_data)
138+
response = @channel.authenticate('1.1', @custom_data)
139139

140140
response.should == {
141-
:auth => "12345678900000001:#{hmac(@client.secret, "socketid:test_channel:a json string")}",
141+
:auth => "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:a json string")}",
142142
:channel_data => 'a json string'
143143
}
144144
end
145+
146+
it 'should fail on invalid socket_ids' do
147+
lambda {
148+
@channel.authenticate('1.1:')
149+
}.should raise_error Pusher::Error
150+
151+
lambda {
152+
@channel.authenticate('1.1foo', 'channel')
153+
}.should raise_error Pusher::Error
154+
155+
lambda {
156+
@channel.authenticate(':1.1')
157+
}.should raise_error Pusher::Error
158+
159+
lambda {
160+
@channel.authenticate('foo1.1', 'channel')
161+
}.should raise_error Pusher::Error
162+
163+
lambda {
164+
@channel.authenticate('foo', 'channel')
165+
}.should raise_error Pusher::Error
166+
end
145167
end
146168
end

spec/client_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,19 @@
225225
lambda {
226226
@client.trigger((0..11).map{|i| 'mychannel#{i}'},
227227
'event', {'some' => 'data'}, {
228-
:socket_id => "1234"
228+
:socket_id => "12.34"
229229
})}.should raise_error(Pusher::Error)
230230
end
231231

232232
it "should pass any parameters in the body of the request" do
233233
@client.trigger(['mychannel', 'c2'], 'event', {'some' => 'data'}, {
234-
:socket_id => "1234"
234+
:socket_id => "12.34"
235235
})
236236
WebMock.should have_requested(:post, @api_path).with { |req|
237237
parsed = MultiJson.decode(req.body)
238238
parsed["name"].should == 'event'
239239
parsed["channels"].should == ["mychannel", "c2"]
240-
parsed["socket_id"].should == '1234'
240+
parsed["socket_id"].should == '12.34'
241241
}
242242
end
243243

@@ -277,10 +277,10 @@
277277
it "should pass any parameters in the body of the request" do
278278
EM.run {
279279
@client.trigger_async('mychannel', 'event', {'some' => 'data'}, {
280-
:socket_id => "1234"
280+
:socket_id => "12.34"
281281
}).callback {
282282
WebMock.should have_requested(:post, @api_path).with { |req|
283-
MultiJson.decode(req.body)["socket_id"].should == '1234'
283+
MultiJson.decode(req.body)["socket_id"].should == '12.34'
284284
}
285285
EM.stop
286286
}

0 commit comments

Comments
 (0)