const assert = require('assert');
const sinon = require('sinon');
describe('sanitizeHtml', function() {
let sanitizeHtml;
it('should be successfully initialized', function() {
sanitizeHtml = require('../index.js');
});
it('should escape self closing tags', () => {
assert.equal(sanitizeHtml('before
after', {
disallowedTagsMode: 'escape',
allowedTags: [],
allowedAttributes: false
}), 'before <img src="test.png" /> after');
});
it('should handle numbers as strings', () => {
assert.equal(sanitizeHtml(5, {
allowedTags: [ 'b', 'em', 'i', 's', 'small', 'strong', 'sub', 'sup', 'time', 'u' ],
allowedAttributes: {},
disallowedTagsMode: 'recursiveEscape'
}), '5');
});
it('should pass through simple, well-formed markup', function() {
assert.equal(sanitizeHtml('
Hello there
Hello there
Hello there
paragraph after closing html
', { enforceHtmlBoundary: true } ), 'Hello there
Hello there
paragraph after closing html
', { enforceHtmlBoundary: false } ), 'Text before html tagHello there
paragraph after closing html
'); }); it('should pass through text outside html tag boundary since option is ON but html tag is not found', function() { assert.equal(sanitizeHtml('Text before div tagHello there
paragraph after closing div
', { enforceHtmlBoundary: true } ), 'Text before div tagHello there
paragraph after closing div
'); }); it('should pass through all markup if allowedTags and allowedAttributes are set to false', function() { assert.equal(sanitizeHtml('Whee!
'), 'Blah blah blahWhee!
'); }); it('should return an empty string when input is explicit "undefined"', function() { assert.equal(sanitizeHtml(undefined), ''); }); it('should return an empty string when input is explicit "null"', function() { assert.equal(sanitizeHtml(null), ''); }); it('should return an empty string when input is not provided', function() { assert.equal(sanitizeHtml(), ''); }); it('should return an empty string when input is an empty string', function() { assert.equal(sanitizeHtml(''), ''); }); it('should reject markup not allowlisted without destroying its text', function() { assert.equal(sanitizeHtml('
Whee
Again
Wowcool', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ])
}), '
Whee
Again
Wowcool
'); }); it('should reject hrefs that are not relative, ftp, http, https or mailto', function() { assert.equal(sanitizeHtml('googlehttps googleftpmailtorelativejavascript'), 'googlehttps googleftpmailtorelativejavascript'); }); it('should cope identically with capitalized attributes and tags and should tolerate capitalized schemes', function() { assert.equal(sanitizeHtml('googlehttps googleftpmailtorelativejavascript'), 'googlehttps googleftpmailtorelativejavascript'); }); it('should drop the content of script elements', function() { assert.equal(sanitizeHtml('Paragraph
'), 'Paragraph
'); }); it('should drop the content of style elements', function() { assert.equal(sanitizeHtml('Paragraph
'), 'Paragraph
'); }); it('should drop the content of textarea elements', function() { assert.equal(sanitizeHtml('Paragraph
'), 'Paragraph
'); }); it('should drop the content of option elements', function() { assert.equal(sanitizeHtml('Paragraph
'), 'Paragraph
'); }); it('should drop the content of textarea elements but keep the closing parent tag, when nested', function() { assert.equal(sanitizeHtml('Paragraph
'), 'Paragraph
'); }); it('should drop the content of disallowed xmp elements rather than re-emit it as live markup', function() { // Regression test for GHSA-rpr9-rxv7-x643: htmlparser2 parses the contents // ofParagraph
'), 'NiftyParagraph
'); }); it('should discard the content of fibble elements if specified for nonTextTags', function() { assert.equal(sanitizeHtml('Paragraph
', { nonTextTags: [ 'fibble' ] }), 'Paragraph
'); }); it('should retain allowed tags within a fibble element if fibble is not specified for nonTextTags', function() { assert.equal(sanitizeHtml('Paragraph
', {}), 'NiftyParagraph
'); }); it('should discard allowed tags within a fibble element if fibble is specified for nonTextTags', function() { assert.equal(sanitizeHtml('Paragraph
', { nonTextTags: [ 'fibble' ] }), 'Paragraph
'); }); it('should preserve textarea content if textareas are allowed', function() { assert.equal(sanitizeHtml('Paragraph
', { allowedTags: [ 'textarea', 'p' ] }), 'Paragraph
'); }); it('should preserve entities as such', function() { assert.equal(sanitizeHtml('<Kapow!>'), '<Kapow!>'); }); it('should dump closing tags which do not have any opening tags.', function() { assert.equal(sanitizeHtml('inner text 1
inner text 2
inner text 3
inner text 1
inner text 2
inner text 3
inner text 1
inner text 2
inner text 3
Whee
'), 'Whee
'); }); it('should dump a sneaky encoded javascript url', function() { assert.equal(sanitizeHtml('Hax'), 'Hax'); }); it('should dump an uppercase javascript url', function() { assert.equal(sanitizeHtml('Hax'), 'Hax'); }); it('should dump a javascript URL with a comment in the middle (probably only respected by browsers in XML data islands, but just in case someone enables those)', function() { assert.equal(sanitizeHtml('Hax'), 'Hax'); }); it('should not mess up a hashcode with a : in it', function() { assert.equal(sanitizeHtml('Hi'), 'Hi'); }); it('should dump character codes 1-32 before testing scheme', function() { assert.equal(sanitizeHtml('Hax'), 'Hax'); }); it('should dump character codes 1-32 even when escaped with padding rather than trailing ;', function() { // htmlparser2 10.x correctly decodes zero-padded numeric entities. // decodes to U+0001, which is stripped as a control char, // revealing the javascript: scheme assert.equal(sanitizeHtml('Hax'), 'Hax'); // decodes to U+FFFD (replacement character per HTML spec), // which is not a control char, so the URL is preserved safely since // browsers don't interpret java�script: as javascript: assert.equal(sanitizeHtml('Hax'), 'Hax'); }); it('should still like nice schemes', function() { assert.equal(sanitizeHtml('Hi'), 'Hi'); }); it('should still like nice relative URLs', function() { assert.equal(sanitizeHtml('Hi'), 'Hi'); }); it('should replace ol to ul', function() { assert.equal(sanitizeHtml('text before
text after
text before text after
'); }); it('should add new text when not initially set and replace attributes when they are changed by transforming function', function () { assert.equal(sanitizeHtml('', { transformTags: { a: function (tagName, attribs) { return { tagName, attribs, text: 'some new text' }; } } }), 'some new text'); }); it('should add new text to an empty tag when transforming function sets it and an identity textFilter is present', function () { assert.equal(sanitizeHtml('', { allowedTags: [ 'a' ], textFilter: function (text) { return text; }, transformTags: { a: function (tagName, attribs) { return { tagName, attribs, text: 'some new text' }; } } }), 'some new text'); }); it('should preserve text when initially set and replace attributes when they are changed by transforming function', function () { assert.equal(sanitizeHtml('some initial text', { transformTags: { a: function (tagName, attribs) { return { tagName, attribs }; } } }), 'some initial text'); }); it('should skip an empty link', function() { assert.strictEqual( sanitizeHtml('', { exclusiveFilter: function (frame) { return frame.tag === 'a' && !frame.text.trim(); } }), 'This is
Linux
12
3
124
' ); }); it('Should collapse nested empty elements', function() { assert.strictEqual( sanitizeHtml('', { exclusiveFilter: function(frame) { return (frame.tag === 'a' || frame.tag === 'p') && !frame.text.trim(); } }), '' ); }); it('Should find child media elements that are in allowedTags', function() { const markup = 'These links hack more hack have disallowed href protocols
', { exclusiveFilter: function (frame) { return frame.tag === 'a' && !frame.attribs.src ? 'excludeTag' : false; } }), 'These links hack more hack have disallowed href protocols
' ); }); it('should keep inner tags when exclusiveFilter returns "excludeTag"', function() { assert.strictEqual( sanitizeHtml('This div is badshould be kept as-is
should be kept as-is
.' ); }); it('should work with escaped tags when exclusiveFilter returns "excludeTag"', function () { assert.strictEqual( sanitizeHtml('hellowhee
', { allowedTags: [ 'p' ], allowedClasses: { p: [ 'nifty' ] } } ), 'whee
' ); }); it('should allow specific classes when allowlisted with allowedClasses for all tags', function() { assert.equal( sanitizeHtml( 'whee
', { allowedTags: [ 'p', 'div' ], allowedClasses: { '*': [ 'nifty' ] } } ), 'whee
' ); }); it('should allow all classes that are allowlisted for a single tag or all tags', function() { assert.equal( sanitizeHtml( 'whee
', { allowedTags: [ 'p', 'div' ], allowedClasses: { '*': [ 'simple' ], p: [ 'nifty' ], div: [ 'dippy' ] } } ), 'whee
' ); }); it('should allow classes that match wildcards for a single tag or all tags', function() { assert.equal( sanitizeHtml( 'whee
', { allowedTags: [ 'p' ], allowedClasses: { '*': [ 'dippy-*-simple' ], p: [ 'nifty-*' ] } } ), 'whee
' ); }); it('should allow all classes if `allowedClasses` contains a single `*`', function() { assert.equal( sanitizeHtml( 'whee
', { allowedTags: [ 'p' ], allowedClasses: { '*': [ '*' ] } } ), 'whee
' ); }); it('should allow all classes for a single tag if `allowedClasses` for the tag is false', function() { assert.equal( sanitizeHtml( 'whee
', { allowedTags: [ 'p' ], allowedClasses: { p: false } } ), 'whee
' ); }); it('should allow only classes that matches `allowedClasses` regex', function() { assert.equal( sanitizeHtml( 'whee
', { allowedTags: [ 'p' ], allowedClasses: { p: [ /^nifty\d{2}$/, /^d\w{4}$/ ] } } ), 'whee
' ); }); it('should allow classes that match `allowedClasses` regex for all tags', function() { assert.equal( sanitizeHtml( 'whee
', { allowedClasses: { '*': [ /^nifty\d{2}$/, /^d\w{4}$/ ] } } ), 'whee
' ); }); it('should allow defining schemes on a per-tag basis', function() { assert.equal( sanitizeHtml( // teeny-tiny valid transparent GIF in a data URL 'whee
', { allowedTags: [ 'p' ], allowedClasses: { p: [ 'nifty' ] } } ), 'whee
' ); }); it('should not crash on bad markup', function() { assert.equal( sanitizeHtml( '',
{
allowedTags: [ 'img' ],
allowedAttributes: {
img: [ 'src' ]
}
}
),
// This is weird but not dangerous. Without the " there
// would probably be some way to make it come out as a
// separate attribute
''
);
});
it('should deliver a warning if using vulnerable tags', function() {
const spy = sinon.spy(console, 'warn');
const message = 'Your `allowedTags` option includes `style`, which is ' +
'inherently vulnerable to XSS attacks. Please remove it from ' +
'`allowedTags`, or, to disable this warning, add the ' +
'`allowVulnerableTags` option and ensure you are accounting for this ' +
'risk.';
sanitizeHtml(
'',
{
allowedTags: [ 'style' ]
}
);
assert(spy.calledWith(message));
// Restore the spied-upon method
/* eslint-disable-next-line no-console */
console.warn.restore();
});
it('should deliver warnings to the logger option instead of the console', function() {
const spy = sinon.spy(console, 'warn');
const warnings = [];
sanitizeHtml(
'',
{
allowedTags: [ 'style' ],
logger: {
warn: (message) => warnings.push(message)
}
}
);
assert.equal(warnings.length, 1);
assert(warnings[0].includes('`style`'));
assert(spy.notCalled);
// Restore the spied-upon method
/* eslint-disable-next-line no-console */
console.warn.restore();
});
it('should not deliver a warning if using the allowVulnerableTags option', function() {
const spy = sinon.spy(console, 'warn');
sanitizeHtml(
'',
{
allowVulnerableTags: true,
allowedTags: [ 'style' ]
}
);
assert(spy.notCalled);
// Restore the spied-upon method
/* eslint-disable-next-line no-console */
console.warn.restore();
});
it('should allow only approved attributes, but to any tags, if tag is declared as "*"', function() {
assert.equal(
sanitizeHtml(
'
Text
', { allowedTags: [ 'p' ], allowedAttributes: { p: [ 'style' ] }, transformTags: { '*': function (tagName, attribs) { return { tagName, attribs: { style: 'text-align: center' } }; } } } ), 'Text
' ); }); it('should not be faked out by double <', function() { assert.equal( sanitizeHtml('<
', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ])
}),
'
'
);
});
it('should accept srcset if allowed', function() {
assert.equal(
sanitizeHtml('
', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] }
}),
'
'
);
assert.equal(
sanitizeHtml('
', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] }
}),
'
'
);
});
it('should drop bogus srcset', function() {
assert.equal(
sanitizeHtml('
', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] }
}),
'
'
);
});
it('should accept srcset with urls containing commas', function() {
assert.equal(
sanitizeHtml('
', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] }
}),
'
'
);
});
it('should apply allowedSchemesByTag restrictions to srcset URLs', function() {
assert.equal(
sanitizeHtml('
', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] },
allowedSchemes: [ 'http', 'https' ],
allowedSchemesByTag: { img: [] }
}),
'bestetestestestestestsettestestest
'; const expected = '
- test
- test
- test
- test
test
TESTETESTESTES
bestetestestestestestsettestestest
'; assert.equal( sanitizeHtml(sanitizeString, { allowedTags: false, allowedAttributes: { '*': [ 'dir' ], p: [ 'dir', 'style' ], li: [ 'style' ], span: [ 'style' ] }, allowedStyles: { '*': { // Matches hex color: [ /#(0x)?[0-9a-f]+/i ], 'text-align': [ /left/, /right/, /center/, /justify/, /initial/, /inherit/ ], 'font-size': [ /36px/ ] } } }).replace(/ /g, ''), expected.replace(/ /g, '') ); }); it('Should remove empty style tags', function() { assert.equal( sanitizeHtml('', { allowedTags: false, allowedAttributes: false }), '' ); }); it('Should remove invalid styles', function() { assert.equal( sanitizeHtml('', { allowedTags: false, allowedAttributes: { span: [ 'style' ] }, allowedStyles: { span: { color: [ /blue/ ], 'text-align': [ /left/ ] } } }), '' ); }); it('Should ignore styles when options.parseStyleAttributes is false', function() { assert.equal( sanitizeHtml('', { allowedTags: false, allowedAttributes: { span: [ 'style' ] }, parseStyleAttributes: false }), '' ); }); it('Should throw an error if both allowedStyles is set and && parseStyleAttributes is set to false', function() { try { sanitizeHtml('', { allowedTags: false, allowedAttributes: { span: [ 'style' ] }, allowedStyles: { p: { 'text-align': [ /^justify$/ ] } }, parseStyleAttributes: false }); assert(false); } catch (err) { assert.equal(err.message, 'allowedStyles option cannot be used together with parseStyleAttributes: false.'); } }); it('Should support !important styles', function() { assert.equal( sanitizeHtml('', { allowedTags: false, allowedAttributes: { span: [ 'style' ] }, allowedStyles: { span: { color: [ /blue/ ] } } }), '' ); }); it('Should allow a specific style from global', function() { assert.equal( sanitizeHtml('', { allowedTags: false, allowedAttributes: { span: [ 'style' ] }, allowedStyles: { '*': { color: [ /yellow/ ], 'text-align': [ /center/ ] }, span: { color: [ /green/ ], 'font-family': [ /helvetica/ ] } } }), '' ); }); it('Should merge tag-specific and global styles without giving a __proto__ key special meaning', function() { // JSON.parse creates an own "__proto__" property, as configuration loaded // from a file would const allowedStyles = JSON.parse(`{ "*": { "color": [], "__proto__": [ "inherited" ] }, "span": { "__proto__": [ "inherited" ], "font-family": [] } }`); allowedStyles['*'].color.push(/yellow/); allowedStyles.span['font-family'].push(/helvetica/); assert.equal( sanitizeHtml('', { allowedTags: false, allowedAttributes: { span: [ 'style' ] }, allowedStyles }), '' ); assert.equal(({}).length, undefined); }); it('should delete the script tag', function() { assert.equal(sanitizeHtml('', { allowedTags: [ 'script' ], allowVulnerableTags: true, allowedAttributes: { script: [ 'src' ] }, allowedScriptHostnames: [ 'www.authorized.com' ] }), ''); }); it('should delete the script tag since src is not a valid URL', function() { assert.equal(sanitizeHtml('', { allowedTags: [ 'script' ], allowVulnerableTags: true, allowedAttributes: { script: [ 'src' ] }, allowedScriptHostnames: [ 'www.unauthorized.com' ] }), ''); }); it('Should allow domains in a script that are in allowedScriptDomains', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'script' ], allowedAttributes: { script: [ 'src' ] }, allowedScriptDomains: [ 'authorized.com' ] }), '' ); }); it('should delete the script tag content', function() { assert.equal(sanitizeHtml('', { allowedTags: [ 'script' ], allowVulnerableTags: true, allowedAttributes: { script: [ 'src' ] }, allowedScriptHostnames: [ 'www.authorized.com' ] }), ''); }); it('should delete the script tag content from script tags with no src when allowedScriptHostnames is present', function() { assert.equal(sanitizeHtml('', { allowedTags: [ 'script' ], allowVulnerableTags: true, allowedAttributes: { script: [ 'src' ] }, allowedScriptHostnames: [ 'www.authorized.com' ] }), ''); }); it('should delete the script tag content from script tags with no src when allowedScriptDomains is present', function() { assert.equal(sanitizeHtml('', { allowedTags: [ 'script' ], allowVulnerableTags: true, allowedAttributes: { script: [ 'src' ] }, allowedScriptDomains: [ 'www.authorized.com' ] }), ''); }); it('Should allow hostnames in a script that are in allowedScriptHostnames', function() { assert.equal(sanitizeHtml('', { allowedTags: [ 'script' ], allowVulnerableTags: true, allowedAttributes: { script: [ 'src' ] }, allowedScriptHostnames: [ 'www.authorized.com' ] }), ''); }); it('Should allow hostnames in an iframe that are in allowedIframeHostnames', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ] }), '' ); }); it('Should remove iframe src urls that are not included in allowedIframeHostnames', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ] }), '' ); }); it('Should not allow iframe urls that do not have proper hostname', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ] }), '' ); }); it('Should allow iframe through if no hostname option is set', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] } }), '' ); }); it('Should allow domains in an iframe that are in allowedIframeDomains', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeDomains: [ 'zoom.us' ] }), '' ); }); it('Should allow second-level domains in an iframe that are in allowedIframeDomains', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeDomains: [ 'zoom.us' ] }), '' ); }); it('Should remove iframe src urls that are not included in allowedIframeDomains', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeDomains: [ 'vimeo.com' ] }), '' ); }); it('Should remove iframe src urls with host that ends as allowed domains but not preceded with a dot', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeDomains: [ 'zoom.us' ] }), '' ); }); it('Should allow hostnames in an iframe that are in allowedIframeHostnames and are not in allowedIframeDomains', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ], allowedIframeDomains: [ 'zoom.us' ] }), '' ); }); it('Should allow hostnames in an iframe that are not in allowedIframeHostnames ' + 'and are allowlisted in allowedIframeDomains', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ], allowedIframeDomains: [ 'zoom.us' ] }), '' ); }); it('Should allow relative URLs for iframes by default', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] } }), '' ); }); it('Should allow relative URLs for iframes', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowIframeRelativeUrls: true }), '' ); }); it('Should remove relative URLs for iframes', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowIframeRelativeUrls: false }), '' ); }); it('Should remove relative URLs for iframes when other hostnames are specified in allowedIframeHostnames', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeHostnames: [ 'www.youtube.com' ] }), '' ); }); it('Should allow relative and allowlisted hostname URLs for iframes', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowIframeRelativeUrls: true, allowedIframeHostnames: [ 'www.youtube.com' ] }), '' ); }); it('Should allow protocol-relative URLs for the right domain for iframes', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ] }), '' ); }); it('Should not allow protocol-relative iframe urls that do not have proper hostname', function() { assert.equal( sanitizeHtml('', { allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ], allowedAttributes: { iframe: [ 'src', 'href' ], a: [ 'src', 'href' ], img: [ 'src' ] }, allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ] }), '' ); }); it('Should only allow attributes to have any combination of specific values', function() { assert.equal( sanitizeHtml('', { allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'iframe' ]), allowedAttributes: { iframe: [ { name: 'sandbox', multiple: true, values: [ 'allow-popups', 'allow-same-origin', 'allow-scripts' ] }, 'allowfullscreen' ] } }), ''); }); it('Should only allow attributes that match a specific value', function() { assert.equal( sanitizeHtml('', { allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'iframe' ]), allowedAttributes: { iframe: [ { name: 'sandbox', multiple: false, values: [ 'allow-popups', 'allow-same-origin', 'allow-scripts' ] } ] } }), ''); } ); it('Should not allow cite urls that do not have an allowed scheme', function() { assert.equal( sanitizeHtml('
- test
- test
- test
- test
test
TESTETESTESTES
HTTP
HTTPS
MAILTO
TEL
ms-calculator
FTP
DATA
LDAP
ACROBAT
VBSCRIPT
FILE
RLOGIN
WEBCAL
JAVASCRIPT
MMS', { allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'q' ]), allowedAttributes: { q: [ 'cite' ] }, allowedSchemes: sanitizeHtml.defaults.allowedSchemes.concat([ 'tel' ]) }), '
HTTP
HTTPS
MAILTO
TEL
ms-calculator
FTP
DATA
LDAP
ACROBAT
VBSCRIPT
FILE
RLOGIN
WEBCAL
JAVASCRIPT
MMS'); }); it('Should encode &, <, > and where necessary, "', function() { assert.equal(sanitizeHtml('"< & >" cool', { allowedTags: [ 'span' ], allowedAttributes: { span: [ 'class' ] } }), '"< & >" cool'); }); it('Should not pass through &0; unescaped if decodeEntities is true (the default)', function() { assert.equal(sanitizeHtml('
World
World
World
</wiggly>World
World
World
<tiggly>JS</tiggly></wiggly>