Skip to content

Commit

Permalink
fix: XSS vulnerability in How to get started tutorial JavaScript
Browse files Browse the repository at this point in the history
closes #82
  • Loading branch information
bryanbierce authored and gr2m committed Apr 30, 2016
1 parent 46f42e7 commit a033c21
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions www/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@
// initialize Hoodie
var hoodie = new Hoodie();

// Map of input characters to escape
var entityMap = {
"&": "&",
"<": "&lt;",
">": "&gt;",
'"': '&quot;',
"'": '&#39;',
"/": '&#x2F;'
};

// Escape HTML characters from input
function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}

// Todos Collection/View
function Todos($element) {
var collection = [];
Expand All @@ -23,7 +40,8 @@ function Todos($element) {
// Handle updating of an "inline edited" todo.
$el.on('keypress', 'input[type=text]', function(event) {
if (event.keyCode === 13) {
hoodie.store.update('todo', $(this).parent().data('id'), {title: event.target.value});
var escapedTitle = escapeHtml(event.target.value);
hoodie.store.update('todo', $(this).parent().data('id'), {title: escapedTitle});
}
});

Expand Down Expand Up @@ -88,12 +106,12 @@ hoodie.store.on('todo:remove', todos.remove);
// clear todos when user logs out,
hoodie.account.on('signout', todos.clear);


// handle creating a new task
$('#todoinput').on('keypress', function(event) {
// ENTER & non-empty.
if (event.keyCode === 13 && event.target.value.length) {
hoodie.store.add('todo', {title: event.target.value});
var escapedTitle = escapeHtml(event.target.value);
hoodie.store.add('todo', {title: escapedTitle});
event.target.value = '';
}
});

0 comments on commit a033c21

Please sign in to comment.