admin管理员组文章数量:1277886
I want a user to be able to submit a url, and then display that url to other users as a link.
If I naively redisplay what the user submitted, I leave myself open to urls like
' ><script>[any javacscript in here]</script>
that when I redisplay it to other users will do something nasty, or at least something that makes me look unprofessional for not preventing it.
Is there a library, preferably in java, that will clean a url so that it retains all valid urls but weeds out any exploits/tomfoolery?
Thanks!
I want a user to be able to submit a url, and then display that url to other users as a link.
If I naively redisplay what the user submitted, I leave myself open to urls like
http://somesite.' ><script>[any javacscript in here]</script>
that when I redisplay it to other users will do something nasty, or at least something that makes me look unprofessional for not preventing it.
Is there a library, preferably in java, that will clean a url so that it retains all valid urls but weeds out any exploits/tomfoolery?
Thanks!
Share Improve this question asked Aug 30, 2012 at 9:56 BruceBruce 3,5486 gold badges32 silver badges37 bronze badges 3- Have a look here. – sp00m Commented Aug 30, 2012 at 10:05
- you want URLEncoding or you don't want to display the whole URL ??? – Harmeet Singh Commented Aug 30, 2012 at 10:06
- This question asks for a **valid URL regular expression: stackoverflow./questions/161738/… **. The answers are helpful. You'd simply need to create a regular expression matcher in java – Redandwhite Commented Aug 30, 2012 at 11:01
3 Answers
Reset to default 3URLs having '
in are perfectly valid. If you are outputting them to an HTML document without escaping, then the problem lies in your lack of HTML-escaping, not in the input checking. You need to ensure that you are calling an HTML encoding method every time you output any variable text (including URLs) into an HTML document.
Java does not have a built-in HTML encoder (poor show!) but most web libraries do (take your pick, or write it yourself with a few string replaces). If you use JSTL tags, you get escapeXml
to do it for free by default:
<a href="<c:out value="${link}"/>">ok</a>
Whilst your main problem is HTML-escaping, it is still potentially beneficial to validate that an input URL is valid to catch mistakes - you can do that by parsing it with new URL(...)
and seeing if you get a MalformedURLException.
You should also check that the URL begins with a known-good protocol such as http://
or https://
. This will prevent anyone using dangerous URL protocols like javascript:
which can lead to cross-site-scripting as easily as HTML-injection can.
I think what you are looking for is output encoding. Have a look at OWASP ESAPI which is tried and tested way to perform encoding in Java.
Also, just a suggestion, if you want to check if a user is submitting malicious URL, you can check that against Google malware database. You can use SafeBrowing API for that.
You can use apache validator URLValidator
UrlValidator urlValidator = new UrlValidator(schemes);
if (urlValidator.isValid("http://somesite.")) {
//valid
}
本文标签: javascripthow can I clean and sanitize a url submitted by a user for redisplay in javaStack Overflow
版权声明:本文标题:javascript - how can I clean and sanitize a url submitted by a user for redisplay in java? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741292717a2370640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论