
Sanitiza HTML no confiable (para prevenir XSS) con una configuración especificada por una Whitelist

xss es un módulo utilizado para filtrar la entrada de los usuarios y prevenir ataques XSS.
(¿Qué es un ataque XSS?)
Página principal del proyecto: http://jsxss.com
Pruébalo en línea: http://jsxss.com/en/try.html
xss() del módulo [email protected]: 6.9 MB/sPara el código de prueba, consulte el directorio benchmark.
npm install xss
bower install xss
O
bower install https://github.com/leizongmin/js-xss.git
var xss = require("xss");
var html = xss('<script>alert("xss");</script>');
console.log(html);
Puedes usar filterXSSWithResult para obtener tanto el HTML saneado como una lista de etiquetas/atributos eliminados:
var xss = require("xss");
var result = xss.filterXSSWithResult('<script>alert("xss");</script><a href="#" onclick="evil()">click</a>');
console.log(result.html);
// Output: "<script>alert(\"xss\");</script><a href=\"#\">click</a>"
console.log(result.removed);
// Output: [
// { type: "tag", tag: "script", html: "<script>", isClosing: false },
// { type: "tag", tag: "script", html: "</script>", isClosing: true },
// { type: "attr", tag: "a", attr: "onclick", value: "evil()" }
// ]
Modo Shim (archivo de referencia test/test.html):
<script src="https://rawgit.com/leizongmin/js-xss/master/dist/xss.js"></script>
<script>
// apply function filterXSS in the same way
var html = filterXSS('<script>alert("xss");</scr' + "ipt>");
alert(html);
</script>
Modo AMD - shim:
<script>
require.config({
baseUrl: "./",
paths: {
xss: "https://rawgit.com/leizongmin/js-xss/master/dist/xss.js",
},
shim: {
xss: { exports: "filterXSS" },
},
});
require(["xss"], function (xss) {
var html = xss('<script>alert("xss");</scr' + "ipt>");
alert(html);
});
</script>
Nota: no utilice la URL https://rawgit.com/leizongmin/js-xss/master/dist/xss.js en un entorno de producción.
Puede usar la herramienta de línea de comandos xss para procesar un archivo. Uso:
xss -i <input_file> -o <output_file>
Ejemplo:
xss -i origin.html -o target.html
Ejecute el siguiente comando, luego puede escribir código HTML en la línea de comandos y comprobar la salida filtrada:
xss -t
Para obtener más detalles, ejecute $ xss -h para verlo.
Al usar la función xss(), el segundo parámetro puede utilizarse para especificar
reglas personalizadas:
options = {}; // Custom rules
html = xss('<script>alert("xss");</script>', options);
Para evitar pasar options cada vez, también puedes hacerlo de una forma más rápida
creando una instancia de FilterXSS:
options = {}; // Custom rules
myxss = new xss.FilterXSS(options);
// then apply myxss.process()
html = myxss.process('<script>alert("xss");</script>');
A continuación se describen los detalles de los parámetros en options.
Al especificar un whiteList, por ejemplo { 'tagName': [ 'attr-1', 'attr-2' ] }. Las etiquetas
y los atributos que no estén en la lista blanca se filtrarán. Por ejemplo:
// only tag a and its attributes href, title, target are allowed
var options = {
whiteList: {
a: ["href", "title", "target"],
},
};
// With the configuration specified above, the following HTML:
// <a href="#" onclick="hello()"><i>Hello</i></a>
// would become:
// <a href="#"><i>Hello</i></a>
Para la lista blanca predeterminada, consulte xss.whiteList.
También se admite allowList, y tiene la misma función que whiteList.
Especificando la función de controlador con onTag:
function onTag(tag, html, options) {
// tag is the name of current tag, e.g. 'a' for tag <a>
// html is the HTML of this tag, e.g. '<a>' for tag <a>
// options is some addition informations:
// isWhite boolean, whether the tag is in whitelist
// isClosing boolean, whether the tag is a closing tag, e.g. true for </a>
// position integer, the position of the tag in output result
// sourcePosition integer, the position of the tag in input HTML source
// If a string is returned, the current tag would be replaced with the string
// If return nothing, the default measure would be taken:
// If in whitelist: filter attributes using onTagAttr, as described below
// If not in whitelist: handle by onIgnoreTag, as described below
}
Al especificar la función de controlador con onTagAttr:
function onTagAttr(tag, name, value, isWhiteAttr) {
// tag is the name of current tag, e.g. 'a' for tag <a>
// name is the name of current attribute, e.g. 'href' for href="#"
// isWhiteAttr whether the attribute is in whitelist
// If a string is returned, the attribute would be replaced with the string
// If return nothing, the default measure would be taken:
// If in whitelist: filter the value using safeAttrValue as described below
// If not in whitelist: handle by onIgnoreTagAttr, as described below
}
Especificando la función de controlador con onIgnoreTag:

Plataforma XDR y SIEM de código abierto para detección de amenazas, análisis de registros, monitoreo de integridad de archivos, evaluación de vulnerabilidades y gestión de cumplimiento normativo en endpoints y cargas de trabajo en la nube.

Motor de red IDS/IPS/NSM de código abierto para inspección de tráfico en tiempo real, detección y prevención de intrusiones, análisis de protocolos y búsqueda de amenazas basada en reglas.

Zeek es un potente marco de análisis de redes que es muy diferente del típico IDS que puedas conocer.