Kong / kong

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

Query Args not forwarded in proxied request #11325

Closed chirag-manwani closed 1 year ago

chirag-manwani commented 1 year ago

Is there an existing issue for this?

Kong version ($ kong version)

3.3.0

Current Behavior

When I send a request to Kong with empty URL parameters but with a ? and there is a plugin that sets an additional query arg, the proxied request to the backend does not have any query args.

Expected Behavior

The proxied request should have the query arg set by the plugin.
(Side Note: I am facing this issue in Kong 3.3.0 but not in Kong 2.2.0)

Steps To Reproduce

  1. Deploy Kong 3.3.0 using any tool
  2. Use deck or any tool to set this config
    _format_version: "1.1"
    services:
    - connect_timeout: 5000
    host: localhost
    name: kong
    port: 8000
    protocol: http
    read_timeout: 5000
    retries: 0
    write_timeout: 5000
    routes:
    - name: first
    paths:
    - /first
    path_handling: v1
    preserve_host: false
    protocols:
    - http
    - https
    regex_priority: 0
    strip_path: false
    https_redirect_status_code: 426
    plugins:
    - name: post-function
      config:
        access:
        - local q = kong.request.get_query() kong.log.inspect(q) q["query_arg_appended"]
          = 1  kong.log.inspect("is_args before setting query args", ngx.var.is_args)
          kong.service.request.set_query(q) kong.log.inspect("is_args before setting
          query args", ngx.var.is_args) kong.log.inspect(kong.request.get_query())
        body_filter: []
        certificate: []
        header_filter: []
        log:
        - kong.log.inspect("is_args in log phase", ngx.var.is_args)
        rewrite: []
      enabled: true
      protocols:
      - grpc
      - grpcs
      - http
      - https
    - name: request-transformer
      config:
        add:
          body: []
          headers: []
          querystring: []
        append:
          body: []
          headers: []
          querystring: []
        http_method: null
        remove:
          body: []
          headers: []
          querystring: []
        rename:
          body: []
          headers: []
          querystring: []
        replace:
          body: []
          headers: []
          querystring: []
          uri: /second
      enabled: true
      protocols:
      - grpc
      - grpcs
      - http
      - https
    - name: second
    paths:
    - /second
    path_handling: v1
    preserve_host: false
    protocols:
    - http
    - https
    regex_priority: 0
    strip_path: false
    https_redirect_status_code: 426
    plugins:
    - name: post-function
      config:
        access:
        - local q = kong.request.get_query() kong.log.inspect(q) kong.response.exit(200,
          q)
        body_filter: []
        certificate: []
        header_filter: []
        log: []
        rewrite: []
      enabled: true
      protocols:
      - grpc
      - grpcs
      - http
      - https
  3. Send request to Kong proxy endpoint- curl --location 'localhost/first?'
  4. You will get a json response which is empty. Ideally you should have received
    {
    "query_arg_appended": "1"
    }

Sending a request without the ? or a ? and at least 1 query arg works as expected.

Anything else?

I am facing the above issue in Kong 3.3.0. Kong 2.2.0 is working as expected. I have not tried it for other versions, but I can in case more information is required.

Kong Logs

kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [info] 16965#0: *896 client closed connection while waiting for request, client: 192.168.224.1, server: 0.0.0.0:8000
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 [kong] access.lua:486 [request-transformer] 8[request-transformer] template `/second` rendered to `/second`
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 [kong] [string "local q = kong.request.get_query() kong.log.i..."]:f:1 [post-function]-----------------------------------+
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 |{}                                                                                                                      |
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 +------------------------------------------------------------------------------------------------------------------------+
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 [kong] [string "local q = kong.request.get_query() kong.log.i..."]:f:1 [post-function]-----------------------------------+
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 |{                                                                                                                       |
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 |  query_arg_appended = "1"                                                                                              |
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 |} nil                                                                                                                   |
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 +------------------------------------------------------------------------------------------------------------------------+
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 [lua] init.lua:1164: balancer(): setting address (try 1): 192.168.29.25:80
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *897 [lua] init.lua:1193: balancer(): enabled connection keepalive (pool=192.168.29.25|80, pool_size=60, idle_timeout=60, max_requests=100)
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *899 [kong] [string "local q = kong.request.get_query() kong.log.i..."]:f:1 [post-function]-----------------------------------+
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *899 |{}                                                                                                                      |
kong-3-3-ubuntu-kong-1   | 2023/07/29 11:39:28 [debug] 16965#0: *899 +------------------------------------------------------------------------------------------------------------------------+
kong-3-3-ubuntu-kong-1   |
kong-3-3-ubuntu-kong-1   | ==> /kong/servroot/logs/access.log <==
kong-3-3-ubuntu-kong-1   | 192.168.224.1 - - [29/Jul/2023:11:39:28 +0000] "GET /second? HTTP/1.1" 200 2 "-" "PostmanRuntime/7.32.3"
kong-3-3-ubuntu-kong-1   | 192.168.224.1 - - [29/Jul/2023:11:39:28 +0000] "GET /first? HTTP/1.1" 200 2 "-" "PostmanRuntime/7.32.3"
kong-3-3-ubuntu-kong-1   |

Response from hitting the admin endpoint

{
    "node_id": "c04918da-f1e4-41a9-ba8e-387588660f4b",
    "tagline": "Welcome to kong",
    "plugins": {
        "available_on_server": {
            "session": {
                "version": "3.3.0",
                "priority": 1900
            },
            "acme": {
                "version": "3.3.0",
                "priority": 1705
            },
            "grpc-gateway": {
                "version": "3.3.0",
                "priority": 998
            },
            "grpc-web": {
                "version": "3.3.0",
                "priority": 3
            },
            "pre-function": {
                "version": "3.3.0",
                "priority": 1000000
            },
            "post-function": {
                "version": "3.3.0",
                "priority": -1000
            },
            "azure-functions": {
                "version": "3.3.0",
                "priority": 749
            },
            "zipkin": {
                "version": "3.3.0",
                "priority": 100000
            },
            "opentelemetry": {
                "version": "0.1.0",
                "priority": 14
            },
            "security": {
                "version": "1.0.0",
                "priority": 9000
            },
            "proxy-cache": {
                "version": "3.3.0",
                "priority": 100
            },
            "config-by-env": {
                "version": "2.2.0",
                "priority": 10000
            },
            "http-log": {
                "version": "3.3.0",
                "priority": 12
            },
            "key-auth": {
                "version": "3.3.0",
                "priority": 1250
            },
            "hmac-auth": {
                "version": "3.3.0",
                "priority": 1030
            },
            "basic-auth": {
                "version": "3.3.0",
                "priority": 1100
            },
            "ldap-auth": {
                "version": "3.3.0",
                "priority": 1200
            },
            "datadog": {
                "version": "3.3.0",
                "priority": 10
            },
            "loggly": {
                "version": "3.3.0",
                "priority": 6
            },
            "syslog": {
                "version": "3.3.0",
                "priority": 4
            },
            "response-ratelimiting": {
                "version": "3.3.0",
                "priority": 900
            },
            "rate-limiting": {
                "version": "3.3.0",
                "priority": 910
            },
            "request-size-limiting": {
                "version": "3.3.0",
                "priority": 951
            },
            "response-transformer": {
                "version": "3.3.0",
                "priority": 800
            },
            "request-transformer": {
                "version": "3.3.0",
                "priority": 801
            },
            "ip-restriction": {
                "version": "3.3.0",
                "priority": 990
            },
            "jwt": {
                "version": "3.3.0",
                "priority": 1450
            },
            "acl": {
                "version": "3.3.0",
                "priority": 950
            },
            "correlation-id": {
                "version": "3.3.0",
                "priority": 1
            },
            "cors": {
                "version": "3.3.0",
                "priority": 2000
            },
            "oauth2": {
                "version": "3.3.0",
                "priority": 1400
            },
            "tcp-log": {
                "version": "3.3.0",
                "priority": 7
            },
            "udp-log": {
                "version": "3.3.0",
                "priority": 8
            },
            "file-log": {
                "version": "3.3.0",
                "priority": 9
            },
            "statsd": {
                "version": "3.3.0",
                "priority": 11
            },
            "bot-detection": {
                "version": "3.3.0",
                "priority": 2500
            },
            "aws-lambda": {
                "version": "3.3.0",
                "priority": 750
            },
            "request-termination": {
                "version": "3.3.0",
                "priority": 2
            },
            "prometheus": {
                "version": "3.3.0",
                "priority": 13
            }
        },
        "enabled_in_cluster": [
            "post-function",
            "request-transformer"
        ]
    },
    "pids": {
        "master": 16964,
        "workers": [
            16965
        ]
    },
    "version": "3.3.0",
    "timers": {
        "running": 33,
        "pending": 1
    },
    "configuration": {
        "lua_max_resp_headers": 100,
        "lua_package_path": "./?.lua;./?/init.lua;",
        "lua_max_uri_args": 100,
        "lua_max_post_args": 100,
        "worker_consistency": "eventual",
        "router_flavor": "traditional_compatible",
        "plugins": [
            "bundled"
        ],
        "host_ports": {},
        "anonymous_reports": false,
        "dns_resolver": {},
        "dns_hostsfile": "/etc/hosts",
        "cassandra_timeout": 60000,
        "pg_timeout": 60000,
        "worker_state_update_frequency": 5,
        "dns_error_ttl": 1,
        "dns_not_found_ttl": 30,
        "dns_stale_ttl": 4,
        "db_cache_warmup_entities": [
            "services"
        ],
        "dns_cache_size": 10000,
        "dns_order": [
            "LAST",
            "SRV",
            "A",
            "CNAME"
        ],
        "dns_no_sync": false,
        "headers": [
            "server_tokens",
            "latency_tokens"
        ],
        "nginx_main_worker_rlimit_nofile": "auto",
        "admin_ssl_cert_default_ecdsa": "/kong/servroot/ssl/admin-kong-default-ecdsa.crt",
        "nginx_events_worker_connections": "auto",
        "lua_package_cpath": "",
        "nginx_events_multi_accept": "on",
        "pluginserver_names": {},
        "nginx_http_charset": "UTF-8",
        "pg_semaphore_timeout": 60000,
        "pg_ro_ssl": false,
        "nginx_http_client_max_body_size": "0",
        "ssl_cert": [
            "/kong/servroot/ssl/kong-default.crt",
            "/kong/servroot/ssl/kong-default-ecdsa.crt"
        ],
        "nginx_http_client_body_buffer_size": "8k",
        "lmdb_environment_path": "dbless.lmdb",
        "untrusted_lua_sandbox_requires": {},
        "pg_database": "kong",
        "cassandra_schema_consensus_timeout": 60000,
        "cassandra_write_consistency": "ONE",
        "cassandra_repl_strategy": "SimpleStrategy",
        "cassandra_refresh_frequency": 60,
        "prefix": "/kong/servroot",
        "upstream_keepalive_pool_size": 60,
        "client_ssl": false,
        "error_default_type": "text/plain",
        "db_resurrect_ttl": 30,
        "nginx_http_ssl_session_tickets": "on",
        "cluster_dp_labels": {},
        "nginx_http_lua_regex_cache_max_entries": "8192",
        "nginx_admin_client_max_body_size": "10m",
        "opentelemetry_tracing": [
            "off"
        ],
        "nginx_admin_client_body_buffer_size": "10m",
        "nginx_stream_ssl_protocols": "TLSv1.2 TLSv1.3",
        "nginx_http_lua_regex_match_limit": "100000",
        "cassandra_repl_factor": 1,
        "cassandra_data_centers": [
            "dc1:2",
            "dc2:3"
        ],
        "upstream_keepalive_idle_timeout": 60,
        "upstream_keepalive_max_requests": 100,
        "client_body_buffer_size": "8k",
        "nginx_stream_ssl_session_timeout": "1d",
        "pg_host": "db",
        "ssl_protocols": "TLSv1.1 TLSv1.2 TLSv1.3",
        "ssl_cert_key": [
            "/kong/servroot/ssl/kong-default.key",
            "/kong/servroot/ssl/kong-default-ecdsa.key"
        ],
        "nginx_http_ssl_protocols": "TLSv1.2 TLSv1.3",
        "mem_cache_size": "128m",
        "ssl_prefer_server_ciphers": "on",
        "nginx_http_ssl_prefer_server_ciphers": "off",
        "nginx_stream_ssl_prefer_server_ciphers": "off",
        "ssl_dhparam": "ffdhe2048",
        "nginx_http_ssl_dhparam": "ffdhe2048",
        "nginx_stream_ssl_dhparam": "ffdhe2048",
        "ssl_session_tickets": "on",
        "nginx_stream_ssl_session_tickets": "on",
        "ssl_session_timeout": "1d",
        "nginx_http_ssl_session_timeout": "1d",
        "nginx_status_directives": {},
        "ssl_session_cache_size": "10m",
        "nginx_admin_directives": [
            {
                "name": "client_body_buffer_size",
                "value": "10m"
            },
            {
                "name": "client_max_body_size",
                "value": "10m"
            }
        ],
        "proxy_error_log": "logs/error.log",
        "nginx_stream_directives": [
            {
                "name": "lua_shared_dict",
                "value": "stream_prometheus_metrics 5m"
            },
            {
                "name": "lua_ssl_protocols",
                "value": "TLSv1.1 TLSv1.2 TLSv1.3"
            },
            {
                "name": "ssl_dhparam",
                "value": "/kong/servroot/ssl/ffdhe2048.pem"
            },
            {
                "name": "ssl_prefer_server_ciphers",
                "value": "off"
            },
            {
                "name": "ssl_protocols",
                "value": "TLSv1.2 TLSv1.3"
            },
            {
                "name": "ssl_session_tickets",
                "value": "on"
            },
            {
                "name": "ssl_session_timeout",
                "value": "1d"
            }
        ],
        "proxy_stream_error_log": "logs/error.log",
        "nginx_supstream_directives": {},
        "admin_error_log": "logs/error.log",
        "nginx_sproxy_directives": {},
        "status_error_log": "logs/status_error.log",
        "lua_ssl_trusted_certificate": [
            "/etc/ssl/certs/ca-certificates.crt"
        ],
        "lua_ssl_verify_depth": 1,
        "lua_ssl_protocols": "TLSv1.1 TLSv1.2 TLSv1.3",
        "nginx_pid": "/kong/servroot/pids/nginx.pid",
        "nginx_stream_lua_ssl_protocols": "TLSv1.1 TLSv1.2 TLSv1.3",
        "lua_socket_pool_size": 30,
        "nginx_err_logs": "/kong/servroot/logs/error.log",
        "db_update_frequency": 5,
        "admin_ssl_cert_key_default": "/kong/servroot/ssl/admin-kong-default.key",
        "nginx_acc_logs": "/kong/servroot/logs/access.log",
        "untrusted_lua": "on",
        "admin_acc_logs": "/kong/servroot/logs/admin_access.log",
        "pg_user": "kong",
        "nginx_conf": "/kong/servroot/nginx.conf",
        "cluster_ocsp": "off",
        "nginx_kong_conf": "/kong/servroot/nginx-kong.conf",
        "cluster_use_proxy": false,
        "nginx_kong_stream_conf": "/kong/servroot/nginx-kong-stream.conf",
        "pg_ssl_verify": false,
        "pg_max_concurrent_queries": 0,
        "kong_process_secrets": "/kong/servroot/.kong_process_secrets",
        "untrusted_lua_sandbox_environment": {},
        "ssl_cert_csr_default": "/kong/servroot/ssl/kong-default.csr",
        "lmdb_map_size": "128m",
        "ssl_cert_default": "/kong/servroot/ssl/kong-default.crt",
        "opentelemetry_tracing_sampling_rate": 0.01,
        "ssl_cert_key_default": "/kong/servroot/ssl/kong-default.key",
        "proxy_server_ssl_verify": true,
        "ssl_cert_default_ecdsa": "/kong/servroot/ssl/kong-default-ecdsa.crt",
        "pg_ro_ssl_verify": false,
        "ssl_cert_key_default_ecdsa": "/kong/servroot/ssl/kong-default-ecdsa.key",
        "pg_port": 5432,
        "client_ssl_cert_default": "/kong/servroot/ssl/kong-default.crt",
        "client_ssl_cert_key_default": "/kong/servroot/ssl/kong-default.key",
        "admin_ssl_cert_default": "/kong/servroot/ssl/admin-kong-default.crt",
        "stream_proxy_ssl_enabled": false,
        "admin_ssl_enabled": false,
        "ssl_cipher_suite": "intermediate",
        "cassandra_ssl": false,
        "cassandra_ssl_verify": false,
        "ssl_ciphers": "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384",
        "proxy_ssl_enabled": true,
        "status_ssl_cert_default": "/kong/servroot/ssl/status-kong-default.crt",
        "lua_ssl_trusted_certificate_combined": "/kong/servroot/.ca_combined",
        "status_ssl_cert_key_default": "/kong/servroot/ssl/status-kong-default.key",
        "vaults": [
            "bundled"
        ],
        "status_ssl_cert_default_ecdsa": "/kong/servroot/ssl/status-kong-default-ecdsa.crt",
        "loaded_vaults": {
            "env": true
        },
        "status_ssl_cert_key_default_ecdsa": "/kong/servroot/ssl/status-kong-default-ecdsa.key",
        "loaded_plugins": {
            "session": true,
            "acme": true,
            "grpc-gateway": true,
            "grpc-web": true,
            "pre-function": true,
            "post-function": true,
            "azure-functions": true,
            "zipkin": true,
            "opentelemetry": true,
            "security": true,
            "proxy-cache": true,
            "config-by-env": true,
            "http-log": true,
            "key-auth": true,
            "hmac-auth": true,
            "basic-auth": true,
            "ldap-auth": true,
            "datadog": true,
            "loggly": true,
            "syslog": true,
            "response-ratelimiting": true,
            "rate-limiting": true,
            "request-size-limiting": true,
            "response-transformer": true,
            "request-transformer": true,
            "ip-restriction": true,
            "jwt": true,
            "acl": true,
            "correlation-id": true,
            "cors": true,
            "oauth2": true,
            "tcp-log": true,
            "udp-log": true,
            "file-log": true,
            "statsd": true,
            "bot-detection": true,
            "aws-lambda": true,
            "request-termination": true,
            "prometheus": true
        },
        "cassandra_lb_policy": "RequestRoundRobin",
        "proxy_listeners": [
            {
                "ip": "0.0.0.0",
                "reuseport": true,
                "port": 8000,
                "ssl": false,
                "http2": false,
                "proxy_protocol": false,
                "deferred": false,
                "bind": false,
                "backlog=16384": true,
                "ipv6only=on": false,
                "listener": "0.0.0.0:8000 reuseport backlog=16384",
                "so_keepalive=on": false,
                "so_keepalive=off": false,
                "so_keepalive=%w*:%w*:%d*": false,
                "ipv6only=off": false
            },
            {
                "ip": "0.0.0.0",
                "reuseport": true,
                "port": 8443,
                "ssl": true,
                "http2": true,
                "proxy_protocol": false,
                "deferred": false,
                "bind": false,
                "backlog=16384": true,
                "ipv6only=on": false,
                "listener": "0.0.0.0:8443 ssl http2 reuseport backlog=16384",
                "so_keepalive=on": false,
                "so_keepalive=off": false,
                "so_keepalive=%w*:%w*:%d*": false,
                "ipv6only=off": false
            }
        ],
        "admin_listeners": [
            {
                "ip": "0.0.0.0",
                "reuseport": false,
                "port": 8001,
                "ssl": false,
                "http2": false,
                "proxy_protocol": false,
                "deferred": false,
                "bind": false,
                "backlog=%d+": false,
                "ipv6only=on": false,
                "listener": "0.0.0.0:8001",
                "so_keepalive=on": false,
                "so_keepalive=off": false,
                "so_keepalive=%w*:%w*:%d*": false,
                "ipv6only=off": false
            }
        ],
        "status_listeners": {},
        "admin_listen": [
            "0.0.0.0:8001"
        ],
        "status_listen": [
            "off"
        ],
        "stream_listen": [
            "off"
        ],
        "cluster_listen": [
            "0.0.0.0:8005"
        ],
        "admin_ssl_cert": {},
        "admin_ssl_cert_key": {},
        "status_ssl_cert": {},
        "status_ssl_cert_key": {},
        "log_level": "debug",
        "nginx_user": "kong kong",
        "nginx_main_user": "kong kong",
        "nginx_daemon": "on",
        "cluster_mtls": "shared",
        "nginx_main_daemon": "on",
        "nginx_worker_processes": "1",
        "nginx_main_worker_processes": "1",
        "trusted_ips": {},
        "real_ip_header": "X-Real-IP",
        "nginx_proxy_real_ip_header": "X-Real-IP",
        "real_ip_recursive": "off",
        "nginx_proxy_real_ip_recursive": "off",
        "cassandra_keyspace": "kong",
        "kic": false,
        "nginx_main_directives": [
            {
                "name": "daemon",
                "value": "on"
            },
            {
                "name": "user",
                "value": "kong kong"
            },
            {
                "name": "worker_processes",
                "value": "1"
            },
            {
                "name": "worker_rlimit_nofile",
                "value": "auto"
            }
        ],
        "cassandra_username": "kong",
        "nginx_events_directives": [
            {
                "name": "multi_accept",
                "value": "on"
            },
            {
                "name": "worker_connections",
                "value": "auto"
            }
        ],
        "stream_listeners": {},
        "nginx_http_directives": [
            {
                "name": "charset",
                "value": "UTF-8"
            },
            {
                "name": "client_body_buffer_size",
                "value": "8k"
            },
            {
                "name": "client_max_body_size",
                "value": "0"
            },
            {
                "name": "lua_regex_cache_max_entries",
                "value": "8192"
            },
            {
                "name": "lua_regex_match_limit",
                "value": "100000"
            },
            {
                "name": "lua_shared_dict",
                "value": "prometheus_metrics 5m"
            },
            {
                "name": "lua_ssl_protocols",
                "value": "TLSv1.1 TLSv1.2 TLSv1.3"
            },
            {
                "name": "ssl_dhparam",
                "value": "/kong/servroot/ssl/ffdhe2048.pem"
            },
            {
                "name": "ssl_prefer_server_ciphers",
                "value": "off"
            },
            {
                "name": "ssl_protocols",
                "value": "TLSv1.2 TLSv1.3"
            },
            {
                "name": "ssl_session_tickets",
                "value": "on"
            },
            {
                "name": "ssl_session_timeout",
                "value": "1d"
            }
        ],
        "nginx_upstream_directives": {},
        "nginx_proxy_directives": [
            {
                "name": "real_ip_header",
                "value": "X-Real-IP"
            },
            {
                "name": "real_ip_recursive",
                "value": "off"
            }
        ],
        "proxy_listen": [
            "0.0.0.0:8000 reuseport backlog=16384",
            "0.0.0.0:8443 http2 ssl reuseport backlog=16384"
        ],
        "port_maps": {},
        "cassandra_read_consistency": "ONE",
        "tracing_instrumentations": [
            "off"
        ],
        "kong_env": "/kong/servroot/.kong_env",
        "tracing_sampling_rate": 0.01,
        "proxy_access_log": "logs/access.log",
        "proxy_stream_access_log": "logs/access.log basic",
        "admin_access_log": "logs/admin_access.log",
        "status_access_log": "off",
        "nginx_http_lua_ssl_protocols": "TLSv1.1 TLSv1.2 TLSv1.3",
        "cluster_control_plane": "127.0.0.1:8005",
        "enabled_headers": {
            "X-Kong-Proxy-Latency": true,
            "X-Kong-Response-Latency": true,
            "X-Kong-Admin-Latency": true,
            "server_tokens": true,
            "latency_tokens": true,
            "X-Kong-Upstream-Status": false,
            "Via": true,
            "Server": true,
            "X-Kong-Upstream-Latency": true
        },
        "db_update_propagation": 1,
        "cluster_listeners": [
            {
                "ip": "0.0.0.0",
                "reuseport": false,
                "port": 8005,
                "ssl": false,
                "http2": false,
                "proxy_protocol": false,
                "deferred": false,
                "bind": false,
                "backlog=%d+": false,
                "ipv6only=on": false,
                "listener": "0.0.0.0:8005",
                "so_keepalive=on": false,
                "so_keepalive=off": false,
                "so_keepalive=%w*:%w*:%d*": false,
                "ipv6only=off": false
            }
        ],
        "db_cache_ttl": 0,
        "status_ssl_enabled": false,
        "cluster_data_plane_purge_delay": 1209600,
        "cluster_max_payload": 4194304,
        "pg_ssl": false,
        "role": "traditional",
        "allow_debug_header": false,
        "_debug_pg_ttl_cleanup_interval": 300,
        "database": "postgres",
        "cassandra_contact_points": [
            "db"
        ],
        "cassandra_port": 9042,
        "admin_ssl_cert_key_default_ecdsa": "/kong/servroot/ssl/admin-kong-default-ecdsa.key",
        "lua_max_req_headers": 100
    },
    "lua_version": "LuaJIT 2.1.0-20220411",
    "hostname": "cbe1a559a6e5"
}

OS: Kong is running on ubuntu inside a docker container. Container on MacOS

I would be more than willing to fix this issue myself, if someone could point me in the correct direction.

chirag-manwani commented 1 year ago

https://github.com/Kong/kong/blob/master/kong/runloop/handler.lua#L1386 I'm currently looking at this piece of code to understand the issue. Will update as soon as I have something.

chirag-manwani commented 1 year ago

In this line, is it expected that if a query arg is being set in a Kong plugin using the Kong 3.3.0 PDK, then the request_uri will be updated to include the query arg?

I tried logging both variables (ngx.var.request_uri and ngx.ctx.request_uri) and found that they still have the original value (in our case /first?) even after setting the query arg using the Kong 3.3.0 PDK. Since the last character is ?, the code proceeds to set the upstream_uri like so and ignores the set query args. In both versions of Kong (2.2.0 and 3.3.0) var.is_args is empty and var.args gets updated after setting a query arg using the PDK.

In Kong 2.2.0 the following code works, as it should.

if var.is_args == "?" or sub(var.request_uri, -1) == "?" then
  var.upstream_uri = upstream_uri .. "?" .. (var.args or "")
end
chirag-manwani commented 1 year ago

The change was introduced in this commit.

I checked with Kong versions 2.5 and 2.6 to verify, it indeed seems to be the case. It stops working as expected Kong 2.6 onwards.

samugi commented 1 year ago

thank you @chirag-manwani for reporting this. Could you please provide a working example where the variable $is_args is empty, as you pointed out in your comment above? I tested this with the details you have provided and while I was able to reproduce the behavior you describe, I always found the is_args variable populated with ?.

chirag-manwani commented 1 year ago

@samugi I think I made a mistake. var.is_args is set to ? after a query arg is set using the PDK in case of Kong 2.2. I logged the values before and after setting the query arg, refer the updated yaml in the issue description. After reading your comment I thought maybe in case of Kong 3.3 var.is_args is not set in the access phase of the plugin, but somewhere else, after access phase finishes. So I tried logging it in the log phase, and I could see it set to ?.

samugi commented 1 year ago

is_args should be actually set by your post-function plugin's custom code, when set_query is executed. Logging it before the query args are configured is expected to return an empty value. Thank you for confirming that. The reason I was asking is that your specific example is using a combination of set and get uri args functions that had similar issues in the past due to indexing, see for example this issue and this fix, which anyway I just confirmed to be unrelated.

Here is a simplified configuration I used to reproduce this:

_format_version: "1.1"
plugins:
- config:
    access:
    - kong.service.request.set_query({ injected = 1 })
    body_filter: []
    certificate: []
    header_filter: []
    log: []
    rewrite: []
  enabled: true
  name: post-function
  protocols:
  - grpc
  - grpcs
  - http
  - https
services:
- connect_timeout: 5000
  host: httpbin.org
  name: kong
  path: /anything
  port: 80
  protocol: http
  read_timeout: 5000
  retries: 0
  routes:
  - https_redirect_status_code: 426
    name: second
    path_handling: v1
    paths:
    - /echo
    preserve_host: false
    protocols:
    - http
    - https
    regex_priority: 0
    request_buffering: true
    response_buffering: true
    strip_path: true
  write_timeout: 5000
curl localhost:8000/echo
{
  "args": {
    "injected": "1"
  }, 
  [...]
}
curl localhost:8000/echo?
{
  "args": {},
  [...]
}

Let's continue the discussion in the PR. Thanks again for reporting this.