Skip to main content

URI Blocker

The uri-blocker plugin rejects requests when their URI matches one or more configured regular expressions. It can protect internal paths, block disallowed query options, and return a controlled status and error message without contacting the upstream service.

Examples​

The examples first block an internal path and return a custom response. The second example updates the same route to block a query option without regard to letter case.

If Admin API authentication is enabled, export an admin-role key as ADMIN_API_KEY before using the Admin API tab.

Block an Internal Path​

Configure one route for an internal path and a public httpbin.org endpoint. Requests that match the internal-path rule are rejected before the upstream is contacted.

Create the route:

curl "http://127.0.0.1:9180/apisix/admin/routes/uri-blocker-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"uris": [
"/internal/*",
"/anything"
],
"plugins": {
"uri-blocker": {
"block_rules": [
"^/internal(?:/|\\?|$)"
],
"rejected_code": 403,
"rejected_msg": "internal path blocked"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

Send a request to the blocked path:

curl --include "http://127.0.0.1:9080/internal/report"

The blocked request returns the configured status and message:

HTTP/1.1 403 Forbidden
...
{"error_msg":"internal path blocked"}

Send a request that does not match the rule:

curl "http://127.0.0.1:9080/anything" \
--silent \
--output /dev/null \
--write-out "Allowed response: %{http_code}\n"

The public request reaches the upstream:

Allowed response: 200

Match a Query Option Without Case Sensitivity​

Update the plugin on the existing route so that a debug=true query option is blocked regardless of letter case:

curl "http://127.0.0.1:9180/apisix/admin/routes/uri-blocker-route" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"plugins": {
"uri-blocker": {
"block_rules": [
"[?&]debug=true(?:&|$)"
],
"rejected_code": 403,
"rejected_msg": "debug mode blocked",
"case_insensitive": true
}
}
}'

Send a request with uppercase query option values:

curl --include "http://127.0.0.1:9080/anything?DEBUG=TRUE"

The case-insensitive rule blocks the request:

HTTP/1.1 403 Forbidden
...
{"error_msg":"debug mode blocked"}

Send a request with a value that does not match the rule:

curl "http://127.0.0.1:9080/anything?debug=false" \
--silent \
--output /dev/null \
--write-out "Allowed response: %{http_code}\n"

The value that does not match reaches the upstream:

Allowed response: 200