/
cookie.go
325 lines (267 loc) · 7.34 KB
/
cookie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
Package cookie implements filters to append to requests or responses.
It implements two filters, one for appending cookies to requests in
the "Cookie" header, and one for appending cookies to responses in the
"Set-Cookie" header.
Both the request and response cookies expect a name and a value argument.
The response cookie accepts an optional argument to control the max-age
property of the cookie, of type number, in seconds.
The response cookie accepts an optional fourth argument, "change-only",
to control if the cookie should be set on every response, or only if the
request doesn't contain a cookie with the provided name and value. If the
fourth argument is "change-only", and a cookie with the same name and value
is found in the request, the cookie is not set. This argument can be used
to disable sliding TTL of the cookie.
The JS cookie behaves exactly as the response cookie, but it doesn't
set the HttpOnly directive, so these cookies will be
accessible from JS code running in web browsers.
Examples:
requestCookie("test-session", "abc")
responseCookie("test-session", "abc", 31536000)
responseCookie("test-session", "abc", 31536000, "change-only")
// response cookie without HttpOnly:
jsCookie("test-session-info", "abc-debug", 31536000, "change-only")
*/
package cookie
import (
"net"
"net/http"
"strings"
"github.com/zalando/skipper/filters"
)
const (
// Deprecated, use filters.RequestCookieName instead
RequestCookieFilterName = filters.RequestCookieName
// Deprecated, use filters.ResponseCookieName instead
ResponseCookieFilterName = filters.ResponseCookieName
// Deprecated, use filters.JsCookieName instead
ResponseJSCookieFilterName = filters.JsCookieName
ChangeOnlyArg = "change-only"
SetCookieHttpHeader = "Set-Cookie"
)
type direction int
const (
request direction = iota
response
responseJS
)
type spec struct {
typ direction
filterName string
}
type filter struct {
typ direction
name string
value string
maxAge int
changeOnly bool
}
type dropCookie struct {
typ direction
name string
}
func NewDropRequestCookie() filters.Spec {
return &dropCookie{
typ: request,
}
}
func NewDropResponseCookie() filters.Spec {
return &dropCookie{
typ: response,
}
}
func (d *dropCookie) Name() string {
switch d.typ {
case request:
return filters.DropRequestCookieName
case response:
return filters.DropResponseCookieName
}
return "unknown"
}
func (d *dropCookie) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) != 1 {
return nil, filters.ErrInvalidFilterParameters
}
s, ok := args[0].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
return &dropCookie{
typ: d.typ,
name: s,
}, nil
}
func removeCookie(request *http.Request, name string) bool {
cookies := request.Cookies()
hasCookie := false
for _, c := range cookies {
if c.Name == name {
hasCookie = true
break
}
}
if hasCookie {
request.Header.Del("Cookie")
for _, c := range cookies {
if c.Name != name {
request.AddCookie(c)
}
}
}
return hasCookie
}
func removeCookieResponse(rsp *http.Response, name string) bool {
cookies := rsp.Cookies()
hasCookie := false
for _, c := range cookies {
if c.Name == name {
hasCookie = true
break
}
}
if hasCookie {
rsp.Header.Del("Set-Cookie")
for _, c := range cookies {
if c.Name != name {
rsp.Header.Add("Set-Cookie", c.String())
}
}
}
return hasCookie
}
func (d *dropCookie) Request(ctx filters.FilterContext) {
if d.typ != request {
return
}
removeCookie(ctx.Request(), d.name)
}
func (d *dropCookie) Response(ctx filters.FilterContext) {
if d.typ != response {
return
}
removeCookieResponse(ctx.Response(), d.name)
}
// Creates a filter spec for appending cookies to requests.
// Name: requestCookie
func NewRequestCookie() filters.Spec {
return &spec{request, filters.RequestCookieName}
}
// Creates a filter spec for appending cookies to responses.
// Name: responseCookie
func NewResponseCookie() filters.Spec {
return &spec{response, filters.ResponseCookieName}
}
// Creates a filter spec for appending cookies to responses without the
// HttpOnly directive.
// Name: jsCookie
func NewJSCookie() filters.Spec {
return &spec{responseJS, filters.JsCookieName}
}
func (s *spec) Name() string { return s.filterName }
func (s *spec) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) < 2 || (len(args) > 2 && s.typ == request) || len(args) > 4 {
return nil, filters.ErrInvalidFilterParameters
}
f := &filter{typ: s.typ}
if name, ok := args[0].(string); ok && name != "" {
f.name = name
} else {
return nil, filters.ErrInvalidFilterParameters
}
if value, ok := args[1].(string); ok {
f.value = value
} else {
return nil, filters.ErrInvalidFilterParameters
}
if len(args) >= 3 {
if maxAge, ok := args[2].(float64); ok {
// https://pkg.go.dev/net/http#Cookie uses zero to omit Max-Age attribute:
// > MaxAge=0 means no 'Max-Age' attribute specified.
// > MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// > MaxAge>0 means Max-Age attribute present and given in seconds
//
// Here we know user specified Max-Age explicitly, so we interpret zero
// as a signal to delete the cookie similar to what user would expect naturally,
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#max-agenumber
// > A zero or negative number will expire the cookie immediately.
if maxAge == 0 {
f.maxAge = -1
} else {
f.maxAge = int(maxAge)
}
} else {
return nil, filters.ErrInvalidFilterParameters
}
}
if len(args) == 4 {
f.changeOnly = args[3] == ChangeOnlyArg
}
return f, nil
}
func (f *filter) Request(ctx filters.FilterContext) {
if f.typ != request {
return
}
ctx.StateBag()["CookieSet:"+f.name] = f.value
ctx.Request().AddCookie(&http.Cookie{Name: f.name, Value: f.value})
}
func (f *filter) Response(ctx filters.FilterContext) {
var set func(filters.FilterContext, string, string, int)
switch f.typ {
case request:
return
case response:
set = configSetCookie(false)
case responseJS:
set = configSetCookie(true)
default:
panic("invalid cookie filter type")
}
ctx.StateBag()["CookieSet:"+f.name] = f.value
if !f.changeOnly {
set(ctx, f.name, f.value, f.maxAge)
return
}
var req *http.Request
if req = ctx.OriginalRequest(); req == nil {
req = ctx.Request()
}
requestCookie, err := req.Cookie(f.name)
if err == nil && requestCookie.Value == f.value {
return
}
set(ctx, f.name, f.value, f.maxAge)
}
func setCookie(ctx filters.FilterContext, name, value string, maxAge int, jsEnabled bool) {
var req = ctx.Request()
if ctx.OriginalRequest() != nil {
req = ctx.OriginalRequest()
}
d := extractDomainFromHost(req.Host)
c := &http.Cookie{
Name: name,
Value: value,
HttpOnly: !jsEnabled,
Secure: true,
Domain: d,
Path: "/",
MaxAge: maxAge,
}
ctx.Response().Header.Add(SetCookieHttpHeader, c.String())
}
func configSetCookie(jscookie bool) func(filters.FilterContext, string, string, int) {
return func(ctx filters.FilterContext, name, value string, maxAge int) {
setCookie(ctx, name, value, maxAge, jscookie)
}
}
func extractDomainFromHost(host string) string {
h, _, err := net.SplitHostPort(host)
if err != nil {
h = host
}
if strings.Count(h, ".") < 2 {
return h
}
return strings.Join(strings.Split(h, ".")[1:], ".")
}