Patterns and syntax¶
The rules engine uses rules definition based on 3-levels of pattern matching:
endpoint: the first level allows to filter on which endpoints should match, by endpoint name.
request: the second level allows to filter on the request’s fingerprint.
stage: the third level allows to filter on which stage the filter should apply.
Under those three levels, you’ll find a list of python code blocks that will be compiled and executed when the matching events occurs.
[rules."*"."*"]
on_request = """
request.headers["X-Forwarded-For"] = "Joe"
"""
[rules."httpbin-*"."GET /*"]
on_remote_response = """
response.headers["Cache-Control"] = "max-age=3600"
"""
rules:
"*":
"*":
on_request: |
request.headers["X-Forwarded-For"] = "Joe"
"httpbin-*":
"GET /*":
on_remote_response: |
response.headers["Cache-Control"] = "max-age=3600"
Matching grammar¶
Each level is a pattern matched against a value computed from the transaction. The same grammar applies to the three levels:
*matches any sequence of characters, including none,/and spaces. It can appear anywhere, any number of times:httpbin-*,GET /users/*,* /health.Every other character is literal, there is no other special character (no
?, no regular expression).The whole value must match, not a part of it:
apidoes not matchapi2normy-api.Matching is case-sensitive.
Endpoint¶
The value is the endpoint name, as defined in your configuration.
# Every endpoint whose name starts with "httpbin-" (httpbin-eu, httpbin-us, ...).
[rules."httpbin-*"."*"]
on_request = """
request.headers["X-Region"] = "any"
"""
# Exactly the endpoint named "api".
[rules."api"."*"]
on_request = """
request.headers["X-Api"] = "yes"
"""
Request¶
The value is the request fingerprint, <METHOD> <path>, for example GET /users/42. The method is upper case
and separated from the path by one space, so you can match on the method (GET *), on the path (* /health), or
on both (POST /orders).
The path is a normalised form of the target, never the raw target as sent. The proxy still forwards the target unchanged: only what rules see is normalised, so that a rule matches what the upstream will act on.
a leading scheme and authority are removed, so
http://example.com/adminand//example.com/adminboth matchGET /admin;percent-escapes of unreserved characters (letters, digits,
-,.,_,~) are decoded, so/%61dminmatchesGET /admin;empty segments are dropped:
/api//adminis matched asGET /api/admin;;parameters are dropped from each segment:/admin;x=1/users;id=2is matched asGET /admin/users;a final slash is kept, once:
/admin//and/admin/;xare matched asGET /admin/. Anon_requestoron_responsepattern whose path has no wildcard (GET /admin,* /admin) also matches the path with its final slash removed, endpoint patterns andon_remote_*levels excepted, soGET /adminmatches/adminand/admin/, andGET /admin/*matches/admin/but not/admin;the query string is not part of it,
GET /users/42?page=2is matched asGET /users/42.
An encoded slash, %2F (any case), stays as sent in that path: /a%2Fb is matched as GET /a%2Fb. Many
upstreams read it as a slash, so on_request and on_response rules are also matched against a second form with
%2F taken as / (and normalised as above): GET /a/b matches /a%2Fb too. A rule runs once per request,
whichever form matched it.
Writing a deny rule that holds
A rule denies what its pattern matches, nothing more. Write the pattern on the normalised path (no ;, no
//) and it holds for the variants above. Known limits, which patterns cannot cover for you:
to deny a path and everything under it, write
GET /admin*(it also matches/administrator):GET /admin/*does not match/adminitself, andGET /adminonly matches that path;double-encoded escapes (
%252F), a%3Bread as;,%00,%5Cand backslashes, case-insensitive paths and any other rewriting specific to your upstream are not normalised;the proxy refuses dot segments (
/./,/../, encoded or not) on its own, rules do not need to cover them.
When the upstream is the authority on access, deny there as well: the rules only know the path as the proxy receives it.
This is the string matched by on_request and on_response, and the one the storage markers set by those rules
rely on.
Changed in version 0.13: on_remote_request and on_remote_response still match the path sent to the remote endpoint, base path and
query string included (GET /api/users/42?page=2, not normalised). A rule that matches only thanks to the query
string logs a deprecation warning, and remote levels will match the path only in the next release: write the
pattern as GET /api/users/42* or GET /api/users/* to be ready.
# on_request: matched as "GET /admin", whatever the target was (/admin?x=1, http://host/admin, /%61dmin).
[rules."*"."GET /admin"]
on_request = "request.headers['X-Admin'] = 'yes'"
# on_remote_request: matched as "GET /api/users/42?page=2" for now, rewrite it to end with a wildcard.
[rules."*"."GET /api/users/*"]
on_remote_request = "request.headers['X-Remote'] = 'yes'"
# GET requests under /users/, whatever the rest of the path is.
[rules."*"."GET /users/*"]
on_request = """
request.headers["X-Read"] = "user"
"""
# Exactly POST /orders.
[rules."*"."POST /orders"]
on_request = """
request.headers["X-Write"] = "order"
"""
# Any method on /health.
[rules."*"."* /health"]
on_request = """
request.headers["X-Probe"] = "yes"
"""
Stage¶
The value is the name of the lifecycle event being triggered: on_request, on_remote_request,
on_remote_response or on_response (see Lifecycle). In a rule, the stage is the key of the script, so
a wildcard there can attach one script to several stages.
# Only before the response goes back to the client.
[rules."*"."*"]
on_response = """
response.headers["X-Served-By"] = "harp"
"""
# Both stages that deal with the remote server.
[rules."*"."GET *"]
"on_remote_*" = """
logger.info("remote exchange for %s", request.url)
"""
Which rule wins¶
None: there is no precedence, a more specific pattern does not override a more general one. Every rule matching the three levels runs, and a later script sees what the earlier ones did to the request or the response.
Rules are stored as a tree and walked depth first: grouped by endpoint pattern, then by request pattern, then by stage, each in order of first appearance across all the loaded files. Scripts under the very same keys run in the order they are written, then in the order the files are loaded.
# Written A, B, C. Run as A, C, B: C shares its endpoint pattern with A.
[rules."*"."*"]
on_request = """
request.headers["X-Trail"] = request.headers.get("X-Trail", "") + ",A"
"""
[rules."api"."*"]
on_request = """
request.headers["X-Trail"] = request.headers.get("X-Trail", "") + ",B"
"""
[rules."*"."GET *"]
on_request = """
request.headers["X-Trail"] = request.headers.get("X-Trail", "") + ",C"
"""
On GET /x for endpoint api, X-Trail ends up as ,A,C,B, not ,A,B,C: C is grouped with A under the
* endpoint pattern, before B. For any other endpoint it is ,A,C.
If a second file, loaded after the first one, adds a script under the same keys:
# Loaded after patterns-order.toml: E joins the existing "*" / "GET *" rule.
[rules."*"."GET *"]
on_request = """
request.headers["X-Trail"] = request.headers.get("X-Trail", "") + ",E"
"""
the result becomes ,A,C,E,B: E joins the existing * / GET * group.