Skip to content

Commit

Permalink
fix escape filter #835
Browse files Browse the repository at this point in the history
  • Loading branch information
devoidfury authored and vecmezoni committed Sep 7, 2016
1 parent 969c194 commit 038ba1e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
2.4.3 (Sep 7 2016)
----------------

* Fix potential cast-related XSS vulnerability in autoescape mode.
* Fix potential cast-related XSS vulnerability in autoescape mode, and with `escape` filter.
[#836](https://github.com/mozilla/nunjucks/pull/836)

2.4.2 (Apr 15 2016)
Expand Down
6 changes: 3 additions & 3 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ var filters = {
},

escape: function(str) {
if(typeof str === 'string') {
return r.markSafe(lib.escape(str));
if(str instanceof r.SafeString) {
return str;
}
return str;
return r.markSafe(lib.escape(str.toString()));
},

safe: function(str) {
Expand Down
9 changes: 9 additions & 0 deletions tests/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@
finish(done);
});

it('should work with non-string values', function(done) {
var res1 = render('{{ foo | escape }}', {foo: ['<html>']}, { autoescape: false });
expect(res1).to.be('&lt;html&gt;');

var res2 = render('{{ foo | escape }}', {foo: {toString: function() { return '<html>'; }}}, { autoescape: false });
expect(res2).to.be('&lt;html&gt;');
finish(done);
});

it('should not escape safe strings with autoescape on', function(done) {
var res1 = render('{{ "<html>" | safe | escape }}', {}, { autoescape: true });
expect(res1).to.be('<html>');
Expand Down

0 comments on commit 038ba1e

Please sign in to comment.