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

Policy: IpCheck default deny. #1273

Merged
merged 1 commit into from
May 31, 2021
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 @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed host header format on http_ng resty [PR #1264](https://github.com/3scale/APIcast/pull/1264) [THREESCALE-2235](https://issues.redhat.com/browse/THREESCALE-2235)
- Fixed issues on OIDC jwk discovery [PR #1268](https://github.com/3scale/APIcast/pull/1268) [THREESCALE-6913](https://issues.redhat.com/browse/THREESCALE-6913)
- Fixed Payload limit content-length response header [PR #1266](https://github.com/3scale/APIcast/pull/1266) [THREESCALE-6736](https://issues.redhat.com/browse/THREESCALE-6736)
- Fixed IPcheck policy issues with invalid IP [PR #1273](https://github.com/3scale/APIcast/pull/1273) [THREESCALE-7075](https://issues.redhat.com/browse/THREESCALE-7075)



### Added
Expand Down
10 changes: 7 additions & 3 deletions gateway/src/apicast/policy/ip_check/ip_check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ end
function _M:access()
local client_ip = ClientIP.get_from(self.client_ip_sources)

if client_ip then
self:check_client_ip(client_ip)
end
if not client_ip then
ngx.log(ngx.INFO, "Rejecting request due to is invalid to retrieve the IP information")
deny_request(self.error_msg)
return
end

self:check_client_ip(client_ip)
end

return _M
4 changes: 2 additions & 2 deletions spec/policy/ip_check/ip_check_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ describe('IP Check policy', function()
end)

describe('when the client IP cannot be obtained', function()
it('does not deny the request', function()
it('denies the request', function()
stub(ClientIP, 'get_from', function() return nil end)
local ip_check = IpCheckPolicy.new(
{ ips = { '1.2.3.4' }, check_type = 'blacklist' }
)

ip_check:access()

assert.stub(ngx.exit).was_not_called()
assert.stub(ngx.exit).was_called()
end)
end)
end)
Expand Down
69 changes: 69 additions & 0 deletions t/apicast-policy-ip-check.t
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,72 @@ is always the valid one.
[ "A custom error message\n", "GET / HTTP/1.1\n"]
--- error_code eval
[403, 200]

=== TEST 11: X-forwarded-for header with invalid data
--- configuration
{
"services": [
{
"id": 42,
"proxy": {
"policy_chain": [
{
"name": "apicast.policy.ip_check",
"configuration": {
"ips": [ "9.9.9.9" ],
"client_ip_sources": [
"X-Forwarded-For"
],
"check_type": "whitelist"
}
},
{ "name": "apicast.policy.echo" }
]
}
}
]
}
--- request
GET /
--- response_body
IP address not allowed
--- more_headers eval
X-forwarded-for: ,9.9.9.9
--- error_code: 403
--- no_error_log
[error]


=== TEST 12: X-forwarded-for header without data
--- configuration
{
"services": [
{
"id": 42,
"proxy": {
"policy_chain": [
{
"name": "apicast.policy.ip_check",
"configuration": {
"ips": [ "9.9.9.9" ],
"client_ip_sources": [
"X-Forwarded-For"
],
"check_type": "whitelist"
}
},
{ "name": "apicast.policy.echo" }
]
}
}
]
}
--- request
GET /
--- response_body
IP address not allowed
--- more_headers eval
X-forwarded-for: ,
--- error_code: 403
--- no_error_log
[error]