-
Notifications
You must be signed in to change notification settings - Fork 742
/
scrubber.go
201 lines (171 loc) · 4.78 KB
/
scrubber.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
package privacy
import (
"encoding/json"
"net"
"github.com/prebid/prebid-server/v2/util/jsonutil"
"github.com/prebid/openrtb/v20/openrtb2"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/openrtb_ext"
"github.com/prebid/prebid-server/v2/util/iputil"
)
type IPConf struct {
IPV6 config.IPv6
IPV4 config.IPv4
}
func scrubDeviceIDs(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.Device != nil {
reqWrapper.Device.DIDMD5 = ""
reqWrapper.Device.DIDSHA1 = ""
reqWrapper.Device.DPIDMD5 = ""
reqWrapper.Device.DPIDSHA1 = ""
reqWrapper.Device.IFA = ""
reqWrapper.Device.MACMD5 = ""
reqWrapper.Device.MACSHA1 = ""
}
}
func scrubUserIDs(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.User != nil {
reqWrapper.User.Data = nil
reqWrapper.User.ID = ""
reqWrapper.User.BuyerUID = ""
reqWrapper.User.Yob = 0
reqWrapper.User.Gender = ""
reqWrapper.User.Keywords = ""
reqWrapper.User.KwArray = nil
}
}
func scrubUserDemographics(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.User != nil {
reqWrapper.User.BuyerUID = ""
reqWrapper.User.ID = ""
reqWrapper.User.Yob = 0
reqWrapper.User.Gender = ""
}
}
func scrubUserExt(reqWrapper *openrtb_ext.RequestWrapper, fieldName string) error {
if reqWrapper.User != nil {
userExt, err := reqWrapper.GetUserExt()
if err != nil {
return err
}
ext := userExt.GetExt()
_, hasField := ext[fieldName]
if hasField {
delete(ext, fieldName)
userExt.SetExt(ext)
}
}
return nil
}
func ScrubEIDs(reqWrapper *openrtb_ext.RequestWrapper) error {
//transmitEids removes user.eids and user.ext.eids
if reqWrapper.User != nil {
reqWrapper.User.EIDs = nil
}
return scrubUserExt(reqWrapper, "eids")
}
func ScrubTID(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.Source != nil {
reqWrapper.Source.TID = ""
}
impWrapper := reqWrapper.GetImp()
for ind, imp := range impWrapper {
impExt := scrubExtIDs(imp.Ext, "tid")
impWrapper[ind].Ext = impExt
}
reqWrapper.SetImp(impWrapper)
}
func scrubGEO(reqWrapper *openrtb_ext.RequestWrapper) {
//round user's geographic location by rounding off IP address and lat/lng data.
//this applies to both device.geo and user.geo
if reqWrapper.User != nil && reqWrapper.User.Geo != nil {
reqWrapper.User.Geo = scrubGeoPrecision(reqWrapper.User.Geo)
}
if reqWrapper.Device != nil && reqWrapper.Device.Geo != nil {
reqWrapper.Device.Geo = scrubGeoPrecision(reqWrapper.Device.Geo)
}
}
func scrubGeoFull(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.User != nil && reqWrapper.User.Geo != nil {
reqWrapper.User.Geo = &openrtb2.Geo{}
}
if reqWrapper.Device != nil && reqWrapper.Device.Geo != nil {
reqWrapper.Device.Geo = &openrtb2.Geo{}
}
}
func scrubDeviceIP(reqWrapper *openrtb_ext.RequestWrapper, ipConf IPConf) {
if reqWrapper.Device != nil {
reqWrapper.Device.IP = scrubIP(reqWrapper.Device.IP, ipConf.IPV4.AnonKeepBits, iputil.IPv4BitSize)
reqWrapper.Device.IPv6 = scrubIP(reqWrapper.Device.IPv6, ipConf.IPV6.AnonKeepBits, iputil.IPv6BitSize)
}
}
func ScrubDeviceIDsIPsUserDemoExt(reqWrapper *openrtb_ext.RequestWrapper, ipConf IPConf, fieldName string, scrubFullGeo bool) {
scrubDeviceIDs(reqWrapper)
scrubDeviceIP(reqWrapper, ipConf)
scrubUserDemographics(reqWrapper)
scrubUserExt(reqWrapper, fieldName)
if scrubFullGeo {
scrubGeoFull(reqWrapper)
} else {
scrubGEO(reqWrapper)
}
}
func ScrubUserFPD(reqWrapper *openrtb_ext.RequestWrapper) {
scrubDeviceIDs(reqWrapper)
scrubUserIDs(reqWrapper)
scrubUserExt(reqWrapper, "data")
reqWrapper.User.EIDs = nil
}
func ScrubGdprID(reqWrapper *openrtb_ext.RequestWrapper) {
scrubDeviceIDs(reqWrapper)
scrubUserDemographics(reqWrapper)
scrubUserExt(reqWrapper, "eids")
}
func ScrubGeoAndDeviceIP(reqWrapper *openrtb_ext.RequestWrapper, ipConf IPConf) {
scrubDeviceIP(reqWrapper, ipConf)
scrubGEO(reqWrapper)
}
func scrubIP(ip string, ones, bits int) string {
if ip == "" {
return ""
}
ipMask := net.CIDRMask(ones, bits)
ipMasked := net.ParseIP(ip).Mask(ipMask)
return ipMasked.String()
}
func scrubGeoPrecision(geo *openrtb2.Geo) *openrtb2.Geo {
if geo == nil {
return nil
}
geoCopy := *geo
if geoCopy.Lat != nil {
lat := *geo.Lat
lat = float64(int(lat*100.0+0.5)) / 100.0
geoCopy.Lat = &lat
}
if geoCopy.Lon != nil {
lon := *geo.Lon
lon = float64(int(lon*100.0+0.5)) / 100.0
geoCopy.Lon = &lon
}
return &geoCopy
}
func scrubExtIDs(ext json.RawMessage, fieldName string) json.RawMessage {
if len(ext) == 0 {
return ext
}
var userExtParsed map[string]json.RawMessage
err := jsonutil.Unmarshal(ext, &userExtParsed)
if err != nil {
return ext
}
_, hasField := userExtParsed[fieldName]
if hasField {
delete(userExtParsed, fieldName)
result, err := jsonutil.Marshal(userExtParsed)
if err == nil {
return result
}
}
return ext
}