Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion conformance/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type result struct {
status int
err error // set when no answer came (a TLS refusal, a closed connection)
header, trailer http.Header
echo *echo // nil when no stub wrote the answer
body string // the answer's body as read, which answer.ownBody compares
echo *echo // nil when no stub wrote the answer
// stubError is set when the answer is a stub's own to a request its HTTP server could not read, which no handler
// sees, so it carries no echo: Go's net/http server writes it with Content-Type: text/plain; charset=utf-8 and a
// body that starts with the status code and its text, such as 400 Bad Request (conn.serve in net/http/server.go,
Expand Down Expand Up @@ -200,6 +201,7 @@ func (w *world) sendOn(client *http.Client, r request) result {
got.err = fmt.Errorf("reading the answer: %w", err)
return got
}
got.body = string(data)
got.echo = readEcho(resp.Header.Get("content-type"), data)
got.stubError = resp.Header.Get("content-type") == "text/plain; charset=utf-8" &&
strings.HasPrefix(string(data), fmt.Sprintf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode)))
Expand Down
1 change: 1 addition & 0 deletions conformance/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func readAnswer(reader *bufio.Reader, start time.Time) result {
got.err = fmt.Errorf("reading the answer: %w", err)
return got
}
got.body = string(data)
got.echo = readEcho(resp.Header.Get("content-type"), data)
got.stubError = resp.Header.Get("content-type") == "text/plain; charset=utf-8" &&
strings.HasPrefix(string(data), fmt.Sprintf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode)))
Expand Down
10 changes: 8 additions & 2 deletions conformance/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,11 @@ type answer struct {
forms map[string]*regexp.Regexp
// made holds response headers the answer must carry with a value the regular expression matches, for a value the
// proxy makes anew on an answer no stub wrote, such as the request id of its own 404.
made map[string]*regexp.Regexp
body string // the body the stub received; "" is not compared
made map[string]*regexp.Regexp
body string // the body the stub received; "" is not compared
// ownBody is the body of an answer the proxy wrote itself, such as its refusal of a browser origin; "" is not
// compared. A stub's echo is never that body, so a row that wants one also fails when a stub answered.
ownBody string
noTrailers bool // the stub must have received no trailer
marked string // the value of the target's unchecked mark the stub must receive
unmarked bool // the stub must not receive the target's unchecked mark
Expand Down Expand Up @@ -321,6 +324,9 @@ func compare(want answer, got result, mark string) []string {
differ("header %s: got %q, want a value %s matches", name, value, want.made[name])
}
}
if want.ownBody != "" && got.body != want.ownBody {
differ("body: got %q, want %q", got.body, want.ownBody)
}
if want.grpcStatus != "" {
status := got.trailer.Get("grpc-status")
if status == "" {
Expand Down
116 changes: 115 additions & 1 deletion conformance/scenarios_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// wait beside the rest of their own scenario alone: a later scenario's quiet and burst count on nothing else reaching
// the proxy meanwhile. The rate limit runs last, so the requests the scenarios before it send to its route come first,
// and its quiet lets that route's bucket fill again before its burst.
var scenarios = []scenario{routing, mutualTLS, cors, auth, toldScheme, timeout, retry, gRPC, paths, asWritten, offRoute, clientKeys, hash, tracing, tracingRetried, rateLimit, cost, backendLimits, traffic}
var scenarios = []scenario{routing, mutualTLS, cors, unlisted, auth, toldScheme, timeout, retry, gRPC, paths, asWritten, offRoute, clientKeys, hash, tracing, tracingRetried, rateLimit, cost, backendLimits, traffic}

// get is a GET over HTTP/1.1 to localhost, a host of the example's local environment.
func get(path string) request {
Expand Down Expand Up @@ -1030,6 +1030,120 @@ func preflightFrom(origin string) request {
// which does not list it, leaves it without access-control-allow-origin.
var ownOriginKeys = []string{"origins.<environment name>.hosts", "origins.<environment name>.origins"}

// unlisted: a browser origin none of an environment's origins and patterns holds, which the environment puts to a
// check of its own, ask_unlisted, asking it the environment's question, on the live environment's host,
// api.example.com. The stub's origin check (stub/check.go, originCheck) admits checkAdmits alone, asked browser-origin,
// and answers any other origin or question 401 with a JSON body and an x-origin-check header. An origin it admits gets
// what a listed origin gets: its preflight answered with Access-Control-Allow-Origin, its request served with it, and
// the auth check still asked after it, while the origin check is never sent the client's authorization. An origin it
// refuses, or asked about which it answers 503 or not within the 1 s the gateway waits, gets the 403 Invalid CORS
// request a refused origin gets without the key, as text/plain with no header of the check's, never the check's own
// answer; a listed origin, an origin a pattern covers, an origin on one of the environment's own hosts, which is
// admitted without the key, and a request with no Origin are never put to the check, which would refuse them; and an
// environment that writes no ask_unlisted refuses an origin no list holds as before.
var unlisted = scenario{name: "an origin no list holds",
exchanges: []exchange{
{name: "a preflight from an origin the check admits", keys: unlistedKeys,
send: request{proto: "http/1.1", method: "OPTIONS", host: "api.example.com", path: "/products/1",
headers: map[string]string{"origin": checkAdmits, "access-control-request-method": "GET"}},
want: answer{status: 200, headers: map[string]string{"access-control-allow-origin": checkAdmits}}},
{name: "a request from an origin the check admits", keys: unlistedKeys,
send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1",
headers: map[string]string{"origin": checkAdmits}},
want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": checkAdmits},
received: map[string]string{"origin": checkAdmits}}},
{name: "a request over HTTP/2 from an origin the check admits", keys: unlistedKeys,
send: request{proto: "h2", method: "GET", host: "api.example.com", path: "/products/1",
headers: map[string]string{"origin": checkAdmits}},
want: answer{status: 200, service: "shop", headers: map[string]string{"access-control-allow-origin": checkAdmits}}},
{name: "a preflight from an origin the check refuses", keys: unlistedKeys,
send: request{proto: "http/1.1", method: "OPTIONS", host: "api.example.com", path: "/products/1",
headers: map[string]string{"origin": checkRefuses, "access-control-request-method": "GET"}},
want: answer{status: 403, headers: refusedOrigin, ownBody: refusedOriginBody}},
{name: "a request from an origin the check refuses", keys: unlistedKeys,
send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1",
headers: map[string]string{"origin": checkRefuses}},
want: answer{status: 403, headers: refusedOrigin, ownBody: refusedOriginBody}},
// A check that fails admits no origin: a 5xx, and no answer within the 1 s the gateway waits, refuse the origin
// as the check's refusal does, with none of the failed check's headers. The late check would have answered 200,
// after the stub's hang of 3 s, so the answer comes at the gateway's 1 s, not at the check's.
{name: "a request from an origin whose check answers 503", keys: unlistedKeys,
send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1",
headers: map[string]string{"origin": checkDown}},
want: answer{status: 403, headers: refusedOrigin, ownBody: refusedOriginBody}},
{name: "a request from an origin whose check answers after the gateway stops waiting", keys: unlistedKeys,
send: request{proto: "http/1.1", method: "GET", host: "api.example.com", path: "/products/1",
headers: map[string]string{"origin": checkLate}},
want: answer{status: 403, headers: refusedOrigin, ownBody: refusedOriginBody,
atLeast: 950 * time.Millisecond, below: 2950 * time.Millisecond}},
{name: "a preflight from a listed origin is not put to the check",
keys: slices.Concat(unlistedKeys, []string{"origins.<environment name>.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"}}},
{name: "a request from a listed origin is not put to the check",
keys: slices.Concat(unlistedKeys, []string{"origins.<environment name>.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"}}},
// 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.<environment name>.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",
keys: slices.Concat(unlistedKeys, []string{"origins.<environment name>.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"}}},
{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": ""}}},
{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}}},
// 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}}},
{name: "an origin the check admits, to an environment that asks no check",
keys: slices.Concat(unlistedKeys, []string{"origins.<environment name>.hosts"}),
send: fromOrigin(checkAdmits),
want: answer{status: 403, headers: refusedOrigin, ownBody: refusedOriginBody}},
}}

// 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.<environment name>.ask_unlisted", "origins.<environment name>.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.
const (
checkAdmits = "https://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,
// with no Access-Control-Allow-Origin and none of the origin check's own headers, and refusedOriginBody its body, which
// the Envoy output's listener writes (listener.yaml).
var refusedOrigin = map[string]string{"access-control-allow-origin": "", "content-type": "text/plain", "x-origin-check": ""}

const refusedOriginBody = "Invalid CORS request"

// auth: every value measured on Envoy 1.40.2. The two denial rows assert that no x-envoy-upstream-service-time comes
// with the check's answer: Envoy's client for the check adds it, and the header mutation filter removes it (without the
// removal both rows got "0"). A header that is absent is absent for every proxy. Envoy also answers, and no row
Expand Down
2 changes: 2 additions & 0 deletions conformance/stub/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (b *backend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b.grpc(w, r)
case b.service == "accounts" && r.URL.Path == "/auth/check":
b.check(w, r)
case b.service == "accounts" && r.URL.Path == "/auth/origin":
originCheck(w, r, b.hang)
default:
b.reply(w, r)
}
Expand Down
34 changes: 34 additions & 0 deletions conformance/stub/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ 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.<environment name>.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
// 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
// no such origin is admitted here, and a row that sends the admitted origin with an authorization header fails when
// the proxy passes the client's credential on to this check, which is asked about an origin, not a user.
//
// Two origins stand for a check that fails, which must admit no origin either: https://down.example gets 503, and
// https://late.example gets 200 only after hang, past the 1 s the gateway waits for this check. Each answer carries
// x-origin-check too, so a row also fails when the proxy passes on a header of the failed check's.
func originCheck(w http.ResponseWriter, r *http.Request, hang time.Duration) {
switch origin := r.Header.Get("origin"); {
case origin == "https://down.example":
w.Header().Set("content-type", "application/json")
w.Header().Set("x-origin-check", "down")
w.WriteHeader(http.StatusServiceUnavailable)
io.WriteString(w, `{"down": "the check"}`)
case origin == "https://late.example":
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" &&
r.Header.Values("authorization") == nil:
w.WriteHeader(http.StatusOK)
default:
w.Header().Set("content-type", "application/json")
w.Header().Set("x-origin-check", "refused")
w.WriteHeader(http.StatusUnauthorized)
io.WriteString(w, `{"refused": "the origin"}`)
}
}

// allowed sets the headers of the check's allowing answer: two the full example's auth.accept admits, x-other, which
// it does not, so the proxy must not pass it on to the service, and x-user-check set to report, a scenario reading it
// back from the service's echo.
Expand Down
6 changes: 4 additions & 2 deletions conformance/stub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ func serve(args []string) {
instance := flags.String("instance", "", "the instance the echo names, such as shop-1")
ports := flags.String("ports", "", "the ports to answer on, separated by commas")
state := flags.String("state", "/state", "the folder every backend shares, which remembers each x-stub-fail-first key")
// 3s is twice the full example's auth.timeout of 1500ms, so the proxy gives up on a check that hangs.
hang := flags.Duration("hang", 3*time.Second, "how long the auth check waits before it answers the token hang")
// 3s is twice the full example's auth.timeout of 1500ms, and three times the 1s the gateway waits for an origin
// check, so the proxy gives up on a check that hangs.
hang := flags.Duration("hang", 3*time.Second,
"how long the auth check waits before it answers the token hang, and the origin check the origin https://late.example")
flags.Parse(args)
if *service == "" || *instance == "" || *ports == "" || flags.NArg() > 0 {
usageError("serve needs -service, -instance and -ports, and nothing after them")
Expand Down
5 changes: 5 additions & 0 deletions conformance/targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ var (
// nginx entry and each HAProxy entry run; it runs the rows of the scheme the check is told alone, as no other row is
// there for plain HTTP under connection.
connectionFile = file{label: "connection", proxyfile: "testdata/connection.yaml", scenarios: []string{toldScheme.name}}
// unlistedFile is the battery's file with an environment that puts a browser origin no list holds to a check of its
// own, which only Envoy's entry runs, as the nginx and HAProxy generators refuse the key; it runs the rows of that
// scenario alone.
unlistedFile = file{label: "unlisted-origins", proxyfile: "testdata/unlisted-origins.yaml", scenarios: []string{unlisted.name}}
)

// haproxyLimitsFile is the battery's file for HAProxy's connection limit, which Envoy's entry and each HAProxy entry
Expand Down Expand Up @@ -110,6 +114,7 @@ func targets(t testing.TB) []target {
tracingByNameFile,
limitsFile,
connectionFile,
unlistedFile,
},
image: image,
// Envoy writes its log lines to stdout every 10 s by default; every 100 ms, a row that reads them (answer.logged)
Expand Down
Loading