
Whitelist로 지정된 구성으로 신뢰할 수 없는 HTML을 정화합니다(XSS 방지).

xss는 XSS 공격을 방지하기 위해 사용자 입력을 필터링하는 데 사용되는 모듈입니다.
(XSS 공격이란 무엇인가요?)
프로젝트 홈페이지: http://jsxss.com
온라인에서 사용해 보기: http://jsxss.com/en/try.html
[email protected]의 xss() 함수: 6.9 MB/s테스트 코드는 benchmark 디렉터리를 참조하세요.
npm install xss
bower install xss
또는
bower install https://github.com/leizongmin/js-xss.git
var xss = require("xss");
var html = xss('<script>alert("xss");</script>');
console.log(html);
filterXSSWithResult를 사용하면 정제된 HTML과 제거된 태그/속성 목록을 모두 가져올 수 있습니다:
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 모드 (참조 파일 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 모드 - 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>
참고: 프로덕션 환경에서는 https://rawgit.com/leizongmin/js-xss/master/dist/xss.js URL을 사용하지 마십시오.
xss 명령줄 도구를 사용하여 파일을 처리할 수 있습니다. 사용법:
xss -i <input_file> -o <output_file>
예시:
xss -i origin.html -o target.html
다음 명령을 실행하면 명령줄에 HTML 코드를 입력할 수 있으며, 필터링된 출력을 확인할 수 있습니다:
xss -t
자세한 내용은 $ xss -h을 실행하여 확인하세요.
xss() 함수를 사용할 때 두 번째 매개변수를 사용하여
사용자 정의 규칙을 지정할 수 있습니다:
options = {}; // Custom rules
html = xss('<script>alert("xss");</script>', options);
매번 options를 전달하지 않으려면, FilterXSS 인스턴스를 생성하여 더 빠르게 처리할 수도 있습니다:
options = {}; // Custom rules
myxss = new xss.FilterXSS(options);
// then apply myxss.process()
html = myxss.process('<script>alert("xss");</script>');
options의 매개변수 세부 사항은 아래에서 설명합니다.
whiteList을 지정하면, 예를 들어 { 'tagName': [ 'attr-1', 'attr-2' ] }와 같이 지정할 수 있습니다. 화이트리스트에 없는 태그와 속성은 필터링되어 제거됩니다. 예를 들어:
// 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>
기본 화이트리스트는 xss.whiteList를 참조하세요.
allowList도 지원되며, whiteList와 동일한 기능을 합니다.
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
}
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
}
onIgnoreTag로 핸들러 함수를 지정하여:
function onIgnoreTag(tag, html, options) {
// Parameters are the same with onTag
// If a string is returned, the tag would be replaced with the string
// If return nothing, the default measure would be taken (specifies using
// escape, as described below)
}
onIgnoreTagAttr로 핸들러 함수를 지정하여:
function onIgnoreTagAttr(tag, name, value, isWhiteAttr) {
// Parameters are the same with onTagAttr
// If a string is returned, the value would be replaced with this string
// If return nothing, then keep default (remove the attribute)
}
escapeHtml로 핸들러 함수를 지정하여 사용자 정의할 수 있습니다. 다음은 기본
함수입니다 (수정은 권장하지 않음):
function escapeHtml(html) {
return html.replace(/</g, "<").replace(/>/g, ">");
}
기본 safeAttrValue는 사용자 지정 화이트리스트에 의해 해당 속성이 허용될 때 action 및 formaction도 검사합니다. 폼 대상은 HTTP(S) 또는 상대 URL(경로, 쿼리, 프래그먼트 포함)을 사용할 수 있으며, javascript: 및 data:를 포함한 다른 스킴은 거부됩니다. href 및 src에 대한 data:image/ 예외는 폼 대상에는 적용되지 않습니다.
safeAttrValue로 핸들러 함수를 지정하여: