Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions api/v1alpha1/csrf_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package v1alpha1

import (
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
)

// CSRF defines the configuration for the Cross-Site Request Forgery (CSRF) filter.
// The CSRF filter checks that the Origin header in HTTP requests matches the destination,
// preventing cross-origin mutating requests (POST, PUT, DELETE, PATCH) from being processed.
// GET and HEAD requests are always allowed.
type CSRF struct {
// ShadowFraction represents the fraction of requests for which the CSRF policy is
// evaluated in shadow (dry-run) mode. For these requests, the filter records whether
// the request would have been allowed or rejected in the `csrf.request_valid` and
// `csrf.request_invalid` stats, but always lets the request through. The remaining
// requests are enforced, i.e. a mutating request with a missing or non-matching
// Origin header is rejected with a 403.
//
// Defaults to 0% (all requests are enforced) if not specified. Set it to 100% to
// dry run the filter, watch the stats to find origins that would be rejected, then
// lower it to roll enforcement out gradually.
//
// +optional
ShadowFraction *gwapiv1.Fraction `json:"shadowFraction,omitempty"`

// AdditionalOrigins specifies additional origins that are allowed to make mutating
// requests, beyond the destination origin. A request whose Origin header matches one
// of them is allowed. The value "*" allows any origin, which effectively disables
// origin validation.
//
// Note: Envoy's CSRF filter compares the host and port of the origin only, so the
// scheme is ignored: "https://www.example.com" and "http://www.example.com" are
// equivalent here, and both allow the request regardless of the scheme the client
// used.
//
// +optional
// +kubebuilder:validation:MaxItems=16
AdditionalOrigins []Origin `json:"additionalOrigins,omitempty"`
}
5 changes: 4 additions & 1 deletion api/v1alpha1/envoyproxy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ type FilterPosition struct {
}

// EnvoyFilter defines the type of Envoy HTTP filter.
// +kubebuilder:validation:Enum=envoy.filters.http.custom_response;envoy.filters.http.health_check;envoy.filters.http.fault;envoy.filters.http.cors;envoy.filters.http.header_mutation;envoy.filters.http.ext_authz;envoy.filters.http.api_key_auth;envoy.filters.http.basic_auth;envoy.filters.http.oauth2;envoy.filters.http.jwt_authn;envoy.filters.http.stateful_session;envoy.filters.http.buffer;envoy.filters.http.lua;envoy.filters.http.ext_proc;envoy.filters.http.wasm;envoy.filters.http.dynamic_modules;envoy.filters.http.geoip;envoy.filters.http.rbac;envoy.filters.http.local_ratelimit;envoy.filters.http.ratelimit;envoy.filters.http.bandwidth_limit;envoy.filters.http.grpc_web;envoy.filters.http.grpc_stats;envoy.filters.http.credential_injector;envoy.filters.http.compressor;envoy.filters.http.dynamic_forward_proxy
// +kubebuilder:validation:Enum=envoy.filters.http.custom_response;envoy.filters.http.health_check;envoy.filters.http.fault;envoy.filters.http.cors;envoy.filters.http.csrf;envoy.filters.http.header_mutation;envoy.filters.http.ext_authz;envoy.filters.http.api_key_auth;envoy.filters.http.basic_auth;envoy.filters.http.oauth2;envoy.filters.http.jwt_authn;envoy.filters.http.stateful_session;envoy.filters.http.buffer;envoy.filters.http.lua;envoy.filters.http.ext_proc;envoy.filters.http.wasm;envoy.filters.http.dynamic_modules;envoy.filters.http.geoip;envoy.filters.http.rbac;envoy.filters.http.local_ratelimit;envoy.filters.http.ratelimit;envoy.filters.http.bandwidth_limit;envoy.filters.http.grpc_web;envoy.filters.http.grpc_stats;envoy.filters.http.credential_injector;envoy.filters.http.compressor;envoy.filters.http.dynamic_forward_proxy
type EnvoyFilter string

const (
Expand All @@ -304,6 +304,9 @@ const (
// EnvoyFilterCORS defines the Envoy HTTP CORS filter.
EnvoyFilterCORS EnvoyFilter = "envoy.filters.http.cors"

// EnvoyFilterCSRF defines the Envoy HTTP CSRF filter.
EnvoyFilterCSRF EnvoyFilter = "envoy.filters.http.csrf"

// EnvoyFilterHeaderMutation defines the Envoy HTTP header mutation filter
EnvoyFilterHeaderMutation EnvoyFilter = "envoy.filters.http.header_mutation"

Expand Down
7 changes: 7 additions & 0 deletions api/v1alpha1/securitypolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ type SecurityPolicySpec struct {
// +optional
CORS *CORS `json:"cors,omitempty"`

// CSRF defines the configuration for Cross-Site Request Forgery (CSRF) protection.
// When enabled, the CSRF filter checks that the Origin header matches the destination
// or one of the additional allowed origins on mutating requests (POST, PUT, DELETE, PATCH).
//
// +optional
CSRF *CSRF `json:"csrf,omitempty"`

// BasicAuth defines the configuration for the HTTP Basic Authentication.
//
// +optional
Expand Down
30 changes: 30 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ spec:
- envoy.filters.http.health_check
- envoy.filters.http.fault
- envoy.filters.http.cors
- envoy.filters.http.csrf
- envoy.filters.http.header_mutation
- envoy.filters.http.ext_authz
- envoy.filters.http.api_key_auth
Expand Down Expand Up @@ -542,6 +543,7 @@ spec:
- envoy.filters.http.health_check
- envoy.filters.http.fault
- envoy.filters.http.cors
- envoy.filters.http.csrf
- envoy.filters.http.header_mutation
- envoy.filters.http.ext_authz
- envoy.filters.http.api_key_auth
Expand Down Expand Up @@ -572,6 +574,7 @@ spec:
- envoy.filters.http.health_check
- envoy.filters.http.fault
- envoy.filters.http.cors
- envoy.filters.http.csrf
- envoy.filters.http.header_mutation
- envoy.filters.http.ext_authz
- envoy.filters.http.api_key_auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,75 @@ spec:
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
csrf:
description: |-
CSRF defines the configuration for Cross-Site Request Forgery (CSRF) protection.
When enabled, the CSRF filter checks that the Origin header matches the destination
or one of the additional allowed origins on mutating requests (POST, PUT, DELETE, PATCH).
properties:
additionalOrigins:
description: |-
AdditionalOrigins specifies additional origins that are allowed to make mutating
requests, beyond the destination origin. A request whose Origin header matches one
of them is allowed. The value "*" allows any origin, which effectively disables
origin validation.

Note: Envoy's CSRF filter compares the host and port of the origin only, so the
scheme is ignored: "https://www.example.com" and "http://www.example.com" are
equivalent here, and both allow the request regardless of the scheme the client
used.
items:
description: |-
Origin is defined by the scheme (protocol), hostname (domain), and port of
the URL used to access it. The hostname can be "precise" which is just the
domain name or "wildcard" which is a domain name prefixed with a single
wildcard label such as "*.example.com".
In addition to that a single wildcard (with or without scheme) can be
configured to match any origin.

For example, the following are valid origins:
- https://foo.example.com
- https://*.example.com
- http://foo.example.com:8080
- http://*.example.com:8080
- https://*
- moz-extension://example.com
- foo://*.example.com:8080
maxLength: 253
minLength: 1
pattern: ^(\*|[A-Za-z][A-Za-z0-9+.-]*:\/\/(\*|(\*\.)?(([\w-]+\.?)+)?[\w-]+)(:\d{1,5})?)$
type: string
maxItems: 16
type: array
shadowFraction:
description: |-
ShadowFraction represents the fraction of requests for which the CSRF policy is
evaluated in shadow (dry-run) mode. For these requests, the filter records whether
the request would have been allowed or rejected in the `csrf.request_valid` and
`csrf.request_invalid` stats, but always lets the request through. The remaining
requests are enforced, i.e. a mutating request with a missing or non-matching
Origin header is rejected with a 403.

Defaults to 0% (all requests are enforced) if not specified. Set it to 100% to
dry run the filter, watch the stats to find origins that would be rejected, then
lower it to roll enforcement out gradually.
properties:
denominator:
default: 100
format: int32
minimum: 1
type: integer
numerator:
format: int32
minimum: 0
type: integer
required:
- numerator
type: object
x-kubernetes-validations:
- message: numerator must be less than or equal to denominator
rule: self.numerator <= self.denominator
type: object
extAuth:
description: ExtAuth defines the configuration for External Authorization.
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ spec:
- envoy.filters.http.health_check
- envoy.filters.http.fault
- envoy.filters.http.cors
- envoy.filters.http.csrf
- envoy.filters.http.header_mutation
- envoy.filters.http.ext_authz
- envoy.filters.http.api_key_auth
Expand Down Expand Up @@ -541,6 +542,7 @@ spec:
- envoy.filters.http.health_check
- envoy.filters.http.fault
- envoy.filters.http.cors
- envoy.filters.http.csrf
- envoy.filters.http.header_mutation
- envoy.filters.http.ext_authz
- envoy.filters.http.api_key_auth
Expand Down Expand Up @@ -571,6 +573,7 @@ spec:
- envoy.filters.http.health_check
- envoy.filters.http.fault
- envoy.filters.http.cors
- envoy.filters.http.csrf
- envoy.filters.http.header_mutation
- envoy.filters.http.ext_authz
- envoy.filters.http.api_key_auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,75 @@ spec:
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
type: string
type: object
csrf:
description: |-
CSRF defines the configuration for Cross-Site Request Forgery (CSRF) protection.
When enabled, the CSRF filter checks that the Origin header matches the destination
or one of the additional allowed origins on mutating requests (POST, PUT, DELETE, PATCH).
properties:
additionalOrigins:
description: |-
AdditionalOrigins specifies additional origins that are allowed to make mutating
requests, beyond the destination origin. A request whose Origin header matches one
of them is allowed. The value "*" allows any origin, which effectively disables
origin validation.

Note: Envoy's CSRF filter compares the host and port of the origin only, so the
scheme is ignored: "https://www.example.com" and "http://www.example.com" are
equivalent here, and both allow the request regardless of the scheme the client
used.
items:
description: |-
Origin is defined by the scheme (protocol), hostname (domain), and port of
the URL used to access it. The hostname can be "precise" which is just the
domain name or "wildcard" which is a domain name prefixed with a single
wildcard label such as "*.example.com".
In addition to that a single wildcard (with or without scheme) can be
configured to match any origin.

For example, the following are valid origins:
- https://foo.example.com
- https://*.example.com
- http://foo.example.com:8080
- http://*.example.com:8080
- https://*
- moz-extension://example.com
- foo://*.example.com:8080
maxLength: 253
minLength: 1
pattern: ^(\*|[A-Za-z][A-Za-z0-9+.-]*:\/\/(\*|(\*\.)?(([\w-]+\.?)+)?[\w-]+)(:\d{1,5})?)$
type: string
maxItems: 16
type: array
shadowFraction:
description: |-
ShadowFraction represents the fraction of requests for which the CSRF policy is
evaluated in shadow (dry-run) mode. For these requests, the filter records whether
the request would have been allowed or rejected in the `csrf.request_valid` and
`csrf.request_invalid` stats, but always lets the request through. The remaining
requests are enforced, i.e. a mutating request with a missing or non-matching
Origin header is rejected with a 403.

Defaults to 0% (all requests are enforced) if not specified. Set it to 100% to
dry run the filter, watch the stats to find origins that would be rejected, then
lower it to roll enforcement out gradually.
properties:
denominator:
default: 100
format: int32
minimum: 1
type: integer
numerator:
format: int32
minimum: 0
type: integer
required:
- numerator
type: object
x-kubernetes-validations:
- message: numerator must be less than or equal to denominator
rule: self.numerator <= self.denominator
type: object
extAuth:
description: ExtAuth defines the configuration for External Authorization.
properties:
Expand Down
Loading
Loading