Treat any external input (HTTP headers, JSON bodies, provider responses, dynamic config) as untrusted: validate its type/shape before indexing or string operations, and ensure errors don’t turn into uncontrolled 500s or leak state/resources.
Apply these rules:
attempt to index a number/table or bad argument escape.set_keepalive/reuse a Redis (or cosocket) connection after the command succeeded; otherwise close.Example pattern (Lua):
local function parse_embedding_response(core, raw_body)
local data = core.json.decode(raw_body)
if type(data) ~= "table" or type(data.data) ~= "table" then
return nil, "invalid embedding response"
end
local vectors = {}
for i, item in ipairs(data.data) do
if type(item) ~= "table" then
return nil, "invalid embedding entry at index " .. (i - 1)
end
-- safe field access after type checks
end
return vectors
end
local function with_redis(conf, fn)
local red, err = redis_util.new(conf)
if not red then return nil, err end
local ok, res, rerr = pcall(fn, red)
if not ok then
red:close()
return nil, res
end
-- only reuse after success
local ok_keep, kerr = red:set_keepalive(conf.redis_keepalive_timeout, conf.redis_keepalive_pool)
if not ok_keep then red:close() end
return res
end