Fix user join ban check for users with blank names (but clean IPs)

This commit is contained in:
Calvin Montgomery 2017-03-15 23:44:03 -07:00
parent f6500ff745
commit a594b19745
4 changed files with 143 additions and 5 deletions

1
.gitignore vendored
View File

@ -12,3 +12,4 @@ torlist
www/cache www/cache
google-drive-subtitles google-drive-subtitles
lib/ lib/
integration-test-config.json

View File

@ -0,0 +1,130 @@
const assert = require('assert');
const KickbanModule = require('../../lib/channel/kickban');
const db = require('../../lib/database');
const dbChannels = require('../../lib/database/channels');
const Promise = require('bluebird');
const Config = require('../../lib/config');
const ChannelModule = require('../../lib/channel/module');
const Flags = require('../../lib/flags');
const TestConfig = require('../../integration-test-config.json');
require('../../lib/counters');
function randomString(length) {
const chars = 'abcdefgihkmnpqrstuvwxyz0123456789';
let str = '';
for (let i = 0; i < length; i++) {
str += chars[Math.floor(Math.random() * chars.length)];
}
return str;
}
Config.set('mysql.password', TestConfig.mysql.password);
db.init();
describe('onPreUserJoin Ban Check', () => {
const channelName = `test_${randomString(20)}`;
const bannedIP = '1.1.1.1';
const bannedName = 'troll';
const mockChannel = {
name: channelName,
modules: {},
is(flag) {
return flag === Flags.C_REGISTERED;
}
};
const module = new KickbanModule(mockChannel);
before(done => {
dbChannels.ban(channelName, bannedIP, bannedName, '', '', () => {
dbChannels.ban(channelName, bannedIP, '', '', '', () => {
dbChannels.ban(channelName, '*', bannedName, '', '', () => {
done();
});
});
});
});
after(done => {
dbChannels.deleteBans(channelName, null, () => {
done();
});
});
it('handles a banned IP with a different name', done => {
const user = {
getName() {
return 'anotherTroll';
},
realip: bannedIP
};
module.onUserPreJoin(user, null, (error, res) => {
assert.equal(error, null, `Unexpected error: ${error}`);
assert.equal(res, ChannelModule.DENY, 'Expected user to be banned');
done();
});
});
it('handles a banned name with a different IP', done => {
const user = {
getName() {
return 'troll';
},
realip: '5.5.5.5'
};
module.onUserPreJoin(user, null, (error, res) => {
assert.equal(error, null, `Unexpected error: ${error}`);
assert.equal(res, ChannelModule.DENY, 'Expected user to be banned');
done();
});
});
it('handles a banned IP with a blank name', done => {
const user = {
getName() {
return '';
},
realip: bannedIP
};
module.onUserPreJoin(user, null, (error, res) => {
assert.equal(error, null, `Unexpected error: ${error}`);
assert.equal(res, ChannelModule.DENY, 'Expected user to be banned');
done();
});
});
it('handles a non-banned IP with a blank name', done => {
const user = {
getName() {
return '';
},
realip: '5.5.5.5'
};
module.onUserPreJoin(user, null, (error, res) => {
assert.equal(error, null, `Unexpected error: ${error}`);
assert.equal(res, ChannelModule.PASSTHROUGH, 'Expected user not to be banned');
done();
});
});
it('handles a non-banned IP with a non-banned name', done => {
const user = {
getName() {
return 'some_user';
},
realip: '5.5.5.5'
};
module.onUserPreJoin(user, null, (error, res) => {
assert.equal(error, null, `Unexpected error: ${error}`);
assert.equal(res, ChannelModule.PASSTHROUGH, 'Expected user not to be banned');
done();
});
});
});

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.34.1", "version": "3.34.2",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },
@ -54,7 +54,8 @@
"postinstall": "./postinstall.sh", "postinstall": "./postinstall.sh",
"server-dev": "babel -D --watch --source-maps --loose es6.destructuring,es6.forOf --out-dir lib/ src/", "server-dev": "babel -D --watch --source-maps --loose es6.destructuring,es6.forOf --out-dir lib/ src/",
"generate-userscript": "$npm_node_execpath gdrive-userscript/generate-userscript $@ > www/js/cytube-google-drive.user.js", "generate-userscript": "$npm_node_execpath gdrive-userscript/generate-userscript $@ > www/js/cytube-google-drive.user.js",
"test": "mocha" "test": "mocha",
"integration-test": "mocha --recursive integration_test"
}, },
"devDependencies": { "devDependencies": {
"coffee-script": "^1.9.2", "coffee-script": "^1.9.2",

View File

@ -59,16 +59,22 @@ KickBanModule.prototype.onUserPreJoin = function (user, data, cb) {
return cb(null, ChannelModule.PASSTHROUGH); return cb(null, ChannelModule.PASSTHROUGH);
} }
var cname = this.channel.name; const cname = this.channel.name;
checkBan(cname, user.realip, user.getName(), function (banned) { const check = (user.getName() !== '') ? checkBan : checkIPBan;
function callback(banned) {
if (banned) { if (banned) {
cb(null, ChannelModule.DENY); cb(null, ChannelModule.DENY);
user.kick("You are banned from this channel."); user.kick("You are banned from this channel.");
} else { } else {
cb(null, ChannelModule.PASSTHROUGH); cb(null, ChannelModule.PASSTHROUGH);
} }
}); }
if (user.getName() !== '') {
checkBan(cname, user.realip, user.getName(), callback);
} else {
checkIPBan(cname, user.realip, callback);
}
}; };
KickBanModule.prototype.onUserPostJoin = function (user) { KickBanModule.prototype.onUserPostJoin = function (user) {