|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package extproc_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "io" |
| 20 | + "testing" |
| 21 | + |
| 22 | + corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 23 | + extprocv3 "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" |
| 24 | + "google.golang.org/protobuf/types/known/structpb" |
| 25 | + |
| 26 | + "github.com/agent-substrate/substrate/cmd/atenet/internal/router/egress" |
| 27 | + "github.com/agent-substrate/substrate/cmd/atenet/internal/router/extproc" |
| 28 | +) |
| 29 | + |
| 30 | +const websocketDenialBody = "WebSocket egress is not supported" |
| 31 | + |
| 32 | +type websocketProcessStream struct { |
| 33 | + extprocv3.ExternalProcessor_ProcessServer |
| 34 | + ctx context.Context |
| 35 | + requests []*extprocv3.ProcessingRequest |
| 36 | + responses []*extprocv3.ProcessingResponse |
| 37 | +} |
| 38 | + |
| 39 | +func (s *websocketProcessStream) Context() context.Context { return s.ctx } |
| 40 | + |
| 41 | +func (s *websocketProcessStream) Recv() (*extprocv3.ProcessingRequest, error) { |
| 42 | + if len(s.requests) == 0 { |
| 43 | + return nil, io.EOF |
| 44 | + } |
| 45 | + req := s.requests[0] |
| 46 | + s.requests = s.requests[1:] |
| 47 | + return req, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (s *websocketProcessStream) Send(resp *extprocv3.ProcessingResponse) error { |
| 51 | + s.responses = append(s.responses, resp) |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +func TestProcessReturnsWebSocketDenialAsImmediateResponse(t *testing.T) { |
| 56 | + for _, leg := range []string{extproc.EgressCleartextFilterChainName, extproc.EgressTLSMITMFilterChainName} { |
| 57 | + t.Run(leg, func(t *testing.T) { |
| 58 | + request := &extprocv3.ProcessingRequest{ |
| 59 | + Request: &extprocv3.ProcessingRequest_RequestHeaders{ |
| 60 | + RequestHeaders: &extprocv3.HttpHeaders{ |
| 61 | + Headers: &corev3.HeaderMap{Headers: []*corev3.HeaderValue{ |
| 62 | + {Key: ":method", RawValue: []byte("GET")}, |
| 63 | + {Key: ":authority", RawValue: []byte("api.example.com")}, |
| 64 | + {Key: "connection", RawValue: []byte("keep-alive, Upgrade")}, |
| 65 | + {Key: "upgrade", RawValue: []byte("websocket")}, |
| 66 | + }}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + Attributes: map[string]*structpb.Struct{ |
| 70 | + "envoy.filters.http.ext_proc": {Fields: map[string]*structpb.Value{ |
| 71 | + extproc.FilterChainNameAttribute: structpb.NewStringValue(leg), |
| 72 | + }}, |
| 73 | + }, |
| 74 | + } |
| 75 | + stream := &websocketProcessStream{ctx: context.Background(), requests: []*extprocv3.ProcessingRequest{request}} |
| 76 | + server := extproc.NewServer(0, nil, extproc.Handlers{ |
| 77 | + extproc.DirectionEgress: egress.New(nil, nil, 0, nil, ""), |
| 78 | + }) |
| 79 | + |
| 80 | + if err := server.Process(stream); err != nil { |
| 81 | + t.Fatalf("Process() error = %v", err) |
| 82 | + } |
| 83 | + if len(stream.responses) != 1 { |
| 84 | + t.Fatalf("responses = %d, want 1", len(stream.responses)) |
| 85 | + } |
| 86 | + response := stream.responses[0].GetImmediateResponse() |
| 87 | + if response == nil { |
| 88 | + t.Fatalf("response = %v, want immediate denial", stream.responses[0]) |
| 89 | + } |
| 90 | + if got := response.GetStatus().GetCode(); got != 403 { |
| 91 | + t.Errorf("status = %d, want 403", got) |
| 92 | + } |
| 93 | + if got := string(response.GetBody()); got != websocketDenialBody { |
| 94 | + t.Errorf("body = %q, want %q", got, websocketDenialBody) |
| 95 | + } |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
0 commit comments