Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

help request: CORS plugin not working properly in Consumer #7072

Closed
endison1986 opened this issue May 18, 2022 · 5 comments
Closed

help request: CORS plugin not working properly in Consumer #7072

endison1986 opened this issue May 18, 2022 · 5 comments

Comments

@endison1986
Copy link

Description

When I try to use the CORS plugin in Consumer, I get an unexpected response header.
So I analyzed the source code and tried to call core.request.header(ctx, "Origin") in _M.header_filter, and finally CORS worked normally.
I speculated and confirmed that _M.rewrite() cannot be called in Consumer, but starting from 2.12.1, APISIX has modified its practice to get the in the request header in_M.rewrite() Origin parameter, is this in line with the original intention of your design?

当我尝试在Consumer中使用CORS插件时,我获得了一个预期外的响应头。
于是我分析了源码,并尝试在_M.header_filter中调用core.request.header(ctx,"Origin"),最终CORS正常的工作了。
我推测并印证了_M.rewrite()无法在Consumer中被调用,但是从2.12.1开始,APISIX修改了做法,在_M.rewrite()中获取请求头中的Origin参数,请问这是符合你们的设计初衷的么?

function _M.rewrite(conf, ctx)
    -- save the original request origin as it may be changed at other phase
    ctx.original_request_origin = core.request.header(ctx, "Origin")
    if ctx.var.request_method == "OPTIONS" then
        return 200
    end
end


function _M.header_filter(conf, ctx)
    // modify here
    local req_origin = core.request.header(ctx, "Origin")
    -- local req_origin =  ctx.original_request_origin
    -- Try allow_origins first, if mismatched, try allow_origins_by_regex.
    local allow_origins
    allow_origins = process_with_allow_origins(conf, ctx, req_origin)
    if not match_origins(req_origin, allow_origins) then
        allow_origins = process_with_allow_origins_by_regex(conf, ctx, req_origin)
    end
    if allow_origins then
        ctx.cors_allow_origins = allow_origins
        set_cors_headers(conf, ctx)
    end
end

Environment

  • APISIX version (run apisix version):2.12.1
  • Operating system (run uname -a):Linux apisix-server-cd8564555-2njxg 3.10.0-514.26.2.el7.x86_64 change: added doc of how to load plugin. #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
  • OpenResty / Nginx version (run openresty -V or nginx -V):openresty/1.19.9.1
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):
@tzssangglass
Copy link
Member

I speculated and confirmed that _M.rewrite() cannot be called in Consumer

Should be solved by this PR: #6502

Please verify with the latest version(v2.13.0 or the master branch).

@endison1986
Copy link
Author

endison1986 commented May 20, 2022

I speculated and confirmed that _M.rewrite() cannot be called in Consumer

Should be solved by this PR: #6502

Please verify with the latest version(v2.13.0 or the master branch).

I have tested in 2.13.1, when the CORS plugin is configured in Consumer, _M.rewrite() is still not called, but I have not tested the master branch,@tzssangglass

我已经在2.13.1中测试,当CORS插件配置在Consumer中时,_M.rewrite()仍然没有被调用,但是我没有测试master分支

@tzssangglass
Copy link
Member

I have tested in 2.13.1, when the CORS plugin is configured in Consumer, _M.rewrite() is still not called, but I have not tested the master branch

2.13.1 should take this fix with it.

Please describe the reproduce the steps, I need to recover them.
You can find the image of the master branch on the docker hub and test it.

@endison1986
Copy link
Author

I confirmed that the source code of apisix-server is indeed 2.13.1, but the version of apisix-dashboard I use is 2.10.1, is it related to his version?

我确认了一下,apisix-server的源码确实是2.13.1,但我使用的apisix-dashboard的版本是2.10.1,跟他的版本是否有关系?

In addition, I will test the CORS plugin again. If there are still problems, I will submit the detailed steps.

另外我会再测试一下CORS这个插件,如果还是有问题的话,我会把详细的步骤提交上来

@tzssangglass
Copy link
Member

I confirmed that the source code of apisix-server is indeed 2.13.1, but the version of apisix-dashboard I use is 2.10.1, is it related to his version?
no related to apisix-dashboard version, but I suggest you test it through the admin API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants