diff --git a/changelog/unreleased/kong/ai-plugin-read-file.yml b/changelog/unreleased/kong/ai-plugin-read-file.yml new file mode 100644 index 000000000000..d10f38c021d9 --- /dev/null +++ b/changelog/unreleased/kong/ai-plugin-read-file.yml @@ -0,0 +1,3 @@ +message: "allow AI plugin to read request from buffered file" +type: feature +scope: "Plugin" diff --git a/kong/plugins/ai-prompt-decorator/handler.lua b/kong/plugins/ai-prompt-decorator/handler.lua index 7103ce5903b4..23a18ea73997 100644 --- a/kong/plugins/ai-prompt-decorator/handler.lua +++ b/kong/plugins/ai-prompt-decorator/handler.lua @@ -55,7 +55,7 @@ function plugin:access(conf) kong.ctx.shared.ai_prompt_decorated = true -- future use -- if plugin ordering was altered, receive the "decorated" request - local request = kong.request.get_body("application/json") + local request = kong.request.get_body("application/json", nil, conf.max_request_body_size) if type(request) ~= "table" then return bad_request("this LLM route only supports application/json requests") end diff --git a/kong/plugins/ai-prompt-decorator/schema.lua b/kong/plugins/ai-prompt-decorator/schema.lua index ad0c5a85d72f..2d8abfab59fc 100644 --- a/kong/plugins/ai-prompt-decorator/schema.lua +++ b/kong/plugins/ai-prompt-decorator/schema.lua @@ -39,7 +39,9 @@ return { { config = { type = "record", fields = { - { prompts = prompts_record } + { prompts = prompts_record }, + { max_request_body_size = { type = "integer", default = 8 * 1024, gt = 0, + description = "max allowed body size allowed to be introspected" } }, } } } diff --git a/kong/plugins/ai-prompt-guard/handler.lua b/kong/plugins/ai-prompt-guard/handler.lua index 321fefad2024..304b9f55e45e 100644 --- a/kong/plugins/ai-prompt-guard/handler.lua +++ b/kong/plugins/ai-prompt-guard/handler.lua @@ -121,7 +121,7 @@ function plugin:access(conf) kong.ctx.shared.ai_prompt_guarded = true -- future use -- if plugin ordering was altered, receive the "decorated" request - local request = kong.request.get_body("application/json") + local request = kong.request.get_body("application/json", nil, conf.max_request_body_size) if type(request) ~= "table" then return bad_request("this LLM route only supports application/json requests") end diff --git a/kong/plugins/ai-prompt-guard/schema.lua b/kong/plugins/ai-prompt-guard/schema.lua index 9c0172752bdb..0864696cd290 100644 --- a/kong/plugins/ai-prompt-guard/schema.lua +++ b/kong/plugins/ai-prompt-guard/schema.lua @@ -32,6 +32,12 @@ return { type = "boolean", required = true, default = false } }, + { max_request_body_size = { + type = "integer", + default = 8 * 1024, + gt = 0, + description = "max allowed body size allowed to be introspected",} + }, } } } diff --git a/kong/plugins/ai-prompt-template/handler.lua b/kong/plugins/ai-prompt-template/handler.lua index 63224223a434..2be9137c9fe2 100644 --- a/kong/plugins/ai-prompt-template/handler.lua +++ b/kong/plugins/ai-prompt-template/handler.lua @@ -64,10 +64,10 @@ function AIPromptTemplateHandler:access(conf) kong.ctx.shared.ai_prompt_templated = true if conf.log_original_request then - kong.log.set_serialize_value(LOG_ENTRY_KEYS.REQUEST_BODY, kong.request.get_raw_body()) + kong.log.set_serialize_value(LOG_ENTRY_KEYS.REQUEST_BODY, kong.request.get_raw_body(conf.max_request_body_size)) end - local request = kong.request.get_body("application/json") + local request = kong.request.get_body("application/json", nil, conf.max_request_body_size) if type(request) ~= "table" then return bad_request("this LLM route only supports application/json requests") end diff --git a/kong/plugins/ai-prompt-template/schema.lua b/kong/plugins/ai-prompt-template/schema.lua index 0c3615557c2e..38e9d418ecd2 100644 --- a/kong/plugins/ai-prompt-template/schema.lua +++ b/kong/plugins/ai-prompt-template/schema.lua @@ -44,6 +44,12 @@ return { required = true, default = false, }}, + { max_request_body_size = { + type = "integer", + default = 8 * 1024, + gt = 0, + description = "max allowed body size allowed to be introspected", + }}, } }} }, diff --git a/kong/plugins/ai-proxy/handler.lua b/kong/plugins/ai-proxy/handler.lua index 8e661e89317e..5ff894c5e054 100644 --- a/kong/plugins/ai-proxy/handler.lua +++ b/kong/plugins/ai-proxy/handler.lua @@ -382,7 +382,7 @@ function _M:access(conf) -- first, calculate the coordinates of the request local content_type = kong.request.get_header("Content-Type") or "application/json" - request_table = kong.request.get_body(content_type) + request_table = kong.request.get_body(content_type, nil, conf.max_request_body_size) if not request_table then if not string.find(content_type, "multipart/form-data", nil, true) then diff --git a/kong/plugins/ai-proxy/schema.lua b/kong/plugins/ai-proxy/schema.lua index 2aa77c566114..0754a0348cd7 100644 --- a/kong/plugins/ai-proxy/schema.lua +++ b/kong/plugins/ai-proxy/schema.lua @@ -13,6 +13,13 @@ local ai_proxy_only_config = { default = "allow", one_of = { "allow", "deny", "always" }}, }, + { + max_request_body_size = { + type = "integer", + default = 8 * 1024, + gt = 0, + description = "max allowed body size allowed to be introspected",} + }, } for i, v in pairs(ai_proxy_only_config) do diff --git a/kong/plugins/ai-request-transformer/handler.lua b/kong/plugins/ai-request-transformer/handler.lua index 0eb5cd89d8f2..222ed079aa85 100644 --- a/kong/plugins/ai-request-transformer/handler.lua +++ b/kong/plugins/ai-request-transformer/handler.lua @@ -55,7 +55,7 @@ function _M:access(conf) -- if asked, introspect the request before proxying kong.log.debug("introspecting request with LLM") local new_request_body, err = ai_driver:ai_introspect_body( - kong.request.get_raw_body(), + kong.request.get_raw_body(conf.max_request_body_size), conf.prompt, http_opts, conf.transformation_extract_pattern diff --git a/kong/plugins/ai-request-transformer/schema.lua b/kong/plugins/ai-request-transformer/schema.lua index c7ce498ba68e..9ebd3b4b8d65 100644 --- a/kong/plugins/ai-request-transformer/schema.lua +++ b/kong/plugins/ai-request-transformer/schema.lua @@ -37,6 +37,14 @@ return { default = true, }}, + { + max_request_body_size = { + type = "integer", + default = 8 * 1024, + gt = 0, + description = "max allowed body size allowed to be introspected",} + }, + -- from forward-proxy { http_proxy_host = typedefs.host }, { http_proxy_port = typedefs.port }, @@ -46,7 +54,7 @@ return { { llm = llm.config_schema }, }, }}, - + }, entity_checks = { { diff --git a/kong/plugins/ai-response-transformer/handler.lua b/kong/plugins/ai-response-transformer/handler.lua index 94a82a5ff2dc..c1e154dbd060 100644 --- a/kong/plugins/ai-response-transformer/handler.lua +++ b/kong/plugins/ai-response-transformer/handler.lua @@ -113,7 +113,9 @@ function _M:access(conf) kong.log.debug("intercepting plugin flow with one-shot request") local httpc = http.new() - local res, err = subrequest(httpc, kong.request.get_raw_body(), http_opts) + local res, err = subrequest(httpc, + kong.request.get_raw_body(conf.max_request_body_size), + http_opts) if err then return internal_server_error(err) end diff --git a/kong/plugins/ai-response-transformer/schema.lua b/kong/plugins/ai-response-transformer/schema.lua index 565d467fe2dc..2f52f6f27e2a 100644 --- a/kong/plugins/ai-response-transformer/schema.lua +++ b/kong/plugins/ai-response-transformer/schema.lua @@ -45,6 +45,13 @@ return { default = true, }}, + { max_request_body_size = { + type = "integer", + default = 8 * 1024, + gt = 0, + description = "max allowed body size allowed to be introspected",} + }, + -- from forward-proxy { http_proxy_host = typedefs.host }, { http_proxy_port = typedefs.port },