As with any user supplied data, the URLs will need to be escaped and filtered appropriately to avoid all sorts of exploits. I want to be able to
Put user supplied URLs in
href
attributes. (Bonus points if I don't get screwed if I forget to write the quotes)<a href="ESCAPED_USER_URL_GOES_HERE">...</a>
Forbid malicious URLs such as
javascript:
stuff or links to evil domain names.Allow some leeway for the users. I don't want to raise an error just because they forgot to add an
http://
or something like that.
Unfortunately, I can't find any "canonical" solution to this sort of problem. The only thing I could find as inspiration is the encodeURI
function from Javascript but that doesn't help with my second point since it just does a simple URL parameter encoding but leaving alone special characters such as :
and /
.