Skip to content

API call rewriting ​

Symptoms of API call rewriting, which maps public paths to backend paths in requests and reverses the mapping for links in answers.

The backend gets the public path (rewrite does nothing) ​

  • Symptom: the backend logs /api/v1/users/7 instead of /internal/users/7.
  • Cause: no rewrite matches. Prefixes match whole segments (/api/v1 does not match /api/v10). A template matches the full path (/users/{id} does not match /users/7/orders) and never a ., .. or %2e segment. methods limits the rewrite. An earlier rewrite in the list matched first. A regex must match the whole path.
  • Check: the first matching rewrite wins, so read the list in order. The generated routes are named <app>/rewrite/<name> in the Envoy configuration dump.
  • Fix: correct the match, or move the more specific rewrite up.

The configuration is refused: "to: ... must be an absolute path" ​

  • Cause: to must start with one / and hold no .., encoded slashes or dots, query or spaces. A regex to may not put a group right after the leading slash, because a capture such as /evil.com would make //evil.com.
  • Fix: put fixed text before the first group, for example /new/\1.

The client still sees backend paths in Location or in JSON ​

  • Cause: the answer was not rewritten. Possible reasons: the inspection service was unreachable and the application fails open (only the request side works then); the rewrite uses a regex match, which is not reversible; the link's path is not the reverse of any rewrite's to; the pointer does not name the value (/_links/*/href, or ~1 for a slash in a member name); the answer is not application/json, is compressed with an encoding that is not decoded (br, zstd), is longer than the response body limit or 4 MiB, or its JSON is cut off or invalid; or the answer has more than 256 links.
  • Check: the trace of the request shows rewrite: ... changes. The counter waap_rewrite_unmatched_total rises for links that no rewrite maps back.
  • Fix: add a rewrite whose to covers the link's path, or correct the pointer.

The backend rejects the request after a rewrite ​

  • Cause: the host is not what the backend expects, a request_headers.set value is wrong, or the backend needs the original path in a header (it is not added automatically).
  • Fix: set host and the headers explicitly; use request_headers.set for a fixed value.

The rewrite test says "no rewrite" for a path that should match ​

  • Cause: the test checks the active version, not unsaved edits; or one of the earlier rewrites matches first (the list is ordered); or the method list excludes the method used.
  • Check: on the Rewrites page, use Test; or waap api rewrite test -app A -path P -method M.
  • Fix: apply the edits first, and move the specific rewrite above the general one.

Saving rewrites is refused with "fill in exactly one" ​

  • Cause: a rewrite has none, or more than one, of: public prefix, exact public path, template and regular expression.
  • Fix: clear all but one match field of that rewrite.