From ccbcf6ec3bc617e173d929dbeca72b9a8e31d259 Mon Sep 17 00:00:00 2001 From: Mudit Ameta Date: Thu, 12 Nov 2026 11:17:42 +0100 Subject: [PATCH] CLP-18389: make ImmediateResponseWriter.AppendHeader append in Envoy ImmediateResponseWriter.AppendHeader did not append. When a filter added the same header more than once to an immediate response, for example two set-cookie headers on a redirect, Envoy kept only the last one. Why: AppendHeader set only append_action to APPEND_IF_EXISTS_OR_ADD on the HeaderValueOption. That is the enum's zero value, so in proto3 it is never sent and Envoy cannot tell it apart from "not set". Envoy's ext_proc then falls back to the deprecated append field, which the HeaderMutation docs say "defaults to false when used in this message", and false means overwrite. So every header but the last with that name was dropped. CommonResponseWriter already handles this by also setting the deprecated Append field to true for APPEND_IF_EXISTS_OR_ADD. ImmediateResponseWriter now does the same. SetHeader is unchanged and still overwrites. How it was found: the China data consent redirect in ingress-sidecar sends a 302 with two set-cookie headers, one setting gyg_cn_data_consent_version and one expiring gyg_cn_data_consent. Its integration test, which runs through a real Envoy (istio proxyv2 1.31.0), only saw the second cookie. In production, users who consented would have had no version cookie and would have been sent back to the consent page in a loop. ingress-sidecar works around this for now with withEnvoyAppend in internal/pkg/filters/redirects/redirects.go, which can be removed once it uses a version of this module with this fix. The new test checks that AppendHeader sets Append to true and that SetHeader leaves it unset. The nolint:staticcheck lines are needed because Append is deprecated, but it is still the only way to make Envoy's ext_proc append. Co-Authored-By: Claude Opus 5.5 --- filter/response.go | 6 ++++++ filter/response_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 filter/response_test.go diff --git a/filter/response.go b/filter/response.go index f733bcc..b913585 100644 --- a/filter/response.go +++ b/filter/response.go @@ -142,6 +142,11 @@ func NewImmediateResponseBuilder() *ImmediateResponseWriter { // headerAction sets a header with the given key and value and the given append action func (irw *ImmediateResponseWriter) headerAction(key string, value string, appendAction corev3.HeaderValueOption_HeaderAppendAction) *ImmediateResponseWriter { + var shouldAppend *wrappers.BoolValue + if appendAction == corev3.HeaderValueOption_APPEND_IF_EXISTS_OR_ADD { + shouldAppend = &wrappers.BoolValue{Value: true} // FIXME: This is not the documented behavior but it seems to be the only way to append a header. + } + //nolint:staticcheck // Envoy still requires Append=true to preserve append semantics. irw.immediateResponse.ImmediateResponse.Headers.SetHeaders = append(irw.immediateResponse.ImmediateResponse.Headers.SetHeaders, &corev3.HeaderValueOption{ Header: &corev3.HeaderValue{ Key: key, @@ -152,6 +157,7 @@ func (irw *ImmediateResponseWriter) headerAction(key string, value string, appen RawValue: []byte(value), // FIXME: This depends on Envoy }, AppendAction: appendAction, + Append: shouldAppend, }) return irw } diff --git a/filter/response_test.go b/filter/response_test.go new file mode 100644 index 0000000..75b3c46 --- /dev/null +++ b/filter/response_test.go @@ -0,0 +1,39 @@ +package filter_test + +import ( + "testing" + + "github.com/getyourguide/extproc-go/filter" + "github.com/stretchr/testify/require" +) + +// Envoy's ext_proc only appends a header when the deprecated append field is true. When it is unset, +// Envoy overwrites, whatever append_action says, so every header but the last with that name is lost. +func TestImmediateResponseHeaderAppend(t *testing.T) { + for _, tt := range []struct { + name string + build func(irw *filter.ImmediateResponseWriter) *filter.ImmediateResponseWriter + wantAppend []bool + }{{ + name: "append header sets append", + build: func(irw *filter.ImmediateResponseWriter) *filter.ImmediateResponseWriter { + return irw.AppendHeader("set-cookie", "a=1").AppendHeader("set-cookie", "b=2") + }, + wantAppend: []bool{true, true}, + }, { + name: "set header does not set append", + build: func(irw *filter.ImmediateResponseWriter) *filter.ImmediateResponseWriter { + return irw.SetHeader("location", "/a") + }, + wantAppend: []bool{false}, + }} { + t.Run(tt.name, func(t *testing.T) { + res := tt.build(filter.NewImmediateResponseBuilder()).ImmediateResponse() + var gotAppend []bool + for _, h := range res.ImmediateResponse.GetHeaders().GetSetHeaders() { + gotAppend = append(gotAppend, h.GetAppend().GetValue()) //nolint:staticcheck // Append is what Envoy reads, see above. + } + require.Equal(t, tt.wantAppend, gotAppend) + }) + } +}