Skip to content

Commit bcaaceb

Browse files
authored
in_http: allow empty Origin header requests to pass CORS checks (#4866)
**Which issue(s) this PR fixes**: **What this PR does / why we need it**: Some requests, such as those made by apps, certain automated scripts, or older browsers, may not include an Origin header. Previously, such requests were blocked by the CORS check, even though they may not necessarily be cross-origin. For CORS, the server is responsible for reporting the allowed origins. The web browser is responsible for enforcing that requests are only sent from allowed domains. So this change updates the CORS handling logic to allow requests with an empty Origin header to pass, ensuring compatibility with legitimate non-browser clients while maintaining security. **Docs Changes**: fluent/fluentd-docs-gitbook#574 **Release Note**: The same as the title. Signed-off-by: Richard Lee <[email protected]>
1 parent 61d7a8e commit bcaaceb

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/fluent/plugin/in_http.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,9 @@ def on_message_complete
504504
# ==========
505505
# For every incoming request, we check if we have some CORS
506506
# restrictions and allow listed origins through @cors_allow_origins.
507+
# If origin is empty, it's likely a server-to-server request and considered safe.
507508
unless @cors_allow_origins.nil?
508-
unless @cors_allow_origins.include?('*') || include_cors_allow_origin
509+
unless @cors_allow_origins.include?('*') || include_cors_allow_origin || @origin.nil?
509510
send_response_and_close(RES_403_STATUS, {'Connection' => 'close'}, "")
510511
return
511512
end

test/plugin/test_in_http.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,26 @@ def test_cors_allow_credentials_for_wildcard_origins
940940
end
941941
end
942942

943+
def test_cors_with_nil_origin
944+
d = create_driver(config + %[
945+
cors_allow_origins ["http://foo.com"]
946+
])
947+
assert_equal ["http://foo.com"], d.instance.cors_allow_origins
948+
949+
time = event_time("2011-01-02 13:14:15 UTC")
950+
event = ["tag1", time, {"a"=>1}]
951+
res_code = nil
952+
953+
d.run do
954+
res = post("/#{event[0]}", {"json"=>event[2].to_json, "time"=>time.to_i.to_s})
955+
res_code = res.code
956+
end
957+
958+
assert_equal "200", res_code
959+
assert_equal [event], d.events
960+
assert_equal_event_time time, d.events[0][1]
961+
end
962+
943963
def test_content_encoding_gzip
944964
d = create_driver
945965

0 commit comments

Comments
 (0)