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

plugin/in_http: recognize CSP reports as JSON data #4282

Merged
merged 1 commit into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}],
]
Comment on lines +524 to +527
Copy link
Member

@ashie ashie Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better that the test contents contain actual syntax of CSP report:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a trivial thing, no need to block merging.

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