Skip to content

Commit 0910a86

Browse files
authored
Fixed crashing when request body is not a JSON object (#24)
- Fixed crashing when the request body is not a JSON object - Pin http_parser.rb gem version to ~> 0.6.0
1 parent 8c979b1 commit 0910a86

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.0.10
2+
- Changed the transitive dependency `http_parser.rb` (ftw) version to `~-> 0.6.0` as newer versions are published without the java support.
3+
- Fixed crashing when the request body payload is not a JSON object. [#24](https://github.com/logstash-plugins/logstash-input-github/pull/24)
4+
15
## 3.0.9
26
- Bump ftw dependency to 0.0.49, for compatibility with Logstash 7.x
37

lib/logstash/inputs/github.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,16 @@ def run(output_queue)
5353

5454
def build_event_from_request(body, headers)
5555
begin
56-
event = LogStash::Event.new(JSON.parse(body))
56+
data = JSON.parse(body)
57+
# The JSON specification defines single values as valid JSONs, it can be a string in double quotes,
58+
# a number, true or false or null. When the body is parsed, those values are transformed into its
59+
# corresponding types. When those types aren't a Hash (aka object), it breaks the LogStash::Event
60+
# contract and crashes.
61+
if data.is_a?(::Hash)
62+
event = LogStash::Event.new(data)
63+
else
64+
event = LogStash::Event.new("message" => body, "tags" => "_invalidjsonobject")
65+
end
5766
rescue JSON::ParserError => e
5867
@logger.info("JSON parse failure. Falling back to plain-text", :error => e, :data => body)
5968
event = LogStash::Event.new("message" => body, "tags" => "_invalidjson")

logstash-input-github.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22

33
s.name = 'logstash-input-github'
4-
s.version = '3.0.9'
4+
s.version = '3.0.10'
55
s.licenses = ['Apache License (2.0)']
66
s.summary = "Reads events from a GitHub webhook"
77
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
2424

2525
s.add_runtime_dependency 'addressable'
2626
s.add_runtime_dependency 'logstash-codec-plain'
27+
s.add_runtime_dependency 'http_parser.rb', '~> 0.6.0'
2728
s.add_runtime_dependency 'ftw', '~> 0.0.49'
2829

2930
s.add_development_dependency 'logstash-devutils'

spec/inputs/github_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,27 @@
8686

8787
end
8888

89+
describe "verify event builder" do
90+
let(:plugin) { LogStash::Plugin.lookup("input", "github").new( {"port" => 9999} ) }
91+
let(:body) {"{}"}
92+
let(:event) {plugin.build_event_from_request(body, {})}
93+
94+
context 'when request body is a minimal JSON value' do
95+
let(:body) {"123"}
96+
it 'should add the body string into the message field and tag' do
97+
expect(event.get("message")).to eq("123")
98+
expect(event.get("tags")).to eq("_invalidjsonobject")
99+
end
100+
end
101+
102+
context 'when request body is a JSON object' do
103+
let(:body) {'{"action": "create"}'}
104+
it 'should parse the body' do
105+
expect(event.get("action")).to eq("create")
106+
end
107+
end
108+
end
109+
89110
describe 'graceful shutdown' do
90111
context 'when underlying webserver crashes' do
91112

0 commit comments

Comments
 (0)