Skip to content

Commit 103b829

Browse files
committed
Land rapid7#5183, Improve developer experience for fail_with
2 parents ed9175d + 37613ad commit 103b829

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/msf/core/exploit.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,10 +1218,31 @@ def reset_session_counts
12181218
# Failure tracking
12191219
##
12201220

1221+
# Raises a Msf::Exploit::Failed exception. It overrides the fail_with method
1222+
# in lib/msf/core/module.rb
1223+
#
1224+
# @param reason [String] A constant from Msf::Module::Failure.
1225+
# If the reason does not come from there, then it will default to
1226+
# Msf::Module::Failure::Unknown.
1227+
# @param mssg [String] (Optional) A message about the failure.
1228+
# @raise [Msf::Exploit::Failed] A custom Msf::Exploit::Failed exception.
1229+
# @return [void]
1230+
# @see Msf::Module::Failure
1231+
# @see Msf::Module#fail_with
1232+
# @example
1233+
# fail_with(Msf::Module::Failure::NoAccess, 'Unable to login to upload payload')
12211234
def fail_with(reason,msg=nil)
1222-
self.fail_reason = reason
1235+
# The reason being registered here will be used later on, so it's important we don't actually
1236+
# provide a made-up one.
1237+
allowed_values = Msf::Module::Failure.constants.collect {|e| Msf::Module::Failure.const_get(e)}
1238+
if allowed_values.include?(reason)
1239+
self.fail_reason = reason
1240+
else
1241+
self.fail_reason = Msf::Module::Failure::Unknown
1242+
end
1243+
12231244
self.fail_detail = msg
1224-
raise Msf::Exploit::Failed, (msg || "No reason given")
1245+
raise Msf::Exploit::Failed, (msg || "No failure message given")
12251246
end
12261247

12271248
def report_failure

lib/msf/core/module.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,18 @@ def debugging?
276276
end
277277

278278
#
279-
# Support fail_with for all module types, allow specific classes to override
279+
# Raises a RuntimeError failure message. This is meant to be used for all non-exploits,
280+
# and allows specific classes to override.
281+
#
282+
# @param reason [String] A reason about the failure.
283+
# @param msg [String] (Optional) A message about the failure.
284+
# @raise [RuntimeError]
285+
# @return [void]
286+
# @note If you are writing an exploit, you don't use this API. Instead, please refer to the
287+
# API documentation from lib/msf/core/exploit.rb.
288+
# @see Msf::Exploit#fail_with
289+
# @example
290+
# fail_with('No Access', 'Unable to login')
280291
#
281292
def fail_with(reason, msg=nil)
282293
raise RuntimeError, "#{reason.to_s}: #{msg}"

0 commit comments

Comments
 (0)