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

in_http: Enable to add Access-Control-Allow-Credentials header #3481

Merged
merged 1 commit into from
Aug 20, 2021
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
23 changes: 21 additions & 2 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class HttpInput < Input
config_param :blocking_timeout, :time, default: 0.5
desc 'Set a allow list of domains that can do CORS (Cross-Origin Resource Sharing)'
config_param :cors_allow_origins, :array, default: nil
desc 'Tells browsers whether to expose the response to frontend when the credentials mode is "include".'
config_param :cors_allow_credentials, :bool, default: nil
desc 'Respond with empty gif image of 1x1 pixel.'
config_param :respond_with_empty_img, :bool, default: false
desc 'Respond status code with 204.'
Expand Down Expand Up @@ -112,6 +114,12 @@ def configure(conf)

super

if @cors_allow_credentials
if @cors_allow_origins.nil? || @cors_allow_origins.include?('*')
raise Fluent::ConfigError, "Cannot enable cors_allow_credentials without specific origins"
end
end

m = if @parser_configs.first['@type'] == 'in_http'
@parser_msgpack = parser_create(usage: 'parser_in_http_msgpack', type: 'msgpack')
@parser_msgpack.time_key = nil
Expand Down Expand Up @@ -279,7 +287,10 @@ def on_request(path_info, params)
private

def on_server_connect(conn)
handler = Handler.new(conn, @km, method(:on_request), @body_size_limit, @format_name, log, @cors_allow_origins, @add_query_params)
handler = Handler.new(conn, @km, method(:on_request),
@body_size_limit, @format_name, log,
@cors_allow_origins, @cors_allow_credentials,
@add_query_params)

conn.on(:data) do |data|
handler.on_read(data)
Expand Down Expand Up @@ -356,7 +367,8 @@ def convert_time_field(record)
class Handler
attr_reader :content_type

def initialize(io, km, callback, body_size_limit, format_name, log, cors_allow_origins, add_query_params)
def initialize(io, km, callback, body_size_limit, format_name, log,
cors_allow_origins, cors_allow_credentials, add_query_params)
@io = io
@km = km
@callback = callback
Expand All @@ -365,6 +377,7 @@ def initialize(io, km, callback, body_size_limit, format_name, log, cors_allow_o
@format_name = format_name
@log = log
@cors_allow_origins = cors_allow_origins
@cors_allow_credentials = cors_allow_credentials
@idle = 0
@add_query_params = add_query_params
@km.add(self)
Expand Down Expand Up @@ -491,6 +504,9 @@ def handle_options_request
send_response_and_close(RES_200_STATUS, header, "")
elsif include_cors_allow_origin
header["Access-Control-Allow-Origin"] = @origin
if @cors_allow_credentials
header["Access-Control-Allow-Credentials"] = true
end
send_response_and_close(RES_200_STATUS, header, "")
else
send_response_and_close(RES_403_STATUS, {}, "")
Expand Down Expand Up @@ -576,6 +592,9 @@ def on_message_complete
header['Access-Control-Allow-Origin'] = '*'
elsif include_cors_allow_origin
header['Access-Control-Allow-Origin'] = @origin
if @cors_allow_credentials
header["Access-Control-Allow-Credentials"] = true
end
end
end

Expand Down
40 changes: 40 additions & 0 deletions test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,46 @@ def test_cors_allowed_wildcard_preflight_for_subdomain
end
end

def test_cors_allow_credentials
d = create_driver(config + %[
cors_allow_origins ["http://foo.com"]
cors_allow_credentials
])
assert_equal true, d.instance.cors_allow_credentials

time = event_time("2011-01-02 13:14:15 UTC")
event = ["tag1", time, {"a"=>1}]
res_code = nil
res_header = nil

d.run do
res = post("/#{event[0]}", {"json"=>event[2].to_json, "time"=>time.to_i.to_s}, {"Origin"=>"http://foo.com"})
res_code = res.code
res_header = res["Access-Control-Allow-Credentials"]
end
assert_equal(
{
response_code: "200",
allow_credentials_header: "true",
events: [event]
},
{
response_code: res_code,
allow_credentials_header: res_header,
events: d.events
}
)
end

def test_cors_allow_credentials_for_wildcard_origins
assert_raise(Fluent::ConfigError) do
create_driver(config + %[
cors_allow_origins ["*"]
cors_allow_credentials
])
end
end

def test_content_encoding_gzip
d = create_driver

Expand Down