From 0b8926715ebff48905c203072898f253b15f8eab Mon Sep 17 00:00:00 2001 From: William Vu Date: Wed, 10 Oct 2018 14:23:13 -0500 Subject: [PATCH] Reactively check for invalid module names --- lib/msf/core/modules/loader/base.rb | 19 +++++++++++++------ tools/dev/msftidy.rb | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/msf/core/modules/loader/base.rb b/lib/msf/core/modules/loader/base.rb index 87eb12f24e..eddad094af 100644 --- a/lib/msf/core/modules/loader/base.rb +++ b/lib/msf/core/modules/loader/base.rb @@ -362,17 +362,24 @@ class Msf::Modules::Loader::Base # @return [nil] if any module name along the chain does not exist. def current_module(module_names) # Don't want to trigger ActiveSupport's const_missing, so can't use constantize. - named_module = module_names.inject(Object) { |parent, module_name| + named_module = module_names.reduce(Object) do |parent, module_name| # Since we're searching parent namespaces first anyway, this is # semantically equivalent to providing false for the 1.9-only # "inherit" parameter to const_defined?. If we ever drop 1.8 # support, we can save a few cycles here by adding it back. - if parent.const_defined?(module_name) - parent.const_get(module_name) - else - break + begin + if parent.const_defined?(module_name) + parent.const_get(module_name) + else + break + end + # HACK: This doesn't slow load time as much as checking proactively + rescue NameError + reversed_name = self.class.reverse_relative_name(module_name) + # TODO: Consolidate this with Msftidy#check_snake_case_filename ? + raise Msf::ModuleLoadError, "#{reversed_name} must be lowercase alphanumeric snake case" end - } + end named_module end diff --git a/tools/dev/msftidy.rb b/tools/dev/msftidy.rb index 7d13ce462b..1895677473 100755 --- a/tools/dev/msftidy.rb +++ b/tools/dev/msftidy.rb @@ -257,7 +257,7 @@ class Msftidy # This check also enforces namespace module name reversibility def check_snake_case_filename if @name !~ /^[a-z0-9]+(?:_[a-z0-9]+)*\.rb$/ - warn('Filenames should be lowercase alphanumeric snake case.') + warn('Filenames must be lowercase alphanumeric snake case.') end end