Skip to content

Commit cb4baed

Browse files
committed
Merge pull request #11 from laszlof/master
Fix XSS vulnerability
2 parents 9053e5a + 9819f71 commit cb4baed

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

jquery.linkify.js

+31-30
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,33 @@ function linkify(string, buildHashtagUrl, includeW3, target, noFollow) {
33
if (noFollow) {
44
relNoFollow = " rel=\"nofollow\"";
55
}
6-
7-
string = string.replace(/((http|https|ftp)\:\/\/|\bw{3}\.)[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z\u00C0-\u017F0-9\-\._\?\,\'\/\\\+&%\$#\=~])*/gi, function(captured) {
8-
var uri;
9-
if (captured.toLowerCase().indexOf("www.") == 0) {
10-
if (!includeW3) {
11-
return captured;
12-
}
13-
uri = "http://" + captured;
14-
} else {
15-
uri = captured;
16-
}
17-
return "<a href=\"" + uri+ "\" target=\"" + target + "\"" + relNoFollow + ">" + captured + "</a>";
18-
});
19-
6+
7+
if (string.toLowerCase().indexOf("www.") === 0 && includeW3) {
8+
string = '<a href="http://' + string + '" target="' + target + '"' + relNoFollow + '>' + string + '</a>';
9+
} else {
10+
string = '<a href="' + string + '" target="' + target + '"' + relNoFollow + '>' + string + '</a>';
11+
}
12+
2013
if (buildHashtagUrl) {
21-
string = string.replace(/\B#(\w+)/g, "<a href=" + buildHashtagUrl("$1") +" target=\"" + target + "\"" + relNoFollow + ">#$1</a>");
14+
string = string.replace(/\B#(\w+)/g, '<a href=' + buildHashtagUrl("$1") + ' target="' + target + '"' + relNoFollow + '>#$1</a>');
2215
}
2316
return string;
2417
}
2518

2619
(function($) {
2720
$.fn.linkify = function(opts) {
2821
return this.each(function() {
29-
var $this = $(this);
3022
var buildHashtagUrl;
3123
var includeW3 = true;
3224
var target = '_self';
3325
var noFollow = true;
26+
var regex = /((http|https|ftp)\:\/\/|\bw{3}\.)[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z\u00C0-\u017F0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*/gi;
27+
var txt = this.innerHTML;
28+
var output = '';
29+
var replacement;
30+
var matchLen;
31+
var lastIndex = 0;
32+
3433
if (opts) {
3534
if (typeof opts == "function") {
3635
buildHashtagUrl = opts;
@@ -49,18 +48,20 @@ function linkify(string, buildHashtagUrl, includeW3, target, noFollow) {
4948
}
5049
}
5150
}
52-
$this.html(
53-
$.map(
54-
$this.contents(),
55-
function(n, i) {
56-
if (n.nodeType == 3) {
57-
return linkify(n.data, buildHashtagUrl, includeW3, target, noFollow);
58-
} else {
59-
return n.outerHTML;
60-
}
61-
}
62-
).join("")
63-
);
51+
52+
while ((match = regex.exec(txt)) !== null) {
53+
matchLen = match[0].length;
54+
replacement = linkify(match[0], buildHashtagUrl, includeW3, target, noFollow);
55+
output += txt.substring(lastIndex, match.index + matchLen).replace(match[0], replacement);
56+
lastIndex = match.index + matchLen;
57+
}
58+
59+
// Include the rest of the text.
60+
if (lastIndex !== txt.length) {
61+
output += txt.substring(lastIndex);
62+
}
63+
64+
$(this).html(output);
6465
});
65-
}
66-
})(jQuery);
66+
};
67+
})(jQuery);

jquery.linkify.min.js

+1-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)