Class: Metasploit::Model::Search::Operation::Jsonb

Inherits:
Base
  • Object
show all
Defined in:
app/models/metasploit/model/search/operation/jsonb.rb

Overview

Search operation with Base#operator with #type ':jsonb'.

Instance Attribute Summary

Attributes inherited from Base

#operator, #value

Instance Method Summary collapse

Methods inherited from Base

#operator_valid

Methods inherited from Base

#initialize, #valid!

Constructor Details

This class inherits a constructor from Metasploit::Model::Base

Instance Method Details

#transform_value(input) ⇒ String (private)

Transform an input String to a JSON if it contains a colon. It returns the String if not. Also, the first colon is used as a delimiter between the key and the value. Any subsequent colon will be part of the value. Finally, it handles double/single quotes to escape any colon that are not suppose to be a delimiter between the key and the value.

Parameters:

  • input (#to_s)

Returns:

  • (String)

    representing a JSON if input contains a colon. Otherwise it is a the same as input



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/metasploit/model/search/operation/jsonb.rb', line 36

def transform_value(input)
  # Regex to find the first colon that is NOT inside quotes
  match = input.match(/((?:[^'":]++|"(?:\\.|[^"])*"|'(?:\\.|[^'])*')*?):(.*)/)

  # If no valid colon is found, return the original string
  return input unless match

  key = match[1].strip  # Extract key (before first valid colon)
  value = match[2].strip # Extract value (after first valid colon)

  # Remove starting and ending quotes and ensure they are valid JSON strings
  key = key.gsub(/^["'](.*)["']$/, '\1').to_json
  value = value.gsub(/^["'](.*)["']$/, '\1').to_json
  "{#{key}: #{value}}"
end

#value=(formatted_value) ⇒ String

Sets Base#value by parsing the formatted_value String and attempting to generate a valid JSON if it contains a colon. Otherwise, it keeps the same value as a String.

Parameters:

  • formatted_value (#to_s)

Returns:

  • (String)

    representing a JSON if formatted_value contains a colon. Otherwise it is a the same as formatted_value



21
22
23
# File 'app/models/metasploit/model/search/operation/jsonb.rb', line 21

def value=(formatted_value)
  @value = transform_value(formatted_value.to_s)
end