bhuvan-somisetty opened a new issue, #13969:
URL: https://github.com/apache/apisix/issues/13969

   ### Current Behavior
   
   In `apisix/plugins/syslog/init.lua`, `send_syslog_data()` fetches the logger 
instance from the lrucache and correctly checks whether it failed:
   
   
https://github.com/apache/apisix/blob/master/apisix/plugins/syslog/init.lua#L45-L78
   
   ```lua
   local function send_syslog_data(conf, log_message, api_ctx)
       local err_msg
       local res = true
   
       core.log.info("sending a batch logs to ", conf.host, ":", conf.port)
   
       -- fetch it from lrucache
       local logger, err = core.lrucache.plugin_ctx(
           lrucache, api_ctx, nil, logger_socket.new, logger_socket, {
               host = conf.host,
               port = conf.port,
               flush_limit = conf.flush_limit,
               drop_limit = conf.drop_limit,
               timeout = conf.timeout,
               sock_type = conf.sock_type,
               pool_size = conf.pool_size,
               tls = conf.tls,
           }
       )
   
       if not logger then
           res = false
           err_msg = "failed when initiating the sys logger processor".. err
       end
   
       -- reuse the logger object
       local ok, err = logger:log(log_message)
       ...
   ```
   
   When `logger_socket.new(...)` fails (e.g. bad host/port, socket/resource 
errors), `logger` is `nil` and the `if not logger then` branch correctly sets 
`res = false` and builds `err_msg`. But there is no `return` in that branch, so 
execution falls through to the very next line, `logger:log(log_message)`, which 
indexes a `nil` value.
   
   This throws an uncaught Lua runtime error ("attempt to index a nil value") 
inside the log-phase batch processor callback, instead of returning the 
already-built `(false, err_msg)` back to the caller like every other error 
branch in this codebase does (e.g. the same pattern in 
`apisix/plugins/lago.lua`'s `send_http_data`, or the `if not ok then` branch a 
few lines below in this same function).
   
   ### Expected Behavior
   
   When the logger fails to initialize, `send_syslog_data` should return early 
with `res, err_msg` (i.e. `false, err_msg`), the same way the function already 
does for the `logger:log()` failure case just below it, instead of crashing on 
a nil index.
   
   ### How to Reproduce
   
   Force `core.lrucache.plugin_ctx(...)` / `logger_socket.new(...)` to fail for 
the syslog plugin (for example, by making the socket connect fail — invalid 
host/port, or exhausting connection resources) and observe an unhandled 
"attempt to index a nil value" error in the log phase instead of a clean, 
logged failure.
   
   ### Suggested fix
   
   Add a `return res, err_msg` right after `err_msg` is set in the `if not 
logger then` block:
   
   ```lua
       if not logger then
           res = false
           err_msg = "failed when initiating the sys logger processor".. err
           return res, err_msg
       end
   ```
   
   This is a small, single-file, single-function fix and should be a good first 
issue for a new contributor.
   
   ### Environment
   
   - APISIX version: master branch (also present in released versions, code 
unchanged for a long time)
   - File: `apisix/plugins/syslog/init.lua`, lines 65-71


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to