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(pdk) make kong.request.get_body() to return nil, err on simple json values #4063

Merged
merged 2 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion kong/pdk/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ local function new(self)
cjson.decode_array_with_array_mt(true)
local json = cjson.decode(body)
cjson.decode_array_with_array_mt(false)
if not json then
if type(json) ~= "table" then
return nil, "invalid json body", CONTENT_TYPE_JSON
end

Expand Down
6 changes: 3 additions & 3 deletions kong/plugins/oauth2/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,6 @@ local function parse_access_token(conf)
local found_in = {}
local access_token = kong.request.get_header(conf.auth_header_name)
if access_token then
if type(access_token) == "table" then --Take the first found
access_token = access_token[1]
end
local parts = {}
for v in access_token:gmatch("%S+") do -- Split by space
table.insert(parts, v)
Expand All @@ -513,6 +510,9 @@ local function parse_access_token(conf)
end
else
access_token = retrieve_parameters()[ACCESS_TOKEN]
if type(access_token) ~= "string" then
return
end
end

if conf.hide_credentials then
Expand Down
240 changes: 230 additions & 10 deletions t/01-pdk/04-request/16-get_body.t
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,227 @@ error: invalid json body



=== TEST 7: request.get_body() content type value is case-insensitive
=== TEST 7: request.get_body() returns error with null json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()

local json, err = pdk.request.get_body()

ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
null
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]



=== TEST 8: request.get_body() returns error with string json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()

local json, err = pdk.request.get_body()

ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
"test"
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]



=== TEST 9: request.get_body() returns error with number json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()

local json, err = pdk.request.get_body()

ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
123
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]



=== TEST 10: request.get_body() returns error with number (float) json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()

local json, err = pdk.request.get_body()

ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
123.23
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]



=== TEST 11: request.get_body() returns error with boolean true json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()

local json, err = pdk.request.get_body()

ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
true
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]



=== TEST 12: request.get_body() returns error with boolean false json body
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()

local json, err = pdk.request.get_body()

ngx.say("type: ", type(json))
ngx.say("error: ", err)
}
}
--- request
POST /t
false
--- more_headers
Content-Type: application/json
--- response_body
type: nil
error: invalid json body
--- no_error_log
[error]



=== TEST 13: request.get_body() returns empty object json body as table without metatable
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()

local json, err = pdk.request.get_body()

ngx.say("type: ", type(json))
ngx.say("meta: ", type(getmetatable(json)))
ngx.say("error: ", type(err))
}
}
--- request
POST /t
{}
--- more_headers
Content-Type: application/json
--- response_body
type: table
meta: nil
error: nil
--- no_error_log
[error]



=== TEST 14: request.get_body() returns empty array json body as table with metatable
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
local PDK = require "kong.pdk"
local pdk = PDK.new()

local json, err = pdk.request.get_body()

ngx.say("type: ", type(json))
ngx.say("meta: ", getmetatable(json) == require "cjson".array_mt and "correct" or "incorrect")
ngx.say("error: ", type(err))
}
}
--- request
POST /t
[]
--- more_headers
Content-Type: application/json
--- response_body
type: table
meta: correct
error: nil
--- no_error_log
[error]



=== TEST 15: request.get_body() content type value is case-insensitive
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -203,7 +423,7 @@ mime=application/x-www-form-urlencoded



=== TEST 8: request.get_body() with application/x-www-form-urlencoded returns request post arguments
=== TEST 16: request.get_body() with application/x-www-form-urlencoded returns request post arguments
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -231,7 +451,7 @@ Accept: application/json, text/html



=== TEST 9: request.get_body() with application/x-www-form-urlencoded returns empty table with header Content-Length: 0
=== TEST 17: request.get_body() with application/x-www-form-urlencoded returns empty table with header Content-Length: 0
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -255,7 +475,7 @@ next: nil



=== TEST 10: request.get_body() with application/x-www-form-urlencoded returns request post arguments case-sensitive
=== TEST 18: request.get_body() with application/x-www-form-urlencoded returns request post arguments case-sensitive
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -283,7 +503,7 @@ fOO: Too



=== TEST 11: request.get_body() with application/x-www-form-urlencoded fetches 100 post arguments by default
=== TEST 19: request.get_body() with application/x-www-form-urlencoded fetches 100 post arguments by default
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -320,7 +540,7 @@ number of query arguments fetched: 100



=== TEST 12: request.get_body() with application/x-www-form-urlencoded fetches max_args argument
=== TEST 20: request.get_body() with application/x-www-form-urlencoded fetches max_args argument
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -357,7 +577,7 @@ number of query arguments fetched: 60



=== TEST 13: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args invalid value
=== TEST 21: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args invalid value
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -379,7 +599,7 @@ error: max_args must be a number



=== TEST 14: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args < 1
=== TEST 22: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args < 1
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -401,7 +621,7 @@ error: max_args must be >= 1



=== TEST 15: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args > 1000
=== TEST 23: request.get_body() with application/x-www-form-urlencoded raises error when trying to fetch with max_args > 1000
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -423,7 +643,7 @@ error: max_args must be <= 1000



=== TEST 16: request.get_body() with application/x-www-form-urlencoded returns nil + error when the body is too big
=== TEST 24: request.get_body() with application/x-www-form-urlencoded returns nil + error when the body is too big
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand Down