apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
14.35k stars 2.5k forks source link

bug: Failed auth with body transformer not working #11608

Open grencik opened 4 days ago

grencik commented 4 days ago

Current Behavior

I have in route key-auth and body-transformer plugins. When key-auth plugin fail ([lua] plugin.lua:1171: run_plugin(): key-auth exits with http status code 401) then body-transformer horibly crash with error

[error] 49#49: *325 failed to run body_filter_by_lua*: /usr/local/apisix/apisix/plugins/body-transformer.lua:220: attempt to index local 'conf' (a nil value)
stack traceback:
        /usr/local/apisix/apisix/plugins/body-transformer.lua:220: in function 'phase_func'
        /usr/local/apisix/apisix/plugin.lua:1203: in function 'common_phase'
        /usr/local/apisix/apisix/init.lua:806: in function 'http_body_filter_phase'
        body_filter_by_lua(nginx.conf:368):2: in main chunk

When I remove key-auth plugin or key-auth doesn't fail, it works as expected.

Expected Behavior

It should probably stop executing next plugins or at least body-transformer should have all data to not crash.

Error Logs

2024/09/26 09:09:26 [warn] 50#50: *14043 [lua] plugin.lua:1171: run_plugin(): key-auth exits with http status code 401, client: 172.68.213.37, server: _, request: "POST /example HTTP/2.0", host: "example.com"
2024/09/26 09:09:26 [error] 50#50: *14043 failed to run body_filter_by_lua*: /usr/local/apisix/apisix/plugins/body-transformer.lua:220: attempt to index local 'conf' (a nil value)
stack traceback:
        /usr/local/apisix/apisix/plugins/body-transformer.lua:220: in function 'phase_func'
        /usr/local/apisix/apisix/plugin.lua:1203: in function 'common_phase'
        /usr/local/apisix/apisix/init.lua:806: in function 'http_body_filter_phase'
        body_filter_by_lua(nginx.conf:368):2: in main chunk, client: 1.1.1.1, server: _, request: "POST /example HTTP/2.0", host: "example.com"
2024/09/26 09:09:26 [error] 50#50: *14043 failed to run body_filter_by_lua*: /usr/local/apisix/apisix/plugins/body-transformer.lua:220: attempt to index local 'conf' (a nil value)
stack traceback:
        /usr/local/apisix/apisix/plugins/body-transformer.lua:220: in function 'phase_func'
        /usr/local/apisix/apisix/plugin.lua:1203: in function 'common_phase'
        /usr/local/apisix/apisix/init.lua:806: in function 'http_body_filter_phase'
        body_filter_by_lua(nginx.conf:368):2: in main chunk, client: 1.1.1.1, server: _, request: "POST /example HTTP/2.0", host: "example.com"

Steps to Reproduce

  1. Create route with plugins key-auth and body-transformer
  2. Make request with failing auth

Environment

shreemaan-abhishek commented 3 days ago

this indeed looks like a bug, please share your apisix resource configurations

grencik commented 3 days ago

Consumer:

{
  "plugins": {
    "key-auth": {
      "key": "abcdef"
    }
  },
  "username": "consumer_user"
}

Route:

{
  "name": "Some route",
  "status": 1,
  "plugins": {
    "key-auth": {
        "_meta": {
            "disable": true
        },
        "hide_credentials": true
    },
    "body-transformer": {
      "request": {
        "input_format": "json",
        "template_is_base64": true,
        "template": "<base64 template>"
      }
    },
    "proxy-rewrite": {
      "regex_uri": [
        "^/some-route",
        "/other-route"
      ],
      "use_real_request_uri_unsafe": false
    }
  },
  "host": "host.example.com",
  "methods": [
    "GET",
    "POST"
  ],
  "uri": "/some-route",
  "upstream": {
    "scheme": "https",
    "type": "roundrobin",
    "nodes": [
      {
        "host": "other-host.example.com",
        "weight": 1,
        "priority": 0,
        "port": 443
      }
    ],
    "hash_on": "vars",
    "timeout": {
      "connect": 6,
      "read": 6,
      "send": 6
    },
    "keepalive_pool": {
      "idle_timeout": 60,
      "requests": 1000,
      "size": 320
    },
    "pass_host": "node"
  }
}

Request:

curl --request POST \
  --url 'https://host.example.com/some-route?apikey=abcdef' \
  --header 'Content-Type: application/json' \
  --data '{
    "attr1": "value 1",
    "attr2": "value 2"
}'