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

[hotfix/resolver] avoid URL decoding in URI proxying #691

Merged
merged 1 commit into from Nov 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion kong/resolver/access.lua
Expand Up @@ -194,7 +194,8 @@ local function find_api(uri)
end

function _M.execute(conf)
local uri = ngx.var.uri
local uri = stringy.split(ngx.var.request_uri, "?")[1]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why the split on "?" ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some plugins, like the Request Transformations, can update the querystring. Here we are calculating the base URI, and then later in kong.lua after all the "access" handlers in the plugins have been executed, we append the possibly updated querystring at the end. So here, since I just want the base URI for resolving the API using the path, I remove the querystring.

I personally don't like the split on every request, but I can't see how we can do this differently. On the other side the split function should be fast because it invokes stringy, which invokes strchr under the hood.


local err, api, hosts, strip_request_path_pattern = find_api(uri)
if err then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
Expand Down
20 changes: 19 additions & 1 deletion spec/integration/proxy/api_resolver_spec.lua
Expand Up @@ -36,7 +36,8 @@ describe("Resolver", function()
{name = "tests-wildcard-subdomain", upstream_url = "http://mockbin.com/status/200", request_host = "*.wildcard.com"},
{name = "tests-wildcard-subdomain-2", upstream_url = "http://mockbin.com/status/201", request_host = "wildcard.*"},
{name = "tests-preserve-host", request_host = "httpbin-nopreserve.com", upstream_url = "http://httpbin.org"},
{name = "tests-preserve-host-2", request_host = "httpbin-preserve.com", upstream_url = "http://httpbin.org", preserve_host = true}
{name = "tests-preserve-host-2", request_host = "httpbin-preserve.com", upstream_url = "http://httpbin.org", preserve_host = true},
{name = "tests-uri", request_host = "mockbin-uri.com", upstream_url = "http://mockbin.org"}
},
plugin = {
{name = "key-auth", config = {key_names = {"apikey"} }, __api = 2}
Expand All @@ -50,6 +51,22 @@ describe("Resolver", function()
spec_helper.stop_kong()
end)

describe("Test URI", function()

it("should URL decode the URI with querystring", function()
local response, status = http_client.get(spec_helper.STUB_GET_URL.."/hello%2F", { hello = "world"}, {host = "mockbin-uri.com"})
assert.equal(200, status)
assert.equal("http://mockbin.org/request/hello%2f?hello=world", cjson.decode(response).url)
end)

it("should URL decode the URI without querystring", function()
local response, status = http_client.get(spec_helper.STUB_GET_URL.."/hello%2F", nil, {host = "mockbin-uri.com"})
assert.equal(200, status)
assert.equal("http://mockbin.org/request/hello%2f", cjson.decode(response).url)
end)

end)

describe("Inexistent API", function()

it("should return Not Found when the API is not in Kong", function()
Expand Down Expand Up @@ -216,4 +233,5 @@ describe("Resolver", function()
assert.equal("httpbin-preserve.com", parsed_response.headers["Host"])
end)
end)

end)