
Bereinigt nicht vertrauenswürdiges HTML (zur Verhinderung von XSS) mit einer durch eine Whitelist festgelegten Konfiguration

xss ist ein Modul, das verwendet wird, um Benutzereingaben zu filtern und XSS-Angriffe zu verhindern.
(Was ist ein XSS-Angriff?)
Projekt-Homepage: http://jsxss.com
Online ausprobieren: http://jsxss.com/en/try.html
xss() aus dem Modul [email protected]: 6,9 MB/sTestcode findest du im Verzeichnis benchmark.
npm install xss
bower install xss
Oder
bower install https://github.com/leizongmin/js-xss.git
var xss = require("xss");
var html = xss('<script>alert("xss");</script>');
console.log(html);
Sie können filterXSSWithResult verwenden, um sowohl das bereinigte HTML als auch eine Liste der entfernten Tags/Attribute zu erhalten:
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()" }
// ]
Shim-Modus (Referenzdatei 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>
AMD-Modus – 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>
Hinweis: Bitte verwenden Sie die URL https://rawgit.com/leizongmin/js-xss/master/dist/xss.js nicht in der Produktionsumgebung.
Sie können das xss-Kommandozeilen-Tool verwenden, um eine Datei zu verarbeiten. Verwendung:
xss -i <input_file> -o <output_file>
Beispiel:
xss -i origin.html -o target.html
Führen Sie den folgenden Befehl aus, dann können Sie HTML-Code in der Befehlszeile eingeben und die gefilterte Ausgabe überprüfen:
xss -t
Weitere Details erhalten Sie, wenn Sie $ xss -h ausführen.
Bei Verwendung der Funktion xss() kann der zweite Parameter verwendet werden, um
benutzerdefinierte Regeln anzugeben:
options = {}; // Custom rules
html = xss('<script>alert("xss");</script>', options);
Um nicht jedes Mal options übergeben zu müssen, kannst du es auch auf einem schnelleren Weg tun, indem du eine FilterXSS-Instanz erstellst:
options = {}; // Custom rules
myxss = new xss.FilterXSS(options);
// then apply myxss.process()
html = myxss.process('<script>alert("xss");</script>');
Details zu den Parametern in options werden im Folgenden beschrieben.
Durch Angabe eines whiteList, z. B. { 'tagName': [ 'attr-1', 'attr-2' ] }. Tags
und Attribute, die nicht in der Whitelist enthalten sind, werden herausgefiltert. Zum Beispiel:
// 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>
Für die Standard-Whitelist siehe xss.whiteList.
allowList wird ebenfalls unterstützt und hat dieselbe Funktion wie whiteList.
Durch Angabe der Handler-Funktion mit 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
}
Durch Angabe der Handler-Funktion mit 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
}

Open-Source-XDR- und SIEM-Plattform für Bedrohungserkennung, Protokollanalyse, Dateiintegritätsüberwachung, Schwachstellenbewertung und Compliance-Management für Endpunkte und Cloud-Workloads.

Open-Source-Netzwerk-IDS/IPS/NSM-Engine für Echtzeit-Datenverkehrsinspektion, Eindringungserkennung und -verhinderung, Protokollanalyse und regelbasiertes Threat Hunting.

Zeek ist ein leistungsstarkes Netzwerkanalyse-Framework, das sich stark von den typischen IDS unterscheidet, die Sie vielleicht kennen.