Internet Explorer是Windows操作系统中默认捆绑的web浏览器。
IE8中对窗口对象提供了名为toStaticHTML的过滤方式。如果向这个函数传送了HTML字符串,在返回之前会删除所有可执行的脚本结构。例如,可使用toStaticHTML方式确保从postMessage调用所接收到的HTML无法执行脚本,但可利用基本格式:
document.attachEvent('onmessage',function(e) { if (e.domain == 'weather.example.com') { spnWeather.innerHTML = window.toStaticHTML(e.data); } }
调用:
window.toStaticHTML("This is some <b>HTML</b> with embedded script following... <script>alert('bang!');</script>!");
会返回:
This is some <b>HTML</b> with embedded script following... !
测试代码:
<script type="text/javascript"> function fuckie() { var szInput = document.shit.input.value; var szStaticHTML = toStaticHTML(szInput);
ResultComment = szStaticHTML; document.shit.output.value = ResultComment; } </script>
<form name="shit"> <textarea name='input' cols=40 rows=20> </textarea> <textarea name='output' cols=40 rows=20> </textarea>
<input type=button value="fuck_me" name="fuck" onclick=fuckie();> </form>
<style>
}@import url('//127.0.0.1/1.css');aaa
{;}
</style>
<div id="x">Fuck Ie</div>
|