-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add search operation model for JSONB #71
Open
cdelafuente-r7
wants to merge
2
commits into
rapid7:master
Choose a base branch
from
cdelafuente-r7:feat/model/search/operation/jsonb
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Search operation with {Metasploit::Model::Search::Operation::Base#operator} with `#type` ':jsonb'. | ||
class Metasploit::Model::Search::Operation::Jsonb < Metasploit::Model::Search::Operation::Base | ||
# | ||
# Validations | ||
# | ||
|
||
validates :value, | ||
:presence => true | ||
|
||
# | ||
# Methods | ||
# | ||
|
||
# Sets {Metasploit::Model::Search::Operation::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. | ||
# | ||
# @param formatted_value [#to_s] | ||
# @return [String] representing a JSON if `formatted_value` contains a colon. | ||
# Otherwise it is a the same as `formatted_value` | ||
def value=(formatted_value) | ||
@value = transform_value(formatted_value.to_s) | ||
end | ||
|
||
|
||
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. | ||
# | ||
# @param input [#to_s] | ||
# @return [String] representing a JSON if `input` contains a colon. Otherwise it is a the same as `input` | ||
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 | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
spec/app/models/metasploit/model/search/operation/jsonb_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
RSpec.describe Metasploit::Model::Search::Operation::Jsonb, type: :model do | ||
context 'validation' do | ||
context 'value' do | ||
before(:example) do | ||
operation.valid? | ||
end | ||
|
||
let(:errors) do | ||
operation.errors[:value] | ||
end | ||
|
||
let(:operation) do | ||
described_class.new(:value => value) | ||
end | ||
|
||
context 'with String' do | ||
let(:value) do | ||
'search_string' | ||
end | ||
|
||
it 'should not record error' do | ||
expect(errors).to be_empty | ||
end | ||
end | ||
|
||
context 'with Integer' do | ||
let(:value) do | ||
3 | ||
end | ||
|
||
it 'should not record error' do | ||
expect(errors).to be_empty | ||
end | ||
end | ||
|
||
context 'with a Symbol' do | ||
let(:value) do | ||
:mysym | ||
end | ||
|
||
it 'should not record error' do | ||
expect(errors).to be_empty | ||
end | ||
end | ||
end | ||
end | ||
|
||
context '#value' do | ||
subject(:value) do | ||
operation.value | ||
end | ||
|
||
let(:operation) do | ||
described_class.new(:value => formatted_value) | ||
end | ||
|
||
context 'with String' do | ||
let(:formatted_value) do | ||
'test value' | ||
end | ||
|
||
it 'should be passed as a String' do | ||
expect(value).to eq(formatted_value.to_s) | ||
end | ||
end | ||
|
||
context 'with Integer' do | ||
let(:formatted_value) do | ||
3 | ||
end | ||
|
||
it 'should be passed as a String' do | ||
expect(value).to eq(formatted_value.to_s) | ||
end | ||
end | ||
|
||
context 'with String containing colon characters' do | ||
{ | ||
'key:value' => '{"key": "value"}', | ||
'key:value:extra' => '{"key": "value:extra"}', | ||
'"quoted:part":value' => '{"quoted:part": "value"}', | ||
'"quoted:part":value:extra' => '{"quoted:part": "value:extra"}', | ||
'a:b:c:d' => '{"a": "b:c:d"}', | ||
'"x:y:z":a:b' => '{"x:y:z": "a:b"}', | ||
"'single:quote':value" => '{"single:quote": "value"}', | ||
"'single:quote':value:extra" => '{"single:quote": "value:extra"}', | ||
"'a:b':c:d" => '{"a:b": "c:d"}', | ||
'"x:y"and"z:w":final' => '{"x:y\"and\"z:w": "final"}', | ||
"'x:y'and'z:w':final" => '{"x:y\'and\'z:w": "final"}', | ||
'"a:b":c:"d:e"' => '{"a:b": "c:\"d:e\""}', | ||
"'a:b':c:'d:e'" => '{"a:b": "c:\'d:e\'"}', | ||
}.each do |input, expected| | ||
|
||
context "with the string #{input}" do | ||
let(:formatted_value) { input } | ||
|
||
it 'should be passed as a valid JSON string' do | ||
expect(value).to eq(expected) | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to: https://github.com/rapid7/metasploit-framework/pull/19820/files