在rails中清理屬性值的最佳方式是什麼?該代碼看起來是這樣的:如何在rails中清理屬性值
<img alt="<%= h 'untrusted-data' %>" src="image-source-here" />
我特意關注上owasp.net XSS prevention cheat sheet.
html_escape方法足以達到目的嗎?由於某種原因,我無法在這裏使用TagHelper
提供的tag
方法。使用Rails 2.3.5版本。
在rails中清理屬性值的最佳方式是什麼?該代碼看起來是這樣的:如何在rails中清理屬性值
<img alt="<%= h 'untrusted-data' %>" src="image-source-here" />
我特意關注上owasp.net XSS prevention cheat sheet.
html_escape方法足以達到目的嗎?由於某種原因,我無法在這裏使用TagHelper
提供的tag
方法。使用Rails 2.3.5版本。
是的,這已經足夠了。 (與其他「雖然,但我想這是一個錯字:)
<img alt="<%=h untrusted %>" src="img.png" />
h
將防止untrusted
包含"
和"
取代它,這樣,攻擊者將無法走出alt
屬性的。此外,她也將無法利用由alt
屬性的東西,因爲沒有分析在完成它。
例如,這將是不同的,如果它是在一個a
的href
屬性,在這種情況下,攻擊者已經能夠甚至在沒有被點擊時運行一些JavaScript代碼能夠走出屬性。 (如javascript:alert(/XSSed/);
)
請注意,如果在屬性爲事件處理程序的HTML屬性中使用不受信任的數據,那麼這是不夠的:http ://erlend.oftedal.no/blog/?blogid = 124 – Erlend 2011-12-28 16:28:04
是的,這是我試圖用'href'例子解釋的那種情況。 – thoferon 2011-12-28 16:33:00
好。這裏是一個用於ruby的html屬性編碼的OWASP實現:https://github.com/thesp0nge/owasp-esapi-ruby/blob/master/lib/codec/encoder.rb – Erlend 2011-12-28 16:37:41
編碼器來自owasp的紅寶石。請參閱encode_for_html_attr。 https://github.com/thesp0nge/owasp-esapi-ruby/blob/master/lib/codec/encoder.rb – Erlend 2011-12-28 16:39:27