forked from askhogan/express-sanitized
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from CommercialTribe/feature/unescape-ampersands
Add unescape ampersand characters to sanitizer and unit tests
- Loading branch information
Showing
2 changed files
with
61 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
const assert = require("assert"); | ||
const expressSanitized = require('../lib/express-sanitized'); | ||
|
||
function next(){} | ||
|
||
describe('expressSanitized', function() { | ||
|
||
it('should sanitize empty', function() { | ||
testSanitizer('', '', next); | ||
}); | ||
|
||
it('should sanitize simple text', function() { | ||
testSanitizer('hello world', 'hello world', next); | ||
}); | ||
|
||
it('should sanitize entities', function() { | ||
testSanitizer('<hello world>&', '<hello world>&', next); | ||
}); | ||
|
||
it('should sanitize more entities', function() { | ||
testSanitizer('&&&&', '&&&&', next); | ||
}); | ||
|
||
it('should remove unknown tags', function() { | ||
testSanitizer('<u:y><b>hello <bogus><i>world</i></bogus></b>', '<b>hello <i>world</i></b>', next); | ||
}); | ||
|
||
it('should remove unsafe tags', function() { | ||
testSanitizer('<b>hello <i>world</i><script src=foo.js></script></b>', '<b>hello <i>world</i></b>', next); | ||
}); | ||
|
||
it('should remove unsafe attributes', function() { | ||
testSanitizer('<b>hello <i onclick="takeOverWorld(this)">world</i></b>', '<b>hello <i>world</i></b>', next); | ||
}); | ||
|
||
it('should escape cruft', function() { | ||
testSanitizer('<b>hello <i>world<</i></b> & tomorrow the universe', '<b>hello <i>world<</i></b> & tomorrow the universe', next); | ||
}); | ||
}); | ||
|
||
function testSanitizer(value, expectedValue, next) { | ||
const req = { | ||
body: { | ||
data: value | ||
}, | ||
query: { | ||
variables: { | ||
field: value | ||
} | ||
} | ||
}; | ||
expressSanitized()(req, '', next); | ||
assert.equal(req.query.variables.field, expectedValue); | ||
assert.equal(req.body.data, expectedValue); | ||
} |