Skip to content

Commit

Permalink
Blacklist data:, script and other URLs from templates, incl whitespac…
Browse files Browse the repository at this point in the history
…e problems
  • Loading branch information
Dima Voytenko committed Dec 9, 2015
1 parent 5320bf1 commit 4b534d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const WHITELISTED_FORMAT_TAGS = [
const BLACKLISTED_ATTR_VALUES = [
/*eslint no-script-url: 0*/ 'javascript:',
/*eslint no-script-url: 0*/ 'vbscript:',
/*eslint no-script-url: 0*/ 'data:',
/*eslint no-script-url: 0*/ '<script',
/*eslint no-script-url: 0*/ '</script',
];


Expand Down Expand Up @@ -169,9 +172,9 @@ export function isValidAttr(attrName, attrValue) {
}

// No attributes with "javascript" or other blacklisted substrings in them.
const attrValueLowercase = attrValue.toLowerCase();
const attrValueNorm = attrValue.toLowerCase().replace(/[\s,\u0000]+/g, '');
for (let i = 0; i < BLACKLISTED_ATTR_VALUES.length; i++) {
if (attrValueLowercase.indexOf(BLACKLISTED_ATTR_VALUES[i]) != -1) {
if (attrValueNorm.indexOf(BLACKLISTED_ATTR_VALUES[i]) != -1) {
return false;
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/functional/test-sanitizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ describe('sanitizeHtml', () => {
'a<a>b</a>');
expect(sanitizeHtml('a<a href="VBSCRIPT:alert">b</a>')).to.be.equal(
'a<a>b</a>');
expect(sanitizeHtml('a<a href="data:alert">b</a>')).to.be.equal(
'a<a>b</a>');
expect(sanitizeHtml('a<a href="DATA:alert">b</a>')).to.be.equal(
'a<a>b</a>');
expect(sanitizeHtml('a<a href="<script">b</a>')).to.be.equal(
'a<a>b</a>');
expect(sanitizeHtml('a<a href="</script">b</a>')).to.be.equal(
'a<a>b</a>');
});

it('should catch attribute value whitespace variations', () => {
expect(sanitizeHtml('a<a href=" j\na\tv\ra s&#00;cript:alert">b</a>'))
.to.be.equal('a<a>b</a>');
});

it('should NOT output security-sensitive attributes', () => {
Expand Down

0 comments on commit 4b534d4

Please sign in to comment.