Appearance
API call rewriting Since v0.2
Use rewriting to publish an API under a public prefix that maps to an internal one, a versioned path with an identifier, a different Host, or extra and removed request headers. Answers are changed back, so clients never see backend paths. The first rewrite that matches a request applies.
Before you begin
- Role:
operatorfor the application. - Rewriting is how the application works, not a protection. It applies in
detectmode too and raises no events. - Everything the product records (events, inventory, learning, descriptions, rate limits) uses the public path the client sent, because Envoy rewrites after inspection.
Add a rewrite
text
1. Open Applications > All applications > the application > Request checks > API protection.
2. Open the Rewrites tab and select Add rewrite.
3. Enter a Name, fill in exactly one of Public prefix, Exact public path, Template or
Regular expression, and the Backend path.
4. Optionally set Methods, Backend host, request headers and the response options.
5. Select Preview changes, add a comment and select Apply.sh
waap api rewrites -app shop > rewrites.yaml
# edit rewrites.yaml, then preview and apply
waap api rewrites -app shop -f rewrites.yaml -dry-run
waap api rewrites -app shop -f rewrites.yaml -m "publish users v1"yaml
# rewrites.yaml, or applications[].policy.api.rewrites
- name: users-v1
match: {template: "/api/v1/users/{id}"}
methods: [GET, PUT]
to: "/internal/users/{id}"
host: users.backend.internal
request_headers:
set: {X-Api-Version: "1"}
remove: [X-Debug]
response:
location: true
links: ["/_links/*/href", "/next"]Change the order
The list is ordered and the first match wins. On the Rewrites tab, select Move up or Move down, then preview and apply. From the CLI, reorder the file and apply it with waap api rewrites -f.
Test a rewrite
text
1. Under the rewrites list, open the Test panel.
2. Enter a request path to see the path, host and headers the backend gets, or a
Location value to see its public form.sh
waap api rewrite test -app shop -path /api/v1/users/7
waap api rewrite test -app shop -location /internal/users/7The test sends nothing. Viewers may use it.
How matching works
| Match | Meaning | to |
|---|---|---|
prefix: /api/v1 | The path and everything below it, by whole segments (/api/v10 does not match). | The replacement prefix. |
path: /status | Exactly this path. | The new path. |
template: /users/{id} | Whole-segment variables. A variable never matches ., .. or an encoded dot. | Variables written {id}, each at most once, with fixed text before the first. |
regex: /old/(\d+) | RE2, must match the whole path; the query string is not part of it. | \1 to \9 for the groups. Fixed text must come before the first group. |
What happens to answers
The rewrite module changes Location and Content-Location headers (absolute backend URLs get the request's own scheme and host). For rewrites with links, it also changes string values at the named JSON pointers in JSON answers (application/json or +json, up to 4 MiB and the application's response body limit). Everything else is kept byte for byte. A link that no rewrite maps back is left as it is and counted in waap_rewrite_unmatched_total. The security trace shows each change.
What cannot be reversed
links is refused with a regex match, a regex rewrite does not change Location, and a template is reversed only when to uses all of its variables.
Fail-open keeps backend links
When the inspection service is unreachable and the application fails open, Envoy still rewrites the request, but the answer keeps backend links.
Paths are normalised by the data plane before the rewrite (dot segments, repeated slashes). A to that starts with // or holds .. is refused when the configuration is saved. Rewrites also apply to WebSocket handshakes.
Settings reference
| Setting | What it does | Default | Allowed values | Applies in |
|---|---|---|---|---|
Name (name) | Identifies the rewrite in configuration and the trace. | none (required) | lowercase letters, digits, dashes | all |
Public prefix (match.prefix) | Matches by whole segments. | none | path | exactly one match field |
Exact public path (match.path) | Matches exactly this path. | none | path | exactly one match field |
Template (match.template) | Matches with whole-segment variables. | none | path with {name} | exactly one match field |
Regular expression (match.regex) | Matches the whole path. | none | RE2, up to 256 bytes | exactly one match field |
Backend path (to) | Where the request goes. | none (required) | path, up to 1024 bytes | all |
Methods (methods) | Only these methods are rewritten. | none (any) | uppercase methods | all |
Backend host (host) | Replaces the Host sent to the backend. | none (unchanged) | host name | all |
Set request headers (request_headers.set) | Headers added to the request. | none | name=value, up to 16; value up to 4096 bytes | all |
Remove request headers (request_headers.remove) | Headers removed from the request. | none | up to 16; hop-by-hop headers, Host, Content-Length and pseudo-headers are refused | all |
Change Location back (response.location) | Changes Location and Content-Location back to the public form. | true | true, false | all |
JSON links (response.links) | JSON pointers whose string values are changed back; * is any member or index. | none | up to 16 pointers | not with a regex match |
Limits
| Limit | Value |
|---|---|
| Rewrites per application | 64 |
| Request headers to set, and to remove, per rewrite | 16 each |
| JSON link pointers per rewrite | 16 |
| Links changed per answer | 256 |
| Regular expression | 256 bytes |
| Backend path | 1024 bytes |
| JSON answer changed | 4 MiB |
Troubleshooting
A link in an answer still shows the backend path
- Cause: no rewrite maps it back, or the rewrite has no matching
linkspointer. - Check: the trace shows each change;
waap_rewrite_unmatched_totalcounts unmapped links. - Fix: add the pointer to JSON links, or add a rewrite for that path.
Preview refuses the rewrite
- Cause: more or fewer than one match field, a
tostarting with//or holding.., orlinkswith aregexmatch. - Fix: use exactly one match field and a plain
to.