Browse Source

allow nil assignment to the datastore

Brent Cook 3 years ago
parent
commit
fe1b24e666

+ 1
- 1
lib/msf/core/data_store.rb View File

@@ -30,7 +30,7 @@ class DataStore < Hash
30 30
     opt = @options[k]
31 31
     unless opt.nil?
32 32
       if opt.validate_on_assignment?
33
-        unless opt.valid?(v)
33
+        unless opt.valid?(v, check_empty: false)
34 34
           raise OptionValidateError.new(["Value '#{v}' is not valid for option '#{k}'"])
35 35
         end
36 36
         v = opt.normalize(v)

+ 2
- 2
lib/msf/core/opt_address.rb View File

@@ -12,8 +12,8 @@ class OptAddress < OptBase
12 12
     return 'address'
13 13
   end
14 14
 
15
-  def valid?(value)
16
-    return false if empty_required_value?(value)
15
+  def valid?(value, check_empty: true)
16
+    return false if check_empty && empty_required_value?(value)
17 17
     return false unless value.kind_of?(String) or value.kind_of?(NilClass)
18 18
 
19 19
     if (value != nil and value.empty? == false)

+ 2
- 2
lib/msf/core/opt_address_range.rb View File

@@ -35,8 +35,8 @@ class OptAddressRange < OptBase
35 35
     return value
36 36
   end
37 37
 
38
-  def valid?(value)
39
-    return false if empty_required_value?(value)
38
+  def valid?(value, check_empty: true)
39
+    return false if check_empty && empty_required_value?(value)
40 40
     return false unless value.kind_of?(String) or value.kind_of?(NilClass)
41 41
 
42 42
     if (value != nil and value.empty? == false)

+ 2
- 2
lib/msf/core/opt_base.rb View File

@@ -85,8 +85,8 @@ module Msf
85 85
     #
86 86
     # If it's required and the value is nil or empty, then it's not valid.
87 87
     #
88
-    def valid?(value)
89
-      if required?
88
+    def valid?(value, check_empty: true)
89
+      if check_empty && required?
90 90
         # required variable not set
91 91
         return false if (value == nil or value.to_s.empty?)
92 92
       end

+ 2
- 2
lib/msf/core/opt_bool.rb View File

@@ -15,8 +15,8 @@ class OptBool < OptBase
15 15
     return 'bool'
16 16
   end
17 17
 
18
-  def valid?(value)
19
-    return false if empty_required_value?(value)
18
+  def valid?(value, check_empty: false)
19
+    return false if check_empty && empty_required_value?(value)
20 20
 
21 21
     if ((value != nil and
22 22
         (value.to_s.empty? == false) and

+ 2
- 2
lib/msf/core/opt_enum.rb View File

@@ -13,8 +13,8 @@ class OptEnum < OptBase
13 13
     return 'enum'
14 14
   end
15 15
 
16
-  def valid?(value=self.value)
17
-    return false if empty_required_value?(value)
16
+  def valid?(value=self.value, check_empty: true)
17
+    return false if check_empty && empty_required_value?(value)
18 18
     return true if value.nil? and !required?
19 19
 
20 20
     (value and self.enums.include?(value.to_s))

+ 3
- 4
lib/msf/core/opt_int.rb View File

@@ -13,16 +13,15 @@ class OptInt < OptBase
13 13
   end
14 14
 
15 15
   def normalize(value)
16
-    if (value.to_s.match(/^0x[a-fA-F\d]+$/))
16
+    if value.to_s.match(/^0x[a-fA-F\d]+$/)
17 17
       value.to_i(16)
18 18
     else
19 19
       value.to_i
20 20
     end
21 21
   end
22 22
 
23
-  def valid?(value)
24
-    return super if !required? and value.to_s.empty?
25
-    return false if empty_required_value?(value)
23
+  def valid?(value, check_empty: true)
24
+    return false if check_empty && empty_required_value?(value)
26 25
 
27 26
     if value and not value.to_s.match(/^0x[0-9a-fA-F]+$|^-?\d+$/)
28 27
       return false

+ 2
- 2
lib/msf/core/opt_path.rb View File

@@ -17,8 +17,8 @@ class OptPath < OptBase
17 17
   end
18 18
 
19 19
   # Generally, 'value' should be a file that exists.
20
-  def valid?(value)
21
-    return false if empty_required_value?(value)
20
+  def valid?(value, check_empty: true)
21
+    return false if check_empty && empty_required_value?(value)
22 22
     if value and !value.empty?
23 23
       if value =~ /^memory:\s*([0-9]+)/i
24 24
         return false unless check_memory_location($1)

+ 3
- 6
lib/msf/core/opt_port.rb View File

@@ -12,12 +12,9 @@ class OptPort < OptInt
12 12
     return 'port'
13 13
   end
14 14
 
15
-  def valid?(value)
16
-    if !required? and value.to_s.empty?
17
-      super
18
-    else
19
-      super && normalize(value) <= 65535 && normalize(value) >= 0
20
-    end
15
+  def valid?(value, check_empty: true)
16
+    port = normalize(value)
17
+    super && port <= 65535 && port >= 0
21 18
   end
22 19
 end
23 20
 

+ 3
- 7
lib/msf/core/opt_regexp.rb View File

@@ -12,16 +12,12 @@ class OptRegexp < OptBase
12 12
     return 'regexp'
13 13
   end
14 14
 
15
-  def valid?(value)
16
-    unless super
17
-      return false
18
-    end
19
-    return true if (not required? and value.nil?)
15
+  def valid?(value, check_empty: true)
16
+    return false if check_empty && empty_required_value?(value)
20 17
 
21 18
     begin
22 19
       Regexp.compile(value)
23
-
24
-      return true
20
+      return super
25 21
     rescue RegexpError, TypeError
26 22
       return false
27 23
     end

+ 2
- 2
lib/msf/core/opt_string.rb View File

@@ -28,9 +28,9 @@ class OptString < OptBase
28 28
     value
29 29
   end
30 30
 
31
-  def valid?(value=self.value)
31
+  def valid?(value=self.value, check_empty: true)
32 32
     value = normalize(value)
33
-    return false if empty_required_value?(value)
33
+    return false if check_empty && empty_required_value?(value)
34 34
     return super
35 35
   end
36 36
 end

+ 1
- 1
spec/lib/rex/proto/http/client_request_spec.rb View File

@@ -180,7 +180,7 @@ RSpec.describe Rex::Proto::Http::ClientRequest do
180 180
         :set_content_len_header => { args: 0, result: "Content-Length: 0\r\n"}
181 181
       }
182 182
     ],
183
-  
183
+
184 184
   ].each do |c, opts, expectations|
185 185
     context c do
186 186
       subject(:client_request) { Rex::Proto::Http::ClientRequest.new(opts) }

Loading…
Cancel
Save