Skip to content

Commit

Permalink
Errors moved into their own namespace - Ref #1315
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanlr committed Mar 19, 2019
1 parent 357e13b commit f165716
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 69 deletions.
6 changes: 3 additions & 3 deletions app/controllers/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def local_db
# @return [ Boolean ]
def update_db_required?
if local_db.missing_files?
raise MissingDatabaseFile if parsed_options[:update] == false
raise Error::MissingDatabaseFile if parsed_options[:update] == false

return true
end
Expand Down Expand Up @@ -62,7 +62,7 @@ def before_scan
# Raises errors if the target is hosted on wordpress.com or is not running WordPress
# Also check if the homepage_url is still the install url
def check_wordpress_state
raise WordPressHostedError if target.wordpress_hosted?
raise Error::WordPressHosted if target.wordpress_hosted?

if Addressable::URI.parse(target.homepage_url).path =~ %r{/wp-admin/install.php$}i

Expand All @@ -71,7 +71,7 @@ def check_wordpress_state
exit(WPScan::ExitCode::VULNERABLE)
end

raise NotWordPressError unless target.wordpress?(parsed_options[:detection_mode]) || parsed_options[:force]
raise Error::NotWordPress unless target.wordpress?(parsed_options[:detection_mode]) || parsed_options[:force]
end

# Loads the related server module in the target
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/custom_directories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def before_scan

return if target.content_dir

raise WpContentDirNotDetected
raise Error::WpContentDirNotDetected
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/password_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def attacker_from_cli_options
when :wp_login
WPScan::Finders::Passwords::WpLogin.new(target)
when :xmlrpc
raise XMLRPCNotDetected unless xmlrpc
raise Error::XMLRPCNotDetected unless xmlrpc

WPScan::Finders::Passwords::XMLRPC.new(xmlrpc)
when :xmlrpc_multicall
raise XMLRPCNotDetected unless xmlrpc
raise Error::XMLRPCNotDetected unless xmlrpc

WPScan::Finders::Passwords::XMLRPCMulticall.new(xmlrpc)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/wp_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class WpVersion < CMSScanner::Version
include Vulnerable

def initialize(number, opts = {})
raise InvalidWordPressVersion unless WpVersion.valid?(number.to_s)
raise Error::InvalidWordPressVersion unless WpVersion.valid?(number.to_s)

super(number, opts)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/wpscan/db/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def remote_file_checksum(filename)
url = "#{remote_file_url(filename)}.sha512"

res = Browser.get(url, request_params)
raise DownloadError, res if res.timed_out? || res.code != 200
raise Error::Download, res if res.timed_out? || res.code != 200

res.body.chomp
end
Expand Down Expand Up @@ -121,7 +121,7 @@ def download(filename)
file_url = remote_file_url(filename)

res = Browser.get(file_url, request_params)
raise DownloadError, res if res.timed_out? || res.code != 200
raise Error::Download, res if res.timed_out? || res.code != 200

File.open(file_path, 'wb') { |f| f.write(res.body) }

Expand Down
6 changes: 5 additions & 1 deletion lib/wpscan/errors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module WPScan
class Error < StandardError
module Error
include CMSScanner::Error

class Standard < StandardError
end
end
end

Expand Down
48 changes: 25 additions & 23 deletions lib/wpscan/errors/http.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
module WPScan
# HTTP Error
class HTTPError < Error
attr_reader :response
module Error
# HTTP Error
class HTTP < Standard
attr_reader :response

# @param [ Typhoeus::Response ] res
def initialize(response)
@response = response
end
# @param [ Typhoeus::Response ] res
def initialize(response)
@response = response
end

def failure_details
msg = response.effective_url
def failure_details
msg = response.effective_url

msg += if response.code.zero? || response.timed_out?
" (#{response.return_message})"
else
" (status: #{response.code})"
end
msg += if response.code.zero? || response.timed_out?
" (#{response.return_message})"
else
" (status: #{response.code})"
end

msg
end
msg
end

def to_s
"HTTP Error: #{failure_details}"
def to_s
"HTTP Error: #{failure_details}"
end
end
end

# Used in the Updater
class DownloadError < HTTPError
def to_s
"Unable to get #{failure_details}"
# Used in the Updater
class Download < HTTP
def to_s
"Unable to get #{failure_details}"
end
end
end
end
10 changes: 6 additions & 4 deletions lib/wpscan/errors/update.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module WPScan
# Error raised when there is a missing DB file and --no-update supplied
class MissingDatabaseFile < Error
def to_s
'Update required, you can not run a scan if a database file is missing.'
module Error
# Error raised when there is a missing DB file and --no-update supplied
class MissingDatabaseFile < Standard
def to_s
'Update required, you can not run a scan if a database file is missing.'
end
end
end
end
38 changes: 20 additions & 18 deletions lib/wpscan/errors/wordpress.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
module WPScan
# WordPress hosted (*.wordpress.com)
class WordPressHostedError < Error
def to_s
'Scanning *.wordpress.com hosted blogs is not supported.'
module Error
# WordPress hosted (*.wordpress.com)
class WordPressHosted < Standard
def to_s
'Scanning *.wordpress.com hosted blogs is not supported.'
end
end
end

# Not WordPress Error
class NotWordPressError < Error
def to_s
'The remote website is up, but does not seem to be running WordPress.'
# Not WordPress Error
class NotWordPress < Standard
def to_s
'The remote website is up, but does not seem to be running WordPress.'
end
end
end

# Invalid Wp Version (used in the WpVersion#new)
class InvalidWordPressVersion < Error
def to_s
'The WordPress version is invalid'
# Invalid Wp Version (used in the WpVersion#new)
class InvalidWordPressVersion < Standard
def to_s
'The WordPress version is invalid'
end
end
end

class WpContentDirNotDetected < Error
def to_s
'Unable to identify the wp-content dir, please supply it with --wp-content-dir'
class WpContentDirNotDetected < Standard
def to_s
'Unable to identify the wp-content dir, please supply it with --wp-content-dir'
end
end
end
end
10 changes: 6 additions & 4 deletions lib/wpscan/errors/xmlrpc.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module WPScan
# XML-RPC Not Detected
class XMLRPCNotDetected < Error
def to_s
'The XML-RPC Interface was not detected.'
module Error
# XML-RPC Not Detected
class XMLRPCNotDetected < Standard
def to_s
'The XML-RPC Interface was not detected.'
end
end
end
end
2 changes: 1 addition & 1 deletion lib/wpscan/finders/finder/wp_version/smart_url_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create_version(number, opts = {})
confidence: opts[:confidence] || 80,
interesting_entries: opts[:entries]
)
rescue WPScan::InvalidWordPressVersion
rescue WPScan::Error::InvalidWordPressVersion
nil # Invalid Version returned as nil and will be ignored by Finders
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/app/controllers/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
let(:cli_args) { "#{super()} --no-update" }

it 'raises an error' do
expect { core.update_db_required? }. to raise_error(WPScan::MissingDatabaseFile)
expect { core.update_db_required? }. to raise_error(WPScan::Error::MissingDatabaseFile)
end
end

Expand Down Expand Up @@ -199,7 +199,7 @@
let(:redirection) { 'http://g.com/' }

it 'raises an error' do
expect { core.before_scan }.to raise_error(CMSScanner::HTTPRedirectError)
expect { core.before_scan }.to raise_error(CMSScanner::Error::HTTPRedirect)
end
end

Expand All @@ -218,7 +218,7 @@
it 'raises an error' do
expect(core.target).to receive(:wordpress?).with(:mixed).and_return(false)

expect { core.before_scan }.to raise_error(WPScan::NotWordPressError)
expect { core.before_scan }.to raise_error(WPScan::Error::NotWordPress)
end
end
end
Expand All @@ -230,7 +230,7 @@
before { expect(core).to receive(:load_server_module) }

it 'raises an error' do
expect { core.before_scan }.to raise_error(WPScan::WordPressHostedError)
expect { core.before_scan }.to raise_error(WPScan::Error::WordPressHosted)
end
end

Expand All @@ -253,7 +253,7 @@

context 'when no --force' do
it 'raises an error' do
expect { core.before_scan }.to raise_error(WPScan::NotWordPressError)
expect { core.before_scan }.to raise_error(WPScan::Error::NotWordPress)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/app/controllers/custom_directories_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
before { expect(controller.target).to receive(:content_dir) }

it 'raises an exception' do
expect { controller.before_scan }.to raise_error(WPScan::WpContentDirNotDetected)
expect { controller.before_scan }.to raise_error(WPScan::Error::WpContentDirNotDetected)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/app/controllers/password_attack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@
let(:attack) { 'xmlrpc' }

it 'raises an error' do
expect { controller.attacker }.to raise_error(WPScan::XMLRPCNotDetected)
expect { controller.attacker }.to raise_error(WPScan::Error::XMLRPCNotDetected)
end
end

context 'when xmlrpc-multicall' do
let(:attack) { 'xmlrpc-multicall' }

it 'raises an error' do
expect { controller.attacker }.to raise_error(WPScan::XMLRPCNotDetected)
expect { controller.attacker }.to raise_error(WPScan::Error::XMLRPCNotDetected)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/app/models/wp_version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
describe '#new' do
context 'when invalid number' do
it 'raises an error' do
expect { described_class.new('aa') }.to raise_error WPScan::InvalidWordPressVersion
expect { described_class.new('aa') }.to raise_error WPScan::Error::InvalidWordPressVersion
end
end

Expand Down

0 comments on commit f165716

Please sign in to comment.