From 848be4240bd5bb59c0a3c0f69e3764c0a1d173f3 Mon Sep 17 00:00:00 2001 From: Tudor Pop Date: Thu, 3 Dec 2026 00:10:16 +0200 Subject: [PATCH] fix(envoy): ask_unlisted gates the check once per virtual host instead of copying every route (Part of #78) v2.3.0 rendered the unlisted-origin ask as a copy of every route in each virtual host, each copy carrying one negated Origin matcher per listed origin. On a file with 242 routes in each of three hosts and 20 listed origins the render grew by 2.67 MB, and Envoy's heap at idle rose from 66.7 to 222.0 MiB with a start-up peak of 236.5 MiB. The ask is now one ExtensionWithMatcher around ext_authz per environment. It skips any request the environment's hosts and lists already admit, is turned on by the virtual host and off by its refused route, and the virtual host's RBAC reads the check's answer. The virtual host's CORS policy admits an origin the check admitted with one RE2 matcher that never matches "*" or "null". The key now adds a constant 28,611 bytes at any route count; at 242 routes Envoy's heap at idle is 24.0 MiB and the container peaks at 35.7 MiB. With the key, only the exact own origin (https, default port) is admitted unasked and gets its own allow-origin header; the same host on another scheme or port is asked like any other unlisted origin. An admitted "null" origin gets no allow-origin header. A file without the key renders byte for byte as v2.3.0. The conformance scenario counts the questions the check receives, so every row asserts whether its request was asked: 20 rows on Envoy 1.40. --- conformance/battery_test.go | 17 ++ conformance/scenario_test.go | 5 + conformance/scenarios_test.go | 118 ++++++--- conformance/stub/backend.go | 10 + conformance/stub/check.go | 11 +- conformance/testdata/unlisted-origins.yaml | 3 +- conformance/world_test.go | 37 +++ docs/auth.md | 10 +- docs/conformance.md | 22 +- docs/cors.md | 10 +- docs/defaults.md | 6 +- docs/origins.md | 59 +++-- docs/portability.md | 2 +- internal/envoy/fragments/filter-cors.yaml | 9 +- internal/envoy/fragments/filter-rbac.yaml | 8 +- .../fragments/filter-unlisted-origin.yaml | 106 +++++--- internal/envoy/fragments/route-refused.yaml | 9 + .../fragments/route-unlisted-origin.yaml | 45 ---- internal/envoy/fragments/virtual-host.yaml | 45 +++- internal/envoy/generate.go | 131 +++++----- internal/format/spec.go | 6 +- internal/origins/origins.go | 10 + internal/render/unlisted_test.go | 228 ++++++++++++++++-- schema/proxyfile.schema.json | 2 +- 24 files changed, 647 insertions(+), 262 deletions(-) delete mode 100644 internal/envoy/fragments/route-unlisted-origin.yaml diff --git a/conformance/battery_test.go b/conformance/battery_test.go index 6ad2b96..143219a 100644 --- a/conformance/battery_test.go +++ b/conformance/battery_test.go @@ -445,6 +445,17 @@ func sendRepeated(w *world, e exchange, l literal, requests *[]sentRequest) []st } mark = w.logMark() } + asksBefore := 0 + var asksDifferences []string + if l.want.asked != nil { + if e.overlap { + panic(fmt.Sprintf("exchange %q counts the origin check's questions, which a row beside it could ask too, so it must not overlap", e.name)) + } + var err error + if asksBefore, err = w.originAsks(); err != nil { + asksDifferences = []string{fmt.Sprintf("reading the origin check's questions before the row: %v", err)} + } + } for i := 1; i <= count; i++ { r := e.send if e.alternate != nil && i%2 == 0 { @@ -504,6 +515,12 @@ func sendRepeated(w *world, e exchange, l literal, requests *[]sentRequest) []st if p := l.want.lineAfter; p != nil && !w.lineAfter(mark, p) { differences = append(differences, fmt.Sprintf("logged: no line the proxy wrote after the row began matches %s", p)) } + if want := l.want.asked; want != nil { + if asksDifferences == nil { + asksDifferences = w.askedSince(asksBefore, *want) + } + differences = append(differences, asksDifferences...) + } differences = append(differences, bothSeen(l.also, sawWant, sawAlso)...) if len(instances) > 1 || instances["no service"] > 0 { var from []string diff --git a/conformance/scenario_test.go b/conformance/scenario_test.go index d85a917..48944ef 100644 --- a/conformance/scenario_test.go +++ b/conformance/scenario_test.go @@ -232,6 +232,11 @@ type answer struct { // expression matches: the request log line of a target whose line is its own and no JSON object, which logged // cannot read (sendRepeated reads it, as it reads counted). lineAfter *regexp.Regexp + // asked, on a row sent in sequence, is how many questions the stub's origin check, the check + // conformance/testdata/unlisted-origins.yaml's ask_unlisted names, must receive while the row's requests are sent + // and answered (sendRepeated reads it); nil is not compared. The answer cannot show it for an origin RBAC admits + // whether or not the check was asked. + asked *int // receivedForms holds headers the stub must have received with a value the regular expression matches, which the // answer need not carry, unlike forms: a value the proxy makes anew for the service alone, such as a traceparent. receivedForms map[string]*regexp.Regexp diff --git a/conformance/scenarios_test.go b/conformance/scenarios_test.go index 459a704..5488cfd 100644 --- a/conformance/scenarios_test.go +++ b/conformance/scenarios_test.go @@ -1032,109 +1032,163 @@ var ownOriginKeys = []string{"origins..hosts", "origins..origins"}), send: request{proto: "http/1.1", method: "OPTIONS", host: "api.example.com", path: "/products/1", headers: map[string]string{"origin": "https://www.example.com", "access-control-request-method": "GET"}}, - want: answer{status: 200, headers: map[string]string{"access-control-allow-origin": "https://www.example.com"}}}, + want: answer{status: 200, headers: map[string]string{"access-control-allow-origin": "https://www.example.com"}, + asked: new(0)}}, {name: "a request from a listed origin is not put to the check", keys: slices.Concat(unlistedKeys, []string{"origins..origins"}), send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1", headers: map[string]string{"origin": "https://www.example.com"}}, - want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": "https://www.example.com"}}}, + want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": "https://www.example.com"}, + asked: new(0)}}, + // A listed origin is compared without its case, as the CORS scenario's preflight in capitals shows without the + // key, so in capitals it is still listed and never put to the check. + {name: "a request from a listed origin in capitals is not put to the check", + keys: slices.Concat(unlistedKeys, []string{"origins..origins"}), + send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1", + headers: map[string]string{"origin": "HTTPS://WWW.EXAMPLE.COM"}}, + want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": "HTTPS://WWW.EXAMPLE.COM"}, + asked: new(0)}}, // An origin the environment's pattern covers is admitted by that list, as a listed one is, so it is never put to // the check, which would refuse it. {name: "a request from an origin a pattern covers is not put to the check", keys: slices.Concat(unlistedKeys, []string{"origins..patterns"}), send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1", headers: map[string]string{"origin": "https://shop.partner.example"}}, - want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": "https://shop.partner.example"}}}, - // An origin on one of the environment's own hosts is admitted without a list today, with no - // Access-Control-Allow-Origin, and stays so: only an origin refused without the key is put to the check, which - // would refuse this one. - {name: "an origin on the environment's own host is admitted as without the key, not put to the check", + want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": "https://shop.partner.example"}, + asked: new(0)}}, + // The environment's own origin, its host on https and the default port, is admitted without a list, as without + // the key, and is never put to the check, which would refuse it. With the key it gets Access-Control-Allow-Origin + // set to it, as every admitted origin on the environment does: the header names the page's own origin, which a + // same-origin request ignores. + {name: "an origin on the environment's own host is admitted with its own allow-origin, not put to the check", keys: slices.Concat(unlistedKeys, []string{"origins..hosts"}), send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1", headers: map[string]string{"origin": "https://api.example.com"}}, - want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": ""}, - received: map[string]string{"origin": "https://api.example.com"}}}, + want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": "https://api.example.com"}, + received: map[string]string{"origin": "https://api.example.com"}, asked: new(0)}}, + // The own host over http, or on another port, is another origin to a browser, which a page there could read the + // answers of only with Access-Control-Allow-Origin. So with the key it is put to the check as an origin no list + // holds is: the stub's check refuses http://api.example.com, which gets the refused origin's 403, and admits + // https://api.example.com:8443, which gets the header set to it. + {name: "the own host over http is put to the check, which refuses it", + keys: slices.Concat(unlistedKeys, []string{"origins..hosts"}), + send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1", + headers: map[string]string{"origin": ownHostHTTP}}, + want: answer{status: 403, headers: refusedOrigin, ownBody: refusedOriginBody, asked: new(1)}}, + {name: "the own host on another port is put to the check, which admits it", + keys: slices.Concat(unlistedKeys, []string{"origins..hosts"}), + send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1", + headers: map[string]string{"origin": ownHostOtherPort}}, + want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": ownHostOtherPort}, + received: map[string]string{"origin": ownHostOtherPort}, asked: new(1)}}, {name: "a request with no Origin is not put to the check", keys: unlistedKeys, send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1"}, - want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": ""}}}, + want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": ""}, asked: new(0)}}, {name: "the auth check still decides a request from an origin the check admits", keys: slices.Concat(unlistedKeys, []string{"auth.ask", "auth.only_with_header"}), send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1", headers: map[string]string{"origin": checkAdmits, "authorization": "Bearer deny-401"}}, - want: answer{status: 401, headers: map[string]string{"access-control-allow-origin": checkAdmits}}}, + want: answer{status: 401, headers: map[string]string{"access-control-allow-origin": checkAdmits}, asked: new(1)}}, // The origin check refuses a request that reaches it with an authorization header, so this row is served only // when the client's credential, which the auth check reads, is kept from the origin check. {name: "the origin check is not sent the client's authorization", keys: slices.Concat(unlistedKeys, []string{"auth.ask", "auth.only_with_header"}), send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1", headers: map[string]string{"origin": checkAdmits, "authorization": "Bearer allow"}}, - want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": checkAdmits}}}, + want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": checkAdmits}, + asked: new(1)}}, {name: "an origin the check admits, to an environment that asks no check", keys: slices.Concat(unlistedKeys, []string{"origins..hosts"}), send: fromOrigin(checkAdmits), - want: answer{status: 403, headers: refusedOrigin, ownBody: refusedOriginBody}}, + want: answer{status: 403, headers: refusedOrigin, ownBody: refusedOriginBody, asked: new(0)}}, }} // unlistedKeys are what the rows of an origin no list holds depend on: the check the environment puts it to, and the // question that check is asked. var unlistedKeys = []string{"origins..ask_unlisted", "origins..question"} -// checkAdmits is the one origin the stub's origin check admits, checkRefuses one it refuses, checkDown one it answers -// 503, and checkLate one it answers 200 after the gateway stops waiting; none is on a list of the live environment, or -// matches its patterns, in any file the rows run on. +// checkAdmits is an origin the stub's origin check admits and checkAdmitsHTTP another, the same host over http, +// checkRefuses one it refuses, checkDown one it answers 503, and checkLate one it answers 200 after the gateway stops +// waiting; none is on a list of the live environment, or matches its patterns, in any file the rows run on. ownHostHTTP +// and ownHostOtherPort are the live environment's host on another scheme and on another port: the check refuses the +// first and admits the second. const ( - checkAdmits = "https://files.acme.example" - checkRefuses = "https://evil.example" - checkDown = "https://down.example" - checkLate = "https://late.example" + ownHostHTTP = "http://api.example.com" + ownHostOtherPort = "https://api.example.com:8443" + + checkAdmits = "https://files.acme.example" + checkAdmitsHTTP = "http://files.acme.example" + checkRefuses = "https://evil.example" + checkDown = "https://down.example" + checkLate = "https://late.example" ) // refusedOrigin is what the answer to a refused origin carries, with the key as without it: the proxy's own text/plain, diff --git a/conformance/stub/backend.go b/conformance/stub/backend.go index 030770c..79d87c6 100644 --- a/conformance/stub/backend.go +++ b/conformance/stub/backend.go @@ -13,6 +13,7 @@ import ( "path/filepath" "strconv" "strings" + "sync/atomic" "time" "unicode/utf8" ) @@ -22,6 +23,10 @@ import ( type backend struct { service, instance, state string hang time.Duration + // originAsks counts the questions the origin check at /auth/origin has received, which the battery reads at + // /stub/origin-asks (world.originAsks): an origin RBAC admits with or without the check's decision is answered the + // same either way, so only the count shows whether the proxy put it to the check. + originAsks atomic.Int64 } func (b *backend) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -34,7 +39,12 @@ func (b *backend) ServeHTTP(w http.ResponseWriter, r *http.Request) { case b.service == "accounts" && r.URL.Path == "/auth/check": b.check(w, r) case b.service == "accounts" && r.URL.Path == "/auth/origin": + // Counted as it arrives, before the answer, so once the proxy has the check's answer the count holds the + // question, and a question the proxy stopped waiting for is counted too. + b.originAsks.Add(1) originCheck(w, r, b.hang) + case b.service == "accounts" && r.URL.Path == "/stub/origin-asks": + io.WriteString(w, strconv.FormatInt(b.originAsks.Load(), 10)) default: b.reply(w, r) } diff --git a/conformance/stub/check.go b/conformance/stub/check.go index 5e2d326..cf44c64 100644 --- a/conformance/stub/check.go +++ b/conformance/stub/check.go @@ -109,8 +109,11 @@ func (b *backend) check(w http.ResponseWriter, r *http.Request) { } // originCheck answers the check an environment puts a browser origin its lists do not hold to -// (origins..ask_unlisted), at /auth/origin: 200 for the one origin it admits, -// https://files.acme.example, asked the question browser-origin with no authorization header, and for any other origin +// (origins..ask_unlisted), at /auth/origin: 200 for the three origins it admits, +// https://files.acme.example, the same host over http, http://files.acme.example, so a row shows that an admitted +// origin of either scheme gets the proxy's answer to an admitted origin, and https://api.example.com:8443, the battery +// file's own host on another port, which the proxy must put to the check as it puts an origin no list holds, each asked +// the question browser-origin with no authorization header, and for any other origin // or question, or a request that carries the client's authorization, 401 with a JSON body and an x-origin-check header // of its own. So a row that wants the proxy's own 403 for a refused origin fails when the proxy passes the check's // denial on, a row that wants an origin never put to the check, a listed one or none, fails when the proxy asks it, as @@ -131,7 +134,9 @@ func originCheck(w http.ResponseWriter, r *http.Request, hang time.Duration) { time.Sleep(hang) w.Header().Set("x-origin-check", "late") w.WriteHeader(http.StatusOK) - case r.Header.Get("x-gateway-question") == "browser-origin" && origin == "https://files.acme.example" && + case r.Header.Get("x-gateway-question") == "browser-origin" && + (origin == "https://files.acme.example" || origin == "http://files.acme.example" || + origin == "https://api.example.com:8443") && r.Header.Values("authorization") == nil: w.WriteHeader(http.StatusOK) default: diff --git a/conformance/testdata/unlisted-origins.yaml b/conformance/testdata/unlisted-origins.yaml index 69bfdcc..ff440e7 100644 --- a/conformance/testdata/unlisted-origins.yaml +++ b/conformance/testdata/unlisted-origins.yaml @@ -2,7 +2,8 @@ # (origins..ask_unlisted): the live environment of examples/full, api.example.com admitting # https://www.example.com by its list and https://*.partner.example by a pattern, asks accounts at /auth/origin about # any other origin, with the question browser-origin, and the stub's origin check admits https://files.acme.example -# alone, and only when it is not sent an authorization header; it answers https://down.example 503 and +# and http://files.acme.example, and https://api.example.com:8443, live's own host on another port, alone, and only +# when it is not sent an authorization header; it answers https://down.example 503 and # https://late.example too late. The pattern covers no origin a row puts to the check, nor the environment's own host. The auth check runs after it, under deny, asked of a request with an authorization # header. The local environment asks no check, so an origin its hosts do not serve is refused there as without the # key. It runs the scenario of an origin no list holds alone, and only Envoy's entry runs it: the nginx and HAProxy diff --git a/conformance/world_test.go b/conformance/world_test.go index 7dd8e1f..7b1029e 100644 --- a/conformance/world_test.go +++ b/conformance/world_test.go @@ -282,6 +282,43 @@ func (w *world) spans() ([]span, error) { return kept, nil } +// originAsks returns how many questions the stub's origin check has received since accounts started +// (conformance/stub/backend.go), read inside accounts, whose port only the proxy's network namespace reaches. +func (w *world) originAsks() (int, error) { + out, err := dockerStdout("exec", accounts, "/stub", "fetch", "http://127.0.0.1:7070/stub/origin-asks") + if err != nil { + return 0, err + } + text, ok := strings.CutPrefix(out, "200 ") + count, convErr := strconv.Atoi(text) + if !ok || convErr != nil { + return 0, fmt.Errorf("the origin check's count answered %q, not 200 and a number", out) + } + return count, nil +} + +// askedSince returns how the questions the origin check received after it had received before differ from want, or +// nothing when they are want. A proxy answers a request it put to the check only after the check's answer, which the +// stub gives after counting the question, so the count holds every such question once the row has its answers; a +// question the proxy stopped waiting for is counted as it arrives, which may be after the proxy's own answer, so a +// count below want is read again every 100 ms, for 5 s at most. +func (w *world) askedSince(before, want int) []string { + deadline := time.Now().Add(5 * time.Second) + for { + now, err := w.originAsks() + if err != nil { + return []string{fmt.Sprintf("reading the origin check's questions: %v", err)} + } + if now-before >= want || time.Now().After(deadline) { + if now-before != want { + return []string{fmt.Sprintf("the origin check was asked %d times, want %d", now-before, want)} + } + return nil + } + time.Sleep(100 * time.Millisecond) + } +} + // waitSpans fetches the collector's spans every 100 ms until it answers, for 10 s at most, so the proxy starts with // its collector listening. func waitSpans(t testing.TB) { diff --git a/docs/auth.md b/docs/auth.md index 2cef8b0..64d21ec 100644 --- a/docs/auth.md +++ b/docs/auth.md @@ -140,16 +140,18 @@ one path of a service that holds more is matched with `path`, not `prefix`. ## The check of an origin no list holds -An environment of `origins` may put a browser origin its lists and hosts do not admit to a check of its own, +An environment of `origins` may put a browser origin its lists and its own origin do not admit to a check of its own, `ask_unlisted`, written as `ask` is, with the question `question` ([An origin no list holds](origins.md#an-origin-no-list-holds)). That check is asked before CORS and before this one, in HTTP mode as this one is, at its own path, with the `Origin` and the question in `x-gateway-question`; it is never sent the client's `Authorization`, and `send`, `accept`, `send_request` and `when_unavailable` do not apply to it. Its `200` admits the origin, and the request then goes through this check as a request from a listed origin does: a -denial of this check reaches the client with the origin's `Access-Control-Allow-Origin`. Any other answer, a `5xx` +denial of this check reaches the client with the origin's `Access-Control-Allow-Origin`, but for an admitted `null`, +which gets none. Any other answer, a `5xx` among them, and no answer within `1s`, refuse the origin with the `403` a foreign origin gets, before this check is -asked. A route with `auth: off` turns this check off, not the origin's. The envoy target writes it; the nginx and -haproxy targets refuse the key ([Portability](portability.md)). +asked. A route with `auth: off` turns this check off, not the origin's. The envoy target writes it once for the +environment, whatever its number of routes; the nginx and haproxy targets refuse the key +([Portability](portability.md)). ## Recipes diff --git a/docs/conformance.md b/docs/conformance.md index b882d70..29a92df 100644 --- a/docs/conformance.md +++ b/docs/conformance.md @@ -180,14 +180,14 @@ those routing's and CORS's rows send it before them, against the 20 the bucket o in this order: in Envoy's entry 616 rows on `full`, 524 on `portable`, 350 on `nginx-file`, 259 on `nginx-plain`, 381 on `nginx-checks`, 537 on `nginx-auth`, 576 on `nginx-tracing`, 39 on `nginx-tracing-by-name` and 3 on `nginx-limits`, nginx's own files, 477 on `haproxy-file`, 364 on `two-checks`, 37 on `haproxy-traffic` and 1 on `haproxy-limits`, -HAProxy's own, 11 on `connection` and 15 on `unlisted-origins`; in each nginx entry on the plain image 350 on +HAProxy's own, 11 on `connection` and 20 on `unlisted-origins`; in each nginx entry on the plain image 350 on `nginx-file`, 259 on `nginx-plain`, 381 on `nginx-checks`, 537 on `nginx-auth`, 524 on `portable`, 3 on `nginx-limits` and 11 on `connection`; in each nginx entry on the image tagged `-otel` 576 on `nginx-tracing` and 39 on `nginx-tracing-by-name`; and in each HAProxy entry 477 on `haproxy-file`, 364 on `two-checks`, 12 on -`haproxy-traffic`, 1 on `haproxy-limits` and 11 on `connection`. That is 4,190 rows in Envoy's entry, 2,065 in each +`haproxy-traffic`, 1 on `haproxy-limits` and 11 on `connection`. That is 4,195 rows in Envoy's entry, 2,065 in each nginx entry on the plain image, 615 in each nginx entry on the image tagged `-otel` and 865 in each HAProxy entry, -11,280 in all, and each passes. The battery counts these rows, and those each scenario's line in the table below +11,285 in all, and each passes. The battery counts these rows, and those each scenario's line in the table below gives for each file of Envoy's entry, before any container starts, and fails when one of these numbers is not what it counts. Before nginx wrote rate limits, RPC retries, balance, hash and DNS names the same entries ran 3,041, 1,310 and 791 rows: 40 rows were added, among them the scenarios client keys and hash, nginx's files gained the traffic @@ -226,14 +226,20 @@ an absolute target written `HTTPS://`, an HTTP/2 request with `:scheme ftp` and `http`, which run on `full`, `portable`, `nginx-auth`, `haproxy-file` and `connection`, and one in the scenario mutual TLS, plain HTTP sent to the TLS port, which runs on every file with `tls`. Before the Envoy output put an origin no list holds to a check of its environment they ran 4,175, 2,065, 615 and 865: `unlisted-origins.yaml` was added with the -scenario an origin no list holds, whose 15 rows run on it alone. In order: +scenario an origin no list holds, whose 15 rows run on it alone. Before the stub's origin check counted the questions +it is asked they ran 4,190, 2,065, 615 and 865: two rows were added in the scenario an origin no list holds, a request +with an empty `Origin` and one from a listed origin in capitals, which run on `unlisted-origins`. Before the stub's +origin check admitted an `http` origin too they ran 4,192, 2,065, 615 and 865: one row was added in that scenario, a +request from `http://files.acme.example`, which runs on `unlisted-origins`. Before an own host on another scheme or +port was put to the check they ran 4,193, 2,065, 615 and 865: two rows were added in that scenario, from +`http://api.example.com` and from `https://api.example.com:8443`, which run on `unlisted-origins`. In order: | Scenario | Keys | What its rows check | |---|---|---| | routing | `routes[].pattern`, `routes[].path`, `routes[].method`, `routes[].rewrite`, `routes[].prefix`, `routes[].to`, `services..http`, `services..addresses`, `services..address`, `services..host_header`, `dns.resolver`, `origins..hosts`, `client_address`, `tls.certificate`, `limits.request_headers.count`, `limits.request_headers.size` | A pattern route, and a path its pattern misses going to the prefix route; a path and method route with its rewrite, and another method going to the prefix route; a service whose address the resolver gives; the prefix route over HTTP/1.1 and over HTTP/2, where the service still hears HTTP/1.1; the host each service hears, `host_header`'s value for `accounts`; a host of another environment; `404` for a host no environment names, with no `content-type`, with `vary: Origin` and with an `x-request-id` the proxy makes; a service at its one address; a path compared by letter case, `/API/webhooks` going to the prefix route; the path `/_pf_auth`, written and with its underscore escaped, routed as any other; an escaped newline, `%0A`, at the end of a path and inside one, matched by the patterns as written and passed on as sent; an upgrade other than WebSocket answered `403`, and an `h2c` upgrade served as a plain request whose service receives no `upgrade` or `http2-settings`, which HAProxy answers `400`, a declared difference; the scheme the request came in on, in `x-forwarded-proto`; with `client_address = connection`, a client's own `x-forwarded-proto` replaced by the connection's scheme, and its own `x-envoy-external-address` and `x-request-id` by values the proxy writes; each of 32 requests that carry no `x-request-id` gets one the proxy makes, a version 4 UUID in lower case, 8-4-4-4-12 hexadecimal digits with the third group starting with `4` and the fourth with `8`, `9`, `a` or `b`, and the answer carries it too; `502` with no `content-type` for a service that refuses connections, and `502` with no body for a service that takes no connection within 5 s ("a service that takes no connection within 5 s is answered 502"), which only `nginx-plain` has, as its `hold` service behind the route `/hold/` ([The world](#the-world)): Envoy and nginx answer it after at least 4.95 s and under 5.95 s, the connect time of both with no `timeouts`, and every other file serves `/hold/x` from `shop`; a service's `X-Accel-Redirect` passed on to the client, not followed; a service's `Keep-Alive` not passed on to the client; the headers the Envoy output removes (`proxy-connection`, `x-forwarded-client-cert` and the `x-envoy-` headers that steer an Envoy) never reach the service; the host of an absolute request line, not the `Host` header, is the host the service hears, where HAProxy answers every absolute target `400`, a declared difference; an `Upgrade` that `Connection` does not name is removed and the request served; header names holding `_` or `.` reach the service; `TE: gzip, trailers` reaches it as `trailers`; `404` for a host with a dot at its end, with no `content-type`, as the `404` of an empty `X-Forwarded-Proto` and the `403` of an upgrade other than WebSocket from no origin; `400` for a method Envoy's parser does not know, with no `x-request-id`, and after the `400` of a byte above 0x7F in the target, of a header name holding `@` and of `HTTP/1.10` the connection closed, so a request written after it gets no answer, and after the `426` of `HTTP/1.9` and the `403` of an upgrade other than WebSocket, from no origin or a foreign one, closed too, where HAProxy keeps it and serves the request after the body, a declared difference; a `QUERY` with a `Content-Type` served and one with none answered `400`, over HTTP/1.1 and HTTP/2, the `400` with no `content-type` and no `x-request-id`, before the `404` of a host no environment names too; a `QUERY` whose `Content-Type` is spaces alone answered `400` over HTTP/1.1 and served over HTTP/2, where HAProxy answers it `400` over HTTP/2, a declared difference, and one whose `Content-Type` is a tab answered `400` over HTTP/1.1; a pattern's `{,3}` read as text, as RE2 reads it; a long path `((a\|a)*c\|a+b)` takes, which PCRE2 gives up on with the pattern as written, reaching its route; and a long path that makes PCRE2 give up on `(a\|aa)+`, which goes on to the prefix route on Envoy and is answered `400` by nginx, a declared difference. A header name outside RFC 9110's token characters, `x@at`, `x(p)` or one holding the byte 0xFF, is answered `400` over HTTP/1.1 and refused over HTTP/2, where Envoy closes the connection and nginx answers `400`, a declared difference; the same requests, written by hand, with a name of token characters reach the service, and `x-ok`, `x_und` and `x.dot` reach it over both. A header value holding a control byte other than a tab is answered `400` over HTTP/1.1 and refused over HTTP/2, where Envoy closes the connection, HAProxy resets the stream and nginx answers `400`; a value holding a tab and UTF-8 reaches the service over both. A client that sends its request and closes its side of the connection before the service answers gets no answer: Envoy closes the connection, and HAProxy answers `400` or `403` or closes it. The row has its service hold its answer for 500 ms (`x-stub-delay`), because a proxy that reads the client's close after the service's answer passes the answer on: with a service that answered at once, a close sent with the request got no answer in 300 of 300 tries through Envoy and each nginx, and one sent 1 ms later got the service's `200` in 297 of 300 through Envoy and 300 of 300 through each nginx, so the row was a race, and it failed once in a whole run on nginx 1.31.8 and once on HAProxy 3.4.7. Under `client_address = forwarded`, an empty `X-Forwarded-Proto` from a client in front is answered `404` ("an empty X-Forwarded-Proto from a client in front is answered 404"), and with `connection` the request is served with the connection's scheme in its place; an `X-Forwarded-Proto` of spaces alone over HTTP/2 is served, where HAProxy answers `404`, a declared difference. A header written `x_forwarded_proto`, a name nginx reads as its own `X-Forwarded-Proto`, reaches the service as sent through Envoy and is answered `400` by nginx, a declared difference; `x_forwarded_for`, which nginx does not read as its own `X-Forwarded-For`, and a header named `constructor` or `__proto__`, over HTTP/1.1 and HTTP/2, reach the service as sent through both. Two `Accept` lines reach the service as one line through Envoy and as two lines through nginx, the same message, and two `Content-Type`, `User-Agent`, `Referer` or `If-Match` lines are joined and served by Envoy and answered `400` by nginx, each a declared difference on nginx's request-line row. With two or three spaces around an absolute target, the target's host is the host the service hears whatever the `Host` header says, and one that ends with a dot gets `404`; a tab after the method is served by Envoy and answered `400` by nginx, a declared difference; an absolute target whose host a query follows is refused. `TE: Trailers` reaches the service as `trailers` when `Connection` names `te` and is removed when it does not, and `Connection: close,` is served. An absolute target with an empty port, `http://localhost:/products/1`, is answered `400`. A chunked body's trailer reaches the service through no proxy, which receives the body; a trailer whose name holds `@` is answered `400` by Envoy and let through by nginx, which serves the body without it, and so is a trailer whose value holds a control byte, which HAProxy lets through too, over HTTP/1.1 and HTTP/2, and over HTTP/2 a trailer is served by Envoy and ends the connection on nginx, each a declared difference. A header line with no colon is answered `400` by Envoy and reaches the service through nginx as a header with an empty value, one of nginx's less strict ways. A header line folded onto the next, with a space or a tab, is served by Envoy and answered `400` by nginx and HAProxy, and a `Host` whose port is `99999` or `+1` is served by Envoy and answered `400` by nginx, declared differences. 100 header lines reach the service and 101 are refused (on `full` and `portable`, whose `limits.request_headers` admits 150, they reach it), and 96 header fields over HTTP/2 reach it, which nginx refuses, one field under Envoy's count, a declared difference, and 97 are refused ("97 header fields over HTTP 2 are refused", on the files that write `limits.request_headers.count`: Envoy resets the stream and nginx refuses the request, and `full` and `portable`, whose count is 150, serve them), and so are 96 beside `:authority` and a `host` field of the same value, which Envoy counts as two fields ("96 header fields over HTTP 2 beside :authority and a host field are refused", on the same files); HTTP/1.0 is answered `426`, with no `x-request-id`, and so are `HTTP/1.2`, `HTTP/1.9`, `HTTP/0.9` with headers, where nginx answers `400`, and `HTTP/2.0` on an HTTP/1 connection, where nginx answers `505`, and a request line with no version, where HAProxy answers `400` and nginx closes the connection, each a declared difference; `HTTP/1.10` and `HTTP/1.01` are answered `400`, and a method in lower case `400`; a header a `Connection` token names does not reach the service, where nginx and HAProxy answer `400`, a declared difference, and neither does `Keep-Alive`; `HTTP2-Settings` on a request that is no upgrade reaches the service; ten `Connection` tokens are answered `400` and nine served, a token and ten empty elements are served, where HAProxy answers `400`, a token and nine elements of white space are answered `400`, and ten tokens `close` are answered `400`, where HAProxy serves them, each a declared difference; a WebSocket over HTTP/2 is refused; over HTTP/2 an `:authority` with an empty port, `localhost:`, reaches the service as sent, where HAProxy sends `localhost`; two `Host` lines of one host are answered `404`, where HAProxy serves the request and nginx answers `400`; two `Content-Length` lines of one value, and `Content-Length` beside `Transfer-Encoding: chunked`, are answered `400`, and `Transfer-Encoding: gzip, chunked` `501`, where HAProxy serves the first two and answers the third `422`; a `POST` with no body reaches the service with `content-length: 0`, which neither nginx nor HAProxy sends; a WebSocket upgrade the service answers `200` without switching reaches the client with no body, where nginx and HAProxy pass the service's; a header of 20,000 bytes and a path of 20,000 bytes reach the service, where HAProxy answers `431` and `414`; on the files that write `limits.request_headers.size`, a head 64 bytes under the size, 61,376 bytes for the 60 KiB `nginx-file` writes, reaches the service as a header block ("a header block 64 bytes under the size", one header of 61,322 bytes) and as a request line ("a request line 64 bytes under the size", a path of `/products/` and 61,332 `a`), and a head one byte over Envoy's bound, 61,441 bytes, is refused: Envoy answers `431` to both, and nginx `400` to the header block ("a header block one byte over the size") and `414` to the request line ("a request line one byte over the size"), its own literal under `limits.request_headers.size`, while `full` and `portable` write 96 KiB and serve both; nginx reads a head of 61,376 bytes at most ([nginx](nginx.md#the-header-limits)), so the rows 64 bytes under have no margin on it; and over HTTP/2 a path of 7,000 bytes above 0x7F reaches the service, which HAProxy refuses and nginx answers `400` on `nginx-file`, each a declared difference; and a service that closes the connection of the first request a client sends on its connection before it answers is answered `502`, and so is one that closes, before its answer, a connection to `catalog` the proxy kept from a request on the client's same connection, over HTTP/1.1 and HTTP/2, the request reaching it once, where HAProxy sent it again before each request had a connection of its own. On `haproxy-file`, whose `test` environment has the hosts `.dot.example.com`, `dot.example.com.`, `-f`, `-i` and `-n`, a request to each reaches the service with that host, and on every other file it gets `404`. With `client_address = connection`, a client's `X-Forwarded-For` reaches the service on one line with the peer's address, and so do two lines of it, which Envoy joins, where nginx answers `400`, a declared difference, and the `x-envoy-` headers only a proxy in front may send (`x-envoy-decorator-operation`, `x-envoy-downstream-service-cluster`, `x-envoy-downstream-service-node`, `x-envoy-original-host`, `x-envoy-original-path`) do not reach it. The check location's own path, `/.proxyfile/check`, written or with its dot escaped, is routed as any other path and reaches `shop` as written, a prefix route's path without its slash, `/catalog`, is routed and not redirected, and a client that closes its side of the connection right after a request with a dot segment with a `;` parameter gets no answer, where nginx answers `400`, a declared difference. In Envoy's entry: 150 rows on `full`, `portable` and `nginx-file`, 149 on `nginx-auth`, which writes no `dns.resolver`, 144 on `haproxy-file`, which writes no `limits.request_headers`, 144 on `nginx-tracing`, which writes no `dns.resolver`, no `tls` and no `client_address`, and 131 on `nginx-plain`, `two-checks` and `nginx-checks`, which write none of those, no `limits.request_headers` and no pattern route. | | mutual TLS | `tls.certificate`, `tls.key`, `tls.clients.ca`, `origins..hosts` | A client with no certificate, one whose certificate another CA signed, and one whose certificate the clients CA signed and whose validity ended reach no service: the handshake fails, or the proxy refuses it, where nginx completes the handshake and answers an empty `400`, the difference nginx's row of `tls.clients.ca` names. A request whose TLS server name is one host of an environment and whose `Host` is another host of the same environment is served by Envoy and answered with an empty `421` by nginx, which asks for client certificates, a declared difference. The other CA carries the clients CA's name, so a proxy that matched the issuer by name would let it through. Every other row sends a client certificate the clients CA signed. A client that offers only TLS 1.1, a TLS 1.2 client that offers only a CBC suite, and one that offers only the group P-384 are refused in the handshake, and one that offers only the group X25519MLKEM768 is served. Plain HTTP sent to the TLS port, with a token the check allows, reaches no service: the Envoy output and HAProxy close the connection, and nginx answers an empty `400`, the difference nginx's row of `tls` names. In Envoy's entry: 9 rows on `full`, `portable`, `nginx-file`, `haproxy-file` and `nginx-auth`, the files that write `tls`. | | CORS | `cors.allow_methods`, `cors.allow_headers`, `cors.expose_headers`, `cors.max_age`, `origins..hosts`, `origins..origins`, `origins..patterns` | A preflight from a listed origin and one from an origin a pattern covers answer `200` with `access-control-allow-origin` and `access-control-max-age`; a preflight and a request from another origin answer `403`, and so does a request from another origin to a path ending `.html`, as `text/plain` on every proxy, whose own answers take no type from the path's ending; a request from a listed origin reaches the service with its `origin` and is answered with `access-control-allow-origin` and `access-control-expose-headers`. The preflight from a listed origin and the request from another origin carry a token the check denies and one the token filter refuses, each answered `401` alone, so on a file that writes the check their `200` and `403` show that the origin is decided before the token and the check. An origin on the environment's own host that neither list holds, `https://api.example.org` to `api.example.org`, is admitted without `access-control-allow-origin`: its request reaches the service and its preflight answers `200`. With no list, an origin on one of the environment's hosts is admitted and every other refused: a request with no `Origin`, and one from `http://localhost` over `http` or `https`, with a port, or from the environment's other host `http://127.0.0.1` reaches the service, and a preflight from `http://localhost` is answered `200` by the proxy; an origin on another environment's host, the host in capitals, with a trailing dot or with other characters for its dots, `null`, a present but empty `Origin`, a preflight from another origin and a WebSocket upgrade from one answer `403`, where HAProxy's parser answers that upgrade, which carries no `Sec-WebSocket-Key`, `400`, a declared difference. An empty `Origin` over HTTP/2 is answered `403` too, and an empty `Origin` line before or after the origin of the host, or a tab before that origin, changes nothing: the service receives the origin as one line. A preflight from a listed origin with no token is answered with the method and the headers it asks for; one from a listed origin in capitals is admitted with the origin as sent, and one from a pattern's origin in capitals is refused. The `Origin` header is read as one line: two `Origin` lines a pattern covers when joined are admitted with the joined line in `access-control-allow-origin`, where nginx answers `403`, a declared difference, two whose first is listed, two of the request's own origin, and one line holding a listed origin after a comma are refused, and an empty `Origin` line after a listed one is left out; a preflight that asks on two lines for two methods or for headers is answered with both, joined with a comma, and by nginx with a comma and a space, a declared difference. A listed origin with a trailing space is admitted, and the service receives it without the space. A service that is down, asked from a listed origin, is answered `502` with `access-control-allow-origin` and `access-control-expose-headers`, and a request from a listed origin to a host no environment names is answered `404` with no `access-control-allow-origin`. A refused path, a preflight to it, an upgrade other than WebSocket, a request with a NUL byte in a header from a listed origin and one with `Content-Length` beside `Transfer-Encoding: chunked` from it are answered with no `access-control-allow-origin`, where HAProxy serves the last with it, a declared difference, and a header line of 9,000 bytes from one is served with it by every proxy, nginx's header buffers reading a line as long as the header limit ([nginx](nginx.md#the-header-limits)). An admitted `Origin` of 600 bytes is echoed beside a service's answer of 100 KB, and so is one of 1,100 bytes, where HAProxy answers `403` before the service, a declared difference. A preflight from a listed origin to `/..;x/products/1`, with no token and with a token the check allows, is answered `200` by Envoy, which resolves the path and asks no check, and `400` by nginx and HAProxy, which with the token ask the check first, each a declared difference, and a denial of the check from a listed origin carries `access-control-allow-origin` and `access-control-expose-headers`. In Envoy's entry: 52 rows on `full`, `portable`, `haproxy-file`, `nginx-auth` and `nginx-tracing`, 50 on `nginx-file`, which writes no check, and 19 on `nginx-plain`, `two-checks` and `nginx-checks`, which write no list of origins and no `cors`. | -| an origin no list holds | `origins..ask_unlisted`, `origins..question`, `origins..origins`, `origins..patterns`, `origins..hosts`, `auth.ask`, `auth.only_with_header` | On `unlisted-origins`, whose environment `live` asks the stub's origin check about an origin its list, its pattern and its hosts do not admit: a preflight from `https://files.acme.example`, which the check admits, answers `200` with `access-control-allow-origin` set to it, and its request reaches the service with its `origin` and is answered with that header, over HTTP/1.1 and HTTP/2; a preflight and a request from `https://evil.example`, which the check refuses with `401`, a JSON body and an `x-origin-check` header, answer `403` with the body `Invalid CORS request` as `text/plain`, with no `access-control-allow-origin` and no header of the check's, as a refused origin is answered without the key; a request from `https://down.example`, which the check answers `503`, and one from `https://late.example`, which it answers `200` only after 3 s, get the same `403`, the late one after the 1 s the gateway waits, so a check that fails admits no origin; a preflight and a request from the listed `https://www.example.com`, a request from `https://shop.partner.example`, which the environment's pattern covers, answered with `access-control-allow-origin` set to it, a request from `https://api.example.com`, the environment's own host, and a request with no `Origin` are served as without the key, which they could not be if the check were asked, as it refuses them; a request from the admitted origin with a token the auth check denies gets the check's `401` with `access-control-allow-origin`, so the auth check still decides after the origin check; a request from the admitted origin with a token the auth check allows is served with that header, which it could not be if the origin check were sent the client's `authorization`, as the stub's origin check refuses a request that carries one; and the admitted origin, to `local`, which writes no `ask_unlisted`, gets the same `403`. Each of these five refusals is checked down to its body. In Envoy's entry: 15 rows on `unlisted-origins`. | +| an origin no list holds | `origins..ask_unlisted`, `origins..question`, `origins..origins`, `origins..patterns`, `origins..hosts`, `auth.ask`, `auth.only_with_header` | On `unlisted-origins`, whose environment `live` asks the stub's origin check about an origin its list, its pattern and its hosts do not admit: a preflight from `https://files.acme.example`, which the check admits, answers `200` with `access-control-allow-origin` set to it, and its request reaches the service with its `origin` and is answered with that header, over HTTP/1.1 and HTTP/2, and a request from `http://files.acme.example`, which the check admits too, is answered with that header set to it; a preflight and a request from `https://evil.example`, which the check refuses with `401`, a JSON body and an `x-origin-check` header, answer `403` with the body `Invalid CORS request` as `text/plain`, with no `access-control-allow-origin` and no header of the check's, as a refused origin is answered without the key; a request from `https://down.example`, which the check answers `503`, and one from `https://late.example`, which it answers `200` only after 3 s, get the same `403`, the late one after the 1 s the gateway waits, so a check that fails admits no origin; a preflight and a request from the listed `https://www.example.com`, a request from `https://shop.partner.example`, which the environment's pattern covers, answered with `access-control-allow-origin` set to it, a request from the listed origin in capitals, `HTTPS://WWW.EXAMPLE.COM`, answered with `access-control-allow-origin` set to it, a request from `https://api.example.com`, the environment's own origin, answered with `access-control-allow-origin` set to it as every origin admitted on the environment is, and a request with no `Origin` are served, and the stub's origin check is asked about none of them; a request with an empty `Origin` is asked about once and refused with the same `403` as `https://evil.example`; the own host on another scheme or port is another origin, so it is asked about once too: `http://api.example.com`, which the check refuses, gets the same `403`, and `https://api.example.com:8443`, which it admits, is served with `access-control-allow-origin` set to it; a request from the admitted origin with a token the auth check denies gets the check's `401` with `access-control-allow-origin`, so the auth check still decides after the origin check; a request from the admitted origin with a token the auth check allows is served with that header, which it could not be if the origin check were sent the client's `authorization`, as the stub's origin check refuses a request that carries one; and the admitted origin, to `local`, which writes no `ask_unlisted`, gets the same `403`. Each of these seven refusals is checked down to its body. Every row reads how many questions the stub's origin check received while it ran: one for each row from an origin no list holds or an own host on another scheme or port on `live`, none for the others. In Envoy's entry: 20 rows on `unlisted-origins`. | | auth | `auth.ask`, `auth.only_with_header`, `auth.send`, `auth.accept`, `auth.send_request`, `auth.timeout`, `auth.when_unavailable`, `routes[].auth`, `remove_from_clients`, `auth.jwt.claims`, `services..host_header`, `cors.expose_headers`, `origins..origins`, `log` | A request with no `authorization` header reaches the service, which it could not if the check were asked; when the check allows, the service receives the check's headers `auth.accept` admits and not another, and what the check was told (the method, the path with its query, `x-country`), never the `x-forwarded-uri` and `x-forwarded-method` the client sent; the check's `401` and `403` reach the client, without the `x-envoy-upstream-service-time` Envoy's client for the check adds; a check that answers `503` lets the request through, as `when_unavailable = allow` says, and on `two-checks` and `nginx-checks`, whose `when_unavailable` is `deny`, gets `403`; the mark of a request the check could not answer reaches the service, and a client's own copy never does, whether the check allows or is not asked; a route with `auth = off` is not checked; a client's own copies of the headers `remove_from_clients` names, of the check's headers and question, and of the headers `auth.jwt.claims` sets never reach the service. A header `remove_from_clients` names does not reach the service when a client sends it on two lines, one with its name in capitals; a client's own `x-request-id` is replaced by another when `remove_from_clients` names it; and on `two-checks`, whose `remove_from_clients` names the prefix `ho*`, a client's `ho-x` does not reach the service and its `Host` does. The check is told it is internal and its deadline, `x-envoy-internal: true` and `x-envoy-expected-rq-timeout-ms: 1500`; a header sent on two lines reaches it as one line, joined with a comma; a client's own `x-forwarded-proto` reaches it under `forwarded`, and the connection's scheme under `connection`; it receives the `Host` the client sent whatever the check service's `host_header`; and the `only_with_header` header sent twice, an empty line before or after the token, asks it. A check's `201` is answered `403`, and a status of `600` is passed on as the check's own, where HAProxy and nginx answer `403`. A denial of 20,000 bytes, and one with a header of 50,000, reach the client, and an allowing answer whose `x-user-id` holds 20,000 bytes sets it on the request where `auth.accept` admits it, where HAProxy answers each `403`, a declared difference. An answer whose body breaks off, and one of 101 header lines, are a check that could not answer: the request is served with the mark under `allow` and answered `403` under `deny`. A `200` that RFC 9112 does not read whole is such a check for Envoy too, in these forms: a `Content-Length` that is not a number, two `Content-Length` lines, a control byte in a header value, a status that is not three digits, a header name outside the token characters, `Content-Length` beside `Transfer-Encoding`, a chunk size that is not hexadecimal, a malformed header line in a `1xx` before it, and a malformed trailer line; HAProxy answers each `403` under `allow` too, a declared difference. Of 24 requests while `check-2`, one of the two check backends of `two-checks` and `nginx-checks`, refuses connections, at least one is answered `403` on those files: no other backend answers a check it refused. Rows for each form of an answer a check can write that RFC 9112 reads in a way of its own hold Envoy's reading, measured on Envoy 1.40.2: a status line of `2 0 0`, `20` or `000`, of `304` or of `101` is passed on as the check's own, and so is a denial with a folded header line; a status of `200.1`, `500`, `599`, or `503` with a body, a `200` with a header line with no colon, a header value with a vertical tab, `Transfer-Encoding: gzip, chunked`, or `identity` beside a `Content-Length`, a chunk that does not end its line, a space before a header's colon, a header name that holds a NUL or the byte `0xE9`, and a `Content-Length` past what any reader holds, are each a check that could not answer, and so is a denial with a header line with no colon; a `200` of `HTTP/2.0`, with two spaces after its version, with a space after a chunk's size, with spaces around its `Content-Length`, with a lone CR before a CRLF, with `_` in a header name, with bytes after its body, with a body of 1 MiB, or holding the relay's own header name `x-proxyfile-check`, is read as allowed. A denial with a body of 1 MiB, of 1 MiB and a byte and of 1,100,000 bytes reaches the client whole, and a denial's header that holds the byte `0xE9` reaches it as the check wrote it, and a denial with no body reaches it with no `content-type`. A `200` whose chunk-size line ends in a bare LF, or whose last chunk's line does, is a check that could not answer on Envoy, and a `200` whose chunk data, trailer line and final line end in a bare LF is read as allowed; nginx's relay and HAProxy's script refuse the first two under `allow` too, a declared difference. A `200` whose accepted header is folded reaches the service with the spaces and tabs of the fold, a tab, spaces on both lines or two folds, where HAProxy joins them by one space, a declared difference, and one whose accepted header is empty reaches the service empty. A `200` whose accepted header takes the request past the header size is answered `500`, where HAProxy answers `431`, a declared difference, and one whose accepted headers take it past the header count, 100 fields as Envoy counts them, is answered `500` by Envoy on every file but `full` and `portable`, which serve it, by nginx and by HAProxy, with an admitted origin's CORS answer, where one they take to the count is served: with `client_address: connection` the `x-forwarded-for` and `x-envoy-external-address` the proxy sets count too, so 87 lines from the client reach the count there and 91 elsewhere. A `200` that sets no accepted header is not counted, so with 96 numbered lines beside it the request is served by every proxy. With the accepted header and 91 numbered lines, a request with a chunked body is served, its `Transfer-Encoding` line not counted, and one with a body framed by its length is answered `500`, its `Content-Length` line counted, on `two-checks` and `nginx-checks`, where `haproxy-file` and `nginx-auth`, with three accepted headers and the two the proxy sets under `client_address: connection`, answer both `500` and `full` and `portable`, whose count is 150, serve both; and a gRPC call over HTTP/2 whose accepted header and 87 numbered fields take it past the count is answered `200` with `grpc-status` `2` and reaches no service, and one with 86 is served, where `haproxy-file` and `nginx-auth` answer 86 with `grpc-status` `2` too and `full` and `portable` serve both. nginx's relay reads some of these forms otherwise, and each such row holds nginx's own answer under `by`, named by the row of `auth.ask` or of `auth.when_unavailable` that declares it. Rows for how nginx asks the check hold Envoy's answer, and nginx's own where it declares a difference: the check location's own path asks the check as any other path does; an upload sent with `Expect: 100-continue` that the check refuses gets `100 Continue` first; a request with a body the check allows reaches the service with its body; a sent header that holds the byte `0xE9` reaches the check with a `!` for it, one that holds well-formed UTF-8 reaches it as sent, one that holds well-formed UTF-8 and then a byte that is not reaches it with a `!` for that byte, a header on two lines of characters outside ASCII reaches it as sent, joined by a comma, and a path of well-formed UTF-8 and then a byte that is not, over HTTP/2, is told to it escaped, as the service receives it; an `only_with_header` header sent as a lone tab is not asked; 100 checked streams at once on one HTTP/2 connection are each answered by their own check; an absolute target's host is the host the check is told, whatever the `Host` header, and its scheme under `forwarded`, where under `connection` the check and the service are told the connection's; a client's own user, question and unchecked mark do not reach the service when the check allows with none of them, and the check receives none of a client's own removed header, user and mark; a dot segment with a `;` parameter asks the check first, and is answered `400` when the check allows; a denial of a request from a listed origin carries the CORS answer; a WebSocket upgrade the check allows reaches the service, and one it denies is answered with the denial; a header the check's answer sets on one request is not on the next request of the same connection, and a check's `2xx` other than `200` is answered at once on a kept-alive connection; a request the check denies, one whose check is unanswered and one whose check's status is passed on each have a body that holds a request read as a body, and the next request on the connection is answered, where nginx and HAProxy answer a status of `600` `403` and the unanswered one is `403` first on `two-checks` and `nginx-checks`; a client's own `x-proxyfile-method` and `x-proxyfile-path` change nothing the check is told; the check is told the path with an escaped letter, with a dot-dot segment and a query, with a query's escapes of unreserved and of reserved characters, and with a quote; an HTTP/2 request is told its `:scheme` under `forwarded`, and under `connection` the check and the service are told the connection's scheme; a client's `HTTP2-Settings` of an h2c upgrade does not reach the check, and with no upgrade it reaches it when `send` covers it; a check's redirect whose `Location` is a path reaches the client as the check sent it; and the request log holds how long a denied request's check took, no check time for a request with no `only_with_header` header, and an unanswered check for a request the check cannot answer under `allow`. A request to a retried route is checked once, before its first try: with one shop backend refusing connections, each of 20 requests the check allows reaches the other backend with the header the check accepted, each of 6 it denies is answered `403` with no service, and each of 10 whose check answers `503` reaches the other backend with the unchecked mark under `allow` and is answered `403` under `deny`. In Envoy's entry: 152 rows on `full`, 151 on `nginx-auth`, which writes no `auth.jwt.claims`, 148 on `portable`, which writes neither that nor `routes[].retries`, 148 on `nginx-tracing`, which writes no `auth.jwt.claims` and no `client_address`, 144 on `haproxy-file`, which writes neither of the first two, no `log` and no `limits.request_headers`, 132 on `nginx-checks`, which writes none of `auth.jwt.claims`, `auth.send`, `log` and `client_address`, no `limits.request_headers`, no route with `auth = off` and no list of origins, 129 on `two-checks`, which writes no `routes[].retries` either, and 3 on `nginx-file` and `nginx-plain`, the rows of `remove_from_clients`, which they write with no check. | | the scheme the check is told | `client_address`, `auth.ask`, `auth.accept`, `auth.send_request` | Each row asks the check, which reports the scheme it was told, and the service reports the `x-forwarded-proto` it received. Under `client_address: connection` every proxy tells the check and the service the connection's scheme, whatever the client writes: an absolute `https` target, and one written `HTTPS://`, is served over TLS and told `https`, but by HAProxy, which answers an absolute target `400`, and on `connection`, over plain HTTP, the Envoy output answers it `403`, and nginx serves it and tells the check and the service `http`, a declared difference; the same request with a token the check denies is answered `401` where the check is asked, and `403` by the Envoy output on `connection` and `400` by HAProxy on every file, so neither asks the check; an absolute `http` target with that token is answered `401`, and `400` by HAProxy; an HTTP/2 request with `:scheme https`, or with `:scheme ftp`, is told `https` over TLS and `http` on `connection`; and a client's own `x-forwarded-proto` of `https`, `http` or `ftp` is replaced by the connection's scheme. Under `forwarded`, on `two-checks`, `nginx-checks` and `nginx-tracing`: an absolute `https` target sent with `x-forwarded-proto: http` is told `https`, the target's scheme, and the service is sent `http`, where HAProxy answers `400`; an HTTP/2 request with `:scheme https` sent with `x-forwarded-proto: http` is told `https` by the Envoy output and `http` by nginx and HAProxy, a declared difference, and the service is sent `http`; and an `x-forwarded-proto` of `ftp` reaches the service as sent, while the Envoy output tells the check `http`, the connection's scheme, and nginx and HAProxy tell it `ftp`, a declared difference, which each one's list of ways it is less strict names. In Envoy's entry: 11 rows on `full`, `portable`, `nginx-auth`, `haproxy-file` and `connection`, and 3 on `two-checks`, `nginx-checks` and `nginx-tracing`, the rows whose keys hold no `client_address`. | | timeout | `auth.timeout`, `auth.when_unavailable`, `timeouts.idle` | The check holds the request for 3 s, past `auth.timeout`'s 1500 ms, and its `401`, and its `200`, send their body of 100 bytes a byte every 100 ms, so it ends after 10 s. The request reaches the service with the mark of a request the check could not answer under `allow`, and is answered `403` under `deny`, on `two-checks` and `nginx-checks`, after at least 1.45 s and in under 2.95 s, 50 ms inside the two times because the row reads a different clock from the proxy's and the check's (below). With `timeouts.idle`, 2 s on `nginx-file`, `nginx-auth`, `nginx-tracing` and `haproxy-traffic` and 90 s on `full` and `portable`, the rows of the idle time run too. In "a service silent past the idle time" the stub holds its answer for 3 s (`x-stub-delay: 3s`), and Envoy and nginx answer `504` as `text/plain`, Envoy's own text where nginx's own page would be `text/html`, after at least 1.95 s and under 2.95 s (measured at 2.002 to 2.005 s on Envoy 1.40.2 and on nginx), where `full` and `portable` serve it with `200` after 2.95 s or more. In "an answer that pauses less than the idle time" the stub sends the status and headers, then a space after each of two pauses of 1.25 s (`x-stub-trickle: 2x1250ms`), then its echo: each pause is 0.75 s under the idle time and the answer ends 0.5 s past it, so a limit on the whole answer would end it, and every proxy serves it with `200` after at least 2.45 s. In "a request body that pauses past the idle time" a `POST` with `Content-Length: 4` is sent by hand, `ab`, then `cd` after 3 s: Envoy answers `408` after at least 1.95 s, so the row shows the idle time ended it, where nginx closes the connection with no answer, a declared difference on `timeouts.idle`, and `full` and `portable` serve it with `200` and the body `abcd`. Each bound keeps 50 ms from the time it tests, for the two clocks, and the `504` comes within 1 s past the idle time, for a loaded runner. In Envoy's entry: 6 rows on `full`, `portable`, `nginx-auth` and `nginx-tracing`, 3 on `haproxy-file`, `two-checks` and `nginx-checks`, which write no `timeouts.idle`, and 3 on `nginx-file` and `haproxy-traffic`, which write no check. | @@ -288,7 +294,7 @@ The battery starts every address the files name on one Docker network, so each r | `proxyfile-conformance-hold` | `10.0.2.30` | `hold` on 8095, which only `conformance/testdata/nginx-plain.yaml` names, so it starts only in a world on a file that names its address: a listener that never accepts a connection. It runs with `net.core.somaxconn` at 1, and fills its queue with two connections of its own at start, so the kernel drops the opening packet of every later connection and a client's connect waits until its own time runs out. | | `proxyfile-conformance-hold-2` | `10.0.2.31` | A second listener that never accepts a connection, on 8095, started with the first and as it is: `conformance/testdata/nginx-plain.yaml`'s `held` service has both addresses, so a call its rpc entry retries meets one that takes no connection on each try. | | `proxyfile-conformance-resolver` | `10.0.0.2`, port 53 | The resolver: `catalog.internal.example` is `10.0.2.21`, `collector.internal.example` is `10.0.3.40`, and any other name is NXDOMAIN. | -| `proxyfile-conformance-accounts` | `127.0.0.1:7070` in the proxy's own network namespace | `accounts`, HTTP/1.1 and gRPC on one port, the auth check `auth.ask` names, and at `/auth/origin` the origin check `unlisted-origins.yaml`'s `ask_unlisted` names. | +| `proxyfile-conformance-accounts` | `127.0.0.1:7070` in the proxy's own network namespace | `accounts`, HTTP/1.1 and gRPC on one port, the auth check `auth.ask` names, and at `/auth/origin` the origin check `unlisted-origins.yaml`'s `ask_unlisted` names, which counts the questions it receives and answers the count at `/stub/origin-asks`. | | `proxyfile-conformance-check-1`, `proxyfile-conformance-check-2` | `10.0.3.31`, `10.0.3.32` | `checks`, the check service of `conformance/testdata/two-checks.yaml` and `conformance/testdata/nginx-checks.yaml`, and the first of `conformance/testdata/connection.yaml`: the stub's `accounts` on 7070 at each address, so a row can make one of the two refuse connections. | | `proxyfile-conformance-collector` | `10.0.3.40`, port 4317 | The tracing collector `nginx-tracing.yaml` sends its spans to, and `nginx-tracing-by-name.yaml` by the name `collector.internal.example`, as OTLP over gRPC, started only in those files' worlds, before the proxy. `examples/full` names the same address, which its world leaves unreachable: no request waits for it. | @@ -305,8 +311,8 @@ protocol, method, path, host and headers, so a row can say which service answere by the value of the `authorization` header (`Bearer allow`, `Bearer deny-401`, `Bearer deny-403`, `Bearer status-503` or `Bearer hang`), so each row chooses the check's answer, and a row that asks the check sends `x-api-key` too, so `two-checks.yaml` and `nginx-checks.yaml` ask it; any other token, or none, is denied with `401`, so a row that wants the check not to be asked -fails when the proxy asks it. The origin check at `/auth/origin` answers `200` to the origin -`https://files.acme.example` asked the question `browser-origin` alone, with no `authorization` header; `503` with a +fails when the proxy asks it. The origin check at `/auth/origin` answers `200` to the origins +`https://files.acme.example`, `http://files.acme.example` and `https://api.example.com:8443` asked the question `browser-origin` alone, with no `authorization` header; `503` with a JSON body and `x-origin-check: down` to `https://down.example`; `200` with `x-origin-check: late` to `https://late.example` only after the stub's hang of 3 s, past the 1 s the gateway waits; and `401` with a JSON body and an `x-origin-check` header to any other origin or question, or to a request that carries `authorization`, so a row diff --git a/docs/cors.md b/docs/cors.md index 937ce2f..433ae16 100644 --- a/docs/cors.md +++ b/docs/cors.md @@ -3,9 +3,13 @@ `cors` holds the CORS settings every environment shares. [origins](origins.md) says how the gateway answers a browser with them, and when `origins` names a file, that file holds the settings instead. `Access-Control-Allow-Origin`, and `Access-Control-Expose-Headers` from `expose_headers`, go only to an origin an environment's `origins` or `patterns` -hold. An origin on the environment's own host that neither holds is admitted without them, also when it is the -request's own host and port; its preflight still carries `Access-Control-Allow-Methods`, `Access-Control-Allow-Headers` -and `Access-Control-Max-Age` from `allow_methods`, `allow_headers` and `max_age` (measured on Envoy 1.40.2). +hold, or that its check admits when it writes `ask_unlisted`, but `null`, and in such an environment to its own origin, +a host on `https` at the default port. Without `ask_unlisted`, an origin on the environment's own host that neither +list holds is admitted without them, also when it is the request's own host and port; its preflight still carries +`Access-Control-Allow-Methods`, `Access-Control-Allow-Headers` and `Access-Control-Max-Age` from `allow_methods`, +`allow_headers` and `max_age` (measured on Envoy 1.40.2). With `ask_unlisted`, the host on another scheme or port is +put to the check, and gets them only when the check admits it +([An origin no list holds](origins.md#an-origin-no-list-holds)). | Key | Meaning | Rule | Class | diff --git a/docs/defaults.md b/docs/defaults.md index 6fb289a..4b7c525 100644 --- a/docs/defaults.md +++ b/docs/defaults.md @@ -140,9 +140,9 @@ runtime value, for example, looks it up by its key. | `rate_limit` | The rate limit filter's `stat_prefix`, written with a `rate_limit`. | | `otel_collector` | The cluster of the tracing endpoint, written with the `otel` provider. | | `jwks` | The cluster of the keys URL's host, written when `auth.jwt.keys` is an https URL. | -| `browser-origins` | The one RBAC policy of each virtual host. | -| `ask_unlisted:` | Followed by the environment's name, the name of the check of an environment that writes `ask_unlisted`, a filter off on every route but the environment's copies of the routes for an origin no list holds, which turn it on by this name. It keeps its decision in the filter state under this name and `.shadow`. | -| `admitted-by-the-check` | The RBAC policy of each such copy of a route, which admits a request only when the environment's check answered `200`. | +| `browser-origins` | The RBAC policy of each virtual host that admits a request with no `Origin`, and one from an origin the environment's hosts, `origins` or `patterns` admit. | +| `ask_unlisted:` | Followed by the environment's name, the name of the check of an environment that writes `ask_unlisted`, a filter off by default, which the environment's virtual host turns on by this name and its refused route turns off again. It keeps its decision in the filter state under this name and `.shadow`. | +| `admitted-by-the-check` | The second RBAC policy of the virtual host of such an environment, which admits a request only when the environment's check answered `200`. | | `x-gateway-action`, `x-gateway-reports` | The question headers of an RPC route, read by the auth check. | | `x-gateway-question` | The question `origins..question` sends the environment's check. | | `x-gateway-timeout-ms` | The check's deadline in milliseconds. | diff --git a/docs/origins.md b/docs/origins.md index 6efe202..3a57701 100644 --- a/docs/origins.md +++ b/docs/origins.md @@ -7,7 +7,8 @@ Each environment becomes one virtual host. It serves the environment's hosts, an part, so a local setup may reach the gateway on any port. A browser request is admitted when: - it carries no `Origin` header, or -- its `Origin` is one of the environment's own hosts, on either scheme and any port, or +- its `Origin` is one of the environment's own hosts, on either scheme and any port (with `ask_unlisted`, only the + exact own origin; see below), or - its `Origin` is in the environment's `origins` or matches one of its `patterns`. Any other browser origin is refused with `403`, preflights, requests and WebSocket upgrades alike, before CORS, the auth @@ -17,16 +18,20 @@ itself, so none reaches a route's service or the auth check; the preflight of an environment's own check, when it writes one. Only an origin that `origins` or `patterns` holds, or that the environment's own check admits, gets -`Access-Control-Allow-Origin` and `Access-Control-Expose-Headers`. To such an origin every answer, its preflight's -included, carries `Access-Control-Allow-Origin` with that origin, and `Access-Control-Expose-Headers` when -`expose_headers` is written, replacing any the service sent. An origin on one of the environment's own hosts that -neither holds is admitted without those two headers, also when its host and port are the request's own `Host`: its -requests reach the service, and its preflight is answered `200` with `Access-Control-Allow-Methods`, -`Access-Control-Allow-Headers` and `Access-Control-Max-Age` from `cors` but no `Access-Control-Allow-Origin`, so a -browser does not act on them (the Fetch standard, "CORS check"). A browser needs no CORS header for a page's own origin, -which it sends with some requests, such as a `POST`, so the gateway admits it. A page on another port or scheme of the -same host is another origin to the browser, which refuses an answer without `Access-Control-Allow-Origin`; to let it -read answers, write its origin in `origins` or `patterns`. Measured on Envoy 1.40.2 with the full example's `live` +`Access-Control-Allow-Origin` and `Access-Control-Expose-Headers`, but an admitted `null`, which gets neither; with that +check, the environment's own origin gets them too ([An origin no list holds](#an-origin-no-list-holds)). To such an +origin every answer, its preflight's included, carries `Access-Control-Allow-Origin` with that origin, and +`Access-Control-Expose-Headers` when `expose_headers` is written, replacing any the service sent. In an environment +that writes no `ask_unlisted`, an origin on one of its own hosts that neither list holds is admitted without those two +headers, also when its host and port are the request's own `Host`: its requests reach the service, and its preflight +is answered `200` with `Access-Control-Allow-Methods`, `Access-Control-Allow-Headers` and `Access-Control-Max-Age` +from `cors` but no `Access-Control-Allow-Origin`, so a browser does not act on them (the Fetch standard, "CORS check"). +A browser needs no CORS header for a page's own origin, which it sends with some requests, such as a `POST`, so the +gateway admits it. A page on another port or scheme of the same host is another origin to the browser, which refuses an +answer without `Access-Control-Allow-Origin`; to let it read answers, write its origin in `origins` or `patterns`. In +an environment that writes `ask_unlisted`, such a page's origin is put to the check as any origin no list holds, and +gets the header only when the check admits it: only the own origin, a host on `https` at the default port, is admitted +without asking. Measured on Envoy 1.40.2 with the full example's `live` environment and `cors`: `https://www.example.com` to `api.example.com`, listed in `origins`, got both headers on its request and its preflight; `https://api.example.org` to `api.example.org`, and `https://api.example.org:8443` to `api.example.org:8443`, in neither list, got neither, and their preflights carried the other three. @@ -44,7 +49,7 @@ Each list is sorted in byte order and holds no value twice, so adding or removin | `origins..hosts` | The host names the environment serves, without a port. | Required. A list; each item is text. Not empty. Lower-case host names, such as `api.example.com`. No host is in two environments. | core | | `origins..origins` | The browser origins admitted exactly, each scheme://host\[:port\]. | A list; each item is text. Lower case, `http` or `https`, no path. | core | | `origins..patterns` | The browser origins admitted by pattern, where \* matches any run of characters. | A list; each item is text. A `*` matches dots too. Each is `scheme://host[:port]` with at least one `*`. | core | -| `origins..ask_unlisted` | The service and the path asked about a browser origin that neither origins nor patterns holds, and that is not on one of the hosts; an origin it admits is answered as a listed one. | A service and a path, such as accounts /check. The service needs an `http` port. It is asked before CORS, and sent the `Origin` and `question`: its `200` admits the origin, and any other answer, or none within `1s`, refuses it with the `403` a foreign origin gets. An origin a list or a host admits is never asked about. | core | +| `origins..ask_unlisted` | The service and the path asked about a browser origin that neither origins nor patterns holds, and that is not the own origin of one of the hosts, on https at the default port; an origin it admits is answered as a listed one, but null, which gets no Access-Control-Allow-Origin. | A service and a path, such as accounts /check. The service needs an `http` port. It is asked before CORS, and sent the `Origin` and `question`: its `200` admits the origin, and any other answer, or none within `1s`, refuses it with the `403` a foreign origin gets. An origin a list holds, and a host's own origin, are never asked about; a host on another scheme or port is. | core | | `origins..question` | The question sent to the check ask\_unlisted names, in x-gateway-question. | Letters, digits, dots, - and \_. Needs `ask_unlisted`. Without it the check is sent no `x-gateway-question`. | core | @@ -68,19 +73,33 @@ put to that service before CORS instead of being refused. The gateway sends it a the client's `Host` and method and `Content-Length: 0`, never the client's path or query, and here never its `Authorization` either: the service is asked about an origin, not a user. The service answers: -- `200` to admit the origin. The request is then answered as one from a listed origin: its preflight gets `200` with +- `200` to admit the origin. The request is then answered as one from a listed origin, but `null` (below) gets no + `Access-Control-Allow-Origin`: its preflight gets `200` with `Access-Control-Allow-Origin` set to the origin and the rest of `cors`, and any other request goes on to the auth check, when `auth.ask` is written, and to its service, and its answer carries `Access-Control-Allow-Origin`, a denial of the auth check's too. - Anything else to refuse it: the client gets the `403` `Invalid CORS request` a foreign origin gets without the key, with none of the service's status, headers or body. A `5xx`, and no answer within `1s`, refuse the origin too. -An origin a list or a host admits, and a request with no `Origin`, are never put to the service, and an environment -that writes no `ask_unlisted` refuses every origin its lists and hosts do not admit, as before. The gateway keeps no -answer: each request from such an origin asks the service again, and a browser keeps an admitted preflight for -`max_age`, as it does a listed origin's. Measured on Envoy 1.40.2, by the scenario "an origin no list holds" of the -[conformance battery](conformance.md). The nginx and HAProxy generators of this version refuse both keys -([Portability](portability.md)). +An origin a list holds, the environment's own origin (one of its hosts on `https` at the default port), and a request +with no `Origin` are never put to the service; the same host on `http` or on another port is put to it as any origin +no list holds, and gains no header the check does not grant. Nor is a request +whose path the gateway refuses with `400` (a `.` or `..` segment with a `;` parameter): RBAC refuses such an origin +there unasked, as without the key. An environment that writes no `ask_unlisted` refuses every origin its lists and +hosts do not admit, as before. The gateway keeps no answer: each request from such an origin asks the service again, +and a browser keeps an admitted preflight for `max_age`, as it does a listed origin's. Measured on Envoy 1.40.2, by +the scenario "an origin no list holds" of the [conformance battery](conformance.md). The nginx and HAProxy generators +of this version refuse both keys ([Portability](portability.md)). + +The Envoy output writes the check once for the environment, never once for each route, so the key adds the same bytes to +the file whatever its number of routes. It adds one filter, which the environment's virtual host turns on and which +skips every request the lists and the own origin admit, and two entries on that virtual host: an RBAC policy that admits +an origin the check admitted, and a CORS matcher that holds every origin with a scheme, so that such an origin gets +`Access-Control-Allow-Origin`. With the key, the environment's own origin gets `Access-Control-Allow-Origin` set to it +too, which grants a page at that origin nothing. An origin with no scheme, such as `null`, which a sandboxed frame or a +local file sends, gets no `Access-Control-Allow-Origin` even when the check admits it, where 2.3.0 gave it one, because +an answer that names `null` is readable by every such page. 2.3.0 wrote a copy of each route instead, which made the +file and Envoy's memory grow with the number of routes times the number of origins. ## The origins file @@ -136,6 +155,6 @@ like this: | `environments[].hosts` | The host names the environment serves, without a port. | Required. A list; each item is text. Not empty. Lower-case host names, such as `api.example.com`. No host is in two environments. | core | | `environments[].origins` | The browser origins admitted exactly, each scheme://host\[:port\]. | A list; each item is text. Lower case, `http` or `https`, no path. | core | | `environments[].patterns` | The browser origins admitted by pattern, where \* matches any run of characters. | A list; each item is text. A `*` matches dots too. Each is `scheme://host[:port]` with at least one `*`. | core | -| `environments[].ask_unlisted` | The service and the path asked about a browser origin that neither origins nor patterns holds, and that is not on one of the hosts; an origin it admits is answered as a listed one. | A service and a path, such as accounts /check. The service needs an `http` port. It is asked before CORS, and sent the `Origin` and `question`: its `200` admits the origin, and any other answer, or none within `1s`, refuses it with the `403` a foreign origin gets. An origin a list or a host admits is never asked about. | core | +| `environments[].ask_unlisted` | The service and the path asked about a browser origin that neither origins nor patterns holds, and that is not the own origin of one of the hosts, on https at the default port; an origin it admits is answered as a listed one, but null, which gets no Access-Control-Allow-Origin. | A service and a path, such as accounts /check. The service needs an `http` port. It is asked before CORS, and sent the `Origin` and `question`: its `200` admits the origin, and any other answer, or none within `1s`, refuses it with the `403` a foreign origin gets. An origin a list holds, and a host's own origin, are never asked about; a host on another scheme or port is. | core | | `environments[].question` | The question sent to the check ask\_unlisted names, in x-gateway-question. | Letters, digits, dots, - and \_. Needs `ask_unlisted`. Without it the check is sent no `x-gateway-question`. | core | diff --git a/docs/portability.md b/docs/portability.md index a79785c..cb14e7a 100644 --- a/docs/portability.md +++ b/docs/portability.md @@ -51,7 +51,7 @@ HAProxy's own OpenTelemetry filter from 3.4 on, where its OpenTracing filter is | `remove_from_clients` | removal by name and by prefix | `proxy_set_header X ""` for each name; no prefix | `http-request del-header`, by name or `-m beg` | core; nginx refuses the prefix form, HAProxy a name or prefix that covers `content-length`, `connection` or `upgrade` | | `cors.*`, `origins..origins`, `.patterns` | the CORS filter | `map` on the `Origin`, `add_header`, a preflight `return 200` | ACLs, `http-request return` for the preflight, `http-after-response set-header` | core | | `origins..hosts` | virtual host domains | `server_name` | `hdr(host)` ACLs and `use_backend` | core | -| `origins..ask_unlisted`, `.question` | an `ext_authz` filter of the environment in shadow mode, before RBAC, on a copy of each route that takes only an origin the environment does not admit, whose RBAC admits the request when the check answered `200` and whose CORS admits any origin | not written by this version's generator: list the origin in `origins` or `patterns` | not written by this version's generator: list the origin in `origins` or `patterns` | core | +| `origins..ask_unlisted`, `.question` | an `ext_authz` filter of the environment in shadow mode, before RBAC, which the environment's virtual host turns on and a matcher skips for an origin the environment admits; the virtual host's RBAC admits the request when the check answered `200`, and its CORS list holds every origin with a scheme, so the environment's own origin (a host on `https` at the default port) gets its own `Access-Control-Allow-Origin` too, and a host on another scheme or port is asked about | not written by this version's generator: list the origin in `origins` or `patterns` | not written by this version's generator: list the origin in `origins` or `patterns` | core | | `routes[].name` | the route's name, in the logs | none beyond a log variable | the ACL's name | core; nginx keeps it in the log only | | `routes[].prefix`, `.path` | prefix and path matches | `location ^~`, `location =` | `path_beg`, `path` | core | | `routes[].pattern` | RE2 | PCRE, `location ~` | PCRE, `path_reg` | core, on the part of regular-expression syntax the three share | diff --git a/internal/envoy/fragments/filter-cors.yaml b/internal/envoy/fragments/filter-cors.yaml index 73cfc81..aa399fe 100644 --- a/internal/envoy/fragments/filter-cors.yaml +++ b/internal/envoy/fragments/filter-cors.yaml @@ -6,10 +6,11 @@ # environment's check, before RBAC (filter-unlisted-origin.yaml). On every other answer to an origin its list holds it # sets Access-Control-Allow-Origin and, when cors sets expose_headers, Access-Control-Expose-Headers, replacing any the # service sent (Envoy 1.40.2, cors_filter.cc); an origin the list does not hold gets neither, also when it is the -# request's own host and port, unless that check admitted it: the copy of the route such an origin takes sets a policy -# of its own, which holds any origin, as only one the check admitted gets past RBAC there (route-unlisted-origin.yaml). -# The policy is per virtual host, and per route on those copies. Its max_age lets a browser cache an admitted preflight, -# so an origin taken off its environment's list may keep working that long in a browser that cached its preflight. +# request's own host and port. In an environment that writes ask_unlisted, the list also holds every origin with a +# scheme but none without one, such as null, as only one that check admitted gets past RBAC beside the listed ones and +# the environment's own origin, which then gets both too (virtual-host.yaml). The policy is per virtual host. Its +# max_age lets a browser cache an admitted preflight, so an origin taken off its environment's list may keep working +# that long in a browser that cached its preflight. name: envoy.filters.http.cors typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.Cors diff --git a/internal/envoy/fragments/filter-rbac.yaml b/internal/envoy/fragments/filter-rbac.yaml index f0345ae..1ca5a8e 100644 --- a/internal/envoy/fragments/filter-rbac.yaml +++ b/internal/envoy/fragments/filter-rbac.yaml @@ -1,10 +1,10 @@ # A browser origin the request's virtual host does not admit is refused here with 403, preflight, request and # WebSocket upgrade alike, before CORS, ext_authz or any upstream sees it; a request without an Origin header passes. # The exception is an environment that writes ask_unlisted: it puts such an origin to its check first, before this -# filter, and the copy of the route the request takes admits it here when the check answered OK -# (filter-unlisted-origin.yaml, route-unlisted-origin.yaml). The rules are per virtual host, so per environment of the -# origins, and those copies carry rules of their own; this filter has none of its own, and without rules it enforces -# nothing (Envoy 1.40.2, rbac.proto: "If absent, no RBAC enforcement occurs"). +# filter, and its virtual host's rules admit it here when the check answered OK (filter-unlisted-origin.yaml, the +# policy admitted-by-the-check in virtual-host.yaml). The rules are per virtual host, so per environment of the +# origins, and no route sets rules of its own; this filter has none of its own, and without rules it enforces nothing +# (Envoy 1.40.2, rbac.proto: "If absent, no RBAC enforcement occurs"). name: envoy.filters.http.rbac typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.rbac.v3.RBAC diff --git a/internal/envoy/fragments/filter-unlisted-origin.yaml b/internal/envoy/fragments/filter-unlisted-origin.yaml index e6a6e0d..3e905ab 100644 --- a/internal/envoy/fragments/filter-unlisted-origin.yaml +++ b/internal/envoy/fragments/filter-unlisted-origin.yaml @@ -2,38 +2,88 @@ # every origin, so a browser origin the environment does not admit by its lists or its hosts is put to a service of # the gateway, asked in HTTP mode as auth.ask's check is (filter-ext-authz.yaml). # -# It is off on every route but the copies its environment adds for such an origin (route-unlisted-origin.yaml), each of -# which turns it on by this filter's name: a FilterConfig entry turns on, for its route, a filter the listener's list -# writes as disabled (Envoy 1.40.2, http_filters.rst, "Disabled flag based route filter chain"). +# It is off on every virtual host but its environment's, which turns it on by this filter's name, and off again on +# that virtual host's refused route (virtual-host.yaml, route-refused.yaml). A FilterConfig entry whose config is empty +# turns on, for its virtual host or route, a filter the listener's list writes as disabled (Envoy 1.40.2 source, +# source/common/router/per_filter_config.cc lines 104 to 108), and a route's entry takes the place of its virtual +# host's (source/common/router/config_impl.cc lines 1413 to 1419, RouteEntryImplBase::filterDisabled, which asks the +# virtual host only when the route has no entry). So the check is written once for the environment, whatever its +# number of routes. +# +# The matcher below skips the check for every request the virtual host's RBAC rules admit without it: one with no +# Origin, and one whose Origin is the environment's own origin, in its list or covered by a pattern, each written +# from the same lists as those rules (admitted in generate.go). Only an origin those rules would refuse is asked about, +# and a request the matcher skips is never sent to the check (match_delegate/config.cc, +# DelegatingStreamFilter::decodeHeaders). Envoy keeps a request's Origin lines as one value joined by commas, because +# the CORS filter registers origin as an inline header and a repeated inline header is appended to the first +# (header_map_impl.cc, insertByKey), so the matcher, RBAC and CORS all decide on the same text. A request with no Origin +# gives the input no value, which no string matches (value_input_matcher.h), so the not_matcher matches it. # # In shadow mode the filter never answers the client itself: it keeps the check's decision in the request's filter -# state under its own name and .shadow (ext_authz.proto, shadow_mode), and RBAC, the filter after it, admits the -# request only when that decision is OK. So an origin the check refuses, or does not answer within the time below, or -# answers with a 5xx, is refused by RBAC with the 403 a foreign origin gets without the key, which the listener's local -# reply mapper answers Invalid CORS request (listener.yaml). The check's status and headers never reach the client -# (measured on Envoy 1.40.2: the check's 401 with a JSON body and a header of its own gave the client 403 as -# text/plain without that header), and shadow mode keeps no body of the check's (ext_authz.proto, ShadowDecision). +# state under this filter's name and .shadow (ext_authz.proto, shadow_mode; the matcher passes the filter its own +# callbacks, so the name is this one, not extension_config's), and RBAC, the filter after it, admits the request only +# when that decision is OK (the policy admitted-by-the-check, virtual-host.yaml). So an origin the check refuses, or +# does not answer within the time below, or answers with a 5xx, is refused by RBAC with the 403 a foreign origin gets +# without the key, which the listener's local reply mapper answers Invalid CORS request (listener.yaml). The check's +# status and headers never reach the client (measured on Envoy 1.40.2: the check's 401 with a JSON body and a header of +# its own gave the client 403 as text/plain without that header), and shadow mode keeps no body of the check's +# (ext_authz.proto, ShadowDecision). name: {{q .Name}} disabled: true typed_config: - "@type": type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthz - http_service: - # ext_authz reads only the cluster and the timeout here; the uri is a required field it never sends. - server_uri: {uri: {{q .URI}}, cluster: {{q .Cluster}}, timeout: {{.Timeout}}} - # The check is asked at its own path, never at the client's. - path_override: {{q .Path}} + "@type": type.googleapis.com/envoy.extensions.common.matching.v3.ExtensionWithMatcher + extension_config: + name: envoy.filters.http.ext_authz + typed_config: + "@type": type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthz + http_service: + # ext_authz reads only the cluster and the timeout here; the uri is a required field it never sends. + server_uri: {uri: {{q .URI}}, cluster: {{q .Cluster}}, timeout: {{.Timeout}}} + # The check is asked at its own path, never at the client's. + path_override: {{q .Path}} {{- with .Question}} - # headers_to_add sets the question over any copy a client sent. - authorization_request: - headers_to_add: [{key: x-gateway-question, value: {{q .}}}] + # headers_to_add sets the question over any copy a client sent. + authorization_request: + headers_to_add: [{key: x-gateway-question, value: {{q .}}}] +{{- end}} + shadow_mode: true + # The Origin is what the check decides on. HTTP mode also always sends the Host, the method and the path above, + # and the client's Authorization, which disallowed_headers keeps from the check: it is asked about an origin, not + # a user (check_request_utils.cc, toRequestMatchers; disallowed_headers overrides allowed_headers, + # ext_authz.proto). + allowed_headers: + patterns: + - exact: origin + disallowed_headers: + patterns: + - exact: authorization + xds_matcher: + matcher_list: + matchers: + - predicate: + or_matcher: + predicate: + - not_matcher: + single_predicate: + input: {name: origin, typed_config: {"@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput, header_name: origin}} + value_match: {safe_regex: {google_re2: {}, regex: ".*"}} +{{- range .SameOrigin}} + - single_predicate: + input: {name: origin, typed_config: {"@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput, header_name: origin}} + value_match: {safe_regex: {google_re2: {}, regex: {{q .}}}} +{{- end}} +{{- range .Origins}} + - single_predicate: + input: {name: origin, typed_config: {"@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput, header_name: origin}} + value_match: {exact: {{q .}}, ignore_case: true} +{{- end}} +{{- range .Patterns}} + - single_predicate: + input: {name: origin, typed_config: {"@type": type.googleapis.com/envoy.type.matcher.v3.HttpRequestHeaderMatchInput, header_name: origin}} + value_match: {safe_regex: {google_re2: {}, regex: {{q .}}}} {{- end}} - shadow_mode: true - # The Origin is what the check decides on. HTTP mode also always sends the Host, the method and the path above, and - # the client's Authorization, which disallowed_headers keeps from the check: it is asked about an origin, not a user - # (check_request_utils.cc, toRequestMatchers; disallowed_headers overrides allowed_headers, ext_authz.proto). - allowed_headers: - patterns: - - exact: origin - disallowed_headers: - patterns: - - exact: authorization + on_match: + action: + name: skip + typed_config: + "@type": type.googleapis.com/envoy.extensions.filters.common.matcher.action.v3.SkipFilter diff --git a/internal/envoy/fragments/route-refused.yaml b/internal/envoy/fragments/route-refused.yaml index 767c9ca..1dc9e32 100644 --- a/internal/envoy/fragments/route-refused.yaml +++ b/internal/envoy/fragments/route-refused.yaml @@ -10,6 +10,15 @@ # 1.33.14 or with the setting false, this route answers 400 to that spelling too. It writes %2e%2e; out as ..; without # resolving it either way. A ; escaped twice, a ; on any other segment and the query pass. The route is not auth off, # so the token filter and the check still run before its 400. +# +# In an environment that writes ask_unlisted, the route turns off the check of an origin no list holds, which its +# virtual host turns on (virtual-host.yaml): RBAC refuses such an origin on this route unasked, as without the key, so +# the check is never asked about a path the route answers 400 in any case. Every other virtual host shares one copy of +# the route without the entry. name: "refused:dot-segment-with-parameter" match: {safe_regex: {regex: ".*/\\.\\.?(?:;|%3[Bb]).*"}} direct_response: {status: 400} +{{- with .Check}} +typed_per_filter_config: + {{q .}}: {"@type": type.googleapis.com/envoy.config.route.v3.FilterConfig, disabled: true} +{{- end}} diff --git a/internal/envoy/fragments/route-unlisted-origin.yaml b/internal/envoy/fragments/route-unlisted-origin.yaml deleted file mode 100644 index f7b8db8..0000000 --- a/internal/envoy/fragments/route-unlisted-origin.yaml +++ /dev/null @@ -1,45 +0,0 @@ -# What an environment that writes ask_unlisted adds to its copy of a route (unlistedRoutes in generate.go), merged into -# the route as written. -# -# The copy takes only a request that carries an Origin none of the principals of the virtual host's RBAC rules admits -# (virtual-host.yaml): every header matcher here must match (route_components.proto, RouteMatch.headers), and each but -# the first is one of those principals inverted, written from the same lists. So an origin admitted without the key, -# by a list or as one of the environment's own hosts, never takes the copy and is never put to the check, and an -# origin refused without the key always does. A request with no Origin fails the first matcher. -match: - headers: - - {name: origin, present_match: true} -{{- range .SameOrigin}} - - {name: origin, string_match: {safe_regex: {regex: {{q .}}}}, invert_match: true} -{{- end}} -{{- range .Origins}} - - {name: origin, string_match: {exact: {{q .}}, ignore_case: true}, invert_match: true} -{{- end}} -{{- range .Patterns}} - - {name: origin, string_match: {safe_regex: {regex: {{q .}}}}, invert_match: true} -{{- end}} -# A route's own entry for a filter takes the place of its virtual host's (Envoy 1.40.2, http_filters.rst, "Route -# specific config"). -typed_per_filter_config: - # The environment's check, which the listener's list writes as disabled, runs on the copy. - {{q .Check}}: {"@type": type.googleapis.com/envoy.config.route.v3.FilterConfig, config: {}} - # RBAC admits the request only when the check's decision is OK: in the filter state, its JSON starts with - # {"check_result":"OK" (ext_authz's ShadowDecision, written with the proto's own field names). An OK decision is - # the check's 200; a refusal, a 5xx and no answer in time are DENIED or ERROR, and a request the check never - # decided has no decision, so RBAC refuses each. - envoy.filters.http.rbac: - "@type": type.googleapis.com/envoy.extensions.filters.http.rbac.v3.RBACPerRoute - rbac: - rules: - action: ALLOW - policies: - admitted-by-the-check: - permissions: [{any: true}] - principals: [{filter_state: {key: {{q .Decision}}, string_match: {prefix: "{\"check_result\":\"OK\""}}}] - # Only an origin the check admitted reaches CORS on the copy, so CORS admits any origin there: it answers the - # preflight and sets Access-Control-Allow-Origin to the origin, as for a listed one. The other settings, methods, - # headers, max_age and forward_not_matching_preflights among them, are the virtual host's: the CORS filter takes - # each from the route's policy when it sets it, and from the virtual host's otherwise (cors_filter.cc). - envoy.filters.http.cors: - "@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.CorsPolicy - allow_origin_string_match: [{safe_regex: {regex: ".*"}}] diff --git a/internal/envoy/fragments/virtual-host.yaml b/internal/envoy/fragments/virtual-host.yaml index 17797da..a8a6f95 100644 --- a/internal/envoy/fragments/virtual-host.yaml +++ b/internal/envoy/fragments/virtual-host.yaml @@ -1,8 +1,11 @@ # One virtual host per environment of the origins, written in the proxyfile or in an origins file. Each key of # typed_per_filter_config names an http filter by its name in the listener's list (Envoy 1.40.2, http_filters.rst, -# "Route specific config"). No route sets its own for these two filters, so these settings hold for every route of -# the virtual host, but the copies an environment that writes ask_unlisted adds for an origin no list holds, which set -# both (route-unlisted-origin.yaml). +# "Route specific config"). No route sets its own entry for RBAC or CORS, so these settings hold for every route of the +# virtual host. +# +# An environment that writes ask_unlisted adds three things here, each written once for the environment: the entry that +# turns its check on (filter-unlisted-origin.yaml), an RBAC policy that admits an origin that check admitted, and a +# CORS matcher that holds such an origin. Without the key the virtual host is written as before it. name: {{q .Name}} domains: [{{range $i, $host := .Hosts}}{{if $i}}, {{end}}{{q $host}}{{end}}] typed_per_filter_config: @@ -13,8 +16,9 @@ typed_per_filter_config: action: ALLOW policies: # Envoy matches the principals with OR semantics (config/rbac/v3/rbac.proto), so a request passes when it - # carries no Origin, when its Origin is one of the virtual host's own domains on either scheme and any port, - # or when the environment's origins or patterns admit it; a single permission with any covers every request. + # carries no Origin, when its Origin is one of the virtual host's own domains on either scheme and any port + # (with ask_unlisted, only on https at the default port: origins.OwnOriginRegex), or when the environment's + # origins or patterns admit it; a single permission with any covers every request. browser-origins: permissions: [{any: true}] principals: @@ -27,16 +31,40 @@ typed_per_filter_config: {{- end}} {{- range .Patterns}} - {header: {name: origin, string_match: {safe_regex: {regex: {{q .}}}}}} +{{- end}} +{{- with .Decision}} + # Policies are matched with OR semantics too, so this one adds an origin the environment's check admitted: + # the check keeps its decision in the filter state, and its JSON starts with {"check_result":"OK" (ext_authz's + # ShadowDecision, written with the proto's own field names) only for the check's 200. A refusal, a 5xx and no + # answer in time are DENIED or ERROR, and a request the check was never asked about has no decision, so this + # policy admits none of them. Only the check writes under its own name, so no client can set it. + admitted-by-the-check: + permissions: [{any: true}] + principals: [{filter_state: {key: {{q .}}, string_match: {prefix: "{\"check_result\":\"OK\""}}}] {{- end}} envoy.filters.http.cors: "@type": type.googleapis.com/envoy.extensions.filters.http.cors.v3.CorsPolicy - allow_origin_string_match:{{if not (or .Origins .Patterns)}} []{{end}} + allow_origin_string_match:{{if not (or .Origins .Patterns .Check)}} []{{end}} {{- range .Origins}} - {exact: {{q .}}, ignore_case: true} {{- end}} {{- range .Patterns}} - {safe_regex: {regex: {{q .}}}} {{- end}} +{{- if .Check}} + # An origin with a scheme that the check admitted gets Access-Control-Allow-Origin and its preflight answered, as a + # listed origin. A string matcher sees nothing but the origin, so it cannot read the check's decision; but RBAC lets + # past it only such an origin, the ones the lists above hold and the environment's own origin, its host on https at + # the default port. So this matcher holds every origin with a scheme (RFC 3986, section 3.1), and the own origin + # gets its own Access-Control-Allow-Origin too: it names the page's own origin, which grants nothing a page at that + # origin lacks. The host on another scheme or port is another origin, which RBAC lets past only when the check + # admitted it (origins.OwnOriginRegex). The matcher never holds the text "*": the CORS filter asks each matcher + # about "*" before the origin, and admits every origin for one that holds it (Envoy 1.40.2, cors_filter.cc, + # isOriginAllowed). Nor does it hold an origin with no scheme, such as null, which a sandboxed frame or a local + # file sends: an answer that names null is readable by any such page, so an admitted null gets no + # Access-Control-Allow-Origin. + - {safe_regex: {regex: "^[a-zA-Z][a-zA-Z0-9+.-]*://.*$"}} +{{- end}} {{- with .AllowMethods}} allow_methods: {{q .}} {{- end}} @@ -52,4 +80,9 @@ typed_per_filter_config: # Its default, true, would send a preflight from an origin the policy does not admit on to the service # (cors.proto); false makes the CORS filter answer it. forward_not_matching_preflights: false +{{- with .Check}} + # The environment's check, which the listener's list writes as disabled, runs on every route of the virtual host but + # the refused one, which turns it off again (route-refused.yaml). + {{q .}}: {"@type": type.googleapis.com/envoy.config.route.v3.FilterConfig, config: {}} +{{- end}} routes: $routes diff --git a/internal/envoy/generate.go b/internal/envoy/generate.go index 105c719..36fcd41 100644 --- a/internal/envoy/generate.go +++ b/internal/envoy/generate.go @@ -38,7 +38,8 @@ var connectStatus string // check), the check of each environment that writes ask_unlisted, the RBAC refusal of a foreign browser origin, CORS, // the rate limit (with a rate_limit), the token filter (with auth.jwt), ext_authz (with auth.ask), the Lua filter that // corrects the status of the bridge's errors and the Connect-gRPC bridge (both with envoy.connect_bridge), the router. -// An origin no list holds is put to its environment's check before RBAC, which reads the check's answer +// An origin no list holds is put to its environment's check before RBAC, which reads the check's answer; the check is +// written once for the environment and turned on by its virtual host, never once for each route // (filter-unlisted-origin.yaml). A foreign origin is refused before ext_authz could call the check; a browser's // preflight carries no authorization, because it sends only the names of the request's own headers (the Fetch // standard, "CORS-preflight fetch"), and it is answered before the token filter and ext_authz in any case. A request @@ -114,13 +115,14 @@ func Generate(in generator.Input) ([]generator.File, error) { clusters = append(clusters, b.fill("cluster.yaml", keysCluster(g))) } // Every virtual host holds the same routes, so they share one node, which the encoder writes out in full at each - // place; an environment that writes ask_unlisted holds them too, among routes of its own (unlistedRoutes). + // place. An environment that writes ask_unlisted holds them after a refused route of its own, which turns its check + // off (route-refused.yaml); the check is turned on once, for the whole virtual host (virtual-host.yaml). sharedRoutes := sequence(b.routes(in)...) var virtualHosts []*yaml.Node for _, environment := range in.Origins.Environments { routes := sharedRoutes if environment.AskUnlisted != nil { - routes = b.unlistedRoutes(in, environment, sharedRoutes) + routes = sequence(slices.Concat([]*yaml.Node{b.refused(unlistedCheck(environment))}, sharedRoutes.Content[1:])...) } virtualHosts = append(virtualHosts, b.fill("virtual-host.yaml", virtualHost(environment, in.Origins.CORS), splice{"routes", routes})) @@ -284,7 +286,7 @@ func health(service model.Service, protocol model.Protocol) data { func (b *builder) routes(in generator.Input) []*yaml.Node { g := in.Gateway // The refusal of a dot segment with a parameter comes first, so no route of the file takes such a path. - routes := []*yaml.Node{b.fill("route-refused.yaml", nil)} + routes := []*yaml.Node{b.refused("")} for _, route := range g.Routes { if route.RPC != nil { for _, r := range in.RPC { @@ -321,6 +323,13 @@ func (b *builder) routes(in generator.Input) []*yaml.Node { return routes } +// refused is the route that refuses a dot segment with a parameter (route-refused.yaml). With check, the name of an +// environment's check of an origin no list holds, it turns that check off; with "", it is the route every virtual +// host without the check holds. +func (b *builder) refused(check string) *yaml.Node { + return b.fill("route-refused.yaml", data{"Check": check}) +} + // decorator is the name of a route's span: the route's name with otel, "" otherwise. With OpenTelemetry Envoy names // every span ingress unless the route names it (measured on Envoy 1.40.2). X-Ray keeps 2.0's routes, which name none. func decorator(g *model.Gateway, route string) string { @@ -413,13 +422,14 @@ func asked(g *model.Gateway, serviceName, path string) data { } // unlistedCheck names the check filter of an environment that writes ask_unlisted. Filter names are unique in the -// listener's list, and each environment's routes turn on their own check by this name. +// listener's list, and the environment's virtual host turns on its own check by this name. func unlistedCheck(environment origins.Environment) string { return "ask_unlisted:" + environment.Name } // unlistedChecks is the data of the check filter of each environment that writes ask_unlisted, in the environments' -// order: the service and the path it asks, the question, and the time it waits. +// order: the service and the path it asks, the question, the time it waits, and what the environment admits without +// asking, which the check's matcher skips. func unlistedChecks(in generator.Input, _ data) []data { var copies []data for _, environment := range in.Origins.Environments { @@ -428,6 +438,7 @@ func unlistedChecks(in generator.Input, _ data) []data { continue } values := asked(in.Gateway, ask.Service.Value, ask.Path) + maps.Copy(values, admitted(environment)) maps.Copy(values, data{"Name": unlistedCheck(environment), "Question": ask.Question, "Timeout": model.FormatSeconds(model.UnlistedCheckMillis)}) copies = append(copies, values) @@ -435,62 +446,6 @@ func unlistedChecks(in generator.Input, _ data) []data { return copies } -// unlistedRoutes is the route list of an environment that writes ask_unlisted: the refused route, then a copy of each -// other route of shared that takes only a request from a browser origin the environment does not admit and asks the -// environment's check about it (route-unlisted-origin.yaml), then the rest of shared as every virtual host holds it. -// Envoy uses the first route that matches, so such a request takes the copy of the route it would take otherwise, and -// any other request the route itself. The copies are filled anew, because the shared nodes are written at every place -// that holds them. -func (b *builder) unlistedRoutes(in generator.Input, environment origins.Environment, shared *yaml.Node) *yaml.Node { - values := virtualHost(environment, in.Origins.CORS) - values["Check"] = unlistedCheck(environment) - // ext_authz in shadow mode keeps its decision in the filter state under its own name and .shadow (ext_authz.proto, - // shadow_mode). - values["Decision"] = unlistedCheck(environment) + ".shadow" - copies := b.routes(in)[1:] - for _, route := range copies { - if err := merge(route, b.fill("route-unlisted-origin.yaml", values)); err != nil && b.err == nil { - b.err = fmt.Errorf("fragment route-unlisted-origin.yaml: %w", err) - } - } - return sequence(slices.Concat(shared.Content[:1], copies, shared.Content[1:])...) -} - -// merge adds the mapping from to the mapping into: a key into lacks is added with its value, a list under a key both -// hold gets from's items after its own, and a mapping under a key both hold is merged in turn. A scalar under a key -// both hold is an error, so no part of a route is overwritten. -func merge(into, from *yaml.Node) error { - if into == nil || from == nil || into.Kind != yaml.MappingNode || from.Kind != yaml.MappingNode { - return errors.New("only a mapping merges into a mapping") - } - for i := 0; i < len(from.Content); i += 2 { - key, value := from.Content[i], from.Content[i+1] - held := valueOf(into, key.Value) - switch { - case held == nil: - into.Content = append(into.Content, key, value) - case held.Kind == yaml.SequenceNode && value.Kind == yaml.SequenceNode: - held.Content = append(held.Content, value.Content...) - default: - if err := merge(held, value); err != nil { - return fmt.Errorf("%s: %w", key.Value, err) - } - } - } - return nil -} - -// valueOf returns the value of key in a mapping node, or nil when the mapping does not hold the key. A mapping's -// Content holds its keys and values in turn. -func valueOf(mapping *yaml.Node, key string) *yaml.Node { - for i := 0; i+1 < len(mapping.Content); i += 2 { - if mapping.Content[i].Value == key { - return mapping.Content[i+1] - } - } - return nil -} - // jwt is the token filter's data, nil without auth.jwt. The authorization header carries the token after the Bearer // scheme, and any other header the bare token. func jwt(g *model.Gateway) data { @@ -541,27 +496,53 @@ type splice struct { // virtualHost is the data of one environment's virtual host. The RBAC principals and the CORS list take the // environment's origins and patterns from the same lists, so CORS admits no origin RBAC refuses. RBAC also admits an -// origin on one of the environment's own hosts (SameOriginRegex), which the CORS list does not hold. +// origin of the environment's own hosts: without ask_unlisted on either scheme and any port (SameOriginRegex), which +// the CORS list does not hold; with it only the own origin (OwnOriginRegex), which the list holds. With ask_unlisted, +// Check names the environment's check, which the virtual host turns on, and Decision the filter state RBAC reads its +// answer from; both are "" without it, which writes neither, nor the CORS matcher of every origin with a scheme. func virtualHost(environment origins.Environment, cors origins.CORS) data { - sameOrigin := make([]string, len(environment.Hosts)) - for i, host := range environment.Hosts { - sameOrigin[i] = origins.SameOriginRegex(host) - } - patterns := make([]string, len(environment.Patterns)) - for i, pattern := range environment.Patterns { - patterns[i] = origins.PatternRegex(pattern) - } - return data{ + values := admitted(environment) + maps.Copy(values, data{ "Name": environment.Name, "Hosts": environment.Hosts, - "SameOrigin": sameOrigin, - "Origins": environment.Origins, - "Patterns": patterns, "AllowMethods": cors.AllowMethods, "AllowHeaders": cors.AllowHeaders, "ExposeHeaders": cors.ExposeHeaders, "MaxAge": cors.MaxAge, + "Check": "", + "Decision": "", + }) + if environment.AskUnlisted != nil { + check := unlistedCheck(environment) + // ext_authz in shadow mode keeps its decision in the filter state under its own name and .shadow + // (ext_authz.proto, shadow_mode). + values["Check"], values["Decision"] = check, check+".shadow" + } + return values +} + +// admitted is what an environment admits without asking any check, each as its matcher reads it: an origin of one of +// its hosts (SameOrigin, a regex for each host), one its list holds (Origins, compared without case) and one a pattern +// covers (Patterns, a regex for each pattern). The RBAC principals and the CORS list of its virtual host, and the +// matcher of its check of an origin no list holds, are each written from it, so the check is skipped for exactly the +// origins RBAC admits by itself. +func admitted(environment origins.Environment) data { + // With ask_unlisted the CORS list holds every origin RBAC lets past it (virtual-host.yaml), so an origin admitted + // without asking gets Access-Control-Allow-Origin: only the exact own origin may be, and another scheme or port of + // a host is asked about. + ownOrigin := origins.SameOriginRegex + if environment.AskUnlisted != nil { + ownOrigin = origins.OwnOriginRegex + } + sameOrigin := make([]string, len(environment.Hosts)) + for i, host := range environment.Hosts { + sameOrigin[i] = ownOrigin(host) + } + patterns := make([]string, len(environment.Patterns)) + for i, pattern := range environment.Patterns { + patterns[i] = origins.PatternRegex(pattern) } + return data{"SameOrigin": sameOrigin, "Origins": environment.Origins, "Patterns": patterns} } // builder fills fragments and keeps the first error, so Generate reads in the order of the tree it builds. diff --git a/internal/format/spec.go b/internal/format/spec.go index 06c6e67..e6b510a 100644 --- a/internal/format/spec.go +++ b/internal/format/spec.go @@ -554,10 +554,12 @@ var ( // A gateway whose customers bring their own domains cannot list them all, so it asks a service of its own about // an origin no list holds. withRule(ask("ask_unlisted", "The service and the path asked about a browser origin that neither origins nor patterns holds, "+ - "and that is not on one of the hosts; an origin it admits is answered as a listed one."), + "and that is not the own origin of one of the hosts, on https at the default port; an origin it admits is answered "+ + "as a listed one, but null, which gets no Access-Control-Allow-Origin."), "The service needs an `http` port. It is asked before CORS, and sent the `Origin` and `question`: its `200` admits "+ "the origin, and any other answer, or none within `"+model.FormatSeconds(model.UnlistedCheckMillis)+"`, refuses it "+ - "with the `403` a foreign origin gets. An origin a list or a host admits is never asked about."), + "with the `403` a foreign origin gets. An origin a list holds, and a host's own origin, are never asked about; "+ + "a host on another scheme or port is."), // The question is a header value, which Envoy's check writes through its substitution formatter, where a % starts // a command, so it keeps to the characters of a route's name. {Name: "question", Doc: "The question sent to the check ask_unlisted names, in x-gateway-question.", diff --git a/internal/origins/origins.go b/internal/origins/origins.go index 0437c16..2efdb2e 100644 --- a/internal/origins/origins.go +++ b/internal/origins/origins.go @@ -278,3 +278,13 @@ func quotedParts(pattern string) []string { func SameOriginRegex(host string) string { return "^https?://" + regexp.QuoteMeta(host) + "(:[0-9]+)?$" } + +// OwnOriginRegex admits only the own origin on one of an environment's hosts: the host on https at the default port, +// which a browser writes with no port (the HTML standard's serialization of an origin). An environment that puts an +// origin no list holds to its check (ask_unlisted) admits only this one without asking, and gives it +// Access-Control-Allow-Origin, which grants a page at that very origin nothing. The host on http or on another port is +// another origin to a browser, so it is asked about as any origin no list holds, and never gains through the key an +// Access-Control-Allow-Origin it did not have without it. +func OwnOriginRegex(host string) string { + return "^https://" + regexp.QuoteMeta(host) + "$" +} diff --git a/internal/render/unlisted_test.go b/internal/render/unlisted_test.go index 676f028..17fb72c 100644 --- a/internal/render/unlisted_test.go +++ b/internal/render/unlisted_test.go @@ -3,6 +3,7 @@ package render import ( "bytes" "fmt" + "maps" "path/filepath" "reflect" "regexp" @@ -17,8 +18,9 @@ import ( // origins..ask_unlisted, asking it the environment's question: a gateway whose customers bring their // own domains cannot list them all. The Envoy output gains one HTTP filter, a second ext_authz before CORS, which asks // the service and path ask_unlisted names, sends the Origin and the question, and refuses an origin it could not ask -// about; it runs on the routes that take such an origin, in that environment alone, and nowhere else. Every other -// filter, every other route and every other environment are written as they are without the key. What Envoy answers +// about; it runs on that environment's routes but the refused one, and nowhere else, and adds no route +// (TestAskUnlistedAddsNoRoute). Every other filter, every route and every other environment are written as they are +// without the key. What Envoy answers // with it, a preflight and a request admitted with Access-Control-Allow-Origin, a refusal 403, a listed origin never // asked, is the conformance battery's scenario "an origin no list holds" (conformance/scenarios_test.go), run on // Envoy 1.40.2: this test holds the file's parts, the battery what they do. @@ -72,8 +74,9 @@ func TestAskUnlisted(t *testing.T) { } live, test := withHosts[0], withHosts[1] liveRoutes := listAt(t, live, "routes") - // The routes that take an origin no list holds are the ones the key adds to live: each matches on the Origin header, - // which none of the file's own routes reads. + // A route the key would add to live matches on the Origin header, which none of the file's own routes reads: v2.3.0 + // added a copy of each route that took only an origin no list holds, and the key adds none now. They are told apart + // here, so that such a copy is never read as one of live's own routes. var extra, own []any for _, route := range liveRoutes { if matchesOrigin(route) { @@ -113,17 +116,14 @@ func TestAskUnlisted(t *testing.T) { // The question reaches the check in x-gateway-question, set over any copy a client sent: by the check's own // headers_to_add, or, as an RPC route's question travels, by each added route's header mutation, which the check's - // allowed_headers lets through. + // allowed_headers lets through. The second counts only where there are added routes to carry it. t.Run("the check is asked the environment's question", func(t *testing.T) { - if len(extra) == 0 { - t.Fatal("live has no route that matches on the Origin header to carry the question") - } headersToAdd, _ := valueAt(check, "http_service", "authorization_request", "headers_to_add").([]any) byCheck := slices.ContainsFunc(headersToAdd, func(h any) bool { return reflect.DeepEqual(h, map[string]any{"key": "x-gateway-question", "value": "browser-origin"}) }) patterns, _ := valueAt(check, "allowed_headers", "patterns").([]any) - byRoutes := slices.ContainsFunc(patterns, exactly("x-gateway-question")) + byRoutes := len(extra) > 0 && slices.ContainsFunc(patterns, exactly("x-gateway-question")) want := map[string]any{"append": map[string]any{"header": map[string]any{"key": "x-gateway-question", "value": "browser-origin"}, "append_action": "OVERWRITE_IF_EXISTS_OR_ADD"}} for _, route := range extra { @@ -138,16 +138,16 @@ func TestAskUnlisted(t *testing.T) { } }) - t.Run("the check runs on the routes that take an origin no list holds in live, and nowhere else", func(t *testing.T) { - if len(extra) == 0 { - t.Fatalf("live's routes are %v; want one or more that match on the Origin header", names(liveRoutes)) - } + // The refused route answers a path before any route an origin could take (route-refused.yaml), and RBAC refuses an + // origin no list holds before it, unasked, as without the key; so the check runs on each of live's other routes, + // where the Origin it is gated on decides whether it is asked. + t.Run("the check runs on live's routes but the refused one, and nowhere else", func(t *testing.T) { off := valueAt(filter, "disabled") == true routeConfig := valueAt(with, slices.Concat(manager, []any{"route_config"})...) // withHosts[0] is live. for i, host := range withHosts { for _, route := range listAt(t, host, "routes") { - want := i == 0 && slices.ContainsFunc(extra, func(r any) bool { return reflect.DeepEqual(r, route) }) + want := i == 0 && valueAt(route, "name") != "refused:dot-segment-with-parameter" if got := runsOn(off, routeConfig, host, route, name); got != want { t.Errorf("virtual host %v, route %v: the check runs = %t, want %t", valueAt(host, "name"), valueAt(route, "name"), got, want) @@ -174,11 +174,154 @@ func TestAskUnlisted(t *testing.T) { }) } -// Envoy takes the first route that matches, so the copies an environment adds for an origin no list holds stand in the -// order of their routes, between the refused route and the routes themselves: an origin the check admits then takes -// the route a listed origin takes. Here an auth-off / comes after /account/, so a copy of / ahead of the copy of -// /account/ would send an admitted origin's /account/x to the public route with the auth check off. -func TestAskUnlistedCopiesKeepTheRouteOrder(t *testing.T) { +// An environment's check of an origin no list holds is written once for the environment, not once for each route. +// v2.3.0 wrote a copy of every route for it, each copy carrying one inverted Origin matcher for each origin the +// environment admits: a file of 242 routes in each of three virtual hosts, with 8 to 20 listed origins in each +// environment, grew from 777 KB to 3.09 MB, and the Envoy that loaded it held 40.6 MiB where it had held 11.5 MiB. With +// the key, each virtual host holds the routes it holds without it, and the output grows by the same bytes whatever the +// number of routes, no more than askUnlistedGrowth. That Envoy still asks exactly the origins no list holds is the +// conformance battery's scenario "an origin no list holds". +func TestAskUnlistedAddsNoRoute(t *testing.T) { + hostsAt := []any{"static_resources", "listeners", 0, "filter_chains", 0, "filters", 0, "typed_config", "route_config", + "virtual_hosts"} + growth := map[int]int{} + for _, routes := range []int{50, 100} { + t.Run(fmt.Sprintf("%d routes in each virtual host", routes), func(t *testing.T) { + without := renderedEnvoy(t, manyRoutesProxyfile(false, routes)) + with := renderedEnvoy(t, manyRoutesProxyfile(true, routes)) + growth[routes] = len(with) - len(without) + // Each virtual host holds the refused route, then the file's routes (TestRefusedRouteComesFirst). + want := []int{routes + 1, routes + 1, routes + 1} + if got := routeCounts(t, decode(t, without), hostsAt); !slices.Equal(got, want) { + t.Errorf("without ask_unlisted, the virtual hosts hold %v routes, want %v", got, want) + } + if got := routeCounts(t, decode(t, with), hostsAt); !slices.Equal(got, want) { + t.Errorf("with ask_unlisted, the virtual hosts hold %v routes, want %v, as without it", got, want) + } + if growth[routes] > askUnlistedGrowth { + t.Errorf("ask_unlisted adds %d bytes to the output, want at most %d", growth[routes], askUnlistedGrowth) + } + }) + } + if len(growth) == 2 && growth[100] != growth[50] { + t.Errorf("ask_unlisted adds %d bytes with 100 routes in each virtual host and %d with 50, want the same", + growth[100], growth[50]) + } +} + +// askUnlistedGrowth is the most, in bytes, that the three environments' checks may add to manyRoutesProxyfile's +// output, whatever its number of routes: for each environment, 4 KiB for its check and 1 KiB for each of the 23 origins +// it admits without asking, which the check must tell apart from the rest (its own host, its 20 origins and its 2 +// patterns). +const askUnlistedGrowth = 3 * (4<<10 + 23<<10) + +// With ask_unlisted, a virtual host's CORS policy holds an origin its environment's check admitted, beside its listed +// ones, with plain string matchers: Envoy's Lua string matcher, the one custom string matcher Envoy 1.40.2 has, is an +// alpha extension, whose API may change in any release, and it keeps a Lua state for each worker outside Envoy's own +// allocator. The CORS filter asks each origin matcher about the text * before it asks about the request's Origin, and +// a matcher that holds * admits every origin (Envoy 1.40.2, cors_filter.cc, isOriginAllowed), so none may hold it. +// None may hold null either, the origin a sandboxed frame or a local file sends: an answer that names it is readable +// by any such page, so an admitted null gets no Access-Control-Allow-Origin. +// That an origin the check admits, and one on the environment's own host, get Access-Control-Allow-Origin is the +// conformance battery's scenario "an origin no list holds". +func TestAskUnlistedCORSMatchers(t *testing.T) { + tree := renderedTree(t, unlistedProxyfile(true)) + + t.Run("no string matcher is Lua", func(t *testing.T) { + if at := typedAt(tree, luaStringMatcherType, ""); at != nil { + t.Errorf("the output writes Envoy's Lua string matcher at %q, want it nowhere", at) + } + }) + + t.Run("no CORS matcher of a virtual host holds * or null, and live's hold an origin no list holds", func(t *testing.T) { + hosts := listAt(t, tree, "static_resources", "listeners", 0, "filter_chains", 0, "filters", 0, "typed_config", + "route_config", "virtual_hosts") + // https://files.acme.example is on no list of the file, so on live only the matcher the key adds may hold it, + // which makes the check of * read that matcher too. + const unlisted = "https://files.acme.example" + for _, host := range hosts { + name := valueAt(host, "name") + matchers, _ := valueAt(host, "typed_per_filter_config", "envoy.filters.http.cors", "allow_origin_string_match").([]any) + holdsUnlisted := false + for _, matcher := range matchers { + star, readable := holdsText(matcher, "*") + switch { + case !readable: + t.Errorf("virtual host %v: the CORS matcher %v is none of exact, prefix, suffix, contains and a "+ + "safe_regex RE2 reads, so whether it holds * cannot be read", name, matcher) + case star: + t.Errorf("virtual host %v: the CORS matcher %v holds *, so CORS admits every origin", name, matcher) + } + if null, _ := holdsText(matcher, "null"); null { + t.Errorf("virtual host %v: the CORS matcher %v holds null, so a sandboxed frame or a local file reads "+ + "the answers", name, matcher) + } + held, _ := holdsText(matcher, unlisted) + holdsUnlisted = holdsUnlisted || held + } + if name == "live" && !holdsUnlisted { + t.Errorf("virtual host live: no CORS matcher of %v holds %s, an origin the check may admit", matchers, unlisted) + } + } + }) +} + +// luaStringMatcherType is the type of Envoy's Lua string matcher (envoy.string_matcher.lua). +const luaStringMatcherType = "type.googleapis.com/envoy.extensions.string_matcher.lua.v3.Lua" + +// typedAt returns the path of each node under tree, at path, whose @type is typeURL. +func typedAt(tree any, typeURL, path string) []string { + var found []string + switch node := tree.(type) { + case map[string]any: + if node["@type"] == typeURL { + found = append(found, path) + } + for _, key := range slices.Sorted(maps.Keys(node)) { + found = append(found, typedAt(node[key], typeURL, join(path, key))...) + } + case []any: + for i, item := range node { + found = append(found, typedAt(item, typeURL, fmt.Sprintf("%s[%d]", path, i))...) + } + } + return found +} + +// holdsText reports whether an Envoy StringMatcher holds text (string.proto): exact, prefix, suffix and contains, each +// without case under ignore_case, and safe_regex, an RE2 that must match the whole text, which Go's regexp reads in +// the same syntax. readable is false for any other matcher, such as a custom one, and for a regex Go cannot read. +func holdsText(matcher any, text string) (holds, readable bool) { + m, ok := matcher.(map[string]any) + if !ok { + return false, false + } + fold := func(s string) string { return s } + if m["ignore_case"] == true { + fold = strings.ToLower + } + if regex, ok := valueAt(m, "safe_regex", "regex").(string); ok { + compiled, err := regexp.Compile("^(?:" + regex + ")$") + if err != nil { + return false, false + } + return compiled.MatchString(text), true + } + for kind, holds := range map[string]func(string, string) bool{"exact": func(a, b string) bool { return a == b }, + "prefix": strings.HasPrefix, "suffix": strings.HasSuffix, "contains": strings.Contains} { + if value, ok := m[kind].(string); ok { + return holds(fold(text), fold(value)), true + } + } + return false, false +} + +// An origin the check admits takes the route a listed origin takes, with the auth check as that route writes it, +// because live keeps one list of routes, as written and in their order: Envoy takes the first route that matches. +// v2.3.0 put a copy of each route ahead of the routes for such an origin; here an auth-off / comes after /account/, so +// a copy of / ahead of /account/ would send an admitted origin's /account/x to the public route with the auth check +// off. No copy is written now, so live's routes are those of the file without the key. +func TestAskUnlistedKeepsTheRouteOrder(t *testing.T) { routes := "routes:\n - {name: files, prefix: /files/, to: app}\n - {name: app, prefix: /, to: app}\n" proxyfile := unlistedProxyfile(true) if !strings.HasSuffix(proxyfile, routes) { @@ -207,8 +350,6 @@ func TestAskUnlistedCopiesKeepTheRouteOrder(t *testing.T) { } want := []string{ "refused:dot-segment-with-parameter", - "account /account/, a copy", - "public /, a copy, auth off", "account /account/", "public /, auth off", } @@ -266,8 +407,51 @@ func unlistedProxyfile(ask bool) string { return b.String() } +// manyRoutesProxyfile is a gateway with the auth check, three environments, one, two and three, each on a host of its +// own and admitting 20 origins and 2 patterns by its lists, and the given number of routes, each at a prefix of its +// own. With ask, each environment puts an origin its lists do not hold to gatekeeper /origin, asking browser-origin. +func manyRoutesProxyfile(ask bool, routes int) string { + var b strings.Builder + b.WriteString("listen: 8000\nservices:\n app: {http: 3000}\n accounts: {http: 7070}\n gatekeeper: {http: 7080}\n") + b.WriteString("auth: {ask: accounts /check, timeout: 1s, enabled: true, when_unavailable: allow}\n") + b.WriteString("origins:\n") + for _, environment := range []string{"one", "two", "three"} { + fmt.Fprintf(&b, " %s:\n hosts: [api.%s.example]\n origins:\n", environment, environment) + // The reader wants the origins sorted, so the number is written with two digits. + for i := range 20 { + fmt.Fprintf(&b, " - \"https://shop-%02d.%s.example\"\n", i, environment) + } + fmt.Fprintf(&b, " patterns: [\"https://*.partner-a.%[1]s.example\", \"https://*.partner-b.%[1]s.example\"]\n", + environment) + if ask { + b.WriteString(" ask_unlisted: gatekeeper /origin\n question: browser-origin\n") + } + } + b.WriteString("routes:\n") + for i := range routes { + fmt.Fprintf(&b, " - {name: r%d, prefix: /r%d/, to: app}\n", i, i) + } + return b.String() +} + +// routeCounts is the number of routes each virtual host of an Envoy tree holds, in the hosts' order. +func routeCounts(t *testing.T, tree any, hostsAt []any) []int { + t.Helper() + var counts []int + for _, host := range listAt(t, tree, hostsAt...) { + counts = append(counts, len(listAt(t, host, "routes"))) + } + return counts +} + // renderedTree renders proxyfile, written to g.yaml in a folder of its own, for envoy and returns the decoded tree. func renderedTree(t *testing.T, proxyfile string) any { + t.Helper() + return decode(t, renderedEnvoy(t, proxyfile)) +} + +// renderedEnvoy renders proxyfile, written to g.yaml in a folder of its own, for envoy and returns envoy.yaml's bytes. +func renderedEnvoy(t *testing.T, proxyfile string) []byte { t.Helper() dir := t.TempDir() testsupport.WriteFile(t, filepath.Join(dir, "g.yaml"), proxyfile) @@ -275,7 +459,7 @@ func renderedTree(t *testing.T, proxyfile string) any { if problems != nil { t.Fatalf("render problems = %q, want none", testsupport.ProblemStringsIn(dir, problems)) } - return decode(t, files[0].Bytes) + return files[0].Bytes } // extAuthzType is the type of ext_authz's settings. diff --git a/schema/proxyfile.schema.json b/schema/proxyfile.schema.json index 69de4c5..e58a2d9 100644 --- a/schema/proxyfile.schema.json +++ b/schema/proxyfile.schema.json @@ -472,7 +472,7 @@ "description": "One environment: its hosts and the browser origins it admits.", "properties": { "ask_unlisted": { - "description": "The service and the path asked about a browser origin that neither origins nor patterns holds, and that is not on one of the hosts; an origin it admits is answered as a listed one.", + "description": "The service and the path asked about a browser origin that neither origins nor patterns holds, and that is not the own origin of one of the hosts, on https at the default port; an origin it admits is answered as a listed one, but null, which gets no Access-Control-Allow-Origin.", "pattern": "^[a-z][a-z0-9-]* /[^\\t\\n\\f\\r ]*$", "type": "string" },