Skip to content

Add CSRF origin validation to SecurityPolicy #8835

Closed

Description

@albsga4

What type of feature is this?

Add native CSRF (Cross-Site Request Forgery) origin validation to SecurityPolicy, following the same pattern as the existing CORS support.

What this feature does / why we need it:

Envoy has a built-in envoy.filters.http.csrf filter that validates the Origin header against allowed origins on mutating requests (POST, PUT, DELETE, PATCH). Currently, there's no way to configure this filter through Envoy Gateway without using EnvoyPatchPolicy.

This is a common production requirement — in our case, 74% of routes (1,395 out of 1,890) need CSRF with origin suffix matching.

Proposed API:

apiVersion: gateway.envoyproxy.io/v1alpha1
kind: SecurityPolicy
spec:
  targetRefs:
    - group: gateway.networking.k8s.io
      kind: HTTPRoute
      name: my-route
  csrf:
    additionalOrigins:
      - type: Suffix
        value: newrelic.com
      - type: Exact
        value: https://example.com

Implementation approach:

The CSRF filter follows the exact same pattern as CORS in Envoy Gateway:

  • API: SecurityPolicy.csrf with additionalOrigins []StringMatch (reuses existing StringMatch type)
  • IR: CSRF struct in SecurityFeatures with AdditionalOrigins []*StringMatch
  • xDS: New csrf.go implementing the httpFilter interface — empty filter at HCM level, per-route CsrfPolicy via typed_per_filter_config

Envoy's CsrfPolicy proto supports:

  • filter_enabled — percentage of requests to check (default 100%)
  • additional_origins — StringMatcher list for allowed origins beyond the destination
  • shadow_enabled — audit mode without blocking

The initial implementation would expose additionalOrigins with full StringMatch support (Exact, Prefix, Suffix, RegularExpression). filter_enabled and shadow_enabled can be added later.

Behavior:

  • CSRF only checks mutating methods (POST, PUT, DELETE, PATCH) — GET/HEAD always pass
  • The Origin header is matched against the request destination AND any additionalOrigins
  • Missing Origin header on mutating requests → 403
  • Non-matching Origin → 403

/cc @arkodg

Activity

  1. albsga4 commented on Apr 24, 2026

    @albsga4
    ContributorAuthor

    PR ready: #8836

    Follows the exact CORS pattern — disabled at HCM level, enabled per-route via typed_per_filter_config. Supports Exact/Prefix/Suffix/RegularExpression origin matching.

  2. added this to the Backlog milestone on Apr 26, 2026
  3. github-actions commented on May 26, 2026

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had activity in the last 30 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions