SafeERBを使うと
active_scaffoldでエラー。
エラー内容
RuntimeError in Admin/products#index Showing vendor/plugins/active_scaffold/frontends/default/views/list.rhtml where line #1 raised: attempted to output tainted string: admin__products-active-scaffold Extracted source (around line #1): 1: <div id="<%= active_scaffold_id -%>" class="active-scaffold active-scaffold-<%= controller_id %> <%= active_scaffold_config.theme %>-theme"> 2: <div class="active-scaffold-header"> 3: <%= render :partial => 'list_header' %> 4: </div>
あれ?
active_scaffoldがいけてないのか、そもそもactive_scaffoldなんて使ってみた自分が
いけてないのかわからんがサニタイズ漏れと。
なので
せっせこ、エラーとなるviewに h を追加。
そうすると
vendor/plugins/active_scaffold/frontends/default/views/_list_record.rhtml 13行目
<%= render_list_column(column_value, column, record) %>
これをhしちゃうとタグのエスケープになっちゃう。
じゃ、sanitize にしてやると
safeERBで引っ掛かる。
あれ?sanitizeはまずいの?
ホワイトリスト制御だからいいんじゃね?
safeERBのREADMEを読んでみると
The returned values from the following methods become untainted:
- ERB::Util#h
- ActionView::Helpers::TagHelper#escape
- ActionView::Helpers::TextHelper#strip_tags
Also, you can always untaint any string manually by calling "untaint" method (standard Ruby feature).
strip_tagsだけやん。escapeってescape_once?
untaintって明示的につけてやればいいとは書いてるが
でもsanitize使いたい(untaintって書きたくないよね)。
ということで
safeERBのソースを眺めるとalias_method_chainを使って
checkの対象を操作しているようなので
$ grep alias_method_chain vendor/plugins/safe_erb/lib/safe_erb common.rb : alias_method_chain :render, :checking_tainted rails_2.r : alias_method_chain :strip_tags, :untaint tag_helper.rb: alias_method_chain :escape_once, :untaint
vendor/plugins/safe_erb/lib/safe_erb/rails_2.rb に追記。
def sanitize_with_untaint(html) str = sanitize_without_untaint(html) str.untaint str end alias_method_chain :sanitize, :untaint
とりあえず解決。
参考
alias_method_chainを分かりやすく解説してくれています(with/withoutとか)。
ヽ( ・∀・)ノくまくまー(2006-05-03)