h()
è utile anche per sfuggire citazioni.
Ad esempio, ho una vista che genera un collegamento utilizzando un campo di testo result[r].thtitle
. Il testo potrebbe includere virgolette singole. Se io non sfuggì result[r].thtitle
nel metodo di conferma, il Javascript romperebbe:
<%= link_to_remote "#{result[r].thtitle}", :url=>{ :controller=>:resource,
:action =>:delete_resourced,
:id => result[r].id,
:th => thread,
:html =>{:title=> "<= Remove"},
:confirm => h("#{result[r].thtitle} will be removed"),
:method => :delete %>
<a href="#" onclick="if (confirm('docs: add column &apos;dummy&apos; will be removed')) { new Ajax.Request('/resource/delete_resourced/837?owner=386&th=511', {asynchronous:true, evalScripts:true, method:'delete', parameters:'authenticity_token=' + encodeURIComponent('ou812')}); }; return false;" title="<= Remove">docs: add column 'dummy'</a>
Nota: la dichiarazione :html
titolo è magicamente sfuggito dalle rotaie.
[Secondo l'OWASP] (https: //www.owasp. org/index.php/XSS_ (Cross_Site_Scripting) _Prevention_Cheat_Sheet # RULE_.231 _-_ HTML_Escape_Before_Inserting_Untrusted_Data_into_HTML_Element_Content), i seguenti sei caratteri devono essere sottoposti a escape per la corretta protezione XSS nel contenuto dell'elemento HTML: '& <>" '/ ' – sffc