From 3066f5032ea2bb30afa39a2ee91c93a0a85d0ce2 Mon Sep 17 00:00:00 2001 From: Jiayi Ding Date: Tue, 4 Jun 2024 10:50:08 +0800 Subject: [PATCH] fix(http-log): add port information to the host header (#13116) Fix https://github.com/Kong/kong/issues/13067 --- .../kong/fix-http-log-host-header.yml | 4 +++ kong/plugins/http-log/handler.lua | 1 - spec/03-plugins/03-http-log/01-log_spec.lua | 34 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 changelog/unreleased/kong/fix-http-log-host-header.yml diff --git a/changelog/unreleased/kong/fix-http-log-host-header.yml b/changelog/unreleased/kong/fix-http-log-host-header.yml new file mode 100644 index 000000000000..76e0cf986e34 --- /dev/null +++ b/changelog/unreleased/kong/fix-http-log-host-header.yml @@ -0,0 +1,4 @@ +message: "**HTTP-Log**: Fix an issue where the plugin doesn't include port information in the HTTP host header when sending requests to the log server." +type: bugfix +scope: Plugin + diff --git a/kong/plugins/http-log/handler.lua b/kong/plugins/http-log/handler.lua index 8bd382f926c0..fd1d0cd48eeb 100644 --- a/kong/plugins/http-log/handler.lua +++ b/kong/plugins/http-log/handler.lua @@ -114,7 +114,6 @@ local function send_entries(conf, entries) httpc:set_timeout(timeout) local headers = { - ["Host"] = host, ["Content-Type"] = content_type, ["Content-Length"] = content_length, ["Authorization"] = userinfo and "Basic " .. encode_base64(userinfo) or nil diff --git a/spec/03-plugins/03-http-log/01-log_spec.lua b/spec/03-plugins/03-http-log/01-log_spec.lua index 55591eb85dde..fb96cb03d38e 100644 --- a/spec/03-plugins/03-http-log/01-log_spec.lua +++ b/spec/03-plugins/03-http-log/01-log_spec.lua @@ -209,6 +209,22 @@ for _, strategy in helpers.each_strategy() do } } + local route5 = bp.routes:insert { + hosts = { "http_host_header.test" }, + service = service1 + } + + bp.plugins:insert { + route = { id = route5.id }, + name = "http-log", + config = { + http_endpoint = "http://" .. helpers.mock_upstream_host + .. ":" + .. helpers.mock_upstream_port + .. "/post_log/http_host_header" + } + } + local route6 = bp.routes:insert { hosts = { "https_logging_faulty.test" }, service = service2 @@ -535,6 +551,24 @@ for _, strategy in helpers.each_strategy() do assert.same(vault_env_value, entries[1].log_req_headers.key2) end) + it("http client implicitly adds Host header", function() + reset_log("http_host_header") + local res = proxy_client:get("/status/200", { + headers = { + ["Host"] = "http_host_header.test" + } + }) + assert.res_status(200, res) + + local entries = get_log("http_host_header", 1) + local host_header + if helpers.mock_upstream_port == 80 then + host_header = helpers.mock_upstream_host + else + host_header = helpers.mock_upstream_host .. ":" .. helpers.mock_upstream_port + end + assert.same(entries[1].log_req_headers['host'] or "", host_header) + end) it("puts changed configuration into effect immediately", function() local admin_client = assert(helpers.admin_client())