Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Use a standard URI parser to break this problem into smaller parts. Let a modern URI library worry about arcane details like spaces, fragments, userinfo, IPv6 hosts, etc.

   uri = URI.parse(target).normalize
   uri.absolute? or raise 'URI not absolute'
   %w[ http https ftp ].include?(uri.scheme) or raise 'Unsupported URI scheme'
   # Etc


That was not an option in this case, as the goal is to validate URLs entered as user input and blacklist certain URL constructs even though they’re technically valid.


So check if uri.host matches your blacklist.


It'd be interesting to look at URI.parse's source code.


It does match a regular expression internally.

https://github.com/ruby/ruby/blob/trunk/lib/uri/rfc2396_pars...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: