apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug: The jwt plugin does not handle the case where `exp` is empty

Open moonming opened this issue 6 years ago • 11 comments

https://github.com/apache/apisix/commit/27875e57901ff1bbb1161bbd55364ecef2990e27#r43958477

moonming avatar Nov 07 '20 06:11 moonming

@tzssangglass please take a look

moonming avatar Nov 07 '20 06:11 moonming

ok

tzssangglass avatar Nov 07 '20 12:11 tzssangglass

if exp is nil, it will cause a runtime error, like attempt to perform arithmetic on field 'exp' (a nil value), because use nil values in the + operator. but I have no idea to write this test case, because it's is a local function.

due to the code in check_schema function, as follows: https://github.com/apache/apisix/blob/4b349dc5b3a72a7819273d35e387e561a4c536c7/apisix/plugins/jwt-auth.lua#L126-L128 so it's not possible to make exp = nil via add consumers and jwt-auth plugin by curl /apisix/admin/consumers -H 'PUT' I can only change the existing values in the etcd, I don't know how to implement this in a test case. I need help.

tzssangglass avatar Nov 08 '20 02:11 tzssangglass

we can add a default value in JSON Schema here.

https://github.com/apache/apisix/blob/4b349dc5b3a72a7819273d35e387e561a4c536c7/apisix/plugins/jwt-auth.lua#L56

then we can remove https://github.com/apache/apisix/blob/4b349dc5b3a72a7819273d35e387e561a4c536c7/apisix/plugins/jwt-auth.lua#L126-L128

membphis avatar Nov 08 '20 03:11 membphis

In this way, still needto handle the case where exp is empty? I think that's unnecessary.

tzssangglass avatar Nov 08 '20 04:11 tzssangglass

In this way, still needto handle the case where exp is empty? I think that's unnecessary.

after we set a default value in jsonschema, eg: 3600 sec, the exp can not be empty.

membphis avatar Nov 08 '20 05:11 membphis

get it

tzssangglass avatar Nov 08 '20 05:11 tzssangglass

new merged code still have problem in my env

2020/11/10 10:51:19 [error] 25300#2021499: *20001 lua entry thread aborted: runtime error: /Users/idbeta/1106/apisix/apisix/plugins/jwt-auth.lua:177: attempt to perform arithmetic on field 'exp' (a nil value)
stack traceback:
coroutine 0:
	/Users/idbeta/1106/apisix/apisix/plugins/jwt-auth.lua: in function 'sign_handler'
	/Users/idbeta/1106/apisix/apisix/plugins/jwt-auth.lua:299: in function 'handler'
	/Users/idbeta/1106/apisix/apisix/api_router.lua:108: in function 'handler'
	...beta/1106/apisix//deps/share/lua/5.1/resty/radixtree.lua:775: in function 'dispatch'
	/Users/idbeta/1106/apisix/apisix/api_router.lua:134: in function 'match'
	/Users/idbeta/1106/apisix/apisix/init.lua:379: in function 'http_access_phase'
	access_by_lua(nginx.conf:177):2: in main chunk, client: 127.0.0.1, server: , request: "GET /apisix/plugin/jwt/sign?key=dd HTTP/1.1", host: "127.0.0.1:9080"

idbeta avatar Nov 10 '20 02:11 idbeta

new merged code still have problem in my env

2020/11/10 10:51:19 [error] 25300#2021499: *20001 lua entry thread aborted: runtime error: /Users/idbeta/1106/apisix/apisix/plugins/jwt-auth.lua:177: attempt to perform arithmetic on field 'exp' (a nil value)
stack traceback:
coroutine 0:
	/Users/idbeta/1106/apisix/apisix/plugins/jwt-auth.lua: in function 'sign_handler'
	/Users/idbeta/1106/apisix/apisix/plugins/jwt-auth.lua:299: in function 'handler'
	/Users/idbeta/1106/apisix/apisix/api_router.lua:108: in function 'handler'
	...beta/1106/apisix//deps/share/lua/5.1/resty/radixtree.lua:775: in function 'dispatch'
	/Users/idbeta/1106/apisix/apisix/api_router.lua:134: in function 'match'
	/Users/idbeta/1106/apisix/apisix/init.lua:379: in function 'http_access_phase'
	access_by_lua(nginx.conf:177):2: in main chunk, client: 127.0.0.1, server: , request: "GET /apisix/plugin/jwt/sign?key=dd HTTP/1.1", host: "127.0.0.1:9080"

check the value in etcd, if you are adding the consumer and jwt-auth plugin through the apisix admin api, exp will at least have default value, so you need to pay attention to whether the values in etcd are being submitted through the apisix admin api, or if you are operating directly on etcd

tzssangglass avatar Nov 10 '20 03:11 tzssangglass

oh, I added consumer from manager-api, the etcd data was like this

{"id":"test_jwt","create_time":1604976497,"update_time":1604976497,"username":"test_jwt","plugins":{"jwt-auth":{"algorithm":"HS256","base64_secret":false,"key":"dd","secret":"my-secret-key"}}}

idbeta avatar Nov 10 '20 03:11 idbeta

this is the wrong data, no exp, maybe the problem is that the manager-api commits the data directly to etcd without going through the apisix admin api

tzssangglass avatar Nov 10 '20 03:11 tzssangglass