logstash-codec-json icon indicating copy to clipboard operation
logstash-codec-json copied to clipboard

Codec should not accept invalid json {}{}

Open jordansissel opened this issue 8 years ago • 1 comments

The codec should not accept this input:

{ "hello": "world" }{ "goodbye": "moon" }

The above is two json objects that, alone, are valid, but combined, are not.

Expected result: Logstash should tag this event as _jsonparsefailure and store the whole payload in the message field.

Actual result: in Logstash 5.x is that the first json object is accepted and the 2nd one is lost/ignored.

jordansissel avatar Mar 24 '17 18:03 jordansissel

diff --git a/spec/codecs/json_spec.rb b/spec/codecs/json_spec.rb
index f86f2b9..a774f7f 100644
--- a/spec/codecs/json_spec.rb
+++ b/spec/codecs/json_spec.rb
@@ -188,4 +188,15 @@ describe LogStash::Codecs::JSON do
     end
   end

+  context "when given invalid json" do
+    context "that looks like two jsons together" do
+      let(:text) { '{ "hello": "world" }{ "goodbye": "moon" }' }
+
+      it "should fail to parse" do
+        subject.decode(text) do |event|
+          expect(event.get("tags")).to include("_jsonparsefailure")
+        end
+      end
+    end
+  end
 end

The above test reproduces this problem and fails because the expected tag is not there.

jordansissel avatar Mar 24 '17 18:03 jordansissel