From 58ffebd65a3a88c6c12856714a5004caf219d5a4 Mon Sep 17 00:00:00 2001 From: Jun Ouyang Date: Mon, 15 Jul 2024 15:38:40 +0800 Subject: [PATCH] fix(core): reduce uninitialized variable message when nginx return 400 (#13201) When Kong receives abnormal traffic, it will trigger 400 responses without initializing any Nginx variable, So it will trigger report.lua error, which is unnecessary. eg: send HTTP traffic to HTTPS port, Nginx will finalize the current request by 400 response in the TLS handshake, So it will never call any openresty HTTP processing phase, it also does not initialize any Nginx variable. Fix #13197 https://konghq.atlassian.net/browse/FTI-6025 --- .../fix-reports-uninitialized-variable-in-400.yml | 4 ++++ kong/reports.lua | 6 ++++++ kong/templates/nginx_kong.lua | 5 +++-- spec/02-integration/05-proxy/22-reports_spec.lua | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 changelog/unreleased/kong/fix-reports-uninitialized-variable-in-400.yml 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 000000000000..398af4beb460 --- /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 333a4fceb007..2ce5777b29f6 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 96d425af93d8..6eca6ef9c6ad 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 eab85e46803f..7b43172860ea 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({