diff --git a/changelog/unreleased/kong/fix-reports-uninitialized-variable-in-400.yml b/changelog/unreleased/kong/fix-reports-uninitialized-variable-in-400.yml new file mode 100644 index 00000000000..398af4beb46 --- /dev/null +++ b/changelog/unreleased/kong/fix-reports-uninitialized-variable-in-400.yml @@ -0,0 +1,4 @@ +message: | + Fixed an issue where unnecessary uninitialized variable error log is reported when 400 bad requests were received. +type: bugfix +scope: Core diff --git a/kong/reports.lua b/kong/reports.lua index 333a4fceb00..2ce5777b29f 100644 --- a/kong/reports.lua +++ b/kong/reports.lua @@ -303,6 +303,12 @@ function get_current_suffix(ctx) return nil end + -- 400 case is for invalid requests, eg: if a client sends a HTTP + -- request to a HTTPS port, it does not initialized any Nginx variables + if proxy_mode == "" and kong.response.get_status() == 400 then + return nil + end + log(WARN, "could not determine log suffix (scheme=", tostring(scheme), ", proxy_mode=", tostring(proxy_mode), ")") end diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua index 96d425af93d..6eca6ef9c6a 100644 --- a/kong/templates/nginx_kong.lua +++ b/kong/templates/nginx_kong.lua @@ -39,6 +39,8 @@ ssl_ciphers ${{SSL_CIPHERS}}; $(el.name) $(el.value); > end +uninitialized_variable_warn off; + init_by_lua_block { > if test and coverage then require 'luacov' @@ -386,9 +388,8 @@ server { location = /kong_error_handler { internal; - default_type ''; - uninitialized_variable_warn off; + default_type ''; rewrite_by_lua_block {;} access_by_lua_block {;} diff --git a/spec/02-integration/05-proxy/22-reports_spec.lua b/spec/02-integration/05-proxy/22-reports_spec.lua index eab85e46803..7b43172860e 100644 --- a/spec/02-integration/05-proxy/22-reports_spec.lua +++ b/spec/02-integration/05-proxy/22-reports_spec.lua @@ -242,6 +242,20 @@ for _, strategy in helpers.each_strategy() do proxy_ssl_client:close() end) + it("when send http request to https port, no other error in error.log", function() + local https_port = assert(helpers.get_proxy_port(true)) + local proxy_client = assert(helpers.proxy_client(nil, https_port)) + local res = proxy_client:get("/", { + headers = { host = "http-service.test" } + }) + reports_send_ping({port=constants.REPORTS.STATS_TLS_PORT}) + + assert.response(res).has_status(400) + assert.logfile().has.no.line("using uninitialized") + assert.logfile().has.no.line("could not determine log suffix (scheme=http, proxy_mode=)") + proxy_client:close() + end) + it("reports h2c requests", function() local h2c_client = assert(helpers.proxy_client_h2c()) local body, headers = h2c_client({