From fa7db423c6db80fed7d2fb14622788b3f64f4514 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Sat, 26 Apr 2014 03:00:56 -0400 Subject: [PATCH 1/5] adding hook filter:topic.reply for antispam --- src/socket.io/posts.js | 20 +++++++++++++++++++- src/topics/create.js | 10 ++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index dde786af7e..4e68fe7a25 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -13,7 +13,24 @@ var async = require('async'), user = require('../user'), websockets = require('./index'), - SocketPosts = {}; + SocketPosts = {}, + + // a shy request-wannabe build from a socket for spam detection purposes + reqFromSocket = function(socket) { + var headers = socket.handshake.headers, + host = headers['host'], + referer = headers['referer']; + + return { + 'ip': headers['x-forwarded-for'] || socket.handshake.address.address, + 'host': host, + 'protocol': headers['secure'] ? 'https' : 'http', + 'secure': !!headers['secure'], + 'url': referer, + 'path': referer.substr(referer.indexOf(host) + host.length), + 'headers': headers + }; + }; SocketPosts.reply = function(socket, data, callback) { @@ -26,6 +43,7 @@ SocketPosts.reply = function(socket, data, callback) { } data.uid = socket.uid; + data.req = reqFromSocket(socket); topics.reply(data, function(err, postData) { if(err) { diff --git a/src/topics/create.js b/src/topics/create.js index c9d6482df8..e8002e45cf 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -141,6 +141,16 @@ module.exports = function(Topics) { async.waterfall([ function(next) { + plugins.fireHook('filter:topic.reply', data, function(err, filteredData) { + if (err) { + return next(err); + } + + content = filteredData.content || data.content; + next(); + }); + }, + function(next) { threadTools.exists(tid, next); }, function(topicExists, next) { From c7bd27fb3f950bc1e7670d801e0c471a4d0bb081 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Sat, 26 Apr 2014 03:16:07 -0400 Subject: [PATCH 2/5] spaces vs tabs ugh --- src/socket.io/posts.js | 32 ++++++++++++++++---------------- src/topics/create.js | 20 ++++++++++---------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 4e68fe7a25..654745cf60 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -16,21 +16,21 @@ var async = require('async'), SocketPosts = {}, // a shy request-wannabe build from a socket for spam detection purposes - reqFromSocket = function(socket) { - var headers = socket.handshake.headers, - host = headers['host'], - referer = headers['referer']; - - return { - 'ip': headers['x-forwarded-for'] || socket.handshake.address.address, - 'host': host, - 'protocol': headers['secure'] ? 'https' : 'http', - 'secure': !!headers['secure'], - 'url': referer, - 'path': referer.substr(referer.indexOf(host) + host.length), - 'headers': headers - }; - }; + reqFromSocket = function(socket) { + var headers = socket.handshake.headers, + host = headers['host'], + referer = headers['referer']; + + return { + 'ip': headers['x-forwarded-for'] || socket.handshake.address.address, + 'host': host, + 'protocol': headers['secure'] ? 'https' : 'http', + 'secure': !!headers['secure'], + 'url': referer, + 'path': referer.substr(referer.indexOf(host) + host.length), + 'headers': headers + }; + }; SocketPosts.reply = function(socket, data, callback) { @@ -43,7 +43,7 @@ SocketPosts.reply = function(socket, data, callback) { } data.uid = socket.uid; - data.req = reqFromSocket(socket); + data.req = reqFromSocket(socket); topics.reply(data, function(err, postData) { if(err) { diff --git a/src/topics/create.js b/src/topics/create.js index e8002e45cf..c0363c3085 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -141,16 +141,16 @@ module.exports = function(Topics) { async.waterfall([ function(next) { - plugins.fireHook('filter:topic.reply', data, function(err, filteredData) { - if (err) { - return next(err); - } - - content = filteredData.content || data.content; - next(); - }); - }, - function(next) { + plugins.fireHook('filter:topic.reply', data, function(err, filteredData) { + if (err) { + return next(err); + } + + content = filteredData.content || data.content; + next(); + }); + }, + function(next) { threadTools.exists(tid, next); }, function(topicExists, next) { From 65ff72b0e35f825fad9c9d91c996f775a3cc4b2f Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Sat, 26 Apr 2014 03:19:56 -0400 Subject: [PATCH 3/5] its 3am --- src/topics/create.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/topics/create.js b/src/topics/create.js index c0363c3085..ebf12ff15e 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -141,14 +141,14 @@ module.exports = function(Topics) { async.waterfall([ function(next) { - plugins.fireHook('filter:topic.reply', data, function(err, filteredData) { - if (err) { - return next(err); - } - - content = filteredData.content || data.content; - next(); - }); + plugins.fireHook('filter:topic.reply', data, function(err, filteredData) { + if (err) { + return next(err); + } + + content = filteredData.content || data.content; + next(); + }); }, function(next) { threadTools.exists(tid, next); From 056b39709fbf10a73cd39e639689722f0628665a Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Sat, 26 Apr 2014 03:26:23 -0400 Subject: [PATCH 4/5] no need for quotes --- src/socket.io/posts.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 654745cf60..d9b9f7ab7c 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -22,13 +22,13 @@ var async = require('async'), referer = headers['referer']; return { - 'ip': headers['x-forwarded-for'] || socket.handshake.address.address, - 'host': host, - 'protocol': headers['secure'] ? 'https' : 'http', - 'secure': !!headers['secure'], - 'url': referer, - 'path': referer.substr(referer.indexOf(host) + host.length), - 'headers': headers + ip: headers['x-forwarded-for'] || socket.handshake.address.address, + host: host, + protocol: headers['secure'] ? 'https' : 'http', + secure: !!headers['secure'], + url: referer, + path: referer.substr(referer.indexOf(host) + host.length), + headers: headers }; }; From 32a5334402ab4ee94ce3e753ef7580ec17c7e519 Mon Sep 17 00:00:00 2001 From: Aziz Khoury Date: Sat, 26 Apr 2014 04:29:53 -0400 Subject: [PATCH 5/5] socket.handshake.address may not always be defined, thanks @psychobunny --- src/socket.io/posts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index d9b9f7ab7c..b3d36e69b4 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -22,7 +22,7 @@ var async = require('async'), referer = headers['referer']; return { - ip: headers['x-forwarded-for'] || socket.handshake.address.address, + ip: headers['x-forwarded-for'] || (socket.handshake.address || {}).address, host: host, protocol: headers['secure'] ? 'https' : 'http', secure: !!headers['secure'],