I’m using Brakeman to identify security issues. It’s flagging up any links which use params.merge as a cross site scripting vulnerability. How can I sanitize something like the following?
- @archives.each do |archive|
= link_to "FTP", params.merge(:action => :ftp, :archive => archive, :recipient => "company")
You should create a new hash based on only the elements of
paramswhich you expect and wish to allow to be a part of theFTPlink and use that to merge your additional parameters.What you have allows me to add whatever I want to that
FTPlink by modifying the querystring, opening up the door to security vulnerabilities. By building a hash for use in place of theparamsin theparams.merge(...you’re effectively whitelisting expected querystring components for use in the template you’re rendering.As a
GETexample, if you expect a URL likeyour controller action you might do
And then pass @cleaned_params to the
link_toThis way if I manually enter a URL like
The
params[:maliciousopt]will never make it into yourFTPlink_toin your view.The same behaviour applies to
POSTrequests, only to be malicious I might add a couple fields to the form before submitting it