web保险防范之XSS漏洞攻击

web安全防范之XSS漏洞攻击
原文出处:http://netsecurity.51cto.com/art/201301/378948.htm

网站通常会遇到的攻击;攻击方法也很多;
原理:向网页内注入可执行代码,从而达到入侵目的。
危害:破坏页面结构,导致页面显示问题;
更严重的会盗取当前用户的cookie,导致你的账号被盗等;

function escape(html) {
		var codeSpan = /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm;  
		var codeBlock = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g;  
		var spans = [];  
		var blocks = [];  
		var text = String(html).replace(/\r\n/g, '\n')
			.replace('/\r/g', '\n');
		text = '\n\n' + text + '\n\n';
		texttext = text.replace(codeSpan, function(code) {
			spans.push(code);
			return '`span`';
		});  
		text += '~0';  
		return text.replace(codeBlock, function (code) {
			blocks.push(code);  
			return '\n\tblock';  
		})
		.replace(/&(?!\w+;)/g, '&')  
		.replace(/</g, '<')  
		.replace(/>/g, '>')  
		.replace(/"/g, '"')  
		.replace(/`span`/g, function() {
			return spans.shift();  
		})
		.replace(/\n\tblock/g, function() {
			return blocks.shift();
		})
		.replace(/~0$/,'')
		.replace(/^\n\n/, '')
		.replace(/\n\n$/, '');
	};