一、前言
出于數據安全性考慮,某些破壞性鏈接應該使用post請求,比如一個刪除記錄的請求。
除了腳本確認以外,服務端還需要post驗證,因為腳本是可以繞過的。想像你的頁面上有一個刪除鏈接,只作了客戶端腳本確認(老的scaffold生成代碼有這問題),被google找到了,它一個請求就會讓你的數據丟失。
rails對于這類請求的處理,是通過verify方法,默認的scaffold生成代碼有如下內容:
??#?GETs?should?be?safe?(see?http://www.w3.org/2001/tag/doc/whenToUseGet.html)
??verify?:method?=>?:post,?:only?=>?[?:destroy,?:create,?:update?],
?????????:redirect_to?=>?{?:action?=>?:list?}
只有post請求時,destroy才會被允許,如果是get,就會被重定向到list。
二、實現我自己實現了一個method_dispatch,當請求一個/test/a時,如果是get,則會直接執行TestController#a;如果是post,則會執行TestController#a_post,a_post應該是protected,這樣不會直接暴露給客戶,get/post就嚴格區分開來了。
method_dispatch現在是直接實現在ApplicationController中的,代碼如下:
class?ApplicationController?<?ActionController::Base
??protected
??def?self.method_dispatch(*methods)
????before_filter?:do_method_dispatch,?:only?=>?methods.flatten.map(&:to_sym)
??end
??private
??def?do_method_dispatch
????if?request.post??&&?respond_to?("#{action_name}_post")
??????eval("#{action_name}_post")
??????return?false
????end
??end
end
由于ApplicationController里面的方法會被子類繼承到,所以必須嚴格處理訪問級別。
使用如下:
class?TestController?<?ApplicationController
??method_dispatch?:a
??def?index
??end
??def?a
????render?:text?=>?'get?a'
??end
??def?b
????render?:text?=>?'get?b'
??end
??protected
??def?a_post
????render?:text?=>?'post?a'
??end
??def?b_post
????render?:text?=>?'post?b'
??end
end
注意a_post,b_post要被保護起來防止直接調用。
index.rhtml里面演示了使用get和post的情況:
<%=?link_to?"Get?a",?:action?=>?'a'?%>
<%=?link_to?"Post?a",?{:action?=>?'a'},?{:post?=>?true}?%><br?/>
<%=?link_to?"Get?b",?:action?=>?'b'?%>
<%=?link_to?"Post?b",?{:action?=>?'b'},?{:post?=>?true}?%><br?/>
rails在處理有:post => true參數的link_to時,生成的代碼如下:
<a?href="/test/a"?onclick="var?f?=?document.createElement('form');
?????? this.parentNode.appendChild(f);?f.method?=?'POST';?f.action?=?this.href;?f.submit();return?false;">Post?a</a>
經測試上面代碼工作情況良好,使用get訪問/test/a時,顯示get a;使用post訪問時,顯示post a。使用get訪問/test/b時,顯示get b;使用post時,顯示get b,因為b并沒有使用method_dispatch。
三、應用下面的posts_controller.rb是scaffold生成的:
class?PostsController?<?ApplicationController
??def?index
????list
????render?:action?=>?'list'
??end
??#?GETs?should?be?safe?(see?http://www.w3.org/2001/tag/doc/whenToUseGet.html)
??verify?:method?=>?:post,?:only?=>?[?:destroy,?:create,?:update?],
?????????:redirect_to?=>?{?:action?=>?:list?}
??def?list
????@post_pages,?@posts?=?paginate?:posts,?:per_page?=>?10
??end
??def?show
????@post?=?Post.find(params[:id])
??end
??def?new
????@post?=?Post.new
??end
??def?create
????@post?=?Post.new(params[:post])
????if?@post.save
??????flash[:notice]?=?'Post?was?successfully?created.'
??????redirect_to?:action?=>?'list'
????else
??????render?:action?=>?'new'
????end
??end
??def?edit
????@post?=?Post.find(params[:id])
??end
??def?update
????@post?=?Post.find(params[:id])
????if?@post.update_attributes(params[:post])
??????flash[:notice]?=?'Post?was?successfully?updated.'
??????redirect_to?:action?=>?'show',?:id?=>?@post
????else
??????render?:action?=>?'edit'
????end
??end
??def?destroy
????Post.find(params[:id]).destroy
????redirect_to?:action?=>?'list'
??end
end
可以看到,它添加了verify,但action過多,需要在verify中維護一份對應方法名,稍不留神就容易出現漏洞。
我把它修改如下:
class?PostsController?<?ApplicationController
??method_dispatch?:new,?:edit,?:destroy
??def?index
????list
????render?:action?=>?'list'
??end
??def?list
????@post_pages,?@posts?=?paginate?:posts,?:per_page?=>?10
??end
??def?show
????@post?=?Post.find(params[:id])
??end
??def?new
????@post?=?Post.new
??end
??def?edit
????@post?=?Post.find(params[:id])
??end
??def?destroy
????render?:inline?=>?<<-EOS
????? Are you sure?
??????<%=?link_to?"Yes",?{},?:post?=>?true?%>
??????<%=?link_to?"No",?:action?=>?'edit', :id => params[:id]%>
????EOS
??end
??protected
??def?destroy_post
????Post.find(params[:id]).destroy
????redirect_to?:action?=>?'list'
??end
??def?edit_post
????@post?=?Post.find(params[:id])
????if?@post.update_attributes(params[:post])
??????flash[:notice]?=?'Post?was?successfully?updated.'
??????redirect_to?:action?=>?'show',?:id?=>?@post
????else
??????render?:action?=>?'edit'
????end
??end
??def?new_post
????@post?=?Post.new(params[:post])
????if?@post.save
??????flash[:notice]?=?'Post?was?successfully?created.'
??????redirect_to?:action?=>?'list'
????else
??????render?:action?=>?'new'
????end
??end
end
相應地,還需要把new.rhtml中的action從create修改到new,把edit.rhtml中的action從update修改到edit。
這樣的修改把必須使用post請求的action隱藏起來,而相應的get操作是不修改或刪除記錄的,如果以post請求,才會自動調用這些保護的方法。