Skip to content

Commit 29b42e8

Browse files
papdanielpapdaniel
andauthored
feat(ip-restriction): support 404 response code (#12076)
Co-authored-by: papdaniel <papfdani@gmailcom>
1 parent 861a6ff commit 29b42e8

File tree

3 files changed

+105
-7
lines changed

3 files changed

+105
-7
lines changed

apisix/plugins/ip-restriction/init.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ local schema = {
3030
maxLength = 1024,
3131
default = "Your IP address is not allowed"
3232
},
33+
response_code = {
34+
type = "integer",
35+
minimum = 403,
36+
maximum = 404,
37+
default = 403
38+
},
3339
whitelist = {
3440
type = "array",
3541
items = {anyOf = core.schema.ip_def},
@@ -108,7 +114,7 @@ function _M.restrict(conf, ctx)
108114
end
109115

110116
if block then
111-
return 403, { message = conf.message }
117+
return conf.response_code, { message = conf.message }
112118
end
113119
end
114120

docs/en/latest/plugins/ip-restriction.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ Single IPs, multiple IPs or even IP ranges in CIDR notation like `10.10.10.0/24`
3636

3737
## Attributes
3838

39-
| Name | Type | Required | Default | Valid values | Description |
40-
|-----------|---------------|----------|---------------------------------|--------------|-------------------------------------------------------------|
41-
| whitelist | array[string] | False | | | List of IPs or CIDR ranges to whitelist. |
42-
| blacklist | array[string] | False | | | List of IPs or CIDR ranges to blacklist. |
43-
| message | string | False | "Your IP address is not allowed" | [1, 1024] | Message returned when the IP address is not allowed access. |
39+
| Name | Type | Required | Default | Valid values | Description |
40+
|---------------|---------------|----------|----------------------------------|--------------|------------------------------------------------------------------------|
41+
| whitelist | array[string] | False | | | List of IPs or CIDR ranges to whitelist. |
42+
| blacklist | array[string] | False | | | List of IPs or CIDR ranges to blacklist. |
43+
| message | string | False | "Your IP address is not allowed" | [1, 1024] | Message returned when the IP address is not allowed access. |
44+
| response_code | integer | False | 403 | [403, 404] | HTTP response code returned when the IP address is not allowed access. |
4445

4546
:::note
4647

t/plugin/ip-restriction.t

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ __DATA__
5757
--- request
5858
GET /t
5959
--- response_body
60-
{"message":"Your IP address is not allowed","whitelist":["10.255.254.0/24","192.168.0.0/16"]}
60+
{"message":"Your IP address is not allowed","response_code":403,"whitelist":["10.255.254.0/24","192.168.0.0/16"]}
6161
6262
6363
@@ -754,3 +754,94 @@ qr/string too short, expected at least 1, got 0/
754754
GET /t
755755
--- response_body_like eval
756756
qr/string too long, expected at most 1024, got 1025/
757+
758+
759+
760+
=== TEST 33: set whitelist and 404 response code
761+
--- config
762+
location /t {
763+
content_by_lua_block {
764+
local t = require("lib.test_admin").test
765+
local code, body = t('/apisix/admin/routes/1',
766+
ngx.HTTP_PUT,
767+
[[{
768+
"uri": "/hello",
769+
"upstream": {
770+
"type": "roundrobin",
771+
"nodes": {
772+
"127.0.0.1:1980": 1
773+
}
774+
},
775+
"plugins": {
776+
"ip-restriction": {
777+
"whitelist": [
778+
"127.0.0.0/24",
779+
"113.74.26.106"
780+
],
781+
"response_code": 404
782+
}
783+
}
784+
}]]
785+
)
786+
787+
if code >= 300 then
788+
ngx.status = code
789+
end
790+
ngx.say(body)
791+
}
792+
}
793+
--- request
794+
GET /t
795+
--- response_body
796+
passed
797+
798+
799+
800+
=== TEST 34: hit route and ip not in the whitelist expect 404
801+
--- http_config
802+
set_real_ip_from 127.0.0.1;
803+
real_ip_header X-Forwarded-For;
804+
--- more_headers
805+
X-Forwarded-For: 114.114.114.114
806+
--- request
807+
GET /hello
808+
--- error_code: 404
809+
--- error_log
810+
ip-restriction exits with http status code 404
811+
812+
813+
814+
=== TEST 35: set wrong response code
815+
--- config
816+
location /t {
817+
content_by_lua_block {
818+
local t = require("lib.test_admin").test
819+
local code, body = t('/apisix/admin/routes/1',
820+
ngx.HTTP_PUT,
821+
[[{
822+
"uri": "/hello",
823+
"upstream": {
824+
"type": "roundrobin",
825+
"nodes": {
826+
"127.0.0.1:1980": 1
827+
}
828+
},
829+
"plugins": {
830+
"ip-restriction": {
831+
"whitelist": [
832+
"127.0.0.0/24",
833+
"113.74.26.106"
834+
],
835+
"response_code": 409
836+
}
837+
}
838+
}]]
839+
)
840+
841+
ngx.say(body)
842+
}
843+
}
844+
--- request
845+
GET /t
846+
--- response_body_like eval
847+
qr/property \\"response_code\\" validation failed: expected 409 to be at most 404/

0 commit comments

Comments
 (0)