Skip to content

Commit 1aaf6df

Browse files
authored
Add support for the SameSite sticky cookie attribute (#4001)
* Add support for the SameSite cookie attribute
1 parent 439ad79 commit 1aaf6df

15 files changed

+524
-24
lines changed

deployments/common/crds/k8s.nginx.org_virtualserverroutes.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ spec:
588588
type: string
589589
path:
590590
type: string
591+
samesite:
592+
type: string
591593
secure:
592594
type: boolean
593595
slow-start:

deployments/common/crds/k8s.nginx.org_virtualservers.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,8 @@ spec:
675675
type: string
676676
path:
677677
type: string
678+
samesite:
679+
type: string
678680
secure:
679681
type: boolean
680682
slow-start:

deployments/helm-chart/crds/k8s.nginx.org_virtualserverroutes.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ spec:
588588
type: string
589589
path:
590590
type: string
591+
samesite:
592+
type: string
591593
secure:
592594
type: boolean
593595
slow-start:

deployments/helm-chart/crds/k8s.nginx.org_virtualservers.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,8 @@ spec:
675675
type: string
676676
path:
677677
type: string
678+
samesite:
679+
type: string
678680
secure:
679681
type: boolean
680682
slow-start:

docs/content/configuration/virtualserver-and-virtualserverroute-resources.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ sessionCookie:
460460
domain: .example.com
461461
httpOnly: false
462462
secure: true
463+
samesite: strict
463464
```
464465
See the [`sticky`](https://nginx.org/en/docs/http/ngx_http_upstream_module.html?#sticky) directive for additional information. The session cookie corresponds to the `sticky cookie` method.
465466

@@ -475,6 +476,7 @@ Note: This feature is supported only in NGINX Plus.
475476
|``domain`` | The domain for which the cookie is set. | ``string`` | No |
476477
|``httpOnly`` | Adds the ``HttpOnly`` attribute to the cookie. | ``boolean`` | No |
477478
|``secure`` | Adds the ``Secure`` attribute to the cookie. | ``boolean`` | No |
479+
|``samesite`` | Adds the ``SameSite`` attribute to the cookie. The allowed values are: ``strict``, ``lax``, ``none`` | ``string`` | No |
478480
{{% /table %}}
479481

480482
### Header

internal/configs/version2/http.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ type SessionCookie struct {
283283
Domain string
284284
HTTPOnly bool
285285
Secure bool
286+
SameSite string
286287
}
287288

288289
// Distribution maps weight to a value in a SplitClient.

internal/configs/version2/nginx-plus.virtualserver.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ upstream {{ $u.Name }} {
1818

1919
{{ with $u.SessionCookie }}
2020
{{ if .Enable }}
21-
sticky cookie {{ .Name }}{{ if .Expires }} expires={{ .Expires }}{{ end }}{{ if .Domain }} domain={{ .Domain }}{{ end }}{{ if .HTTPOnly }} httponly{{ end }}{{ if .Secure }} secure{{ end }}{{ if .Path }} path={{ .Path }}{{ end }};
21+
sticky cookie {{ .Name }}{{ if .Expires }} expires={{ .Expires }}{{ end }}{{ if .Domain }} domain={{ .Domain }}{{ end }}{{ if .HTTPOnly }} httponly{{ end }}{{ if .SameSite}} samesite={{.SameSite | toLower }}{{ end }}{{ if .Secure }} secure{{ end }}{{ if .Path }} path={{ .Path }}{{ end }};
2222
{{ end }}
2323
{{ end }}
2424

internal/configs/version2/template_helper.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ func hasCIKey(key string, d map[string]string) bool {
2020
return ok
2121
}
2222

23+
// toLower takes a string and make it lowercase.
24+
//
25+
// Example:
26+
//
27+
// {{ if .SameSite}} samesite={{.SameSite | toLower }}{{ end }}
28+
func toLower(s string) string {
29+
return strings.ToLower(s)
30+
}
31+
2332
var helperFunctions = template.FuncMap{
2433
"headerListToCIMap": headerListToCIMap,
2534
"hasCIKey": hasCIKey,
35+
"toLower": toLower,
2636
}

0 commit comments

Comments
 (0)