Skip to content
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

respect escaped quotes in the valueRxString regex #27

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/logstash/filters/kv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ class LogStash::Filters::KV < LogStash::Filters::Base
def register
@trim_re = Regexp.new("[#{@trim}]") if @trim
@trimkey_re = Regexp.new("[#{@trimkey}]") if @trimkey

valueRxString = "(?:\"([^\"]+)\"|'([^']+)'"
doubleQuotes = '"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"'
singleQuotes = "'([^'\\\\]*(?:\\\\.[^'\\\\]*)*)'"
valueRxString = "(?:" + doubleQuotes + '|' + singleQuotes
valueRxString += "|\\(([^\\)]+)\\)|\\[([^\\]]+)\\]" if @include_brackets
valueRxString += "|((?:\\\\ |[^" + @field_split + "])+))"
@scan_re = Regexp.new("((?:\\\\ |[^" + @field_split + @value_split + "])+)\s*[" + @value_split + "]\s*" + valueRxString)
Expand Down
68 changes: 46 additions & 22 deletions spec/filters/kv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' bracketsone=(hello world) bracketstwo=[hello world]" do
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' bracketsone=(hello world) bracketstwo=[hello world] singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
insist { subject["hello"] } == "world"
insist { subject["foo"] } == "bar"
insist { subject["baz"] } == "fizz"
insist { subject["doublequoted"] } == "hello world"
insist { subject["singlequoted"] } == "hello world"
insist { subject["bracketsone"] } == "hello world"
insist { subject["bracketstwo"] } == "hello world"
insist { subject["singlequotedescaped"] } == "hello\\' world"
insist { subject["doublequotedescaped"] } == "hello\\\" world"
end
end

Expand All @@ -32,13 +34,15 @@
}
CONFIG

sample "hello = world foo =bar baz= fizz doublequoted = \"hello world\" singlequoted= 'hello world' brackets =(hello world)" do
sample "hello = world foo =bar baz= fizz doublequoted = \"hello world\" singlequoted= 'hello world' brackets =(hello world) singlequotedescaped ='hello\\' world' doublequotedescaped= \"hello\\\" world\"" do
insist { subject["hello"] } == "world"
insist { subject["foo"] } == "bar"
insist { subject["baz"] } == "fizz"
insist { subject["doublequoted"] } == "hello world"
insist { subject["singlequoted"] } == "hello world"
insist { subject["brackets"] } == "hello world"
insist { subject["singlequotedescaped"] } == "hello\\' world"
insist { subject["doublequotedescaped"] } == "hello\\\" world"
end
end

Expand All @@ -63,13 +67,15 @@
}
CONFIG

sample "hello:=world foo:bar baz=:fizz doublequoted:\"hello world\" singlequoted:'hello world' brackets:(hello world)" do
sample "hello:=world foo:bar baz=:fizz doublequoted:\"hello world\" singlequoted:'hello world' brackets:(hello world) singlequotedescaped:'hello\\' world' doublequotedescaped:\"hello\\\" world\"" do
insist { subject["hello"] } == "=world"
insist { subject["foo"] } == "bar"
insist { subject["baz="] } == "fizz"
insist { subject["doublequoted"] } == "hello world"
insist { subject["singlequoted"] } == "hello world"
insist { subject["brackets"] } == "hello world"
insist { subject["singlequotedescaped"] } == "hello\\' world"
insist { subject["doublequotedescaped"] } == "hello\\\" world"
end
end
end
Expand Down Expand Up @@ -167,13 +173,15 @@
}
CONFIG

sample "?hello=world&foo=bar&baz=fizz&doublequoted=\"hello world\"&singlequoted='hello world'&ignoreme&foo12=bar12" do
sample "?hello=world&foo=bar&baz=fizz&doublequoted=\"hello world\"&singlequoted='hello world'&ignoreme&foo12=bar12&singlequotedescaped='hello\\' world'&doublequotedescaped=\"hello\\\" world\"" do
insist { subject["hello"] } == "world"
insist { subject["foo"] } == "bar"
insist { subject["baz"] } == "fizz"
insist { subject["doublequoted"] } == "hello world"
insist { subject["singlequoted"] } == "hello world"
insist { subject["foo12"] } == "bar12"
insist { subject["singlequotedescaped"] } == "hello\\' world"
insist { subject["doublequotedescaped"] } == "hello\\\" world"
end
end

Expand Down Expand Up @@ -228,12 +236,14 @@
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'" do
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
insist { subject["__hello"] } == "world"
insist { subject["__foo"] } == "bar"
insist { subject["__baz"] } == "fizz"
insist { subject["__doublequoted"] } == "hello world"
insist { subject["__singlequoted"] } == "hello world"
insist { subject["__singlequotedescaped"] } == "hello\\' world"
insist { subject["__doublequotedescaped"] } == "hello\\\" world"
end

end
Expand Down Expand Up @@ -326,13 +336,15 @@
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'" do
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
insist { subject["kv"]["hello"] } == "world"
insist { subject["kv"]["foo"] } == "bar"
insist { subject["kv"]["baz"] } == "fizz"
insist { subject["kv"]["doublequoted"] } == "hello world"
insist { subject["kv"]["singlequoted"] } == "hello world"
insist {subject["kv"].count } == 5
insist { subject["kv"]["singlequotedescaped"] } == "hello\\' world"
insist { subject["kv"]["doublequotedescaped"] } == "hello\\\" world"
insist {subject["kv"].count } == 7
end

end
Expand All @@ -358,12 +370,14 @@
}
}
CONFIG
sample("data" => "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'") do
sample("data" => "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"") do
insist { subject["hello"] } == "world"
insist { subject["foo"] } == "bar"
insist { subject["baz"] } == "fizz"
insist { subject["doublequoted"] } == "hello world"
insist { subject["singlequoted"] } == "hello world"
insist { subject["singlequotedescaped"] } == "hello\\' world"
insist { subject["doublequotedescaped"] } == "hello\\\" world"
end
end

Expand All @@ -375,12 +389,14 @@
}
}
CONFIG
sample({"@data" => "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'"}) do
sample({"@data" => "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\""}) do
insist { subject["hello"] } == "world"
insist { subject["foo"] } == "bar"
insist { subject["baz"] } == "fizz"
insist { subject["doublequoted"] } == "hello world"
insist { subject["singlequoted"] } == "hello world"
insist { subject["singlequotedescaped"] } == "hello\\' world"
insist { subject["doublequotedescaped"] } == "hello\\\" world"
end
end

Expand All @@ -394,13 +410,15 @@
}
}
CONFIG
sample("data" => "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'") do
sample("data" => "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"") do
insist { subject["kv"]["hello"] } == "world"
insist { subject["kv"]["foo"] } == "bar"
insist { subject["kv"]["baz"] } == "fizz"
insist { subject["kv"]["doublequoted"] } == "hello world"
insist { subject["kv"]["singlequoted"] } == "hello world"
insist { subject["kv"].count } == 5
insist { subject["kv"]["singlequotedescaped"] } == "hello\\' world"
insist { subject["kv"]["doublequotedescaped"] } == "hello\\\" world"
insist { subject["kv"].count } == 7
end
end

Expand All @@ -423,63 +441,67 @@
config <<-CONFIG
filter {
kv {
include_keys => [ "foo", "singlequoted" ]
include_keys => [ "foo", "singlequoted", "doublequotedescaped" ]
}
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'" do
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
insist { subject["foo"] } == "bar"
insist { subject["singlequoted"] } == "hello world"
insist { subject["doublequotedescaped"] } == "hello\\\" world"
end
end

describe "test exclude_keys" do
config <<-CONFIG
filter {
kv {
exclude_keys => [ "foo", "singlequoted" ]
exclude_keys => [ "foo", "singlequoted", "doublequotedescaped" ]
}
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'" do
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
insist { subject["hello"] } == "world"
insist { subject["baz"] } == "fizz"
insist { subject["doublequoted"] } == "hello world"
insist { subject["singlequotedescaped"] } == "hello\\' world"
end
end

describe "test include_keys with prefix" do
config <<-CONFIG
filter {
kv {
include_keys => [ "foo", "singlequoted" ]
include_keys => [ "foo", "singlequoted", "doublequotedescaped" ]
prefix => "__"
}
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'" do
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
insist { subject["__foo"] } == "bar"
insist { subject["__singlequoted"] } == "hello world"
insist { subject["__doublequotedescaped"] } == "hello\\\" world"
end
end

describe "test exclude_keys with prefix" do
config <<-CONFIG
filter {
kv {
exclude_keys => [ "foo", "singlequoted" ]
exclude_keys => [ "foo", "singlequoted", "doublequotedescaped" ]
prefix => "__"
}
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'" do
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
insist { subject["__hello"] } == "world"
insist { subject["__baz"] } == "fizz"
insist { subject["__doublequoted"] } == "hello world"
insist { subject["__singlequotedescaped"] } == "hello\\' world"
end
end

Expand Down Expand Up @@ -526,8 +548,8 @@
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'" do
%w(hello foo baz doublequoted singlequoted).each do |field|
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
%w(hello foo baz doublequoted singlequoted singlequotedescaped doublequotedescaped).each do |field|
reject { subject }.include?(field)
end
end
Expand All @@ -543,13 +565,15 @@
}
CONFIG

sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world'" do
sample "hello=world foo=bar baz=fizz doublequoted=\"hello world\" singlequoted='hello world' singlequotedescaped='hello\\' world' doublequotedescaped=\"hello\\\" world\"" do
insist { subject["hello"] } == "world"
insist { subject["foo"] } == "bar"
insist { subject["goo"] } == "yyy"
insist { subject["baz"] } == "fizz"
insist { subject["doublequoted"] } == "hello world"
insist { subject["singlequoted"] } == "hello world"
insist { subject["singlequotedescaped"] } == "hello\\' world"
insist { subject["doublequotedescaped"] } == "hello\\\" world"
end
end

Expand Down