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: Add support for subdomain in CORS domain #2337

Merged
merged 2 commits into from
Mar 22, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/fluent/plugin/in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def handle_options_request
if @cors_allow_origins.include?('*')
header["Access-Control-Allow-Origin"] = "*"
send_response_and_close("200 OK", header, "")
elsif @cors_allow_origins.include?(@origin)
elsif include_cors_allow_origin
header["Access-Control-Allow-Origin"] = @origin
send_response_and_close("200 OK", header, "")
else
Expand All @@ -414,7 +414,7 @@ def on_message_complete
# For every incoming request, we check if we have some CORS
# restrictions and white listed origins through @cors_allow_origins.
unless @cors_allow_origins.nil?
unless @cors_allow_origins.include?('*') or @cors_allow_origins.include?(@origin)
unless @cors_allow_origins.include?('*') or include_cors_allow_origin
send_response_and_close("403 Forbidden", {'Connection' => 'close'}, "")
return
end
Expand Down Expand Up @@ -464,7 +464,7 @@ def on_message_complete
unless @cors_allow_origins.nil?
if @cors_allow_origins.include?('*')
header['Access-Control-Allow-Origin'] = '*'
elsif @cors_allow_origins.include?(@origin)
elsif include_cors_allow_origin
header['Access-Control-Allow-Origin'] = @origin
end
end
Expand Down Expand Up @@ -512,6 +512,17 @@ def send_response_nobody(code, header)
data << "\r\n"
write data
end

def include_cors_allow_origin
if @cors_allow_origins.include?(@origin)
return true
end
filtered_cors_allow_origins = @cors_allow_origins.select {|origin| origin != ""}
return filtered_cors_allow_origins.find do |origin|
(start_str,end_str) = origin.split("*",2)
@origin.start_with?(start_str) and @origin.end_with?(end_str)
end != nil
end
end
end
end
57 changes: 57 additions & 0 deletions test/plugin/test_in_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,63 @@ def test_cors_preflight
end
end

def test_cors_allowed_wildcard_for_subdomain
d = create_driver(CONFIG + 'cors_allow_origins ["http://*.foo.com"]')

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

d.run do
events.each do |tag, time, record|
headers = {"Origin" => "http://subdomain.foo.com"}

res = post("/#{tag}", {"json" => record.to_json, "time" => time.to_i}, headers)

assert_equal "200", res.code
assert_equal "http://subdomain.foo.com", res["Access-Control-Allow-Origin"]
end
end
end

def test_cors_allowed_exclude_empty_string
d = create_driver(CONFIG + 'cors_allow_origins ["", "http://*.foo.com"]')

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

d.run do
events.each do |tag, time, record|
headers = {"Origin" => "http://subdomain.foo.com"}

res = post("/#{tag}", {"json" => record.to_json, "time" => time.to_i}, headers)

assert_equal "200", res.code
assert_equal "http://subdomain.foo.com", res["Access-Control-Allow-Origin"]
end
end
end

def test_cors_allowed_wildcard_preflight_for_subdomain
d = create_driver(CONFIG + 'cors_allow_origins ["http://*.foo.com"]')

d.run do
header = {
"Origin" => "http://subdomain.foo.com",
"Access-Control-Request-Method" => "POST",
"Access-Control-Request-Headers" => "Content-Type",
}
res = options("/cors.test", {}, header)

assert_equal "200", res.code
assert_equal "http://subdomain.foo.com", res["Access-Control-Allow-Origin"]
assert_equal "POST", res["Access-Control-Allow-Methods"]
end
end

def test_content_encoding_gzip
d = create_driver

Expand Down