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

Mapping rules: Avoid issues with double slash. #1159

Merged
merged 1 commit into from
Jan 23, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Add the correct host header when using an http proxy [THREESCALE-4178](https://issues.jboss.org/browse/THREESCALE-4178) [PR #1143](https://github.com/3scale/APIcast/pull/1143)
- Normalize policy names capitalization [THREESCALE-4150](https://issues.jboss.org/browse/THREESCALE-4150) [PR #1154](https://github.com/3scale/APIcast/pull/1154)
- Fix issues with non-alphanumeric variables in liquid [THREESCALE-3968](https://issues.jboss.org/browse/THREESCALE-3968) [PR #1158](https://github.com/3scale/APIcast/pull/1158)
- Fix issues with double mapping rules [THREESCALE-3950](https://issues.jboss.org/browse/THREESCALE-3950) [PR #1159](https://github.com/3scale/APIcast/pull/1159)



### Fixed
Expand Down
5 changes: 5 additions & 0 deletions gateway/src/apicast/mapping_rule.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ local function hash_to_array(hash)
end

local function regexpify(pattern)
-- some urls in APIAP uses double / that it's a valid url, but ngx.var.uri
-- eliminates the duplicates, so mapping rule needs to remove the duplicates
-- ones.
pattern = re_gsub(pattern,[[\/\/+]], '/', 'oj')

pattern = re_gsub(pattern, [[\?.*]], '', 'oj')
-- dollar sign is escaped by another $, see https://github.com/openresty/lua-nginx-module#ngxresub
pattern = re_gsub(pattern, [[\{.+?\}]], [[([\w-.~%!$$&'()*+,;=@:]+)]], 'oj')
Expand Down
8 changes: 8 additions & 0 deletions gateway/src/apicast/policy/soap/soap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local MappingRule = require('apicast.mapping_rule')
local Usage = require('apicast.usage')
local mapping_rules_matcher = require('apicast.mapping_rules_matcher')
local MimeType = require('resty.mime')
local resty_url = require 'resty.url'

local policy = require('apicast.policy')

Expand Down Expand Up @@ -90,6 +91,13 @@ end
function _M:rewrite(context)
local soap_action_uri = extract_soap_uri()

-- If the soap_action_uri is a complete url, the mapping rule is only the
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm missing something. How is this change in the SOAP policy related to the original issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The action in the content-type can be a full url, so we need to pass it correctly:

it('calculates the usage and merges it with the one in the context', function()
ngx.req.get_headers = function()
return { ["Content-Type"] = 'application/soap+xml;action=' .. full_url }
end

-- path, so parse to send only the path.
local url, err = resty_url.parse(soap_action_uri)
if not err then
soap_action_uri = url.path
end

if soap_action_uri then
local soap_usage = usage_from_matching_rules(
soap_action_uri, self.mapping_rules)
Expand Down
23 changes: 23 additions & 0 deletions spec/mapping_rule_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ describe('mapping_rule', function()
assert.is_true(mapping_rule:matches('GET', '/foo/a:b/bar'))
assert.is_true(mapping_rule:matches('GET', "/foo/a%b/bar"))
end)

it('double slashes are transformed correctly to a simple one', function()
local test_cases = {
["/foo//bar"] = "/foo/bar",
["/foo///bar"] = "/foo/bar",
["/foo/ /bar"] = "/foo/ /bar",
["/foo/bar///"] = "/foo/bar/",
["///foo///bar///"] = "/foo/bar/",
}

for key, value in pairs(test_cases) do
local mapping_rule = MappingRule.from_proxy_rule({
http_method = 'GET',
pattern = key,
querystring_parameters = { },
metric_system_name = 'hits',
delta = 1
})

assert.is_true(mapping_rule:matches('GET', value), "Invalid key:" .. key)
end

end)
end)

describe('.any_method', function()
Expand Down
8 changes: 4 additions & 4 deletions spec/policy/soap/soap_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ describe('SOAP policy', function()
describe('.rewrite', function()
local context -- Context shared between policies

local full_url = "http://www.example.com:80/path/to/myfile.html?" ..
"key1=value1&key2=value2#SomewhereInTheDocument"

local base_url = "http://www.example.com:80"
local base_path = "/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument"
local full_url = base_url .. base_path
-- Define a config with 3 rules. Their patterns have values that allow us
-- to easily associate them with a SOAP action receive via SOAPAction
-- header or via Content-Type. The third one is used to tests matching of
Expand All @@ -24,7 +24,7 @@ describe('SOAP policy', function()
delta = 20
},
{
pattern = full_url,
pattern = base_path,
metric_system_name = 'hits',
delta = 30
},
Expand Down