Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safer loading for translation repositories #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/fast_gettext/translation_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ module FastGettext
# Responsibility:
# - decide which repository to choose from given input
module TranslationRepository
VALID_TYPES = %i[base chain db logger merge mo po yaml].freeze

extend self

def build(name, options)
type = options[:type] || :mo
type = options[:type] ? options[:type].to_sym : :mo

raise ArgumentError, "Invalid translation repository type" unless VALID_TYPES.include?(type)

class_name = type.to_s.split('_').map(&:capitalize).join
unless FastGettext::TranslationRepository.constants.map{|c|c.to_s}.include?(class_name)
require "fast_gettext/translation_repository/#{type}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just .untaint here ?

require "#{__dir__}/translation_repository/#{type}.rb".untaint
end
eval(class_name).new(name,options)
end
Expand Down
4 changes: 1 addition & 3 deletions spec/fast_gettext/translation_repository/merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@
end

it "can work in SAFE mode" do
pending_if RUBY_VERSION > "2.0" do
`ruby spec/cases/safe_mode_can_handle_locales.rb 2>&1`.should == 'true'
end
`ruby spec/cases/safe_mode_can_handle_locales.rb 2>&1`.should == 'true'
end
end
4 changes: 1 addition & 3 deletions spec/fast_gettext/translation_repository/mo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@
end

it "can work in SAFE mode" do
pending_if RUBY_VERSION > "2.0" do
`ruby spec/cases/safe_mode_can_handle_locales.rb 2>&1`.should == 'true'
end
`ruby spec/cases/safe_mode_can_handle_locales.rb 2>&1`.should == 'true'
end
end
2 changes: 1 addition & 1 deletion spec/fast_gettext/translation_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe FastGettext::TranslationRepository do
describe "build" do
it "auto requires class by default" do
lambda { FastGettext::TranslationRepository.build('xx', { :type => 'invalid'}) }.should raise_error(LoadError)
lambda { FastGettext::TranslationRepository.build('xx', { :type => 'invalid'}) }.should raise_error(ArgumentError)
end

it "can have auto-require disabled" do
Expand Down