Skip to content

Commit

Permalink
respect escaped quotes in the valueRxString regex
Browse files Browse the repository at this point in the history
  • Loading branch information
beniwohli committed Mar 9, 2016
1 parent 189cd92 commit 8b04a70
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
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

0 comments on commit 8b04a70

Please sign in to comment.