mirror of
https://github.com/openappsec/attachment.git
synced 2025-12-31 05:39:07 +03:00
501 lines
21 KiB
Lua
Executable File
501 lines
21 KiB
Lua
Executable File
local module_name = ...
|
|
local prefix = module_name:match("^(.-)handler$")
|
|
local nano = require(prefix .. "nano_ffi")
|
|
local kong = kong
|
|
|
|
local NanoHandler = {}
|
|
|
|
NanoHandler.PRIORITY = 3000
|
|
NanoHandler.VERSION = "1.0.0"
|
|
|
|
NanoHandler.sessions = {}
|
|
NanoHandler.processed_requests = {}
|
|
|
|
function NanoHandler.init_worker()
|
|
nano.init_attachment()
|
|
end
|
|
|
|
function NanoHandler.access(conf)
|
|
kong.log.err("1-111111111 ACCESS PHASE START -------------------------------------------------------------------------------------------------------------------------------------------------")
|
|
|
|
local route = kong.router.get_route()
|
|
if not route then
|
|
kong.log.err("ACCESS SKIPPED: no route matched")
|
|
return
|
|
end
|
|
|
|
local request_path = kong.request.get_path()
|
|
if request_path and (
|
|
request_path:match("^/status") or
|
|
request_path:match("^/_health") or
|
|
request_path:match("^/metrics")
|
|
) then
|
|
kong.log.err("ACCESS SKIPPED: internal endpoint: ", request_path)
|
|
return
|
|
end
|
|
|
|
if ngx.var.internal then
|
|
kong.log.err("ACCESS SKIPPED: internal subrequest")
|
|
return
|
|
end
|
|
|
|
local request_uri = ngx.var.request_uri
|
|
if not request_uri or request_uri == "" then
|
|
kong.log.err("ACCESS SKIPPED: TLS handshake or no URI")
|
|
return
|
|
end
|
|
|
|
local headers = kong.request.get_headers()
|
|
local session_id = nano.generate_session_id()
|
|
kong.service.request.set_header("x-session-id", tostring(session_id))
|
|
|
|
if NanoHandler.processed_requests[session_id] then
|
|
kong.ctx.plugin.blocked = true
|
|
return
|
|
end
|
|
|
|
local session_data = nano.init_session(session_id)
|
|
if not session_data then
|
|
kong.log.err("Failed to initialize session - failing open (no session created)")
|
|
kong.ctx.plugin.inspection_complete = true
|
|
return
|
|
end
|
|
|
|
kong.ctx.plugin.session_data = session_data
|
|
kong.ctx.plugin.session_id = session_id
|
|
|
|
local meta_data = nano.handle_start_transaction()
|
|
if not meta_data then
|
|
kong.log.err("Failed to handle start transaction - cleaning up session and failing open")
|
|
kong.ctx.plugin.inspection_complete = true
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return
|
|
end
|
|
|
|
local req_headers = nano.handleHeaders(headers)
|
|
|
|
local has_content_length = tonumber(ngx.var.http_content_length) and tonumber(ngx.var.http_content_length) > 0
|
|
local contains_body = has_content_length and 1 or 0
|
|
|
|
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
|
|
kong.ctx.plugin.blocked = true
|
|
kong.ctx.plugin.inspection_complete = true
|
|
local result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return result
|
|
end
|
|
|
|
if contains_body == 1 then
|
|
local body = kong.request.get_raw_body()
|
|
if body and #body > 0 then
|
|
verdict, response = nano.send_body(session_id, session_data, body, nano.HttpChunkType.HTTP_REQUEST_BODY)
|
|
if verdict == nano.AttachmentVerdict.DROP then
|
|
kong.ctx.plugin.blocked = true
|
|
kong.ctx.plugin.inspection_complete = true
|
|
local result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return result
|
|
end
|
|
else
|
|
kong.log.err("Request body not in memory, attempting to read from buffer/file")
|
|
|
|
local body_data = ngx.var.request_body
|
|
if body_data and #body_data > 0 then
|
|
kong.log.err("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)
|
|
if verdict == nano.AttachmentVerdict.DROP then
|
|
kong.ctx.plugin.blocked = true
|
|
kong.ctx.plugin.inspection_complete = true
|
|
local result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return result
|
|
end
|
|
else
|
|
local body_file = ngx.var.request_body_file
|
|
if body_file then
|
|
kong.log.err("Reading request body from file: ", body_file)
|
|
local file = io.open(body_file, "rb")
|
|
if file then
|
|
local entire_body = file:read("*all")
|
|
file:close()
|
|
|
|
if entire_body and #entire_body > 0 then
|
|
kong.log.err("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)
|
|
if verdict == nano.AttachmentVerdict.DROP then
|
|
kong.ctx.plugin.blocked = true
|
|
kong.ctx.plugin.inspection_complete = true
|
|
local result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return result
|
|
else
|
|
kong.log.err("Empty body file")
|
|
end
|
|
end
|
|
else
|
|
kong.log.err("Request body expected but no body data or file available")
|
|
end
|
|
end
|
|
end
|
|
|
|
local ok, verdict, response = pcall(function()
|
|
return nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_REQUEST_END)
|
|
end)
|
|
|
|
if not ok then
|
|
kong.log.err("Error ending request inspection: ", verdict, " - failing open")
|
|
kong.ctx.plugin.inspection_complete = true
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return
|
|
end
|
|
|
|
if verdict == nano.AttachmentVerdict.DROP then
|
|
kong.ctx.plugin.blocked = true
|
|
kong.ctx.plugin.inspection_complete = true
|
|
local result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return result
|
|
end
|
|
end
|
|
else
|
|
local ok, verdict, response = pcall(function()
|
|
return nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_REQUEST_END)
|
|
end)
|
|
|
|
if not ok then
|
|
kong.log.err("Error ending request inspection (no body): ", verdict, " - failing open")
|
|
kong.ctx.plugin.inspection_complete = true
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return
|
|
end
|
|
|
|
if verdict == nano.AttachmentVerdict.DROP then
|
|
kong.ctx.plugin.blocked = true
|
|
kong.ctx.plugin.inspection_complete = true
|
|
local result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
kong.ctx.plugin.session_id = nil
|
|
kong.ctx.plugin.session_data = nil
|
|
return result
|
|
end
|
|
end
|
|
end
|
|
|
|
function NanoHandler.header_filter(conf)
|
|
kong.log.err("2-222222222 HEADER_FILTER PHASE START -------------------------------------------------------------------------------------------------------------------------------------------------")
|
|
local ctx = kong.ctx.plugin
|
|
|
|
if ctx.blocked then
|
|
return
|
|
end
|
|
|
|
if ctx.inspection_complete then
|
|
kong.log.err("Inspection already completed, skipping header_filter")
|
|
return
|
|
end
|
|
|
|
local session_id = ctx.session_id
|
|
local session_data = ctx.session_data
|
|
if not session_id or not session_data then
|
|
kong.log.err("No session data found in header_filter")
|
|
return
|
|
end
|
|
|
|
local headers = kong.response.get_headers()
|
|
local header_data = nano.handleHeaders(headers)
|
|
local status_code = kong.response.get_status()
|
|
local content_length = tonumber(headers["content-length"]) or 0
|
|
|
|
kong.log.err("2-BEFORE send_response_headers call - session_id=", session_id, " status=", status_code, " content_length=", content_length)
|
|
|
|
local ok, verdict, response = pcall(function()
|
|
return nano.send_response_headers(session_id, session_data, header_data, status_code, content_length)
|
|
end)
|
|
|
|
if not ok then
|
|
kong.log.err("2-ERROR in send_response_headers: ", tostring(verdict), " - failing open, skipping response inspection")
|
|
ctx.inspection_complete = true
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
ctx.session_id = nil
|
|
ctx.session_data = nil
|
|
return
|
|
end
|
|
|
|
kong.log.err("2-Response headers verdict: ", verdict, " (INSPECT=", nano.AttachmentVerdict.INSPECT, ", ACCEPT=", nano.AttachmentVerdict.ACCEPT, ", DROP=", nano.AttachmentVerdict.DROP, ")")
|
|
|
|
if verdict == nano.AttachmentVerdict.DROP then
|
|
kong.ctx.plugin.blocked = true
|
|
ctx.inspection_complete = true
|
|
local custom_result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
ctx.session_id = nil
|
|
ctx.session_data = nil
|
|
return custom_result
|
|
end
|
|
|
|
kong.log.err("2-Response headers verdict: ", verdict, " - continuing to body_filter (will inspect body chunks)")
|
|
end
|
|
|
|
function NanoHandler.body_filter(conf)
|
|
local ctx = kong.ctx.plugin
|
|
local eof = ngx.arg[2] -- Read EOF flag first (no memory impact)
|
|
|
|
-- CRITICAL: Check blocked/complete status BEFORE reading chunk into Lua memory
|
|
if ctx.blocked then
|
|
if eof and ctx.session_data and not ctx.session_cleaned then
|
|
kong.log.err("3-BLOCKED + EOF: cleaning up session to prevent memory leak")
|
|
nano.fini_session(ctx.session_data)
|
|
nano.cleanup_all()
|
|
ctx.session_id = nil
|
|
ctx.session_data = nil
|
|
ctx.session_cleaned = true
|
|
end
|
|
return -- Exit without reading chunk
|
|
end
|
|
|
|
if ctx.inspection_complete then
|
|
-- Only log on EOF to reduce spam
|
|
if eof then
|
|
kong.log.err("3-INSPECTION_COMPLETE: EOF received, passing through")
|
|
end
|
|
return -- Exit without reading chunk - PREVENTS MEMORY LEAK
|
|
end
|
|
|
|
local session_id = ctx.session_id
|
|
local session_data = ctx.session_data
|
|
|
|
if not session_id or not session_data then
|
|
-- Only log on EOF to reduce spam
|
|
if eof then
|
|
kong.log.err("3-NO_SESSION: EOF received, passing through")
|
|
end
|
|
return -- Exit without reading chunk - PREVENTS MEMORY LEAK
|
|
end
|
|
|
|
-- Initialize tracking on first chunk
|
|
if not ctx.body_buffer_chunk then
|
|
ctx.body_buffer_chunk = 0
|
|
ctx.body_filter_start_time = ngx.now() * 1000
|
|
ctx.total_body_size = 0
|
|
end
|
|
|
|
-- CRITICAL: Check timeout BEFORE reading chunk into memory
|
|
local current_time = ngx.now() * 1000
|
|
local elapsed = current_time - ctx.body_filter_start_time
|
|
if elapsed > 150000 then
|
|
kong.log.err("Body filter timeout exceeded (", elapsed, "ms) - finalizing session and entering passthrough mode")
|
|
kong.log.err("------------------------------------------------------------------------")
|
|
kong.log.err("SETTING inspection_complete=true in body_filter (TIMEOUT)")
|
|
kong.log.err("------------------------------------------------------------------------")
|
|
ctx.inspection_complete = true
|
|
ctx.session_cleaned = true
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
ctx.session_id = nil
|
|
ctx.session_data = nil
|
|
|
|
-- CRITICAL: Force garbage collection to reclaim all chunk memory immediately
|
|
kong.log.err("Forcing garbage collection after timeout cleanup")
|
|
collectgarbage("collect") -- Full GC to reclaim ~1000 chunks worth of memory
|
|
local mem_after = collectgarbage("count")
|
|
kong.log.err("Memory after timeout GC: ", string.format("%.2f", mem_after), " KB (", string.format("%.2f", mem_after/1024), " MB)")
|
|
|
|
return -- Exit WITHOUT reading chunk - prevents memory accumulation
|
|
end
|
|
|
|
-- NOW read chunk into Lua memory (only when actively inspecting and not timed out)
|
|
local chunk = ngx.arg[1]
|
|
local chunk_size = chunk and #chunk or 0
|
|
|
|
-- Track total body size
|
|
ctx.total_body_size = ctx.total_body_size + chunk_size
|
|
|
|
-- Log chunk processing
|
|
kong.log.err("3-BODY_FILTER: chunk_size=" .. chunk_size .. " eof=" .. tostring(eof) .. " total=" .. ctx.total_body_size)
|
|
|
|
-- Aggressive GC for large responses (every 10MB)
|
|
if ctx.total_body_size > 0 and ctx.total_body_size % 10485760 < chunk_size then
|
|
kong.log.err("Large response streaming (", ctx.total_body_size, " bytes total), running GC")
|
|
collectgarbage("step", 1000)
|
|
end
|
|
|
|
if chunk and #chunk > 0 then
|
|
ctx.body_seen = true
|
|
|
|
-- Only send to nano if inspection not yet complete
|
|
if not ctx.inspection_complete then
|
|
local ok, result = pcall(function()
|
|
return {nano.send_body(session_id, session_data, chunk, nano.HttpChunkType.HTTP_RESPONSE_BODY)}
|
|
end)
|
|
|
|
if ok then
|
|
local verdict = result[1]
|
|
local response = result[2]
|
|
local modifications = result[3]
|
|
|
|
kong.log.err("CHUNK #", ctx.body_buffer_chunk, " VERDICT: ", verdict, " (INSPECT=", nano.AttachmentVerdict.INSPECT, ", ACCEPT=", nano.AttachmentVerdict.ACCEPT, ", DROP=", nano.AttachmentVerdict.DROP, ")")
|
|
|
|
if modifications then
|
|
chunk = nano.handle_body_modifications(chunk, modifications, ctx.body_buffer_chunk)
|
|
ngx.arg[1] = chunk
|
|
end
|
|
|
|
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
|
|
|
|
if verdict == nano.AttachmentVerdict.DROP then
|
|
ctx.plugin.blocked = true
|
|
ctx.inspection_complete = true
|
|
ctx.session_cleaned = true
|
|
local custom_result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
ctx.session_id = nil
|
|
ctx.session_data = nil
|
|
|
|
-- Force GC after DROP to reclaim memory before returning custom response
|
|
collectgarbage("collect")
|
|
|
|
return custom_result
|
|
end
|
|
else
|
|
kong.log.err("nano.send_body failed, failing open: ", tostring(result))
|
|
end
|
|
else
|
|
-- Inspection already complete - just count the chunk and pass through
|
|
kong.log.err("CHUNK #", ctx.body_buffer_chunk, " - skipping nano.send_body (inspection complete)")
|
|
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
|
|
end
|
|
end
|
|
|
|
-- Only process EOF if inspection is not yet complete (session still active)
|
|
if not ctx.inspection_complete and (eof or (ctx.expect_body == false and not ctx.body_seen)) then
|
|
local ok, result = pcall(function()
|
|
return {nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_RESPONSE_END)}
|
|
end)
|
|
|
|
if ok then
|
|
local verdict = result[1]
|
|
local response = result[2]
|
|
|
|
kong.log.err("3-Response END verdict: " .. tostring(verdict))
|
|
|
|
if verdict == nano.AttachmentVerdict.DROP then
|
|
ctx.plugin.blocked = true
|
|
ctx.inspection_complete = true
|
|
ctx.session_cleaned = true
|
|
local custom_result = nano.handle_custom_response(session_data, response)
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
ctx.session_id = nil
|
|
ctx.session_data = nil
|
|
return custom_result
|
|
else
|
|
-- Normal case: ACCEPT or INSPECT verdict
|
|
kong.log.err("------------------------------------------------------------------------")
|
|
kong.log.err("SETTING inspection_complete=true in body_filter (EOF processing complete)")
|
|
kong.log.err("------------------------------------------------------------------------")
|
|
ctx.inspection_complete = true
|
|
ctx.session_cleaned = true
|
|
|
|
kong.log.err("EOF reached - finalizing session in body_filter")
|
|
nano.fini_session(session_data)
|
|
nano.cleanup_all()
|
|
ctx.session_id = nil
|
|
ctx.session_data = nil
|
|
|
|
-- Force GC to reclaim chunk memory immediately after session cleanup
|
|
if ctx.total_body_size and ctx.total_body_size > 1048576 then -- If processed > 1MB
|
|
collectgarbage("collect")
|
|
kong.log.debug("Ran GC after processing ", ctx.total_body_size, " bytes")
|
|
end
|
|
end
|
|
else
|
|
kong.log.err("nano.end_inspection failed, failing open: ", tostring(result))
|
|
ctx.inspection_complete = true
|
|
end
|
|
end
|
|
end
|
|
|
|
function NanoHandler.log(conf)
|
|
kong.log.err("4-44444444444444444444-------------------------------------------------------------------------------------------------------------------------------------------------")
|
|
local ctx = kong.ctx.plugin
|
|
|
|
-- CRITICAL: Force garbage collection in log phase to prevent OOM
|
|
-- This is the last chance to reclaim memory before request completes
|
|
local mem_before = collectgarbage("count")
|
|
if mem_before > 10240 then -- If memory > 10 MB, force full GC
|
|
kong.log.err("High memory detected in log phase: ", string.format("%.2f", mem_before), " KB - forcing full GC")
|
|
collectgarbage("collect")
|
|
local mem_after = collectgarbage("count")
|
|
kong.log.err("Memory after log phase GC: ", string.format("%.2f", mem_after), " KB (freed ", string.format("%.2f", mem_before - mem_after), " KB)")
|
|
end
|
|
|
|
-- Log memory usage periodically (every 100th request)
|
|
if ngx.worker.id() == 0 then
|
|
local request_count = ngx.shared.kong_cache and ngx.shared.kong_cache:incr("request_count", 1, 0) or 0
|
|
if request_count % 100 == 0 then
|
|
local mem_kb = collectgarbage("count")
|
|
kong.log.err("MEMORY: Lua memory usage: ", string.format("%.2f", mem_kb), " KB (", string.format("%.2f", mem_kb/1024), " MB)")
|
|
end
|
|
end
|
|
|
|
if ctx.session_id and ctx.session_data then
|
|
-- If inspection already complete, it was cleaned up in body_filter - skip
|
|
if ctx.inspection_complete then
|
|
kong.log.err("Log phase: inspection already complete, session already finalized in body_filter")
|
|
return
|
|
end
|
|
|
|
-- If we reach here, body_filter never completed (connection closed/timeout/etc)
|
|
-- We MUST clean up here to prevent memory leak
|
|
kong.log.err("Log phase: body_filter never completed - finalizing session now to prevent memory leak")
|
|
|
|
local session_data = ctx.session_data
|
|
local session_id = ctx.session_id
|
|
|
|
kong.log.err("Log phase: finalizing session ", session_id)
|
|
|
|
nano.fini_session(session_data)
|
|
|
|
nano.cleanup_all()
|
|
|
|
ctx.session_id = nil
|
|
ctx.session_data = nil
|
|
ctx.inspection_complete = true
|
|
|
|
kong.log.err("Session ", session_id, " finalized in log phase due to incomplete body_filter processing")
|
|
else
|
|
kong.log.err("Log phase: no session data to finalize")
|
|
end
|
|
end
|
|
|
|
return NanoHandler
|