Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix XSS in select2 fields #193

Merged
merged 2 commits into from
Jun 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions app/assets/javascripts/openproject.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ window.OpenProject = (function ($) {
return (str+'').replace(REGEXP_ESCAPE, "\\$1");
};

/**
* Use select2's escapeMarkup function for correctly escaping
* text and preventing XSS.
*/
Helpers.markupEscape = (function(){
try {
var escapeMarkup = jQuery.fn.select2.defaults.escapeMarkup;
if(typeof escapeMarkup === "undefined") {
throw 'jQuery.fn.select2.defaults.escapeMarkup is undefined';
}
return escapeMarkup;
} catch (e){
console.log('Error: jQuery.fn.select2.defaults.escapeMarkup not found.\n' +
'Exception: ' + e.toString());
throw e;
}
}());

/**
* replace wrong with right in text
*
Expand Down Expand Up @@ -231,12 +249,13 @@ window.OpenProject = (function ($) {

// fallback to base behavior
if (result.matches === undefined) {
return replaceSpecialChars(format(result.text, query.term));
return replaceSpecialChars(
Helpers.markupEscape(format(result.text, query.term)));
}

// shortcut for empty searches
if (query.sterm.length === 0) {
return result.text;
return Helpers.markupEscape(result.text);
}

var matches = result.matches.slice(),
Expand All @@ -248,7 +267,7 @@ window.OpenProject = (function ($) {
text = Helpers.replace(text, match[0], format(match[0], match[1]));
}

return replaceSpecialChars(text);
return replaceSpecialChars(Helpers.markupEscape(text));
};
})();

Expand Down
16 changes: 10 additions & 6 deletions app/assets/javascripts/timelines_autocompleter.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,22 @@
markup = [];

if (match < 0) {
return "<span data-value='" + item.id + "'>" + item.name + "</span>";
return "<span data-value='" + item.id + "'>" +
OpenProject.Helpers.markupEscape(item.name) + "</span>";
}

markup.push(item.name.substring(0, match));
markup.push(OpenProject.Helpers.markupEscape(
item.name.substring(0, match)));
markup.push("<span class='select2-match' data-value='" + item.id + "'>");
markup.push(item.name.substring(match, match + tl));
markup.push(OpenProject.Helpers.markupEscape(
item.name.substring(match, match + tl)));
markup.push("</span>");
markup.push(item.name.substring(match + tl, item.name.length));
return markup.join("")
markup.push(OpenProject.Helpers.markupEscape(
item.name.substring(match + tl, item.name.length)));
return markup.join("");
},
formatSelection: function (item) {
return item.name;
return OpenProject.Helpers.markupEscape(item.name);
},
initSelection: function (element, callback) {
var data = [];
Expand Down