code_junkie

Best way to handle security and avoid XSS with user entered URLs

Nov 11th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.33 KB | None | 0 0
  1. <a href="http://stackoverflow.com">stackoverflow.com</a>
  2.  
  3. <a href="alert('hacked!');">stackoverflow.com</a>
  4.  
  5. /// <summary>
  6. /// returns "safe" URL, stripping anything outside normal charsets for URL
  7. /// </summary>
  8. public static string SanitizeUrl(string url)
  9. {
  10. return Regex.Replace(url, @"[^-A-Za-z0-9+&@#/%?=~_|!:,.;()]", "");
  11. }
Add Comment
Please, Sign In to add comment