Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: forward-auth request body is too large #10425

Closed
wants to merge 15 commits into from
Closed
6 changes: 5 additions & 1 deletion apisix/plugins/forward-auth.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,20 @@ function _M.access(conf, ctx)
method = conf.request_method
}

local httpc = http.new()
if params.method == "POST" then
params.body = core.request.get_body()
local client_body_reader, err = httpc:get_client_body_reader()
if not err then
params.body = client_body_reader
end
shreemaan-abhishek marked this conversation as resolved.
Show resolved Hide resolved
end

if conf.keepalive then
params.keepalive_timeout = conf.keepalive_timeout
params.keepalive_pool = conf.keepalive_pool
end

local httpc = http.new()
httpc:set_timeout(conf.timeout)

local res, err = httpc:request_uri(conf.uri, params)
Expand Down
62 changes: 61 additions & 1 deletion t/plugin/forward-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ property "request_method" validation failed: matches none of the enum values
end
end
end
end]],
[[
-- test large body
return function(conf, ctx)
local core = require("apisix.core")
if core.request.get_method() == "POST" and core.request.header(ctx, "Authorization") == "large-body" then
local req_body, err = core.request.get_body()
if err then
core.response.exit(400)
end
core.response.exit(200)
end
end]]
}
}
Expand Down Expand Up @@ -254,6 +266,23 @@ property "request_method" validation failed: matches none of the enum values
"type": "roundrobin"
}
}]],
},
{
url = "/apisix/admin/routes/7",
data = [[{
"plugins": {
"forward-auth": {
"uri": "http://127.0.0.1:1984/auth",
"request_headers": ["Authorization"],
"request_method": "POST"
},
"proxy-rewrite": {
"uri": "/echo"
}
},
"upstream_id": "u1",
"uri": "/large-body"
}]],
}
}

Expand All @@ -266,7 +295,7 @@ property "request_method" validation failed: matches none of the enum values
}
}
--- response_body eval
"201passed\n" x 9
"201passed\n" x 10



Expand Down Expand Up @@ -374,3 +403,34 @@ GET /hello
--- more_headers
Authorization: 111
--- error_code: 200



=== TEST 13: test large body
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin")
local http = require("resty.http")
local httpc = http.new()

local large_body = t.read_file("t/plugin/forward-auth/fw-auth-large-file.bin")
local uri = "http://127.0.0.1:" .. ngx.var.server_port
.. "/large-body"
local res, err = httpc:request_uri(uri,
{
method = ngx.HTTP_POST,
body = large_body,
headers = {
Authorization = "large-body"
}
}
)

if not res then
ngx.log(ngx.ERR, err)
return
end
}
}
--- error_code: 200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you test the response body or the response header too?

Binary file added t/plugin/forward-auth/fw-auth-large-file.bin
shreemaan-abhishek marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
Loading