Land #10788, Clarify "NameError: wrong constant name" message on invalid module name

GSoC/Meterpreter_Web_Console
Brent Cook 2018-10-10 15:34:55 -05:00
commit 3349ecf212
No known key found for this signature in database
GPG Key ID: 1FFAA0B24B708F96
4 changed files with 31 additions and 12 deletions

View File

@ -48,10 +48,22 @@ class ValidationError < ArgumentError
end
end
###
#
# This exception is raised when the module cache is invalidated. It is
# handled internally by the ModuleManager.
# This exception is raised when a module fails to load.
#
# It is used by Msf::Modules::Loader::Base.
#
###
class ModuleLoadError < RuntimeError
end
###
#
# This exception is raised when the module cache is invalidated.
#
# It is handled internally by the ModuleManager.
#
###
class ModuleCacheInvalidated < RuntimeError

View File

@ -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
@ -522,7 +529,7 @@ class Msf::Modules::Loader::Base
# @return [String] The module full name
#
# @see namespace_module_names
def reverse_relative_name(relative_name)
def self.reverse_relative_name(relative_name)
relative_name.split('__').map(&:downcase).join('/')
end

View File

@ -738,7 +738,7 @@ RSpec.describe Msf::Modules::Loader::Base do
it 'should be reversible' do
namespace_module_name = subject.send(:namespace_module_name, module_full_name)
relative_name = namespace_module_name.gsub(/^.*::/, '')
reversed_name = subject.send(:reverse_relative_name, relative_name)
reversed_name = described_class.reverse_relative_name(relative_name)
expect(reversed_name).to eq module_full_name
end
@ -752,7 +752,7 @@ RSpec.describe Msf::Modules::Loader::Base do
it 'should be reversible' do
namespace_module_names = subject.send(:namespace_module_names, module_full_name)
relative_name = namespace_module_names.last
reversed_name = subject.send(:reverse_relative_name, relative_name)
reversed_name = described_class.reverse_relative_name(relative_name)
expect(reversed_name).to eq module_full_name
end

View File

@ -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