Ruby Sanitize Code ... why and sanitized

I am currently using the following code to sanitize a string before saving it:

ERB::Util::h(string)

My problem occurs when the line has already been cleared as follows:

string = "Watching baseball `&` football"

The sanitized line will look like this:

sanitized_string = "Watching baseball `&` football"

Can I sanitize by simply turning <in &lt;and> in &gt;by substitution?

+3
source share
4 answers

Uncheck the box first, then run again:

require 'cgi'
string = "Watching baseball &amp; football"

CGI.escapeHTML(CGI.unescapeHTML(string))

=> "Watching baseball &amp; football"
+3
source

A quick approach based on this snippet from Erubis .

ESCAPE_TABLE = { '<'=>'&lt;', '>'=>'&gt;' }
def custom_h(value)
   value.to_s.gsub(/[<>]/) { |s| ESCAPE_TABLE[s] }
end
0
source

, , , :

mystring.gsub( /<(.|\n)*?>/, '' )
0

, .

A better approach might be to unencode your string before disinfecting it - h () has an inverse that you could wrap your lines in the first place?

0
source

Source: https://habr.com/ru/post/1720729/


All Articles