XSS CODE
跨站脚本攻击
This commit is contained in:
26
example/allows_attr_prefix.js
Normal file
26
example/allows_attr_prefix.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* 应用实例:允许标签以data-开头的属性
|
||||
*
|
||||
* @author 老雷<leizongmin@gmail.com>
|
||||
*/
|
||||
|
||||
var xss = require('../');
|
||||
|
||||
var source = '<div a="1" b="2" data-a="3" data-b="4">hello</div>';
|
||||
var html = xss(source, {
|
||||
onIgnoreTagAttr: function (tag, name, value, isWhiteAttr) {
|
||||
if (name.substr(0, 5) === 'data-') {
|
||||
// 通过内置的escapeAttrValue函数来对属性值进行转义
|
||||
return name + '="' + xss.escapeAttrValue(value) + '"';
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
console.log('%s\nconvert to:\n%s', source, html);
|
||||
|
||||
/*
|
||||
运行结果:
|
||||
<div a="1" b="2" data-a="3" data-b="4">hello</div>
|
||||
convert to:
|
||||
<div data-a="3" data-b="4">hello</div>
|
||||
*/
|
||||
26
example/allows_tag_prefix.js
Normal file
26
example/allows_tag_prefix.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* 应用实例:允许名称以x-开头的标签
|
||||
*
|
||||
* @author 老雷<leizongmin@gmail.com>
|
||||
*/
|
||||
|
||||
var xss = require('../');
|
||||
|
||||
var source = '<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>';
|
||||
var html = xss(source, {
|
||||
onIgnoreTag: function (tag, html, options) {
|
||||
if (tag.substr(0, 2) === 'x-') {
|
||||
// 不对其属性列表进行过滤
|
||||
return html;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
console.log('%s\nconvert to:\n%s', source, html);
|
||||
|
||||
/*
|
||||
运行结果:
|
||||
<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>
|
||||
convert to:
|
||||
<x><x-1>he<x-2 checked></x-2>wwww</x-1><a>
|
||||
*/
|
||||
27
example/analyse_img_list.js
Normal file
27
example/analyse_img_list.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* 应用实例:分析HTML代码中的图片列表
|
||||
*
|
||||
* @author 老雷<leizongmin@gmail.com>
|
||||
*/
|
||||
|
||||
var xss = require('../');
|
||||
|
||||
var source = '<img src="img1">a<img src="img2">b<img src="img3">c<img src="img4">d';
|
||||
var list = [];
|
||||
var html = xss(source, {
|
||||
onTagAttr: function (tag, name, value, isWhiteAttr) {
|
||||
if (tag === 'img' && name === 'src') {
|
||||
// 使用内置的friendlyAttrValue函数来对属性值进行转义,可将<这类的实体标记转换成打印字符<
|
||||
list.push(xss.friendlyAttrValue(value));
|
||||
}
|
||||
// 不返回任何值,表示还是按照默认的方法处理
|
||||
}
|
||||
});
|
||||
|
||||
console.log('image list:\n%s', list.join(', '));
|
||||
|
||||
/*
|
||||
运行结果:
|
||||
image list:
|
||||
img1, img2, img3, img4
|
||||
*/
|
||||
21
example/strip_tag.js
Normal file
21
example/strip_tag.js
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* 应用实例:去除HTML标签(只保留文本内容)
|
||||
*
|
||||
* @author 老雷<leizongmin@gmail.com>
|
||||
*/
|
||||
|
||||
var xss = require('../');
|
||||
|
||||
var source = '<strong>hello</strong><script>alert(/xss/);</script>end';
|
||||
var html = xss(source, {
|
||||
whiteList: [], // 白名单为空,表示过滤所有标签
|
||||
stripIgnoreTag: true, // 过滤所有非白名单标签的HTML
|
||||
stripIgnoreTagBody: ['script'] // script标签较特殊,需要过滤标签中间的内容
|
||||
});
|
||||
|
||||
console.log('text: %s', html);
|
||||
|
||||
/*
|
||||
运行结果:
|
||||
text: helloend
|
||||
*/
|
||||
Reference in New Issue
Block a user