forked from prebid/prebid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.go
259 lines (217 loc) · 6.95 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
package usersync
import (
"errors"
"net/http"
"time"
"github.com/prebid/prebid-server/v3/config"
"github.com/prebid/prebid-server/v3/openrtb_ext"
"github.com/prebid/prebid-server/v3/util/jsonutil"
)
const uidCookieName = "uids"
// uidTTL is the default amount of time a uid stored within a cookie is considered valid. This is
// separate from the cookie ttl.
const uidTTL = 14 * 24 * time.Hour
// Cookie is the cookie used in Prebid Server.
//
// To get an instance of this from a request, use ReadCookie.
// To write an instance onto a response, use WriteCookie.
type Cookie struct {
uids map[string]UIDEntry
optOut bool
}
// UIDEntry bundles the UID with an Expiration date.
type UIDEntry struct {
// UID is the ID given to a user by a particular bidder
UID string `json:"uid"`
// Expires is the time at which this UID should no longer apply.
Expires time.Time `json:"expires"`
}
// NewCookie returns a new empty cookie.
func NewCookie() *Cookie {
return &Cookie{
uids: make(map[string]UIDEntry),
}
}
// ReadCookie reads the cookie from the request
func ReadCookie(r *http.Request, decoder Decoder, host *config.HostCookie) *Cookie {
if hostOptOutCookie := checkHostCookieOptOut(r, host); hostOptOutCookie != nil {
return hostOptOutCookie
}
// Read cookie from request
cookieFromRequest, err := r.Cookie(uidCookieName)
if err != nil {
return NewCookie()
}
decodedCookie := decoder.Decode(cookieFromRequest.Value)
return decodedCookie
}
// PrepareCookieForWrite ejects UIDs as long as the cookie is too full
func (cookie *Cookie) PrepareCookieForWrite(cfg *config.HostCookie, encoder Encoder, ejector Ejector) (string, error) {
for len(cookie.uids) > 0 {
encodedCookie, err := encoder.Encode(cookie)
if err != nil {
return encodedCookie, nil
}
// Convert to HTTP Cookie to Get Size
httpCookie := &http.Cookie{
Name: uidCookieName,
Value: encodedCookie,
Expires: time.Now().Add(cfg.TTLDuration()),
Path: "/",
}
cookieSize := len([]byte(httpCookie.String()))
isCookieTooBig := cookieSize > cfg.MaxCookieSizeBytes && cfg.MaxCookieSizeBytes > 0
if !isCookieTooBig {
return encodedCookie, nil
} else if len(cookie.uids) == 1 {
return "", errors.New("uid that's trying to be synced is bigger than MaxCookieSize")
}
uidToDelete, err := ejector.Choose(cookie.uids)
if err != nil {
return encodedCookie, err
}
delete(cookie.uids, uidToDelete)
}
return "", nil
}
// WriteCookie sets the prepared cookie onto the header
func WriteCookie(w http.ResponseWriter, encodedCookie string, cfg *config.HostCookie, setSiteCookie bool) {
ttl := cfg.TTLDuration()
httpCookie := &http.Cookie{
Name: uidCookieName,
Value: encodedCookie,
Expires: time.Now().Add(ttl),
Path: "/",
}
if cfg.Domain != "" {
httpCookie.Domain = cfg.Domain
}
if setSiteCookie {
httpCookie.Secure = true
httpCookie.SameSite = http.SameSiteNoneMode
}
w.Header().Add("Set-Cookie", httpCookie.String())
}
// Sync tries to set the UID for some syncer key. It returns an error if the set didn't happen.
func (cookie *Cookie) Sync(key string, uid string) error {
if !cookie.AllowSyncs() {
return errors.New("the user has opted out of prebid server cookie syncs")
}
if checkAudienceNetwork(key, uid) {
return errors.New("audienceNetwork uses a UID of 0 as \"not yet recognized\"")
}
// Sync
cookie.uids[key] = UIDEntry{
UID: uid,
Expires: time.Now().Add(uidTTL),
}
return nil
}
// SyncHostCookie syncs the request cookie with the host cookie
func SyncHostCookie(r *http.Request, requestCookie *Cookie, host *config.HostCookie) {
if uid, _, _ := requestCookie.GetUID(host.Family); uid == "" && host.CookieName != "" {
if hostCookie, err := r.Cookie(host.CookieName); err == nil {
requestCookie.Sync(host.Family, hostCookie.Value)
}
}
}
func checkHostCookieOptOut(r *http.Request, host *config.HostCookie) *Cookie {
if host.OptOutCookie.Name != "" {
optOutCookie, err := r.Cookie(host.OptOutCookie.Name)
if err == nil && optOutCookie.Value == host.OptOutCookie.Value {
hostOptOut := NewCookie()
hostOptOut.SetOptOut(true)
return hostOptOut
}
}
return nil
}
// AllowSyncs is true if the user lets bidders sync cookies, and false otherwise.
func (cookie *Cookie) AllowSyncs() bool {
return cookie != nil && !cookie.optOut
}
// SetOptOut is used to change whether or not we're allowed to sync cookies for this user.
func (cookie *Cookie) SetOptOut(optOut bool) {
cookie.optOut = optOut
if optOut {
cookie.uids = make(map[string]UIDEntry)
}
}
// GetUID Gets this user's ID for the given syncer key.
func (cookie *Cookie) GetUID(key string) (uid string, isUIDFound bool, isUIDActive bool) {
if cookie != nil {
if uid, ok := cookie.uids[key]; ok {
return uid.UID, true, time.Now().Before(uid.Expires)
}
}
return "", false, false
}
// GetUIDs returns this user's ID for all the bidders
func (cookie *Cookie) GetUIDs() map[string]string {
uids := make(map[string]string)
if cookie != nil {
// Extract just the uid for each bidder
for bidderName, uidWithExpiry := range cookie.uids {
uids[bidderName] = uidWithExpiry.UID
}
}
return uids
}
// Unsync removes the user's ID for the given syncer key from this cookie.
func (cookie *Cookie) Unsync(key string) {
delete(cookie.uids, key)
}
// HasLiveSync returns true if we have an active UID for the given syncer key, and false otherwise.
func (cookie *Cookie) HasLiveSync(key string) bool {
_, _, isLive := cookie.GetUID(key)
return isLive
}
// HasAnyLiveSyncs returns true if this cookie has at least one active sync.
func (cookie *Cookie) HasAnyLiveSyncs() bool {
now := time.Now()
if cookie != nil {
for _, value := range cookie.uids {
if now.Before(value.Expires) {
return true
}
}
}
return false
}
func checkAudienceNetwork(key string, uid string) bool {
return key == string(openrtb_ext.BidderAudienceNetwork) && uid == "0"
}
// cookieJson defines the JSON contract for the cookie data's storage format.
//
// This exists so that Cookie (which is public) can have private fields, and the rest of
// the code doesn't have to worry about the cookie data storage format.
type cookieJson struct {
UIDs map[string]UIDEntry `json:"tempUIDs,omitempty"`
OptOut bool `json:"optout,omitempty"`
}
func (cookie *Cookie) MarshalJSON() ([]byte, error) { // nosemgrep: marshal-json-pointer-receiver
return jsonutil.Marshal(cookieJson{
UIDs: cookie.uids,
OptOut: cookie.optOut,
})
}
func (cookie *Cookie) UnmarshalJSON(b []byte) error {
var cookieContract cookieJson
if err := jsonutil.Unmarshal(b, &cookieContract); err != nil {
return err
}
cookie.optOut = cookieContract.OptOut
if cookie.optOut {
cookie.uids = nil
} else {
cookie.uids = cookieContract.UIDs
}
if cookie.uids == nil {
cookie.uids = make(map[string]UIDEntry)
}
// Audience Network Handling
if id, ok := cookie.uids[string(openrtb_ext.BidderAudienceNetwork)]; ok && id.UID == "0" {
delete(cookie.uids, string(openrtb_ext.BidderAudienceNetwork))
}
return nil
}