Fix error on invalid regex for /clean

This commit is contained in:
Calvin Montgomery 2018-01-17 21:54:43 -08:00
parent 326e67893c
commit 8399eab33f
3 changed files with 69 additions and 2 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery",
"name": "CyTube",
"description": "Online media synchronizer and chat",
"version": "3.52.2",
"version": "3.52.3",
"repository": {
"url": "http://github.com/calzoneman/sync"
},

View File

@ -1218,6 +1218,12 @@ PlaylistModule.prototype.clean = function (test) {
* == Command Handlers ==
*/
/*
* TODO: investigate how many people are relying on regex
* capabilities for /clean. Might be user friendlier to
* replace it with a glob matcher (or at least remove the
* -flags)
*/
function generateTargetRegex(target) {
const flagsre = /^(-[img]+\s+)/i
var m = target.match(flagsre);
@ -1242,7 +1248,15 @@ PlaylistModule.prototype.handleClean = function (user, msg, meta) {
"/cleantitle <title>"
});
}
var target = generateTargetRegex(args.join(" "));
var target;
try {
target = generateTargetRegex(args.join(" "));
} catch (error) {
user.socket.emit("errorMsg", {
msg: `Invalid target: ${args.join(" ")}`
});
return;
}
this.channel.logger.log("[playlist] " + user.getName() + " used " + cmd +
" with target regex: " + target);

53
test/channel/playlist.js Normal file
View File

@ -0,0 +1,53 @@
const PlaylistModule = require('../../lib/channel/playlist');
const assert = require('assert');
const Config = require('../../lib/config');
describe('PlaylistModule', () => {
describe('#handleClean', () => {
let fakeChannel = {
uniqueName: 'testChannel',
logger: {
log() {
}
},
broadcastToRoom() {
},
broadcastAll() {
},
modules: {
permissions: {
canDeleteVideo() {
return true;
}
}
}
};
let fakeUser = {
getName() {
return 'testUser';
},
socket: {
emit() {
}
}
};
let playlistModule = new PlaylistModule(fakeChannel);
it('rejects invalid regexes', () => {
let sentError = false;
fakeUser.socket.emit = (event, payload) => {
assert.strictEqual(event, 'errorMsg');
assert.deepStrictEqual(payload, {
msg: "Invalid target: -i * -m"
});
sentError = true;
};
playlistModule.handleClean(fakeUser, "/clean -i * -m", {});
assert(sentError, 'Expected error due to invalid regex');
});
});
});