Change link avoidance mechanism in filters

This commit is contained in:
Calvin Montgomery 2015-03-24 09:23:38 -07:00
parent e1c8d5c6c9
commit 0b86eeea6d
1 changed files with 21 additions and 21 deletions

View File

@ -8,6 +8,9 @@ var url = require("url");
const SHADOW_TAG = "[shadow]"; const SHADOW_TAG = "[shadow]";
const LINK = /(\w+:\/\/(?:[^:\/\[\]\s]+|\[[0-9a-f:]+\])(?::\d+)?(?:\/[^\/\s]*)*)/ig; const LINK = /(\w+:\/\/(?:[^:\/\[\]\s]+|\[[0-9a-f:]+\])(?::\d+)?(?:\/[^\/\s]*)*)/ig;
const LINK_PLACEHOLDER = '\ueeee';
const LINK_PLACEHOLDER_RE = /\ueeee/g;
const TYPE_CHAT = { const TYPE_CHAT = {
msg: "string", msg: "string",
meta: "object,optional" meta: "object,optional"
@ -304,34 +307,31 @@ ChatModule.prototype.formatMessage = function (username, data) {
return obj; return obj;
}; };
const link = /(\w+:\/\/(?:[^:\/\[\]\s]+|\[[0-9a-f:]+\])(?::\d+)?(?:\/[^\/\s]*)*)/ig;
ChatModule.prototype.filterMessage = function (msg) { ChatModule.prototype.filterMessage = function (msg) {
var filters = this.channel.modules.filters.filters; var filters = this.channel.modules.filters.filters;
var chan = this.channel; var chan = this.channel;
var parts = msg.split(link);
var convertLinks = this.channel.modules.options.get("enable_link_regex"); var convertLinks = this.channel.modules.options.get("enable_link_regex");
var links = msg.match(LINK);
var intermediate = msg.replace(LINK, LINK_PLACEHOLDER);
for (var j = 0; j < parts.length; j++) { var result = filters.filter(intermediate, false);
/* substring is a URL */ result = result.replace(LINK_PLACEHOLDER_RE, function () {
if (convertLinks && parts[j].match(link)) { var link = links.shift();
var original = parts[j]; if (!link) {
parts[j] = filters.filter(parts[j], true); return '';
/* no filters changed the URL, apply link filter */
if (parts[j] === original) {
parts[j] = url.format(url.parse(parts[j]));
parts[j] = parts[j].replace(link, "<a href=\"$1\" target=\"_blank\">$1</a>");
}
} else {
/* substring is not a URL */
parts[j] = filters.filter(parts[j], false);
} }
}
msg = parts.join(""); var filtered = filters.filter(link, true);
/* Anti-XSS */ if (filtered !== link) {
return XSS.sanitizeHTML(msg); return filtered;
} else if (convertLinks) {
return "<a href=\"" + link + "\" target=\"_blank\">" + link + "</a>";
} else {
return link;
}
});
return XSS.sanitizeHTML(result);
}; };
ChatModule.prototype.sendModMessage = function (msg, minrank) { ChatModule.prototype.sendModMessage = function (msg, minrank) {