Skip to content

Commit eb1feaf

Browse files
committed
api: add CSRF origin validation to SecurityPolicy
Add native CSRF protection to SecurityPolicy following the CORS pattern. The CSRF filter validates the Origin header on mutating requests (POST, PUT, DELETE, PATCH) against the destination and additional allowed origins. API: SecurityPolicy.csrf with additionalOrigins []StringMatch IR: CSRF struct in SecurityFeatures xDS: csrf.go implementing httpFilter interface (disabled at HCM 0%, enabled per-route 100%) Fixes #8835 Signed-off-by: asalvador <asalvador@newrelic.com>
1 parent cab6837 commit eb1feaf

39 files changed

Lines changed: 986 additions & 874 deletions

.github/workflows/build_and_test.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,6 @@ jobs:
181181
ipFamily: ipv4
182182
profile: xds-name-scheme-v2
183183
gwapiChannel: experimental
184-
- version: v1.35.0
185-
ipFamily: ipv4
186-
profile: watch-namespaces
187-
gwapiChannel: experimental
188184
steps:
189185
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
190186
- uses: ./tools/github-actions/setup-deps
@@ -241,9 +237,6 @@ jobs:
241237
- version: v1.35.0
242238
ipFamily: ipv4
243239
profile: xds-name-scheme-v2
244-
- version: v1.35.0
245-
ipFamily: ipv4
246-
profile: watch-namespaces
247240

248241
steps:
249242
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

.github/workflows/experimental_conformance.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ jobs:
4545
# only run dual test on latest version to save time
4646
ipFamily: dual
4747
profile: gateway-namespace-mode
48-
- version: v1.35.0
49-
ipFamily: ipv4
50-
profile: watch-namespaces
5148
steps:
5249
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
5350
- uses: ./tools/github-actions/setup-deps

api/v1alpha1/csrf_types.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright Envoy Gateway Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
// The full text of the Apache license is available in the LICENSE file at
4+
// the root of the repo.
5+
6+
package v1alpha1
7+
8+
// CSRF defines the configuration for the Cross-Site Request Forgery (CSRF) filter.
9+
// The CSRF filter checks that the Origin header in HTTP requests matches the destination,
10+
// preventing cross-origin mutating requests (POST, PUT, DELETE, PATCH) from being processed.
11+
// GET and HEAD requests are always allowed.
12+
type CSRF struct {
13+
// AdditionalOrigins specifies additional origins that are allowed to make requests.
14+
// These are checked against the Origin header and if matched, the request is allowed.
15+
// Each origin can be an exact, prefix, suffix, or regex match using StringMatch.
16+
//
17+
// +optional
18+
AdditionalOrigins []StringMatch `json:"additionalOrigins,omitempty"`
19+
}

api/v1alpha1/fault_injection.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ type FaultInjectionAbort struct {
5353
// GrpcStatus specifies the GRPC status code to be returned
5454
//
5555
// +optional
56-
// +kubebuilder:validation:Minimum=0
57-
// +kubebuilder:validation:Maximum=16
5856
GrpcStatus *int32 `json:"grpcStatus,omitempty"`
5957

6058
// Percentage specifies the percentage of requests to be aborted. Default 100%, if set 0, no requests will be aborted. Accuracy to 0.0001%.

api/v1alpha1/securitypolicy_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ type SecurityPolicySpec struct {
7373
// +optional
7474
CORS *CORS `json:"cors,omitempty"`
7575

76+
// CSRF defines the configuration for Cross-Site Request Forgery (CSRF) protection.
77+
// When enabled, the CSRF filter checks that the Origin header matches the destination
78+
// or one of the additional allowed origins on mutating requests (POST, PUT, DELETE, PATCH).
79+
//
80+
// +optional
81+
CSRF *CSRF `json:"csrf,omitempty"`
82+
7683
// BasicAuth defines the configuration for the HTTP Basic Authentication.
7784
//
7885
// +optional

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,6 @@ spec:
349349
description: GrpcStatus specifies the GRPC status code to
350350
be returned
351351
format: int32
352-
maximum: 16
353-
minimum: 0
354352
type: integer
355353
httpStatus:
356354
description: StatusCode specifies the HTTP status code to

charts/gateway-crds-helm/templates/generated/gateway.envoyproxy.io_securitypolicies.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,43 @@ spec:
618618
pattern: ^([0-9]{1,5}(h|m|s|ms)){1,4}$
619619
type: string
620620
type: object
621+
csrf:
622+
description: |-
623+
CSRF defines the configuration for Cross-Site Request Forgery (CSRF) protection.
624+
When enabled, the CSRF filter checks that the Origin header matches the destination
625+
or one of the additional allowed origins on mutating requests (POST, PUT, DELETE, PATCH).
626+
properties:
627+
additionalOrigins:
628+
description: |-
629+
AdditionalOrigins specifies additional origins that are allowed to make requests.
630+
These are checked against the Origin header and if matched, the request is allowed.
631+
Each origin can be an exact, prefix, suffix, or regex match using StringMatch.
632+
items:
633+
description: |-
634+
StringMatch defines how to match any strings.
635+
This is a general purpose match condition that can be used by other EG APIs
636+
that need to match against a string.
637+
properties:
638+
type:
639+
default: Exact
640+
description: Type specifies how to match against a string.
641+
enum:
642+
- Exact
643+
- Prefix
644+
- Suffix
645+
- RegularExpression
646+
type: string
647+
value:
648+
description: Value specifies the string value that the match
649+
must have.
650+
maxLength: 1024
651+
minLength: 1
652+
type: string
653+
required:
654+
- value
655+
type: object
656+
type: array
657+
type: object
621658
extAuth:
622659
description: ExtAuth defines the configuration for External Authorization.
623660
properties:

charts/gateway-helm/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ helm uninstall eg -n envoy-gateway-system
8888
| deployment.pod.affinity | object | `{}` | |
8989
| deployment.pod.annotations."prometheus.io/port" | string | `"19001"` | |
9090
| deployment.pod.annotations."prometheus.io/scrape" | string | `"true"` | |
91-
| deployment.pod.extraVolumeMounts | list | `[]` | |
92-
| deployment.pod.extraVolumes | list | `[]` | |
9391
| deployment.pod.labels | object | `{}` | |
9492
| deployment.pod.nodeSelector | object | `{}` | |
9593
| deployment.pod.tolerations | list | `[]` | |

charts/gateway-helm/charts/crds/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ spec:
348348
description: GrpcStatus specifies the GRPC status code to
349349
be returned
350350
format: int32
351-
maximum: 16
352-
minimum: 0
353351
type: integer
354352
httpStatus:
355353
description: StatusCode specifies the HTTP status code to

0 commit comments

Comments
 (0)