Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I avoid html_safe when using gsub to insert links?

a simplified example would be this:

def expand_links(message)
  message = strip_tags(message)
  message = message.gsub('[register]') { link_to('register', new_user_path) }
  message = message.gsub('[login]') { link_to('login', new_sessions_path) }
  message.html_safe
end

I'm using strip_tags but to be sure there isn't a chance of XSS.

so what would be the proper way to do this without strip_tags and html_safe?

like image 290
localhostdotdev Avatar asked Sep 11 '26 17:09

localhostdotdev


1 Answers

here is what I did from @iceman's suggestion:

  def expand_links(message)
    message = strip_tags(message)
    message = message.gsub('[register]') { link_to('register', new_user_path) }
    message = message.gsub('[login]') { link_to('login', new_sessions_path) }
    sanitize(message, tags: ['a'], attributes: ['href'])
  end

(BTW sanitize doesn't work with symbols)

https://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html

like image 80
localhostdotdev Avatar answered Sep 13 '26 16:09

localhostdotdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!