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

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

Merged
merged 3 commits into from Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/msf/core/exceptions.rb
Expand Up @@ -48,10 +48,22 @@ def to_s
end
end


###
#
# This exception is raised when a module fails to load.
#
# It is used by Msf::Modules::Loader::Base.
#
###
wvu marked this conversation as resolved.
Show resolved Hide resolved
class ModuleLoadError < RuntimeError
end

###
#
# This exception is raised when the module cache is invalidated.
#
# This exception is raised when the module cache is invalidated. It is
# handled internally by the ModuleManager.
# It is handled internally by the ModuleManager.
#
###
class ModuleCacheInvalidated < RuntimeError
Expand Down
21 changes: 14 additions & 7 deletions lib/msf/core/modules/loader/base.rb
Expand Up @@ -362,17 +362,24 @@ def create_namespace_module(namespace_module_names)
# @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
Expand Down Expand Up @@ -522,7 +529,7 @@ def namespace_module_names(module_full_name)
# @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

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/msf/core/modules/loader/base_spec.rb
Expand Up @@ -738,7 +738,7 @@ module Auxiliary__Rspec__Mock
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
Expand All @@ -752,7 +752,7 @@ module Auxiliary__Rspec__Mock
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
Expand Down
2 changes: 1 addition & 1 deletion tools/dev/msftidy.rb
Expand Up @@ -257,7 +257,7 @@ def line_has_require?(line, lib)
# 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

Expand Down