From 1a4765a3c645e17903db8f161fac1cf2eef82b4a Mon Sep 17 00:00:00 2001 From: wiaamm Date: Mon, 17 Nov 2025 20:38:25 +0200 Subject: [PATCH] fix response body --- .../open-appsec-waf-kong-plugin/handler.lua | 95 ++++--------------- 1 file changed, 18 insertions(+), 77 deletions(-) diff --git a/attachments/kong/plugins/open-appsec-waf-kong-plugin/handler.lua b/attachments/kong/plugins/open-appsec-waf-kong-plugin/handler.lua index f86ded0..d73f4f2 100755 --- a/attachments/kong/plugins/open-appsec-waf-kong-plugin/handler.lua +++ b/attachments/kong/plugins/open-appsec-waf-kong-plugin/handler.lua @@ -180,17 +180,22 @@ function NanoHandler.body_filter(conf) return end - local body = kong.response.get_raw_body() + -- Get the current chunk from ngx.arg + local chunk = ngx.arg[1] + local eof = ngx.arg[2] - if body and #body > 0 then + -- Process body chunk if present + if chunk and #chunk > 0 then ctx.body_seen = true - local verdict, response, modifications = nano.send_body(session_id, session_data, body, nano.HttpChunkType.HTTP_RESPONSE_BODY) + kong.log.debug("Processing response body chunk, size: ", #chunk, " bytes, EOF: ", eof) + + local verdict, response, modifications = nano.send_body(session_id, session_data, chunk, nano.HttpChunkType.HTTP_RESPONSE_BODY) ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0 if modifications then - body = nano.handle_body_modifications(body, modifications, ctx.body_buffer_chunk) - kong.response.set_raw_body(body) + 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 @@ -202,25 +207,14 @@ function NanoHandler.body_filter(conf) nano.cleanup_all() return result end - return - elseif ctx.expect_body then - kong.log.debug("Response body not in memory, attempting to read from buffer/file") - - local body_data = ngx.var.response_body - if body_data and #body_data > 0 then - kong.log.debug("Found response body in nginx var, size: ", #body_data) - ctx.body_seen = true - local verdict, response, modifications = nano.send_body(session_id, session_data, body_data, nano.HttpChunkType.HTTP_RESPONSE_BODY) - - ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0 - - if modifications then - body_data = nano.handle_body_modifications(body_data, modifications, ctx.body_buffer_chunk) - kong.response.set_raw_body(body_data) - end - - ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1 + end + -- Handle end of response + if eof then + kong.log.debug("Response body end of stream reached") + + if ctx.body_seen or ctx.expect_body == false then + local verdict, response = nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_RESPONSE_END) if verdict == nano.AttachmentVerdict.DROP then nano.fini_session(session_data) ctx.session_finalized = true @@ -228,64 +222,11 @@ function NanoHandler.body_filter(conf) nano.cleanup_all() return result end - return - else - -- Try to read from response body file - local body_file = ngx.var.response_body_file - if body_file then - kong.log.debug("Reading response 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.debug("Sending entire response body of size ", #entire_body, " bytes to C module") - ctx.body_seen = true - local verdict, response, modifications = nano.send_body(session_id, session_data, entire_body, nano.HttpChunkType.HTTP_RESPONSE_BODY) - - ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0 - - if modifications then - entire_body = nano.handle_body_modifications(entire_body, modifications, ctx.body_buffer_chunk) - kong.response.set_raw_body(entire_body) - end - - ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1 - - if verdict == nano.AttachmentVerdict.DROP then - nano.fini_session(session_data) - ctx.session_finalized = true - local result = nano.handle_custom_response(session_data, response) - nano.cleanup_all() - return result - end - return - end - else - kong.log.warn("Failed to open response body file: ", body_file) - end - else - kong.log.debug("Response body expected but no body data or file available") - end - end - end - - if ctx.body_seen or ctx.expect_body == false then - local verdict, response = nano.end_inspection(session_id, session_data, nano.HttpChunkType.HTTP_RESPONSE_END) - if verdict == nano.AttachmentVerdict.DROP then nano.fini_session(session_data) - ctx.session_finalized = true - local result = nano.handle_custom_response(session_data, response) - -- Clean up allocated memory nano.cleanup_all() - return result + ctx.session_finalized = true end - - nano.fini_session(session_data) - -- Clean up allocated memory - nano.cleanup_all() - ctx.session_finalized = true end end