Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sanitize-html

Package Overview
Dependencies
Maintainers
13
Versions
114
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sanitize-html - npm Package Compare versions

Comparing version 1.7.0 to 1.7.1

78

index.js
var htmlparser = require('htmlparser2');
var _ = require('lodash');
var extend = require('xtend');
var quoteRegexp = require('regexp-quote');
require('array-includes').shim(); // Array.prototype.includes polyfill
function each(obj, cb) {
obj && Object.keys(obj).forEach(function (key) {
cb(obj[key], key)
})
}
module.exports = sanitizeHtml;

@@ -32,3 +39,3 @@

} else {
_.defaults(options, sanitizeHtml.defaults);
options = extend(sanitizeHtml.defaults, options);
}

@@ -38,17 +45,3 @@ // Tags that contain something other than HTML. If we are not allowing

// drop the tag but keep its content.
var nonTextTagsMap = {
script: true,
style: true
};
var allowedTagsMap;
if(options.allowedTags) {
allowedTagsMap = {};
_.each(options.allowedTags, function(tag) {
allowedTagsMap[tag] = true;
});
}
var selfClosingMap = {};
_.each(options.selfClosing, function(tag) {
selfClosingMap[tag] = true;
});
var nonTextTagsArray = [ 'script', 'style' ];
var allowedAttributesMap;

@@ -59,10 +52,10 @@ var allowedAttributesGlobMap;

allowedAttributesGlobMap = {};
_.each(options.allowedAttributes, function(attributes, tag) {
allowedAttributesMap[tag] = {};
each(options.allowedAttributes, function(attributes, tag) {
allowedAttributesMap[tag] = [];
var globRegex = [];
_.each(attributes, function(name) {
attributes.forEach(function(name) {
if(name.indexOf('*') >= 0) {
globRegex.push(quoteRegexp(name).replace(/\\\*/g, '.*'));
} else {
allowedAttributesMap[tag][name] = true;
allowedAttributesMap[tag].push(name);
}

@@ -74,19 +67,16 @@ });

var allowedClassesMap = {};
_.each(options.allowedClasses, function(classes, tag) {
each(options.allowedClasses, function(classes, tag) {
// Implicitly allows the class attribute
if(allowedAttributesMap) {
if (!allowedAttributesMap[tag]) {
allowedAttributesMap[tag] = {};
allowedAttributesMap[tag] = [];
}
allowedAttributesMap[tag]['class'] = true;
allowedAttributesMap[tag].push('class');
}
allowedClassesMap[tag] = {};
_.each(classes, function(name) {
allowedClassesMap[tag][name] = true;
});
allowedClassesMap[tag] = classes;
});
var transformTagsMap = {};
_.each(options.transformTags, function(transform, tag){
each(options.transformTags, function(transform, tag){
if (typeof transform === 'function') {

@@ -110,3 +100,3 @@ transformTagsMap[tag] = transform;

var skip = false;
if (_.has(transformTagsMap, name)) {
if (transformTagsMap[name]) {
var transformedTag = transformTagsMap[name](name, attribs);

@@ -121,5 +111,5 @@

if (allowedTagsMap && !_.has(allowedTagsMap, name)) {
if (options.allowedTags && !options.allowedTags.includes(name)) {
skip = true;
if (_.has(nonTextTagsMap, name)) {
if (nonTextTagsArray.includes(name)) {
skipText = true;

@@ -135,6 +125,6 @@ }

result += '<' + name;
if (!allowedAttributesMap || _.has(allowedAttributesMap, name)) {
_.each(attribs, function(value, a) {
if (!allowedAttributesMap || _.has(allowedAttributesMap[name], a) || (_.has(allowedAttributesGlobMap, name) &&
allowedAttributesGlobMap[name].test(a))) {
if (!allowedAttributesMap || allowedAttributesMap[name]) {
each(attribs, function(value, a) {
if (!allowedAttributesMap || allowedAttributesMap[name].includes(a) ||
(allowedAttributesGlobMap[name] && allowedAttributesGlobMap[name].test(a))) {
if ((a === 'href') || (a === 'src')) {

@@ -162,3 +152,3 @@ if (naughtyHref(name, value)) {

}
if (_.has(selfClosingMap, name)) {
if (options.selfClosing.includes(name)) {
result += " />";

@@ -174,3 +164,3 @@ } else {

var tag = stack[stack.length-1] && stack[stack.length-1].tag;
if (_.has(nonTextTagsMap, tag)) {
if (nonTextTagsArray.includes(tag)) {
result += text;

@@ -216,3 +206,3 @@ } else {

if (_.has(selfClosingMap, name)) {
if (options.selfClosing.includes(name)) {
// Already output />

@@ -255,7 +245,7 @@ return;

if (_.has(options.allowedSchemesByTag, name)) {
return !_.contains(options.allowedSchemesByTag[name], scheme);
if (options.allowedSchemesByTag[name]) {
return !options.allowedSchemesByTag[name].includes(scheme);
}
return !_.contains(options.allowedSchemes, scheme);
return !options.allowedSchemes || !options.allowedSchemes.includes(scheme);
}

@@ -269,4 +259,4 @@

classes = classes.split(/\s+/);
return _.filter(classes, function(c) {
return _.has(allowed, c);
return classes.filter(function(clss) {
return allowed.includes(clss);
}).join(' ');

@@ -273,0 +263,0 @@ }

{
"name": "sanitize-html",
"version": "1.7.0",
"version": "1.7.1",
"description": "Clean up user-submitted HTML, preserving whitelisted elements and whitelisted attributes on a per-element basis",

@@ -24,6 +24,7 @@ "main": "index.js",

"dependencies": {
"array-includes": "^2.0.0",
"htmlparser2": "3.8.x",
"lodash": "2.4.x",
"regexp-quote": "0.0.0"
"regexp-quote": "0.0.0",
"xtend": "^4.0.0"
}
}

@@ -227,2 +227,4 @@ # sanitize-html

1.7.1: removed lodash dependency, adding lighter dependencies and polyfills in its place. Thanks to Joseph Dykstra.
1.7.0: introduced `allowedSchemesByTag` option. Thanks to Cameron Will.

@@ -229,0 +231,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc