ruby - rails security: converting parameters to symbols for hash lookup -


i have hash of constants refer throughout code like:

categories = {   business:  '1002',   education:  '1003',   entertainment:  '1004',   # etc... } 

in 1 of controllers need test existing of category via parameter, i'd like:

categories.has_key? params[:category].to_sym 

however seems invitation denial of service attack, attacker blow ruby symbol table providing random strings category params.

seems easiest solution convert category keys strings rather symbols:

categories = {   'business' =>  '1002',   'education' =>  '1003',   'entertainment' =>  '1004',   # etc... } 

or perhaps:

def self.valid_category(category_s)    categories.keys.any? { |key| key.to_s == category_s } end 

is there better or more idiomatic way in rails?

is there better or more idiomatic way in rails?

the common approach i've seen second solution provided, i.e.:

def self.valid_category(category_s)   categories.keys.any? { |key| key.to_s == category_s } end 

although, name method self.valid_category?. maybe move .to_s on category variable method, like:

def self.valid_category?(category)   category = category.to_s   categories.keys.any? { |key| key.to_s == category } end 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -