-
Notifications
You must be signed in to change notification settings - Fork 70
Validate all the things #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,21 @@ var Config = require('./config'); | |
var Token = require('./token'); | ||
var WebHook = require('./webhook'); | ||
|
||
var validateChannel = function(channel) { | ||
if (typeof channel !== "string" || channel === "" || channel.match(/[^A-Za-z0-9_\-=@,.;]/)) { | ||
throw new Error("Invalid channel name: '" + channel + "'"); | ||
} | ||
if (channel.length > 200) { | ||
throw new Error("Channel name too long: '" + channel + "'"); | ||
} | ||
} | ||
|
||
var validateSocketId = function(socketId) { | ||
if (typeof socketId !== "string" || socketId === "" || !socketId.match(/^\d+\.\d+$/)) { | ||
throw new Error("Invalid socket id: '" + socketId + "'"); | ||
} | ||
} | ||
|
||
/** Callback passed to all REST API methods. | ||
* | ||
* @callback requestCallback | ||
|
@@ -79,12 +94,9 @@ Pusher.forCluster = function(cluster, options) { | |
* @returns {String} authentication signature | ||
*/ | ||
Pusher.prototype.authenticate = function(socketId, channel, data) { | ||
if (typeof socketId !== "string" || socketId === "") { | ||
throw new Error("Invalid socket id: '" + socketId + "'"); | ||
} | ||
if (typeof channel !== "string" || channel === "") { | ||
throw new Error("Invalid channel name: '" + channel + "'"); | ||
} | ||
validateSocketId(socketId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we please have some test cases for validating the socket_id? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
validateChannel(channel); | ||
|
||
return auth.getSocketSignature(this.config.token, channel, socketId, data); | ||
}; | ||
|
||
|
@@ -107,6 +119,9 @@ Pusher.prototype.authenticate = function(socketId, channel, data) { | |
* @see RequestError | ||
*/ | ||
Pusher.prototype.trigger = function(channels, event, data, socketId, callback) { | ||
if (socketId) { | ||
validateSocketId(socketId); | ||
} | ||
if (!(channels instanceof Array)) { | ||
// add single channel to array for multi trigger compatibility | ||
channels = [channels]; | ||
|
@@ -118,12 +133,7 @@ Pusher.prototype.trigger = function(channels, event, data, socketId, callback) { | |
throw new Error("Can't trigger a message to more than 10 channels"); | ||
} | ||
for (var i = 0; i < channels.length; i++) { | ||
if (channels[i].length > 200) { | ||
throw new Error("Too long channel name: '" + channels[i] + "'"); | ||
} | ||
if (!channels[i].match(/^[a-zA-Z0-9_\-=@,.;]+$/)) { | ||
throw new Error("Invalid channel name: '" + channels[i] + "'"); | ||
} | ||
validateChannel(channels[i]) | ||
} | ||
events.trigger(this, channels, event, data, socketId, callback); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is @zimbatm's comment about the required regexp here relevant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\A
and\Z
aren't valid in JS.^
and$
work the same, except in multiline mode. I have checked this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍