Skip to content

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: operator for the application.
  • Rewriting is how the application works, not a protection. It applies in detect mode 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/7

The test sends nothing. Viewers may use it.

How matching works ​

MatchMeaningto
prefix: /api/v1The path and everything below it, by whole segments (/api/v10 does not match).The replacement prefix.
path: /statusExactly 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 ​

SettingWhat it doesDefaultAllowed valuesApplies in
Name (name)Identifies the rewrite in configuration and the trace.none (required)lowercase letters, digits, dashesall
Public prefix (match.prefix)Matches by whole segments.nonepathexactly one match field
Exact public path (match.path)Matches exactly this path.nonepathexactly one match field
Template (match.template)Matches with whole-segment variables.nonepath with {name}exactly one match field
Regular expression (match.regex)Matches the whole path.noneRE2, up to 256 bytesexactly one match field
Backend path (to)Where the request goes.none (required)path, up to 1024 bytesall
Methods (methods)Only these methods are rewritten.none (any)uppercase methodsall
Backend host (host)Replaces the Host sent to the backend.none (unchanged)host nameall
Set request headers (request_headers.set)Headers added to the request.nonename=value, up to 16; value up to 4096 bytesall
Remove request headers (request_headers.remove)Headers removed from the request.noneup to 16; hop-by-hop headers, Host, Content-Length and pseudo-headers are refusedall
Change Location back (response.location)Changes Location and Content-Location back to the public form.truetrue, falseall
JSON links (response.links)JSON pointers whose string values are changed back; * is any member or index.noneup to 16 pointersnot with a regex match

Limits ​

LimitValue
Rewrites per application64
Request headers to set, and to remove, per rewrite16 each
JSON link pointers per rewrite16
Links changed per answer256
Regular expression256 bytes
Backend path1024 bytes
JSON answer changed4 MiB

Troubleshooting ​

  • Cause: no rewrite maps it back, or the rewrite has no matching links pointer.
  • Check: the trace shows each change; waap_rewrite_unmatched_total counts 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 to starting with // or holding .., or links with a regex match.
  • Fix: use exactly one match field and a plain to.