Kong / kong

🦍 The Cloud-Native API Gateway and AI Gateway.
https://konghq.com/install/#kong-community
Apache License 2.0
39.22k stars 4.81k forks source link

Error occurs on endpoints when `tags` parameter is empty #13591

Closed hutchic closed 3 days ago

hutchic commented 2 months ago

Is there an existing issue for this?

Kong version ($ kong version)

Kong 2.7.1

Current Behavior

Requests with ?tags= to the admin api results in a 500 error

curl -X GET 'http://localhost:8001/ca_certificates?tags='

Kong logs

2024/08/29 05:18:38 [error] 1335#0: *10353 [lua] api_helpers.lua:540: handle_error(): /usr/local/share/lua/5.1/lapis/application.lua:440: /usr/local/share/lua/5.1/pl/stringx.lua:32: argument 1 expected a 'string', got a 'userdata'
stack traceback:
   [C]: in function 'error'
   /usr/local/share/lua/5.1/pl/utils.lua:291: in function 'assert_arg'
   /usr/local/share/lua/5.1/pl/stringx.lua:32: in function 'assert_string'
   /usr/local/share/lua/5.1/pl/stringx.lua:194: in function 'split'
   /usr/local/share/lua/5.1/kong/api/endpoints.lua:166: in function 'extract_options'
   /usr/local/share/lua/5.1/kong/api/endpoints.lua:209: in function 'page_collection'
   /usr/local/share/lua/5.1/kong/api/endpoints.lua:319: in function 'fn'
   /usr/local/share/lua/5.1/kong/api/api_helpers.lua:311: in function </usr/local/share/lua/5.1/kong/api/api_helpers.lua:289>

stack traceback:
   [C]: in function 'error'
   /usr/local/share/lua/5.1/lapis/application.lua:440: in function 'handler'
   /usr/local/share/lua/5.1/lapis/application.lua:186: in function 'resolve'
   /usr/local/share/lua/5.1/lapis/application.lua:217: in function </usr/local/share/lua/5.1/lapis/application.lua:215>
   [C]: in function 'xpcall'
   /usr/local/share/lua/5.1/lapis/application.lua:215: in function 'dispatch'
   /usr/local/share/lua/5.1/lapis/nginx.lua:231: in function 'serve'
   /usr/local/share/lua/5.1/kong/init.lua:1867: in function 'admin_content'
   content_by_lua(nginx-kong.conf:333):2: in main chunk, client: 172.19.0.1, server: kong_admin, request: "GET /ca_certificates?tags= HTTP/1.1", host: "localhost:8001"
172.19.0.1 - - [29/Aug/2024:05:18:39 +0000] "GET /ca_certificates?tags= HTTP/1.1" 500 42 "-" "curl/7.81.0"

Expected Behavior

The API should return a valid response, such as an empty list of CA certificates or a relevant error message indicating that the tags parameter is required, if applicable. The system should handle empty tags gracefully without causing an internal server error.

Steps To Reproduce

run kong run the curl curl -X GET 'http://localhost:8001/ca_certificates?tags='

Anything else?

No response

chobits commented 2 months ago

For kong master, I could still reproduce it:

xc ~ $ curl -X GET 'http://localhost:8001/ca_certificates?tags=1'
{"next":null,"data":[]}

xc ~ $ curl -X GET 'http://localhost:8001/ca_certificates?tags='
{"message":"An unexpected error occurred"}

xc ~ $ curl -X GET 'http://localhost:8001/ca_certificates'
{"next":null,"data":[]}xc ~ $

and it reported the runtime error in admin error log file:

2024/09/02 10:30:14 [error] 96780#0: *647 [lua] api_helpers.lua:541: handle_error(): ...l-bin/build/kong-dev/share/lua/5.1/lapis/application.lua:440:
 ...ng/bazel-bin/build/kong-dev/share/lua/5.1/pl/stringx.lua:32: 
     argument 1 expected a 'string', got a 'userdata'
stack traceback:
        [C]: in function 'error'
        ...kong/bazel-bin/build/kong-dev/share/lua/5.1/pl/utils.lua:291: in function 'assert_arg'
        ...ng/bazel-bin/build/kong-dev/share/lua/5.1/pl/stringx.lua:32: in function 'assert_string'
        ...ng/bazel-bin/build/kong-dev/share/lua/5.1/pl/stringx.lua:194: in function 'split'
        ./kong/api/endpoints.lua:167: in function 'extract_options'
        ./kong/api/endpoints.lua:210: in function 'page_collection'
        ./kong/api/endpoints.lua:320: in function 'fn'
        ./kong/api/api_helpers.lua:312: in function <./kong/api/api_helpers.lua:290>
nowNick commented 1 month ago

Internal ticket tracking code: KAG-5496