Skip to content

feat(BackendTrafficPolicy): Support matching on response header for response override - #9354

Open
HusseinKabbout wants to merge 16 commits into
envoyproxy:mainfrom
HusseinKabbout:response-override-with-response-header-match
Open

feat(BackendTrafficPolicy): Support matching on response header for response override#9354
HusseinKabbout wants to merge 16 commits into
envoyproxy:mainfrom
HusseinKabbout:response-override-with-response-header-match

Conversation

@HusseinKabbout

@HusseinKabbout HusseinKabbout commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

This PR was created because we would like to be able to generate something similar to the following envoy native config:

    envoy.filters.http.custom_response:
      "@type": type.googleapis.com/envoy.extensions.filters.http.custom_response.v3.CustomResponse
      custom_response_matcher:
        matcher_list:
          matchers:
          # 1) error + Content-Type: application/json  -> JSON error page
          - predicate:
              and_matcher:
                predicate:
                - single_predicate:
                    input: { typed_config: { "@type": type.googleapis.com/envoy.type.matcher.v3.HttpResponseStatusCodeClassMatchInput } }
                    value_match: { exact: "5xx" }
                - single_predicate:
                    input: { typed_config: { "@type": type.googleapis.com/envoy.type.matcher.v3.HttpResponseHeaderMatchInput, header_name: content-type } }
                    value_match: { prefix: "application/json" }   # also matches "application/json; charset=utf-8" 
            on_match:
              action:
                typed_config:
                  "@type": type.googleapis.com/envoy.extensions.http.custom_response.local_response_policy.v3.LocalResponsePolicy
                  status_code: 503
                  body: { filename: "/var/www/errors/5xx.json" }
                  response_headers_to_add: [ { header: { key: content-type, value: "application/json" } } ]
          # 2) error + Content-Type: text/html -> HTML error page 
          - predicate:
              and_matcher:
                predicate:
                - single_predicate:
                    input: { typed_config: { "@type": type.googleapis.com/envoy.type.matcher.v3.HttpResponseStatusCodeClassMatchInput } }
                    value_match: { exact: "5xx" }
                - single_predicate:
                    input: { typed_config: { "@type": type.googleapis.com/envoy.type.matcher.v3.HttpResponseHeaderMatchInput, header_name: content-type } }
                    value_match: { prefix: "text/html" }
            on_match:
              action:
                typed_config:
                  "@type": type.googleapis.com/envoy.extensions.http.custom_response.local_response_policy.v3.LocalResponsePolicy
                  status_code: 503
                  body: { filename: "/var/www/errors/5xx.html" }
                  response_headers_to_add: [ { header: { key: content-type, value: "text/html" } } ]
          # 3) catch-all (no/other Content-Type) -> TEXT error page  ← your "default to text" 
          - predicate:
              single_predicate:
                input: { typed_config: { "@type": type.googleapis.com/envoy.type.matcher.v3.HttpResponseStatusCodeClassMatchInput } }
                value_match: { exact: "5xx" }
            on_match:
              action:
                typed_config:
                  "@type": type.googleapis.com/envoy.extensions.http.custom_response.local_response_policy.v3.LocalResponsePolicy
                  status_code: 503
                  body: { filename: "/var/www/errors/5xx.txt" }
                  response_headers_to_add: [ { header: { key: content-type, value: "text/plain" } } ]

#8608 and #8856 were created in the past but they talk about supporting request header matching, which envoy does not support (yet ;D). What would be nice for now is support the response header matching, which envoy currently supports, in envoy gateway. In the future, request headers can be easily added since they would be very similar to the response header implementation.

The main use case for this is custom error pages which should return different response bodies based on the response content-type (see example from above).


PR Checklist

  • Authorship & ownership: Coding agents / AI assistants are welcome, but I have reviewed every change, understand how and why it works, can explain and maintain it, and take full responsibility for this PR. I have not submitted generated output I do not understand.
  • DCO: All commits are signed off (git commit -s). See DCO: Sign your work.
  • API agreed first: If this PR contains API changes (changes under /api), the API was discussed and agreed before the implementation. The API change can be in a separate PR, or in the same PR, but the API must be agreed before implementation. N/A if this PR does not contain API changes.
  • Required checks pass: make generate gen-check, make lint, and the unit-test/coverage build pass. (Flaky e2e failures are not considered breakages, but gen-check, lint, and coverage MUST pass.)
  • Tests added/updated: New/changed code is covered by appropriate tests. N/A if this PR does not contain code changes.
  • Docs: User-facing changes update the docs, either in this PR or a follow-up PR. N/A if this PR does not contain user-facing changes.
  • Release notes: For any non-trivial change, added a release-note fragment under release-notes/current/<section>/<pr-number>-<slug>.md (see release-notes/current/README.md for sections and naming). N/A if this PR does not contain non-trivial changes.
  • Generated files committed: Ran make gen-check and committed the result if API/helm charts/modules changed.
  • Scope & compatibility: The PR is reasonably scoped (no unrelated changes) and preserves backward compatibility, or any breaking change is called out above and documented in release-notes/current/breaking_changes/.
  • Codex review: Requested a Codex review and addressed all of its comments.
  • Copilot review: Requested a Copilot review and addressed all of its comments.

@HusseinKabbout
HusseinKabbout requested a review from a team as a code owner June 25, 2026 10:53
@netlify

netlify Bot commented Jun 25, 2026

Copy link
Copy Markdown

Deploy Preview for cerulean-figolla-1f9435 ready!

Name Link
🔨 Latest commit 03593d0
🔍 Latest deploy log https://app.netlify.com/projects/cerulean-figolla-1f9435/deploys/6a73fa0c47384e0008047035
😎 Deploy Preview https://deploy-preview-9354--cerulean-figolla-1f9435.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@HusseinKabbout
HusseinKabbout force-pushed the response-override-with-response-header-match branch from e2cc8fa to e55ccd8 Compare June 25, 2026 10:53

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e2cc8fa982

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread api/v1alpha1/shared_types.go Outdated
@HusseinKabbout

Copy link
Copy Markdown
Contributor Author

Matching on request headers was merged and released in Envoy 1.39. Once this is merged, I can implement also the request headers part.

@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 20 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.21%. Comparing base (3afe13e) to head (03593d0).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
internal/xds/translator/custom_response.go 81.48% 12 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9354      +/-   ##
==========================================
+ Coverage   76.18%   76.21%   +0.03%     
==========================================
  Files         261      261              
  Lines       43528    43606      +78     
==========================================
+ Hits        33162    33236      +74     
+ Misses       8165     8163       -2     
- Partials     2201     2207       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

kabbohus added 5 commits July 29, 2026 11:23
…nse headers in addition to response status codes

Signed-off-by: kabbohus <hussein.kabbout@united-security-providers.ch>
Signed-off-by: kabbohus <hussein.kabbout@united-security-providers.ch>
…nse headers

Signed-off-by: kabbohus <hussein.kabbout@united-security-providers.ch>
Signed-off-by: kabbohus <hussein.kabbout@united-security-providers.ch>
Signed-off-by: kabbohus <hussein.kabbout@united-security-providers.ch>
@zirain
zirain force-pushed the response-override-with-response-header-match branch from 6daffcc to c5fc326 Compare July 29, 2026 03:23
@zirain zirain added this to the v1.9.0-rc.1 Release milestone Aug 2, 2026
@zirain

zirain commented Aug 2, 2026

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3aba51d06b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread api/v1alpha1/shared_types.go Outdated
Comment thread api/v1alpha1/shared_types.go Outdated

@zhaohuabing zhaohuabing left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RegularExpression values are not validated → invalid regex reaches Envoy

buildResponseOverride (internal/gatewayapi/backendtrafficpolicy.go:2529) calls irStringMatch and never validates the pattern. Everywhere else in the translator, user-supplied regexes go through regex.Validate() — including the sibling code in the same file for header/path/query matches (backendtrafficpolicy.go:2126, :2181, :2240), plus route.go:1175, securitypolicy.go:2978. Envoy's RegexMatcher.regex PGV constraint is only min_bytes: 1, so proto.ToAnyWithValidation won't catch it either.

Comment thread internal/xds/translator/custom_response.go
Comment thread internal/gatewayapi/testdata/backendtrafficpolicy-with-response-override.out.yaml Outdated
Comment thread internal/ir/xds.go Outdated
kabbohus added 2 commits August 4, 2026 12:44
…redicates could be expressed in the CR

Signed-off-by: kabbohus <hussein.kabbout@united-security-providers.ch>

@zhaohuabing zhaohuabing left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@zhaohuabing
zhaohuabing requested a review from a team August 5, 2026 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants