From 572659231bd5ed8297d6ea35294dd433ee45e3ad Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Mon, 20 Mar 2017 16:11:05 +0100 Subject: [PATCH] [proxy] fix accessing undefined variable with a regression test --- CHANGELOG.md | 1 + apicast/src/apicast.lua | 22 +++++++++++++++------- apicast/src/proxy.lua | 2 +- t/003-apicast.t | 14 +++++++++++++- 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 328760a9d..87bde715b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - `http_ng` client supports auth passsed in the url, and default client options if the request options are missing for methods with body (POST, PUT, etc.) [PR #310](https://github.com/3scale/apicast/pull/310) - Fixed lazy configuration loader to recover from failures [PR #313](https://github.com/3scale/apicast/pull/313) +- Fixed undefined variable `p` in post\_action [PR #316](https://github.com/3scale/apicast/pull/316) ### Removed diff --git a/apicast/src/apicast.lua b/apicast/src/apicast.lua index 70fe997d8..fc4015cf7 100644 --- a/apicast/src/apicast.lua +++ b/apicast/src/apicast.lua @@ -51,6 +51,8 @@ end function _M:rewrite() ngx.on_abort(_M.cleanup) + ngx.var.original_request_id = ngx.var.request_id + local host = ngx.var.host -- load configuration if not configured -- that is useful when lua_code_cache is off @@ -65,18 +67,24 @@ end function _M.post_action() local request_id = ngx.var.original_request_id - local p = post_action_proxy[request_id] - post_action_proxy[request_id] = nil - p:post_action() + local p = ngx.ctx.proxy or post_action_proxy[request_id] + + if p then + p:post_action() + else + ngx.log(ngx.INFO, 'could not find proxy for request id: ', request_id) + end end function _M.access() local p = ngx.ctx.proxy local fun = p:call() -- proxy:access() or oauth handler - local request_id = ngx.var.request_id - post_action_proxy[request_id] = p - ngx.var.original_request_id = request_id - return fun() + + local ok, err = fun() + + post_action_proxy[ngx.var.original_request_id] = p + + return ok, err end _M.body_filter = noop diff --git a/apicast/src/proxy.lua b/apicast/src/proxy.lua index d87d699fc..a1ddd399e 100644 --- a/apicast/src/proxy.lua +++ b/apicast/src/proxy.lua @@ -91,7 +91,7 @@ local function find_service_strict(self, host) if found then break end end - return found or ngx.log(ngx.ERR, 'service not found for host ', host) + return found or ngx.log(ngx.WARN, 'service not found for host ', host) end local function find_service_cascade(self, host) diff --git a/t/003-apicast.t b/t/003-apicast.t index 6b0b34da1..9cae03adc 100644 --- a/t/003-apicast.t +++ b/t/003-apicast.t @@ -70,7 +70,7 @@ GET /?user_key=value no mapping rules! --- error_code: 412 --- error_log -skipping after action, no cached key +could not find proxy for request === TEST 3: authentication credentials invalid The message is configurable and default status is 403. @@ -517,3 +517,15 @@ all ok $::dns->("localhost.example.com", "127.0.0.1") --- no_error_log [error] + +=== TEST 15: invalid service +The message is configurable and default status is 403. +--- http_config + lua_package_path "$TEST_NGINX_LUA_PATH"; +--- config + include $TEST_NGINX_APICAST_CONFIG; +--- request +GET /?user_key=value +--- error_code: 404 +--- no_error_log +[error]