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

Request validation plugin #1709

Merged
merged 17 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions apisix/plugins/request-validation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,38 @@ function _M.rewrite(conf)

if conf.body_schema then
Copy link
Member

Choose a reason for hiding this comment

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

another style, I think this one is better:

if not conf.body_schema then
    return
end

ngx.req.read_body()
local body = ngx.req.get_body_data()
local req_body, err

if headers["content-type"] == "application/x-www-form-urlencoded" then
    req_body, err = ngx.decode_args(body)
else -- JSON as default
    req_body, err = core.json.decode(body)
end

if not req_body then
    ... ...
    return 
end

local ok, err = core.schema.check(conf.body_schema, req_body)
...

ngx.req.read_body()
local req_body, error
local body = ngx.req.get_body_data()

if headers["content-type"] then
if headers["content-type"] == "application/json" then
local data, error = json_decode(body)
if not body then
local filename = ngx.req.get_body_file()
if not filename then
return core.response.exit(500)
end
local fd = io.open(filename, 'rb')
if not fd then
return core.response.exit(500)
end
body = fd:read('*a')
end

if headers["content-type"] == "application/x-www-form-urlencoded" then
req_body, err = ngx.decode_args(body)
else -- JSON as default
req_body, error = core.json.decode(body)
end

if not data then
core.log.error('failed to decode the req body', error)
core.response.exit(400)
return
end
if not req_body then
core.log.error('failed to decode the req body', error)
return core.response.exit(400, error)
end

local ok, err = core.schema.check(conf.body_schema, data)
if not ok then
core.log.error("req schema validation failed", err)
core.response.exit(400, err)
end
end
else
core.response.exit(400)
local ok, err = core.schema.check(conf.body_schema, req_body)
if not ok then
core.log.error("req schema validation failed", err)
return core.response.exit(400, err)
end
end
end


return _M
30 changes: 30 additions & 0 deletions t/plugin/request-validation.t
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,33 @@ required field missing
property "required_payload" is required


sshniro marked this conversation as resolved.
Show resolved Hide resolved

=== TEST 8: required header added in header
--- config
location /t {
content_by_lua_block {
local http = require "resty.http"
local httpc = http.new()
local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/opentracing"
local res, err = httpc:request_uri(uri,
{
method = "GET",
headers = {
["Content-Type"] = "application/json",
["required_payload"] = "test payload"
}
})

if res.status == 200 then
ngx.say("hello1 world")
else
ngx.say("failed")
end
}
}
--- request
GET /t
--- response_body
hello1 world
--- no_error_log
[error]
sshniro marked this conversation as resolved.
Show resolved Hide resolved