Created
September 27, 2012 16:17
-
-
Save granolocks/3794899 to your computer and use it in GitHub Desktop.
shell words escape
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'sinatra' | |
| require 'pry' | |
| # run with: | |
| # ruby params.rb | |
| # test route example: http://localhost:4567/blah/blee!!;;; | |
| get '/:test/:test2' do | |
| sanitize_params | |
| return @params.inspect | |
| end | |
| def sanitize_params | |
| if @params.size > 0 | |
| @params = shell_escape_hash(@params) | |
| end | |
| end | |
| def shell_escape_hash(input_hash={}) | |
| input_hash = input_hash.inject({}) do |c,(k,v)| | |
| if v.class == Array | |
| c[k] = v.map{|e| Shellwords.escape(e)} | |
| elsif v.class == Hash | |
| # Yay recursion! | |
| c[k] = shell_escape_hash(v) | |
| else | |
| c[k] = Shellwords.escape(v) | |
| end | |
| c | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment