|
| 1 | +// Copyright The OWASP Coraza contributors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "cmp" |
| 8 | + "crypto/tls" |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/magefile/mage/sh" |
| 16 | + "golang.org/x/net/http2" |
| 17 | +) |
| 18 | + |
| 19 | +// E2e runs e2e tests. Requires docker. |
| 20 | +func E2e() error { |
| 21 | + envoyHost := cmp.Or(os.Getenv("ENVOY_HOST"), "localhost:8080") |
| 22 | + httpbinHost := cmp.Or(os.Getenv("HTTPBIN_HOST"), "localhost:8081") |
| 23 | + |
| 24 | + if err := runCorazaE2e(envoyHost, httpbinHost); err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + |
| 28 | + if err := runHttpTrailerE2e(envoyHost); err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +// runCorazaE2e runs Coraza e2e tests with a built plugin against the example deployment |
| 35 | +func runCorazaE2e(envoyHost, httpbinHost string) error { |
| 36 | + dockerComposeFilePath := "e2e/coraza/docker-compose.yml" |
| 37 | + var err error |
| 38 | + if err = sh.RunV("docker", "compose", "--file", dockerComposeFilePath, "up", "-d", "envoy"); err != nil { |
| 39 | + sh.RunV("docker", "compose", "-f", dockerComposeFilePath, "logs", "envoy") |
| 40 | + return err |
| 41 | + } |
| 42 | + defer func() { |
| 43 | + _ = sh.RunV("docker", "compose", "--file", dockerComposeFilePath, "down", "-v") |
| 44 | + }() |
| 45 | + |
| 46 | + // --nulled-body is needed because coraza-proxy-wasm returns a 200 OK with a nulled body when if the interruption happens after phase 3 |
| 47 | + if err = sh.RunV("go", "run", "github.com/corazawaf/coraza/v3/http/e2e/cmd/httpe2e@main", "--proxy-hostport", |
| 48 | + "http://"+envoyHost, "--httpbin-hostport", "http://"+httpbinHost, "--nulled-body"); err != nil { |
| 49 | + sh.RunV("docker", "compose", "-f", dockerComposeFilePath, "logs", "envoy") |
| 50 | + } |
| 51 | + return err |
| 52 | +} |
| 53 | + |
| 54 | +// runHttpTrailerE2e runs HTTP trailer E2E tests |
| 55 | +// It is meant to check that HTTP2 request payloads with trailers are scanned at phase 2 before being sent to upstream. |
| 56 | +// This might happen because the end_of_stream parameter from OnHttpRequestBody is never set to true in HTTP2 if trailers |
| 57 | +// are available. In order to mitigate this, OnHttp[Request|Response]Trailers callbacks have been implemented as an enforcement |
| 58 | +// point of the body phase rules. |
| 59 | +// The test expects Coraza to enforce the interruption (403) during phase="http_request_body" and not phase="http_response_headers", |
| 60 | +// which would mean that the payload was sent to upstream before being scanned and was blocked on the way back. |
| 61 | +func runHttpTrailerE2e(envoyHost string) error { |
| 62 | + fmt.Printf("Running HTTP trailer test\n") |
| 63 | + dockerComposeFilePath := "e2e/http_trailer/docker-compose.yml" |
| 64 | + if err := sh.RunV("go", "run", "filippo.io/mkcert@v1.4.4", "-key-file", "e2e/http_trailer/server.key", |
| 65 | + "-cert-file", "e2e/http_trailer/server.crt", "example.com"); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + defer func() { |
| 69 | + _ = os.Remove("e2e/http_trailer/server.key") |
| 70 | + _ = os.Remove("e2e/http_trailer/server.crt") |
| 71 | + }() |
| 72 | + if err := sh.RunV("docker", "compose", "--file", dockerComposeFilePath, "up", "-d", "envoy"); err != nil { |
| 73 | + sh.RunV("docker", "compose", "-f", dockerComposeFilePath, "logs", "envoy") |
| 74 | + return err |
| 75 | + } |
| 76 | + defer func() { |
| 77 | + _ = sh.RunV("docker", "compose", "--file", dockerComposeFilePath, "down", "-v") |
| 78 | + }() |
| 79 | + |
| 80 | + client := &http.Client{ |
| 81 | + Transport: &http2.Transport{ |
| 82 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + // Wait for envoy to be ready |
| 87 | + ready := false |
| 88 | + for range 20 { |
| 89 | + resp, err := client.Get("https://" + envoyHost) |
| 90 | + if err == nil && resp.StatusCode == http.StatusOK { |
| 91 | + resp.Body.Close() |
| 92 | + ready = true |
| 93 | + break |
| 94 | + } |
| 95 | + time.Sleep(500 * time.Millisecond) |
| 96 | + fmt.Println("Waiting for Envoy to be ready...") |
| 97 | + } |
| 98 | + if !ready { |
| 99 | + sh.RunV("docker", "compose", "-f", dockerComposeFilePath, "logs", "envoy") |
| 100 | + return fmt.Errorf("timeout waiting for Envoy") |
| 101 | + } |
| 102 | + |
| 103 | + // Run the actual test. |
| 104 | + req, err := http.NewRequest("POST", "https://"+envoyHost, strings.NewReader("{\"foo\": \"<script foo>\"}")) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("creating request: %w", err) |
| 107 | + } |
| 108 | + req.Header.Set("Content-Type", "application/json") |
| 109 | + req.Trailer = http.Header{"Custom-Trailer": {"This is a custom trailer"}} |
| 110 | + |
| 111 | + resp, err := client.Do(req) |
| 112 | + if err != nil { |
| 113 | + sh.RunV("docker", "compose", "-f", dockerComposeFilePath, "logs", "envoy") |
| 114 | + return fmt.Errorf("sending request: %w", err) |
| 115 | + } |
| 116 | + defer resp.Body.Close() |
| 117 | + |
| 118 | + output, err := sh.Output("docker", "compose", "-f", dockerComposeFilePath, "logs", "envoy") |
| 119 | + if err != nil { |
| 120 | + return fmt.Errorf("getting envoy logs: %w", err) |
| 121 | + } |
| 122 | + |
| 123 | + // The request is expected to be blocked at phase 2, before reaching the backend. |
| 124 | + if resp.StatusCode != http.StatusForbidden { |
| 125 | + return fmt.Errorf("unexpected status code: got %d, want %d", resp.StatusCode, http.StatusForbidden) |
| 126 | + } |
| 127 | + if !strings.Contains(output, "phase=\"http_request_body\"") { |
| 128 | + return fmt.Errorf("expected phase=\"http_request_body\" in envoy logs transaction interrupted line, got:\n%s", output) |
| 129 | + } |
| 130 | + fmt.Printf("✅ HTTP trailer test passed\n") |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments