如何在服务器端防止href属性的xss? [英] how to prevent xss of href attribute at the server side?

查看:47
本文介绍了如何在服务器端防止href属性的xss?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站(服务器端为c#)中,我让用户提交如下链接:

In my website ( server side is c# ) , I let users submit links like this:

<a href="$">link text</a>

无论他们输入什么,都将仅保留在"href"属性内.我想通过"href" URL阻止XSS.

Whatever they input will stay inside the "href" attribute only. i want to prevent XSS through the "href" URL.

现在,我检查c#对象URI是否为有效网址.

Right now i check if it's a valid url the c# object URI.

我的问题是:

  1. 使用URI验证网址是否足以阻止url xss?
  2. 如果没有,我应该如何防止这种xss?

例如,如何防止有效网址的XSS网络钓鱼攻击为:" http://xss.rocks/xss.css "

For example how can i prevent XSS phishing attack of a valid url as: "http://xss.rocks/xss.css"

推荐答案

作为URI进行验证是一个不错的开始.之后,确认 方案 是白名单选项,例如 http / https .之后,请确保在输出之前对其进行编码(请参阅OWASP链接).基本上,您可以对任何html属性编码进行完全相同的操作,除了您还需要了解恶意方案之外.

Validating as a URI is a good start. After that, verify the scheme is a white-listed option such as http/https. After that, make sure that you encode it before outputting it (see OWASP link). Basically, you do exactly what you do for any html attribute encoding, except you also need to be aware of malicious schemes.

要注意的事情: javascript:alert(1); (验证方案), https://example.com?xss="onclick = alert(1);//(转义输出).

Things to look out for: javascript:alert(1); (validate scheme), https://example.com?xss=" onclick=alert(1);// (escape output).

请参阅:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆