在我的一个缓解 XSS 攻击的应用程序中,我使用 OWASP 的 html 编码器,但不幸的是这还不够。
渗透测试人员发现了反映的跨站点脚本威胁,其中输入字段内容或有效负载是 URL 编码的,XSS 编码/过滤器不会对数据进行编码并允许输入传递到 Javascript 函数。
输入为:%3Cscript%3Ealert%285%29%3C%2Fscript%3E 或 39848%3balert(1)%2f%2f920
我也尝试过 Jsoup en html sanitize。他们都只过滤 html 的输出/输入,但这已经是编码形式了。
function set() {
var span = window.parent.document.all.<%=Encode.forHtml(request.getParameter("address"))%>;
var action = '<%=Encode.forHtml(request.getParameter("action"))%>';
<logic:present name="postcodeForm" property="result">
<bean:size id="resultSize" name="postcodeForm" property="result"/>
<logic:notMatch name="resultSize" value="1">
span.innerHTML = "<bean:message key='error.result'/>";
span.className = "errSpec";
</logic:notMatch>
<logic:match name="resultSize" value="1">
<logic:iterate id="adres" name="postcodeForm" property="result">
span.innerHTML = "<bean:write name='address' property='Street'/> "
+ <%=Integer.parseInt(Encode.forHtml(searchString.substring(searchString.indexOf(',') + 1)))%>
<logic:present parameter="houseNumber">
<%=Encode.forHtml(request.getParameter("houseNumber"))%>
</logic:present>
+ "<br/>" + "<bean:write name='address' property='postcode'/> <bean:write name='address' property='houseNr'/>";
span.className = "";
if (action == 'postcode') {
var streetname= "<bean:write name='address' property='streetName' filter="false"/>";
任何想法或帮助都非常感谢。
提前致谢!
如果您的所有请求参数都是 Javascript 代码中出现的字符串值,那么复杂性会大大降低。然后你只需要确保
"
变为 \"
)并且\
变为 \\
),因此参数本身不能包含转义序列。为此不需要 HTML 编码。
您当前的代码不将
address
视为字符串值,您应该编写 window.parent.document.all["XXX"]
而不是 window.parent.document.all.XXX
。这是使用以下服务器端代码生成的:
var span = window.parent.document.all["<%=
request.getParameter("address")
.replaceAll('"', '\\"')
.replaceAll('\\', '\\\\')
%>"];
var action = "<%=
request.getParameter("action")
.replaceAll('"', '\\"')
.replaceAll('\\', '\\\\')
%>";
即使攻击者将
address
设置为值 "];alert(1)
,这仍然会被视为字符串:
var span = window.parent.document.all["\"];alert(1)"];
(我不明白你目前对
houseNumber
的处理,但这也必须作为字符串处理。)