From 250c0683a040095a01fe6d83aa4f3ecabafdf4fd Mon Sep 17 00:00:00 2001 From: Eloy Coto Date: Mon, 29 Jun 2020 17:12:16 +0200 Subject: [PATCH] Policy: Routing fix URL encoding. This commit fixes an issue when a special character uses some encoding. The value to the upstream API if the routing policy was used, is also encoded, so something like "/foo/test space" should land in APICast as: `/foo/test%20space/` And the path to the Upstream API should contain `test%20space`, but because `ngx.req.set_uri` mark the URL to be encoded again, the URL that will be received in the Upstream API will be `/test%2520space/` This commit keeps the URL encoding correctly across all the flow. Fix THREESCALE-5454 Signed-off-by: Eloy Coto --- CHANGELOG.md | 1 + gateway/src/apicast/upstream.lua | 3 +- t/apicast-policy-routing.t | 69 ++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 097affe32..bf131f9bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed issues with HTTPS_PROXY and large bodies [THREESCALE-3863](https://issues.jboss.org/browse/THREESCALE-3863) [PR #1191](https://github.com/3scale/APIcast/pull/1191) - Fixed issues with path routing and query args [THREESCALE-5149](https://issues.redhat.com/browse/THREESCALE-5149) [PR #1190](https://github.com/3scale/APIcast/pull/1190) - Fixed issue with IPCheck policy when forwarder-for value contains port [THREESCALE-5258](https://issues.redhat.com/browse/THREESCALE-5258) [PR #1192](https://github.com/3scale/APIcast/pull/1192) +- Fixed issues with URL encode on routing policy [THREESCALE-5454](https://issues.redhat.com/browse/THREESCALE-5454) [PR #1208](https://github.com/3scale/APIcast/pull/1208) ### Added diff --git a/gateway/src/apicast/upstream.lua b/gateway/src/apicast/upstream.lua index cff5b5f96..2d97bbbe4 100644 --- a/gateway/src/apicast/upstream.lua +++ b/gateway/src/apicast/upstream.lua @@ -159,9 +159,8 @@ function _M:rewrite_request() end local uri = self.uri - if uri.path then - ngx.req.set_uri(prefix_path(uri.path)) + ngx.req.set_uri(ngx.unescape_uri(prefix_path(uri.path))) end if uri.query then diff --git a/t/apicast-policy-routing.t b/t/apicast-policy-routing.t index 0ff0fd3fc..c441d5d58 100644 --- a/t/apicast-policy-routing.t +++ b/t/apicast-policy-routing.t @@ -2125,3 +2125,72 @@ GET /?user_key=value could not find upstream for service: 42 --- no_error_log [error] + + +=== TEST 32: URL is not double encoded. +--- configuration +{ + "services": [ + { + "id": 42, + "backend_version": 1, + "backend_authentication_type": "service_token", + "backend_authentication_value": "token-value", + "proxy": { + "policy_chain": [ + { + "name": "apicast.policy.routing", + "configuration": { + "rules": [ + { + "url": "http://test:$TEST_NGINX_SERVER_PORT/second/", + "owner_id": 4, + "condition": { + "operations": [ + { + "match": "liquid", + "liquid_value": "test", + "op": "==", + "value": "test" + } + ] + } + } + ] + } + }, + { + "name": "apicast.policy.apicast" + } + ], + "proxy_rules": [ + { + "pattern": "/foo", + "http_method": "GET", + "metric_system_name": "test", + "delta": 1 + } + ] + } + } + ] +} +--- backend + location /transactions/authrep.xml { + content_by_lua_block { + ngx.say("OK") + } + } +--- upstream + location / { + content_by_lua_block { + ngx.say(ngx.var.uri); + } + } +--- request +GET /foo/test%20space?user_key=foo +--- response_body +/second/foo/test space +--- error_code: 200 +--- no_error_log +[error]