From acf23e9c616a6782aaa4b7a172070460cc170754 Mon Sep 17 00:00:00 2001 From: Dean Welch Date: Mon, 13 Mar 2023 12:58:52 +0000 Subject: [PATCH] Sets `config.eager_load` to false for production envs --- config/application.rb | 2 +- .../web_services/http_db_manager_service.rb | 50 +++++++------------ lib/msfenv.rb | 41 +++++++++++++++ spec/zeitwerk_compliance_spec.rb | 14 ++++++ 4 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 spec/zeitwerk_compliance_spec.rb diff --git a/config/application.rb b/config/application.rb index 2a7089f73567..bda8166b912e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -47,7 +47,7 @@ class Application < Rails::Application when "test" config.eager_load = false when "production" - config.eager_load = true + config.eager_load = false end if ActiveRecord.respond_to?(:legacy_connection_handling=) diff --git a/lib/msf/core/web_services/http_db_manager_service.rb b/lib/msf/core/web_services/http_db_manager_service.rb index ef3d8a20fb48..90ebf1457cfd 100644 --- a/lib/msf/core/web_services/http_db_manager_service.rb +++ b/lib/msf/core/web_services/http_db_manager_service.rb @@ -61,43 +61,31 @@ def require_environment!(parsed_options) Rails.application.require_environment! end - # def init_servlets(http_server) - # servlet_path = File.dirname(__FILE__) + '/servlet/*' - # Dir.glob(servlet_path).collect{|file_path| - # servlet_class = File.basename(file_path, '.rb').classify - # require file_path - # servlet_class_constant = servlet_class.constantize - # http_server.mount servlet_class_constant.api_path, servlet_class_constant - # } - # end - - - -end - + def print_line(msg) + $console_printer.print_line(msg) + end -def print_line(msg) - $console_printer.print_line(msg) -end + def print_warning(msg) + $console_printer.print_warning(msg) + end -def print_warning(msg) - $console_printer.print_warning(msg) -end + def print_good(msg) + $console_printer.print_good(msg) + end -def print_good(msg) - $console_printer.print_good(msg) -end + def print_error(msg, exception = nil) + unless exception.nil? + msg += "\n Call Stack:" + exception.backtrace.each {|line| + msg += "\n" + msg += "\t #{line}" + } + end -def print_error(msg, exception = nil) - unless exception.nil? - msg += "\n Call Stack:" - exception.backtrace.each {|line| - msg += "\n" - msg += "\t #{line}" - } + $console_printer.print_error(msg) end - $console_printer.print_error(msg) + end $console_printer = Rex::Ui::Text::Output::Stdio.new diff --git a/lib/msfenv.rb b/lib/msfenv.rb index b7cc04296ba5..5f51a85bb936 100644 --- a/lib/msfenv.rb +++ b/lib/msfenv.rb @@ -41,3 +41,44 @@ def noop_error_formatter.message_for(_spot) end MsfAutoload.instance + +def _warn_deprecation_message(method) + stack_size = 3 + warning_message = "[DEPRECATION] The global method #{method.inspect} is deprecated, please raise a Github issue with this output. Called from: #{caller(1, stack_size).to_a}" + warn(warning_message) + # Additionally write to ~/.msf4/logs/framework.log - as this gets attached to Github issues etc + elog(warning_message) +end + +# @deprecated In most scenarios you should delegate to either a framework module object, or Rex::Ui::Text::DispatcherShell etc +def print_line(msg) + _warn_deprecation_message __method__ + $stdout.puts(msg) +end + +# @deprecated In most scenarios you should delegate to either a framework module object, or Rex::Ui::Text::DispatcherShell etc +def print_warning(msg) + _warn_deprecation_message __method__ + $stderr.puts(msg) +end + +# @deprecated In most scenarios you should delegate to either a framework module object, or Rex::Ui::Text::DispatcherShell etc +def print_good(msg) + _warn_deprecation_message __method__ + $stdout.puts(msg) +end + +# @deprecated In most scenarios you should delegate to either a framework module object, or Rex::Ui::Text::DispatcherShell etc +def print_error(msg, exception = nil) + _warn_deprecation_message __method__ + + unless exception.nil? + msg += "\n Call Stack:" + exception.backtrace.each {|line| + msg += "\n" + msg += "\t #{line}" + } + end + + $stderr.puts(msg) +end diff --git a/spec/zeitwerk_compliance_spec.rb b/spec/zeitwerk_compliance_spec.rb new file mode 100644 index 000000000000..89d51c726924 --- /dev/null +++ b/spec/zeitwerk_compliance_spec.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require 'spec_helper' + +# https://github.com/rails/rails/blob/0fec9536ca43e209064e60f48b0def6bfe539fe2/guides/source/classic_to_zeitwerk_howto.md#rspec +RSpec.describe 'ZeitwerkCompliance' do + it 'loads all files without errors' do + expect do + # Ensure we've configured zeitwerk + require 'msfenv' + Zeitwerk::Loader.eager_load_all + end.not_to raise_error + end +end