apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug(cors): attempt to concatenate field 'conf_id' (a nil value)

Open membphis opened this issue 6 years ago • 19 comments

APISIX version: 1.5

2020/08/06 15:15:13 [error] 49#49: *56762 failed to run header_filter_by_lua*: /usr/local/apisix/apisix/core/lrucache.lua:198: attempt to concatenate field 'conf_id' (a nil value)
stack traceback:
        /usr/local/apisix/apisix/core/lrucache.lua:198: in function 'plugin_ctx'
        /usr/local/apisix/apisix/plugins/cors.lua:144: in function 'phase_fun'
        /usr/local/apisix/apisix/init.lua:142: in function 'run_plugin'
        /usr/local/apisix/apisix/init.lua:550: in function 'common_phase'
        /usr/local/apisix/apisix/init.lua:561: in function 'http_header_filter_phase'

membphis avatar Aug 06 '20 07:08 membphis

the user enabled cors plugin in global rules. this maybe useful.

image

membphis avatar Aug 06 '20 07:08 membphis

@ShiningRush do you have time to handle this bug?

membphis avatar Aug 06 '20 07:08 membphis

Thanks @membphis for your help and dedication! I used apisix-docker's dockerfile (centos) as a blueprint to build a version 1.5 docker image.

I configured the following in the global routing rules: PUT text.com/apisix/admin/global_rules/1

{
        "plugins": {
            "limit-count": {
                "time_window": 60,
                "policy": "local",
                "count": 100,
                "key": "http_x_real_ip",
                "rejected_code": 503
            },
            "cors":{}
        }
}

Access to all routes has the following error in error.log

2020/08/06 15:15:13 [error] 49#49: *56762 failed to run header_filter_by_lua*: /usr/local/apisix/apisix/core/lrucache.lua:198: attempt to concatenate field 'conf_id' (a nil value)
stack traceback:
        /usr/local/apisix/apisix/core/lrucache.lua:198: in function 'plugin_ctx'
        /usr/local/apisix/apisix/plugins/cors.lua:144: in function 'phase_fun'
        /usr/local/apisix/apisix/init.lua:142: in function 'run_plugin'
        /usr/local/apisix/apisix/init.lua:550: in function 'common_phase'
        /usr/local/apisix/apisix/init.lua:561: in function 'http_header_filter_phase'

After @membphis instructed, I closed the global cors configuration. The interface is currently accessible, and no other errors

Currently, I need the function of the cors plug-in while I am using it. How can I solve this problem?

backnero avatar Aug 06 '20 07:08 backnero

I found that it is because of global header_filter does not generate conf_id, in https://github.com/apache/apisix/blob/master/apisix/init.lua#L555

    if api_ctx.global_rules then
        local plugins = core.tablepool.fetch("plugins", 32, 0)
        local values = api_ctx.global_rules.values
        for _, global_rule in config_util.iterate_values(values) do
            // here should generate global rule's id and type like L312
            core.table.clear(plugins)
            plugins = plugin.filter(global_rule, plugins)
            run_plugin(phase_name, plugins, api_ctx)
        end

I will fix it tonight.

ShiningRush avatar Aug 06 '20 07:08 ShiningRush

As a workaround, you can using CORS plugin append to each route instead of global rules util the bug is resolved. @backnero

ShiningRush avatar Aug 06 '20 07:08 ShiningRush

Yes, I try to configure the cors plug-in on Service and Route, not in the global, it can be used normally

backnero avatar Aug 06 '20 07:08 backnero

@backnero Hi, I have a try in my environment, but does not reproduce the problem, can you give more detail about the bug?such as is route bind custom plugin.

ShiningRush avatar Aug 07 '20 04:08 ShiningRush

Okay, I added the SSL configuration (for example: xxx.com), and added the route of xxx.com/v1/xxx. When I set to enable cors globally, this error will appear. At this time, the root path and routing are not accessible. Turn off the global cors plugin and configure only the cors plugin of xxx.com/v1/xxx, the root path and routing can be accessed。

The same configuration is usually used in version 1.4.1, which is normal. But in version 1.5, need to turn off the global cors plugin and enable cors for specific services or routes

backnero avatar Aug 07 '20 05:08 backnero

@backnero you need to provide the parameter of route and SSL object. then we can use it directly.

we need a way to reproduce this issue for fixing.

membphis avatar Aug 18 '20 12:08 membphis

I have the same problem, see #2194.

Issue description

when route not found and return core.response.exit(404, {error_msg = "failed to match any routes"}), the apisix throw an error.

Environment

  • apisix version (cmd: apisix version): 1.5
  • OS: Ubuntu 18.04

Minimal test code / Steps to reproduce the issue

  1. add global plugin cors
curl -X PUT \
  https://{apisix_listen_address}/apisix/admin/global_rules/1 \
  -H 'Content-Type: application/json' \
  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
  -d '{
        "plugins": {
            "cors": { }
        }
    }'
  1. Access a nonexistent Route, for example: http://172.0.0.1:9080/test
  2. the apisix source code snippet
-- ### init.lua
function _M.http_access_phase()
    .....
     -- load and run global rule
     if router.global_rules and router.global_rules.values
        and #router.global_rules.values > 0 then
         local plugins = core.tablepool.fetch("plugins", 32, 0)
         local values = router.global_rules.values
         for _, global_rule in config_util.iterate_values(values) do
             api_ctx.conf_type = "global_rule"
             api_ctx.conf_version = global_rule.modifiedIndex
             api_ctx.conf_id = global_rule.value.id
             core.table.clear(plugins)
             api_ctx.plugins = plugin.filter(global_rule, plugins)
             run_plugin("rewrite", plugins, api_ctx)
             run_plugin("access", plugins, api_ctx)
         end

         core.tablepool.release("plugins", plugins)
        **--  Note: api_ctx is cleared here
         api_ctx.plugins = nil
         api_ctx.conf_type = nil
         api_ctx.conf_version = nil
         api_ctx.conf_id = nil**
        
         api_ctx.global_rules = router.global_rules
     end
     .....
     -- Note: here no route matched, so exit 404
     local route = api_ctx.matched_route
     if not route then
         return core.response.exit(404,
                     {error_msg = "failed to match any routes"})
     end
     ...
end
-- Continue with the following code
 function _M.http_header_filter_phase()
     common_phase("header_filter")
 end
local function common_phase(phase_name)
    ...
    if api_ctx.global_rules then
         local plugins = core.tablepool.fetch("plugins", 32, 0)
         local values = api_ctx.global_rules.values
         for _, global_rule in config_util.iterate_values(values) do
             core.table.clear(plugins)
             plugins = plugin.filter(global_rule, plugins)
             -- Note: here run global plugin
             run_plugin(phase_name, plugins, api_ctx)
         end
         core.tablepool.release("plugins", plugins)
     end
     ...
end

local function run_plugin(phase, plugins, api_ctx)
    .....
    for i = 1, #plugins, 2 do
         local phase_fun = plugins[i][phase]
         if phase_fun then
             phase_fun(plugins[i + 1], api_ctx)
         end
     end
     .....
end
-- ### cors.lua
function _M.header_filter(conf, ctx)
    .....
    -- Note: here the ctx is api_ctx
    local multiple_origin, err = core.lrucache.plugin_ctx(plugin_name, ctx,
                                                create_mutiple_origin_cache, conf)
    .....
end

-- ### lrucache.lua
function _M.plugin_ctx(plugin_name, api_ctx, create_obj_fun, ...)
    -- Note: here used api_ctx.conf_id is nil
    local key = api_ctx.conf_type .. "#" .. api_ctx.conf_id
    return _plugin(plugin_name, key, api_ctx.conf_version, create_obj_fun, ...)
end

What's the actual result? (including assertion message & call stack if applicable)

2020/09/09 11:37:14 [error] 9701#9701: 373 failed to run header_filter_by_lua: ...user-center//deps/share/lua/5.1/apisix/core/lrucache.lua:202: attempt to concaten ate field 'conf_id' (a nil value) 2297217 stack traceback: 2297218 ...user-center//deps/share/lua/5.1/apisix/core/lrucache.lua:202: in function 'plugin_ctx' 2297219 ...-user-center//deps/share/lua/5.1/apisix/plugins/cors.lua:144: in function 'phase_fun' 2297220 ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:142: in function 'run_plugin' 2297221 ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:562: in function 'common_phase' 2297222 ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:573: in function 'http_header_filter_phase' 2297223 header_filter_by_lua:2: in main chunk, client: 172.30.212.206, server: , request: "GET /dfy-h5-user HTTP/1.0", host: "m.dafangya.com.cn"

What's the expected result?

404 page.

griffenliu avatar Sep 11 '20 02:09 griffenliu

I want to use the prometheus plug-ins. But when I access http://localhost:9080/apisix/prometheus/metrics based on the document https://github.com/apache/apisix/blob/master/doc/zh-cn/plugins/prometheus.md.

I got the following error too:

2020/09/17 20:45:31 [error] 3087#3087: *11669858 failed to run header_filter_by_lua*: ...user-center//deps/share/lua/5.1/apisix/core/lrucache.lua:202: attempt to concatenate
 field 'conf_id' (a nil value)                                                                                                                                               
stack traceback:                                                                                                                                                             
        ...user-center//deps/share/lua/5.1/apisix/core/lrucache.lua:202: in function 'plugin_ctx'                                                                            
        ...-user-center//deps/share/lua/5.1/apisix/plugins/cors.lua:144: in function 'phase_fun'                                                                             
        ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:142: in function 'run_plugin'
        ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:550: in function 'common_phase'
        ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:561: in function 'http_header_filter_phase'
        header_filter_by_lua:2: in main chunk, client: 172.30.213.200, server: , request: "GET /apisix/prometheus/metrics HTTP/1.1", host: "172.30.212.70:9080"

griffenliu avatar Sep 17 '20 12:09 griffenliu

@membphis please take a look

moonming avatar Sep 18 '20 03:09 moonming

I think this is the same problem, the key point is related with this comment

ShiningRush avatar Sep 18 '20 03:09 ShiningRush

I had the same problem in version 2.0,

backnero avatar Nov 19 '20 05:11 backnero

@ShiningRush do you have time to fix this bug?

membphis avatar Nov 23 '20 14:11 membphis

I will try to produce the bug in master, We have not been able to produce this bug stably before : (

ShiningRush avatar Nov 24 '20 01:11 ShiningRush

I had the same problem in version 2.0,

please provide your steps about how to produce this issue, we need it. many thx

membphis avatar Nov 24 '20 10:11 membphis

What is the meaning of this code

https://github.com/apache/apisix/blob/master/apisix/init.lua#L365-L368

365        api_ctx.plugins = nil
366        api_ctx.conf_type = nil
367        api_ctx.conf_version = nil
368        api_ctx.conf_id = nil
  • apisix 2.1 global_rules enable cors plugin, upstream response 404 will cause this problem
2021/01/08 10:58:50 [error] 79#79: *440247 failed to run header_filter_by_lua*: /usr/local/apisix/apisix/core/lrucache.lua:162: invalid value (nil) at index 1 in table for 'concat'
stack traceback:
        [C]: in function 'concat'
        /usr/local/apisix/apisix/core/lrucache.lua:162: in function 'plugin_ctx'
        /usr/local/apisix/apisix/plugins/cors.lua:168: in function 'phase_func'
        /usr/local/apisix/apisix/init.lua:160: in function 'run_plugin'
        /usr/local/apisix/apisix/init.lua:615: in function 'common_phase'
        /usr/local/apisix/apisix/init.lua:652: in function 'http_header_filter_phase'

dickens7 avatar Jan 08 '21 11:01 dickens7

@dickens7 what version are you using? the latest version of APISIX is 2.2, we may have fixed this issue.

if you still have this issue, please create a new issue and report your APISIX version.

membphis avatar Jan 11 '21 08:01 membphis