Skip to content

Conversation

@yuval-k
Copy link
Contributor

@yuval-k yuval-k commented Nov 26, 2025

Fixes #12940

Description

Early header mutations

Change Type

/kind feature

Changelog

Add earlyRequestHeaderModifier to HTTPListenerPolicy. this allows performing header modifications before a route is selected.

Signed-off-by: Yuval Kohavi <[email protected]>
@gateway-bot gateway-bot added kind/feature Categorizes issue or PR as related to a new feature. release-note labels Nov 26, 2025
Signed-off-by: Yuval Kohavi <[email protected]>
@yuval-k yuval-k marked this pull request as ready for review November 26, 2025 20:51
Copilot AI review requested due to automatic review settings November 26, 2025 20:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for early request header modification to the HTTPListenerPolicy resource, enabling header mutations to be applied before route selection occurs. This feature is particularly useful for security scenarios, such as ensuring that authentication headers cannot be spoofed by clients and are only added by authorized systems like ExternalAuth.

Key Changes

  • Added earlyRequestHeaderModifier field to HTTPListenerPolicy API that accepts standard Gateway API HTTPHeaderFilter spec (add/set/remove operations)
  • Introduced shared ConvertMutations utility function to eliminate code duplication between listener and route-level header mutation logic
  • Configured Envoy's earlyHeaderMutationExtensions in the HttpConnectionManager to apply mutations before routing

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
api/v1alpha1/http_listener_policy_types.go Added EarlyRequestHeaderModifier field to API spec
install/helm/kgateway-crds/templates/gateway.kgateway.dev_httplistenerpolicies.yaml Added CRD schema for early request header modifier field
api/v1alpha1/zz_generated.deepcopy.go Generated deepcopy code for new field
internal/kgateway/extensions2/pluginutils/headers.go New shared utility to convert Gateway API header filters to Envoy mutations
internal/kgateway/extensions2/plugins/trafficpolicy/header_modifiers.go Refactored to use shared conversion utility
internal/kgateway/extensions2/plugins/httplistenerpolicy/httplistener_plugin.go Implemented early header mutation support in plugin
internal/kgateway/extensions2/plugins/httplistenerpolicy/merge.go Added merge logic for early header mutation extensions
test/e2e/features/http_listener_policy/types.go Added manifest path for E2E test
test/e2e/features/http_listener_policy/suite.go Added E2E test verifying route matching with early-added headers
test/e2e/features/http_listener_policy/testdata/http-listener-policy-early-header-route-match.yaml Test manifest with route that requires header added by policy
internal/kgateway/translator/gateway/gateway_translator_test.go Added translator test case for early header mutations
internal/kgateway/translator/gateway/testutils/inputs/httplistenerpolicy/early-header-mutation.yaml Input test data with add/set/remove operations
internal/kgateway/translator/gateway/testutils/outputs/httplistenerpolicy/early-header-mutation.yaml Expected Envoy configuration output

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


// EarlyRequestHeaderModifier defines header modifications to be applied early in the request processing,
// before route selection.
// For example, if you use ExternalAuthz to add a header, you may want to remove it here, to make
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

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

The documentation mentions "ExternalAuthz" but the correct term appears to be "ExternalAuth" based on the comment. Consider using consistent terminology.

Suggested change
// For example, if you use ExternalAuthz to add a header, you may want to remove it here, to make
// For example, if you use ExternalAuth to add a header, you may want to remove it here, to make

Copilot uses AI. Check for mistakes.
Signed-off-by: Yuval Kohavi <[email protected]>
@yuval-k yuval-k enabled auto-merge November 27, 2025 02:24
@yuval-k yuval-k force-pushed the yuval-k/early-header-mutations branch from 9579eea to 325adc1 Compare December 1, 2025 20:33
Signed-off-by: Yuval Kohavi <[email protected]>
@yuval-k yuval-k force-pushed the yuval-k/early-header-mutations branch from 325adc1 to a06c617 Compare December 1, 2025 20:33
Copy link
Contributor

@lgadban lgadban left a comment

Choose a reason for hiding this comment

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

Just a couple of relatively minor things.
Would be nice to have a better API name but I can't really come up with anything.
LGTM otherwise though.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's borderline nit, but given the primary usecase we care about is sanitizing headers, would prefer a test that removes a header from the incoming request

Comment on lines +130 to +131
// For example, if you use ExternalAuthz to add a header, you may want to remove it here, to make
// sure it did not come from the client.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// For example, if you use ExternalAuthz to add a header, you may want to remove it here, to make
// sure it did not come from the client.
// For example, if your external auth service adds a header, you should sanitize that header by removing it here,
// to make sure it did not come from the client.

Copy link
Contributor

Choose a reason for hiding this comment

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

Really just trying to get 'sanitize' somewhere in here.

@yuval-k yuval-k added this pull request to the merge queue Dec 2, 2025
Merged via the queue into main with commit c07d80d Dec 2, 2025
30 checks passed
@yuval-k yuval-k deleted the yuval-k/early-header-mutations branch December 2, 2025 13:30
sheidkamp pushed a commit to sheidkamp/kgateway that referenced this pull request Dec 3, 2025
howardjohn pushed a commit to howardjohn/kgateway that referenced this pull request Dec 5, 2025
Signed-off-by: Yuval Kohavi <[email protected]>
Signed-off-by: John Howard <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature Categorizes issue or PR as related to a new feature. release-note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Early sanitization of incoming headers

5 participants