revert to working version

This commit is contained in:
wiaamm
2025-12-09 14:23:55 +02:00
parent b170bd9528
commit eefed0cacb

View File

@@ -1,3 +1,4 @@
cat /usr/local/share/lua/5.1/kong/plugins/open-appsec-waf-kong-plugin/handler.lua
local module_name = ... local module_name = ...
local prefix = module_name:match("^(.-)handler$") local prefix = module_name:match("^(.-)handler$")
local nano = require(prefix .. "nano_ffi") local nano = require(prefix .. "nano_ffi")
@@ -8,40 +9,53 @@ local NanoHandler = {}
NanoHandler.PRIORITY = 3000 NanoHandler.PRIORITY = 3000
NanoHandler.VERSION = "1.0.0" NanoHandler.VERSION = "1.0.0"
NanoHandler.sessions = {}
NanoHandler.processed_requests = {}
function NanoHandler.init_worker() function NanoHandler.init_worker()
nano.init_attachment() nano.init_attachment()
end end
function NanoHandler.access(conf) function NanoHandler.access(conf)
kong.log.debug("Entering access phase")
local ctx = kong.ctx.plugin
local headers = kong.request.get_headers() local headers = kong.request.get_headers()
local session_id = nano.generate_session_id() local session_id = nano.generate_session_id()
kong.service.request.set_header("x-session-id", tostring(session_id)) kong.service.request.set_header("x-session-id", tostring(session_id))
local session_data = nano.init_session(session_id) if NanoHandler.processed_requests[session_id] then
if not session_data then kong.ctx.plugin.blocked = true
kong.log.err("Failed to initialize session - failing open")
ctx.session_data = nil
ctx.session_id = nil
ctx.cleanup_needed = false
return return
end end
ctx.session_data = session_data local session_data = nano.init_session(session_id)
ctx.session_id = session_id if not session_data then
kong.log.err("Failed to initialize session - failing open")
return
end
kong.ctx.plugin.session_data = session_data
kong.ctx.plugin.session_id = session_id
local meta_data = nano.handle_start_transaction() local meta_data = nano.handle_start_transaction()
if not meta_data then if not meta_data then
kong.log.err("Failed to handle start transaction - failing open") kong.log.err("Failed to handle start transaction - failing open")
ctx.cleanup_needed = true nano.fini_session(session_data)
nano.cleanup_all()
-- collectgarbage("restart")
-- collectgarbage("collect")
kong.ctx.plugin.session_data = nil
kong.ctx.plugin.session_id = nil
return return
end end
local req_headers = nano.handleHeaders(headers) local req_headers = nano.handleHeaders(headers)
if not req_headers then if not req_headers then
kong.log.err("Failed to handle request headers - failing open") kong.log.err("Failed to handle request headers - failing open")
ctx.cleanup_needed = true nano.fini_session(session_data)
nano.cleanup_all()
-- collectgarbage("restart")
-- collectgarbage("collect")
kong.ctx.plugin.session_data = nil
kong.ctx.plugin.session_id = nil
return return
end end
@@ -50,7 +64,8 @@ function NanoHandler.access(conf)
local verdict, response = nano.send_data(session_id, session_data, meta_data, req_headers, contains_body, nano.HttpChunkType.HTTP_REQUEST_FILTER) local verdict, response = nano.send_data(session_id, session_data, meta_data, req_headers, contains_body, nano.HttpChunkType.HTTP_REQUEST_FILTER)
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
ctx.cleanup_needed = true kong.ctx.plugin.blocked = true
kong.ctx.plugin.cleanup_needed = true
return nano.handle_custom_response(session_data, response) return nano.handle_custom_response(session_data, response)
end end
@@ -59,7 +74,8 @@ function NanoHandler.access(conf)
if body and #body > 0 then if body and #body > 0 then
verdict, response = nano.send_body(session_id, session_data, body, nano.HttpChunkType.HTTP_REQUEST_BODY) verdict, response = nano.send_body(session_id, session_data, body, nano.HttpChunkType.HTTP_REQUEST_BODY)
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
ctx.cleanup_needed = true kong.ctx.plugin.blocked = true
kong.ctx.plugin.cleanup_needed = true
return nano.handle_custom_response(session_data, response) return nano.handle_custom_response(session_data, response)
end end
else else
@@ -70,7 +86,8 @@ function NanoHandler.access(conf)
kong.log.debug("Found request body in nginx var, size: ", #body_data) kong.log.debug("Found request body in nginx var, size: ", #body_data)
verdict, response = nano.send_body(session_id, session_data, body_data, nano.HttpChunkType.HTTP_REQUEST_BODY) verdict, response = nano.send_body(session_id, session_data, body_data, nano.HttpChunkType.HTTP_REQUEST_BODY)
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
ctx.cleanup_needed = true kong.ctx.plugin.blocked = true
kong.ctx.plugin.cleanup_needed = true
return nano.handle_custom_response(session_data, response) return nano.handle_custom_response(session_data, response)
end end
else else
@@ -82,11 +99,14 @@ function NanoHandler.access(conf)
local entire_body = file:read("*all") local entire_body = file:read("*all")
file:close() file:close()
if entire_body and #entire_body > 0 then if not entire_body then
kong.log.err("Failed to read body file: ", body_file)
elseif entire_body and #entire_body > 0 then
kong.log.debug("Sending entire body of size ", #entire_body, " bytes to C module") kong.log.debug("Sending entire body of size ", #entire_body, " bytes to C module")
verdict, response = nano.send_body(session_id, session_data, entire_body, nano.HttpChunkType.HTTP_REQUEST_BODY) verdict, response = nano.send_body(session_id, session_data, entire_body, nano.HttpChunkType.HTTP_REQUEST_BODY)
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
ctx.cleanup_needed = true kong.ctx.plugin.blocked = true
kong.ctx.plugin.cleanup_needed = true
return nano.handle_custom_response(session_data, response) return nano.handle_custom_response(session_data, response)
end end
else else
@@ -99,34 +119,52 @@ function NanoHandler.access(conf)
end end
end end
local ok, verdict, response = pcall(function() local verdict, response = nano.AttachmentVerdict.INSPECT, nil
local ok, pcall_verdict, pcall_response = pcall(function()
return nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_REQUEST_END) return nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_REQUEST_END)
end) end)
if not ok then if not ok then
kong.log.err("Error ending request inspection - failing open") kong.log.err("Error ending request inspection: ", pcall_verdict, " - failing open")
ctx.cleanup_needed = true nano.fini_session(session_data)
nano.cleanup_all()
-- collectgarbage("restart")
-- collectgarbage("collect")
kong.ctx.plugin.session_data = nil
kong.ctx.plugin.session_id = nil
return return
end end
verdict, response = pcall_verdict, pcall_response
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
ctx.cleanup_needed = true kong.ctx.plugin.blocked = true
return nano.handle_custom_response(session_data, response) local result = nano.handle_custom_response(session_data, response)
nano.fini_session(session_data)
nano.cleanup_all()
-- collectgarbage("restart")
-- collectgarbage("collect")
kong.ctx.plugin.session_data = nil
kong.ctx.plugin.session_id = nil
return result
end end
else else
verdict, response = nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_REQUEST_END) verdict, response = nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_REQUEST_END)
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
ctx.cleanup_needed = true kong.ctx.plugin.blocked = true
kong.ctx.plugin.cleanup_needed = true
return nano.handle_custom_response(session_data, response) return nano.handle_custom_response(session_data, response)
end end
end end
NanoHandler.processed_requests[session_id] = true
end end
function NanoHandler.header_filter(conf) function NanoHandler.header_filter(conf)
kong.log.debug("Entering header_filter phase, cleanup_needed: ", tostring(kong.ctx.plugin.cleanup_needed))
local ctx = kong.ctx.plugin local ctx = kong.ctx.plugin
if ctx.cleanup_needed then kong.log.err("header_filter: ctx.blocked=", ctx.blocked, " ctx.cleanup_needed=", ctx.cleanup_needed, " ctx.session_id=", ctx.session_id, " ctx.session_data=", ctx.session_data and "EXISTS" or "NIL")
kong.log.debug("Cleanup needed in header_filter, skipping processing")
if ctx.blocked or ctx.cleanup_needed then
return return
end end
@@ -134,7 +172,7 @@ function NanoHandler.header_filter(conf)
local session_data = ctx.session_data local session_data = ctx.session_data
if not session_id or not session_data then if not session_id or not session_data then
kong.log.debug("No session data found in header_filter") kong.log.err("No session data found in header_filter - session_id:", session_id, " session_data:", session_data)
return return
end end
@@ -143,7 +181,6 @@ function NanoHandler.header_filter(conf)
if not header_data then if not header_data then
kong.log.err("Failed to handle response headers - failing open") kong.log.err("Failed to handle response headers - failing open")
ctx.cleanup_needed = true
return return
end end
@@ -152,7 +189,8 @@ function NanoHandler.header_filter(conf)
local verdict, response = nano.send_response_headers(session_id, session_data, header_data, status_code, content_length) local verdict, response = nano.send_response_headers(session_id, session_data, header_data, status_code, content_length)
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
ctx.cleanup_needed = true kong.ctx.plugin.blocked = true
kong.ctx.plugin.cleanup_needed = true
return nano.handle_custom_response(session_data, response) return nano.handle_custom_response(session_data, response)
end end
@@ -160,33 +198,43 @@ function NanoHandler.header_filter(conf)
end end
function NanoHandler.body_filter(conf) function NanoHandler.body_filter(conf)
kong.log.debug("Entering body_filter phase, cleanup_needed: ", tostring(kong.ctx.plugin.cleanup_needed))
local ctx = kong.ctx.plugin local ctx = kong.ctx.plugin
if ctx.cleanup_needed then local chunk = ngx.arg[1]
kong.log.debug("Cleanup needed in body_filter, skipping processing") local eof = ngx.arg[2]
ctx.response_body_size = (ctx.response_body_size or 0) + (chunk and #chunk or 0)
kong.log.err("Response body size so far: ", ctx.response_body_size)
--kong.log.err("body_filter START: ctx.blocked=", ctx.blocked, " ctx.cleanup_needed=", ctx.cleanup_needed, " ctx.session_id=", ctx.session_id, " ctx.session_data=", ctx.session_data and "EXISTS" or "NIL")
if ctx.blocked or ctx.cleanup_needed then
kong.log.err("Fail-open mode - blocked/cleanup chunk without inspection, passing through")
ngx.arg[1] = chunk
return return
end end
--kong.log.err("In body_filter phase")
local session_id = ctx.session_id local session_id = ctx.session_id
local session_data = ctx.session_data local session_data = ctx.session_data
if not session_id or not session_data then -- kong.log.err("Session id after: ", session_id, " session_data: ", session_data and "EXISTS" or "NIL")
kong.log.debug("No session data found in body_filter") if not session_id or not session_data or ctx.session_finalized then
kong.log.err("No session data found in body_filter - session_id:", session_id, " session_data:", session_data)
ngx.arg[1] = chunk
return return
end end
kong.log.err("Session id after 2")
-- Timeout handling
if not ctx.body_filter_start_time then if not ctx.body_filter_start_time then
ctx.body_filter_start_time = ngx.now() ctx.body_filter_start_time = ngx.now()
end end
local elapsed_time = ngx.now() - ctx.body_filter_start_time local elapsed_time = ngx.now() - ctx.body_filter_start_time
if elapsed_time > 150 then if elapsed_time > 150 then
kong.log.warn("Body filter timeout after ", elapsed_time, " seconds - failing open") kong.log.warn("Body filter timeout after ", elapsed_time, " seconds - failing open")
ctx.cleanup_needed = true ctx.cleanup_needed = true
ctx.timeout_passthrough = true
return return
end end
local chunk = ngx.arg[1]
local eof = ngx.arg[2]
if chunk and #chunk > 0 then if chunk and #chunk > 0 then
ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0 ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0
@@ -202,6 +250,8 @@ function NanoHandler.body_filter(conf)
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1 ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
ctx.blocked = true
ctx.session_finalized = true
ctx.cleanup_needed = true ctx.cleanup_needed = true
ngx.arg[1] = "" ngx.arg[1] = ""
ngx.arg[2] = true ngx.arg[2] = true
@@ -210,10 +260,16 @@ function NanoHandler.body_filter(conf)
end end
if eof then if eof then
kong.log.err("End of response body reached in body_filter")
-- Timeout handling
if ctx.body_seen or ctx.expect_body == false then if ctx.body_seen or ctx.expect_body == false then
kong.log.err("Finalizing session in body_filter")
local verdict, response = nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_RESPONSE_END) local verdict, response = nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_RESPONSE_END)
if verdict == nano.AttachmentVerdict.DROP then if verdict == nano.AttachmentVerdict.DROP then
kong.log.debug("Dropping response in body_filter after end_inspection") kong.log.err("Dropping response in body_filter after end_inspection")
ctx.blocked = true
ctx.session_finalized = true
ctx.cleanup_needed = true ctx.cleanup_needed = true
ngx.arg[1] = "" ngx.arg[1] = ""
ngx.arg[2] = true ngx.arg[2] = true
@@ -221,13 +277,16 @@ function NanoHandler.body_filter(conf)
end end
end end
-- Cleanup in log phase instead
ctx.cleanup_needed = true ctx.cleanup_needed = true
ctx.session_finalized = true
end end
end end
function NanoHandler.log(conf) function NanoHandler.log(conf)
local ctx = kong.ctx.plugin local ctx = kong.ctx.plugin
kong.log.debug("Entering log phase cleanup, cleanup_needed: ", tostring(ctx.cleanup_needed)) kong.log.err("log phase: ctx.blocked=", ctx.blocked, " ctx.cleanup_needed=", ctx.cleanup_needed, " ctx.session_id=", ctx.session_id, " ctx.session_data=", ctx.session_data and "EXISTS" or "NIL")
-- Cleanup session if it was blocked (kong.response.exit was called)
if ctx.cleanup_needed or ctx.session_data then if ctx.cleanup_needed or ctx.session_data then
nano.fini_session(ctx.session_data) nano.fini_session(ctx.session_data)
nano.cleanup_all() nano.cleanup_all()