Skip to content

Commit

Permalink
Merge pull request #4282 from GiedriusS/recognize_csp
Browse files Browse the repository at this point in the history
plugin/in_http: recognize CSP reports as JSON data
  • Loading branch information
ashie committed Apr 30, 2024
2 parents 284bf40 + 86ebfd3 commit 2faeb04
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/fluent/plugin/in_http.rb
Expand Up @@ -573,14 +573,16 @@ def on_message_complete
params.update WEBrick::HTTPUtils.parse_form_data(@body, boundary)
elsif /^application\/json/.match?(@content_type)
params['json'] = @body
elsif /^application\/csp-report/.match?(@content_type)
params['json'] = @body
elsif /^application\/msgpack/.match?(@content_type)
params['msgpack'] = @body
elsif /^application\/x-ndjson/.match?(@content_type)
params['ndjson'] = @body
end
path_info = uri.path

if (@add_query_params)
if (@add_query_params)

query_params = WEBrick::HTTPUtils.parse_query(uri.query)

Expand Down
24 changes: 23 additions & 1 deletion test/plugin/test_in_http.rb
Expand Up @@ -517,6 +517,28 @@ def test_application_json
assert_equal_event_time time, d.events[1][1]
end

def test_csp_report
d = create_driver
time = event_time("2011-01-02 13:14:15 UTC")
time_i = time.to_i
events = [
["tag1", time, {"a"=>1}],
["tag2", time, {"a"=>2}],
]
res_codes = []

d.run(expect_records: 2) do
events.each do |tag, t, record|
res = post("/#{tag}?time=#{time_i.to_s}", record.to_json, {"Content-Type"=>"application/csp-report; charset=utf-8"})
res_codes << res.code
end
end
assert_equal ["200", "200"], res_codes
assert_equal events, d.events
assert_equal_event_time time, d.events[0][1]
assert_equal_event_time time, d.events[1][1]
end

def test_application_msgpack
d = create_driver
time = event_time("2011-01-02 13:14:15 UTC")
Expand Down Expand Up @@ -982,7 +1004,7 @@ def test_cors_disallowed
assert_equal ["403", "403"], res_codes
end

def test_add_query_params
def test_add_query_params
d = create_driver(config + "add_query_params true")
assert_equal true, d.instance.add_query_params

Expand Down

0 comments on commit 2faeb04

Please sign in to comment.