mirror of
https://github.com/openappsec/attachment.git
synced 2025-11-19 18:44:25 +03:00
fix response body
This commit is contained in:
@@ -180,17 +180,22 @@ function NanoHandler.body_filter(conf)
|
|||||||
return
|
return
|
||||||
end
|
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
|
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
|
ctx.body_buffer_chunk = ctx.body_buffer_chunk or 0
|
||||||
|
|
||||||
if modifications then
|
if modifications then
|
||||||
body = nano.handle_body_modifications(body, modifications, ctx.body_buffer_chunk)
|
chunk = nano.handle_body_modifications(chunk, modifications, ctx.body_buffer_chunk)
|
||||||
kong.response.set_raw_body(body)
|
ngx.arg[1] = chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
|
ctx.body_buffer_chunk = ctx.body_buffer_chunk + 1
|
||||||
@@ -202,25 +207,14 @@ function NanoHandler.body_filter(conf)
|
|||||||
nano.cleanup_all()
|
nano.cleanup_all()
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
return
|
end
|
||||||
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
|
|
||||||
|
|
||||||
|
-- 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
|
if verdict == nano.AttachmentVerdict.DROP then
|
||||||
nano.fini_session(session_data)
|
nano.fini_session(session_data)
|
||||||
ctx.session_finalized = true
|
ctx.session_finalized = true
|
||||||
@@ -228,64 +222,11 @@ function NanoHandler.body_filter(conf)
|
|||||||
nano.cleanup_all()
|
nano.cleanup_all()
|
||||||
return result
|
return result
|
||||||
end
|
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)
|
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()
|
nano.cleanup_all()
|
||||||
return result
|
ctx.session_finalized = true
|
||||||
end
|
end
|
||||||
|
|
||||||
nano.fini_session(session_data)
|
|
||||||
-- Clean up allocated memory
|
|
||||||
nano.cleanup_all()
|
|
||||||
ctx.session_finalized = true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user