Skip to content

Commit 0b1dfe4

Browse files
committed
Merge pull request #10 from pusher/validate_socket_id
Validate all the things
2 parents 311437c + 39d0c47 commit 0b1dfe4

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

lib/pusher.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ var Config = require('./config');
1010
var Token = require('./token');
1111
var WebHook = require('./webhook');
1212

13+
var validateChannel = function(channel) {
14+
if (typeof channel !== "string" || channel === "" || channel.match(/[^A-Za-z0-9_\-=@,.;]/)) {
15+
throw new Error("Invalid channel name: '" + channel + "'");
16+
}
17+
if (channel.length > 200) {
18+
throw new Error("Channel name too long: '" + channel + "'");
19+
}
20+
}
21+
22+
var validateSocketId = function(socketId) {
23+
if (typeof socketId !== "string" || socketId === "" || !socketId.match(/^\d+\.\d+$/)) {
24+
throw new Error("Invalid socket id: '" + socketId + "'");
25+
}
26+
}
27+
1328
/** Callback passed to all REST API methods.
1429
*
1530
* @callback requestCallback
@@ -79,12 +94,9 @@ Pusher.forCluster = function(cluster, options) {
7994
* @returns {String} authentication signature
8095
*/
8196
Pusher.prototype.authenticate = function(socketId, channel, data) {
82-
if (typeof socketId !== "string" || socketId === "") {
83-
throw new Error("Invalid socket id: '" + socketId + "'");
84-
}
85-
if (typeof channel !== "string" || channel === "") {
86-
throw new Error("Invalid channel name: '" + channel + "'");
87-
}
97+
validateSocketId(socketId);
98+
validateChannel(channel);
99+
88100
return auth.getSocketSignature(this.config.token, channel, socketId, data);
89101
};
90102

@@ -107,6 +119,9 @@ Pusher.prototype.authenticate = function(socketId, channel, data) {
107119
* @see RequestError
108120
*/
109121
Pusher.prototype.trigger = function(channels, event, data, socketId, callback) {
122+
if (socketId) {
123+
validateSocketId(socketId);
124+
}
110125
if (!(channels instanceof Array)) {
111126
// add single channel to array for multi trigger compatibility
112127
channels = [channels];
@@ -118,12 +133,7 @@ Pusher.prototype.trigger = function(channels, event, data, socketId, callback) {
118133
throw new Error("Can't trigger a message to more than 10 channels");
119134
}
120135
for (var i = 0; i < channels.length; i++) {
121-
if (channels[i].length > 200) {
122-
throw new Error("Too long channel name: '" + channels[i] + "'");
123-
}
124-
if (!channels[i].match(/^[a-zA-Z0-9_\-=@,.;]+$/)) {
125-
throw new Error("Invalid channel name: '" + channels[i] + "'");
126-
}
136+
validateChannel(channels[i])
127137
}
128138
events.trigger(this, channels, event, data, socketId, callback);
129139
};

tests/integration/pusher/authenticate.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ describe("Pusher", function() {
9292
}).to.throwException(/^Invalid socket id: ''$/);
9393
});
9494

95+
it("should raise an exception if socket id is invalid", function() {
96+
expect(function() {
97+
pusher.authenticate("1.1:", "test")
98+
}).to.throwException(/^Invalid socket id/);
99+
expect(function() {
100+
pusher.authenticate(":1.1", "test")
101+
}).to.throwException(/^Invalid socket id/);
102+
expect(function() {
103+
pusher.authenticate(":\n1.1", "test")
104+
}).to.throwException(/^Invalid socket id/);
105+
expect(function() {
106+
pusher.authenticate("1.1\n:", "test")
107+
}).to.throwException(/^Invalid socket id/);
108+
});
109+
95110
it("should raise an exception if channel name is not a string", function() {
96111
expect(function() {
97112
pusher.authenticate("111.222", undefined)

tests/integration/pusher/trigger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ describe("Pusher", function() {
220220
pusher.trigger(channel, "test");
221221
}).to.throwError(function(e) {
222222
expect(e).to.be.an(Error);
223-
expect(e.message).to.equal("Too long channel name: '" + channel + "'");
223+
expect(e.message).to.equal("Channel name too long: '" + channel + "'");
224224
});
225225
});
226226

0 commit comments

Comments
 (0)