过滤所见所得编辑器里的危险脚本
2012-06-06 17:10
183 查看
转载地址:http://bbs.51js.com/thread-77637-1-1.html
所见所得的编辑器现在用得越来越多,原因之一,用户体验好。但是作为开发者,我们也应该清醒的认识到,这样的编辑器往往成了危险脚本、木马的温床。我们不能容忍蛀虫就在我们自己的东西里面滋生。
下面我就来尝试用正则替换的办法,使得编辑器里面的脚本无所遁形。可能我想的不是很全面,希望有漏网之鱼的,朋友们请提出。
脚本藏身之处不过有四:
1、<script>标签、<link>标签、<style>标签、iframe标签
2、on开头的标签属性
3、javascript(vbscript)伪协议
4、css的epression
下面是他们的字符串规则:
1、<(script|link|style|iframe)(.|\n)*<\/\1>\s*
2、\s*on[a-z]+\s*=\s*("[^"]+"|'[^']+'|[^\s]+)\s*(?=>)
3、\s*(href|src)\s*=\s*("\s*(javascript|vbscript):[^"]+"|'\s*(javascript|vbscript):[^']+'|(javascript|vbscript):[^\s]+)\s*(?=>)
4、epression\((.|\n)*\);?
了解他们的规则后,抓虫行动就水到渠成。下面看具体代码:
<textarea id="bug" cols="80" rows="16">
<button id="kick">抓虫1</button>
<script>
function kickBug(str) {
return str.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig,"");
}
</script>
<iframe></iframe>
<link href='test.css'></link>
<style>
a {
height:expression(alert('hei'));
}
</style>
</textarea>
<button id="kick">抓虫1</button>
<script>
function kickBug(str) {
return str.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig,"");
}
if(!/msie/i.test(navigator.userAgent)){
HTMLElement.prototype.__defineGetter__("innerText",function(){
return this.textContent;
});
HTMLElement.prototype.__defineSetter__("innerText",function(text){
this.textContent = text;
});
}
document.getElementById("kick").onclick = function() {
var bug = document.getElementById("bug");
bug.innerText = kickBug(bug.innerText);
}
</script>
复制代码运行代码另存代码
<textarea id="bug" cols="80" rows="5">
<a onclick="test();
test1()" onblur=
"test3()">test</a>
</textarea>
<button id="kick">抓虫2</button>
<script>
function kickBug(str) {
return str.replace(/<[a-z][^>]*\s*on[a-z]+\s*=[^>]+/ig,function($0,$1){
return $0.replace(/\s*on[a-z]+\s*=\s*("[^"]+"|'[^']+'|[^\s]+)\s*/ig,"");
});
}
if(!/msie/i.test(navigator.userAgent)){
HTMLElement.prototype.__defineGetter__("innerText",function(){
return this.textContent;
});
HTMLElement.prototype.__defineSetter__("innerText",function(text){
this.textContent = text;
});
}
document.getElementById("kick").onclick = function() {
var bug = document.getElementById("bug");
bug.innerText = kickBug(bug.innerText);
}
</script>
复制代码运行代码另存代码
<textarea id="bug" cols="80" rows="5">
<a onclick="test();" href="
jAvascript:alert('a')" href="jAvascript:"
href="vbscript:alert()"
>test</a>
</textarea>
<button id="kick">抓虫3</button>
<script>
function kickBug(str) {
return str.replace(/<[a-z][^>]*\s*(href|src)\s*=[^>]+/ig,function($0,$1){
$0 = $0.replace(/(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);});
return $0.replace(/\s*(href|src)\s*=\s*("\s*(javascript|vbscript):[^"]+"|'\s*(javascript|vbscript):[^']+'|(javascript|vbscript):[^\s]+)/ig,"");
});
}
if(!/msie/i.test(navigator.userAgent)){
HTMLElement.prototype.__defineGetter__("innerText",function(){
return this.textContent;
});
HTMLElement.prototype.__defineSetter__("innerText",function(text){
this.textContent = text;
});
}
document.getElementById("kick").onclick = function() {
var bug = document.getElementById("bug");
bug.innerText = kickBug(bug.innerText);
}
</script>
复制代码运行代码另存代码
<textarea id="bug" cols="80" rows="5">
expression()
<a style="color:expression(
'red'
)">test</a>
</textarea>
<button id="kick">抓虫4</button>
<script>
function kickBug(str) {
return str.replace(/<[a-z][^>]*\s*style\s*=[^>]+/ig,function($0,$1){
$0 = $0.replace(/(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);});
return $0.replace(/\s*style\s*=\s*("[^"]+(expression)[^"]+"|'[^']+\2[^']+'|[^\s]+\2[^\s]+)\s*/ig,"");
});
}
if(!/msie/i.test(navigator.userAgent)){
HTMLElement.prototype.__defineGetter__("innerText",function(){
return this.textContent;
});
HTMLElement.prototype.__defineSetter__("innerText",function(text){
this.textContent = text;
});
}
document.getElementById("kick").onclick = function() {
var bug = document.getElementById("bug");
bug.innerText = kickBug(bug.innerText);
}
</script>
复制代码运行代码另存代码
在winter的提醒下,加多了一个过滤iframe的。
加入过滤link标签、style标签
处理了类如e的html实体
所见所得的编辑器现在用得越来越多,原因之一,用户体验好。但是作为开发者,我们也应该清醒的认识到,这样的编辑器往往成了危险脚本、木马的温床。我们不能容忍蛀虫就在我们自己的东西里面滋生。
下面我就来尝试用正则替换的办法,使得编辑器里面的脚本无所遁形。可能我想的不是很全面,希望有漏网之鱼的,朋友们请提出。
脚本藏身之处不过有四:
1、<script>标签、<link>标签、<style>标签、iframe标签
2、on开头的标签属性
3、javascript(vbscript)伪协议
4、css的epression
下面是他们的字符串规则:
1、<(script|link|style|iframe)(.|\n)*<\/\1>\s*
2、\s*on[a-z]+\s*=\s*("[^"]+"|'[^']+'|[^\s]+)\s*(?=>)
3、\s*(href|src)\s*=\s*("\s*(javascript|vbscript):[^"]+"|'\s*(javascript|vbscript):[^']+'|(javascript|vbscript):[^\s]+)\s*(?=>)
4、epression\((.|\n)*\);?
了解他们的规则后,抓虫行动就水到渠成。下面看具体代码:
<textarea id="bug" cols="80" rows="16">
<button id="kick">抓虫1</button>
<script>
function kickBug(str) {
return str.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig,"");
}
</script>
<iframe></iframe>
<link href='test.css'></link>
<style>
a {
height:expression(alert('hei'));
}
</style>
</textarea>
<button id="kick">抓虫1</button>
<script>
function kickBug(str) {
return str.replace(/<(script|link|style|iframe)(.|\n)*\/\1>\s*/ig,"");
}
if(!/msie/i.test(navigator.userAgent)){
HTMLElement.prototype.__defineGetter__("innerText",function(){
return this.textContent;
});
HTMLElement.prototype.__defineSetter__("innerText",function(text){
this.textContent = text;
});
}
document.getElementById("kick").onclick = function() {
var bug = document.getElementById("bug");
bug.innerText = kickBug(bug.innerText);
}
</script>
复制代码运行代码另存代码
<textarea id="bug" cols="80" rows="5">
<a onclick="test();
test1()" onblur=
"test3()">test</a>
</textarea>
<button id="kick">抓虫2</button>
<script>
function kickBug(str) {
return str.replace(/<[a-z][^>]*\s*on[a-z]+\s*=[^>]+/ig,function($0,$1){
return $0.replace(/\s*on[a-z]+\s*=\s*("[^"]+"|'[^']+'|[^\s]+)\s*/ig,"");
});
}
if(!/msie/i.test(navigator.userAgent)){
HTMLElement.prototype.__defineGetter__("innerText",function(){
return this.textContent;
});
HTMLElement.prototype.__defineSetter__("innerText",function(text){
this.textContent = text;
});
}
document.getElementById("kick").onclick = function() {
var bug = document.getElementById("bug");
bug.innerText = kickBug(bug.innerText);
}
</script>
复制代码运行代码另存代码
<textarea id="bug" cols="80" rows="5">
<a onclick="test();" href="
jAvascript:alert('a')" href="jAvascript:"
href="vbscript:alert()"
>test</a>
</textarea>
<button id="kick">抓虫3</button>
<script>
function kickBug(str) {
return str.replace(/<[a-z][^>]*\s*(href|src)\s*=[^>]+/ig,function($0,$1){
$0 = $0.replace(/(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);});
return $0.replace(/\s*(href|src)\s*=\s*("\s*(javascript|vbscript):[^"]+"|'\s*(javascript|vbscript):[^']+'|(javascript|vbscript):[^\s]+)/ig,"");
});
}
if(!/msie/i.test(navigator.userAgent)){
HTMLElement.prototype.__defineGetter__("innerText",function(){
return this.textContent;
});
HTMLElement.prototype.__defineSetter__("innerText",function(text){
this.textContent = text;
});
}
document.getElementById("kick").onclick = function() {
var bug = document.getElementById("bug");
bug.innerText = kickBug(bug.innerText);
}
</script>
复制代码运行代码另存代码
<textarea id="bug" cols="80" rows="5">
expression()
<a style="color:expression(
'red'
)">test</a>
</textarea>
<button id="kick">抓虫4</button>
<script>
function kickBug(str) {
return str.replace(/<[a-z][^>]*\s*style\s*=[^>]+/ig,function($0,$1){
$0 = $0.replace(/(6[5-9]|[78][0-9]|9[0789]|1[01][0-9]|12[012]);?/g,function($0,$1){return String.fromCharCode($1);});
return $0.replace(/\s*style\s*=\s*("[^"]+(expression)[^"]+"|'[^']+\2[^']+'|[^\s]+\2[^\s]+)\s*/ig,"");
});
}
if(!/msie/i.test(navigator.userAgent)){
HTMLElement.prototype.__defineGetter__("innerText",function(){
return this.textContent;
});
HTMLElement.prototype.__defineSetter__("innerText",function(text){
this.textContent = text;
});
}
document.getElementById("kick").onclick = function() {
var bug = document.getElementById("bug");
bug.innerText = kickBug(bug.innerText);
}
</script>
复制代码运行代码另存代码
在winter的提醒下,加多了一个过滤iframe的。
加入过滤link标签、style标签
处理了类如e的html实体
相关文章推荐
- 在MVC4中采用UEditer(百度编辑器)脚本。报错,显示从客户端(<p>2323</p>)中检测到有潜在危险的Request.Form 值的处理办法
- tms脚本编辑器
- VS2017做为Unity3D的脚本编辑器需要的最精简组件
- EBMIDE——服务脚本IOS编辑器(二)
- 《Linux命令、编辑器与Shell编程》读书笔记12-函数和脚本
- GalGame脚本编辑器lneditor
- SED单行脚本快速参考(Unix 流编辑器)
- 【unity小技巧之五】unity编辑器脚本添加类目到unity Preferences中
- Unity3D编辑器:删掉MissingScirpt脚本
- Unity-编辑器创建Lua脚本模版-热更新
- 书写优雅的shell脚本(插曲)- ed编辑器
- SED单行脚本快速参考(Unix 流编辑器)
- 五.百度UEditor编辑器之JS脚本报错修改
- Unity编辑器拓展之五:修改脚本icon
- shell脚本学习笔记 (流编辑器sed)
- UnityProject面板中右键创建新的Lua脚本以及修改文件默认打开方式的编辑器
- 【VB脚本备忘】【for PPT】规范表格格式(含OFFICE内置编辑器BUG描述)
- SED单行脚本快速参考(Unix 流编辑器)
- 书写优雅的shell脚本(插曲)- ed编辑器
- patterson-consulting即将推出免费版VBS脚本编辑器