Referer Restriction
The referer-restriction plugin filters requests according to the host in the Referer header. It can limit a route or service to requests referred by trusted sites or reject traffic referred by known unwanted sites before the request reaches the upstream.
The Referer header is supplied by the client. Browsers can omit it because of referrer policies, and other clients can forge it. Use this plugin to filter traffic associated with referring sites, not to authenticate or strongly authorize clients.
Examples
The examples show how to allow requests referred by trusted sites and how to block an unwanted referring domain while accepting requests without the header.
Allow Requests from Trusted Sites
Configure the route to accept an exact host and its subdomains. Requests referred by other hosts are rejected with the default response.
The plugin evaluates the host parsed from the header. An entry that begins with * uses suffix matching, so *.trusted.example.com matches docs.trusted.example.com. A missing or malformed header is rejected by default.
- Admin API
- ADC
- Ingress Controller
Create the route:
curl "http://127.0.0.1:9180/apisix/admin/routes/referer-restriction-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"uri": "/anything",
"plugins": {
"referer-restriction": {
"whitelist": [
"trusted.example.com",
"*.trusted.example.com"
]
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
Configure ADC to connect to the target backend. The remaining ADC commands read the connection settings from these environment variables:
- APISIX
- API7 Gateway
export ADC_BACKEND=apisix
export ADC_SERVER="http://127.0.0.1:9180"
export ADC_TOKEN="${ADMIN_API_KEY}"
export ADC_BACKEND=api7ee
export ADC_SERVER="https://{DASHBOARD_ADDR}"
export ADC_TOKEN="{DASHBOARD_TOKEN}"
export ADC_GATEWAY_GROUP="{GATEWAY_GROUP}"
Create the service and route:
services:
- name: referer-restriction-service
labels:
docs-example: referer-restriction
routes:
- name: referer-restriction-route
uris:
- /anything
plugins:
referer-restriction:
whitelist:
- trusted.example.com
- '*.trusted.example.com'
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
Preview the changes and confirm that they only affect resources for this example:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=referer-restriction
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=referer-restriction
Set up the Ingress Controller and gateway in the aic namespace before continuing.
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
targetPort: 80
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: referer-restriction-route
spec:
plugins:
- name: referer-restriction
config:
whitelist:
- trusted.example.com
- '*.trusted.example.com'
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: aic
name: referer-restriction-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: referer-restriction-route
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: v1
kind: Service
metadata:
namespace: aic
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
targetPort: 80
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: referer-restriction-route
spec:
ingressClassName: apisix
plugins:
- name: referer-restriction
enable: true
config:
whitelist:
- trusted.example.com
- '*.trusted.example.com'
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: aic
name: referer-restriction-route
spec:
ingressClassName: apisix
http:
- name: referer-restriction-route
match:
paths:
- /anything
backends:
- serviceName: httpbin-external-domain
servicePort: 80
plugin_config_name: referer-restriction-route
Apply the configuration:
kubectl apply -f referer-restriction-ic.yaml
Send a request with a trusted referring host:
curl "http://127.0.0.1:9080/anything" \
-H "Referer: https://docs.trusted.example.com/start" \
--silent \
--output /dev/null \
--write-out "Allowed response: %{http_code}\n"
The trusted host reaches the upstream:
Allowed response: 200
Send a request referred by a host that is not on the list:
curl --include "http://127.0.0.1:9080/anything" \
-H "Referer: https://untrusted.example.net/start"
The request is rejected before it reaches the upstream:
HTTP/1.1 403 Forbidden
...
{"message":"Your referer host is not allowed"}
Block Requests from a Referring Domain
Replace the allowlist with a denylist, customize the rejection message, and allow requests that do not include a Referer header.
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes/referer-restriction-route/plugins/referer-restriction" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"blacklist": [
"*.tracker.example.com"
],
"bypass_missing": true,
"message": "referring site blocked"
}'
In adc.yaml, replace the plugin configuration on the existing route:
plugins:
referer-restriction:
blacklist:
- '*.tracker.example.com'
bypass_missing: true
message: referring site blocked
Preview the updated service:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=referer-restriction
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=referer-restriction
Update the existing plugin configuration with the Kubernetes API used by the deployment:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: referer-restriction-route
spec:
plugins:
- name: referer-restriction
config:
blacklist:
- '*.tracker.example.com'
bypass_missing: true
message: referring site blocked
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: referer-restriction-route
spec:
ingressClassName: apisix
plugins:
- name: referer-restriction
enable: true
config:
blacklist:
- '*.tracker.example.com'
bypass_missing: true
message: referring site blocked
Apply the updated plugin configuration:
kubectl apply -f referer-restriction-ic.yaml
Send a request from a blocked referring host:
curl --include "http://127.0.0.1:9080/anything" \
-H "Referer: https://ads.tracker.example.com/campaign"
The matching host returns the custom response:
HTTP/1.1 403 Forbidden
...
{"message":"referring site blocked"}
Send a request without the Referer header:
curl "http://127.0.0.1:9080/anything" \
--silent \
--output /dev/null \
--write-out "Allowed response: %{http_code}\n"
The request bypasses the restriction and reaches the upstream:
Allowed response: 200