Skip to content

Commit

Permalink
Code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
VeronikaSolovei9 committed Oct 29, 2023
1 parent 1ee688b commit ea36a24
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 93 deletions.
6 changes: 6 additions & 0 deletions analytics/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func (ea enabledAnalytics) LogAuctionObject(ao *analytics.AuctionObject, ac priv
for name, module := range ea {
component := privacy.Component{Type: privacy.ComponentTypeAnalytics, Name: name}
if ac.Allow(privacy.ActivityReportAnalytics, component, privacy.ActivityRequest{}) {
if !ac.Allow(privacy.ActivityTransmitUserFPD, component, privacy.ActivityRequest{}) {
// scrub UFPD from ao.RequestWrapper
}
if !ac.Allow(privacy.ActivityTransmitPreciseGeo, component, privacy.ActivityRequest{}) {
// scrub geo from ao.RequestWrapper
}
module.LogAuctionObject(ao)
}
}
Expand Down
20 changes: 4 additions & 16 deletions exchange/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,8 @@ func (rs *requestSplitter) cleanOpenRTBRequests(ctx context.Context,
} else {
// run existing policies (GDPR, CCPA, COPPA, LMT)
// potentially block passing IDs based on GDPR
if gdprEnforced {
if gdprErr == nil {
if !auctionPermissions.PassID {
privacy.ScrubGdprID(reqWrapper)
}
} else {
privacy.ScrubGdprID(reqWrapper)
}
if gdprEnforced && (gdprErr != nil || !auctionPermissions.PassID) {
privacy.ScrubGdprID(reqWrapper)
}
// potentially block passing IDs based on CCPA
if ccpaEnforcer.ShouldEnforce(bidderRequest.BidderName.String()) {
Expand All @@ -211,14 +205,8 @@ func (rs *requestSplitter) cleanOpenRTBRequests(ctx context.Context,
} else {
// run existing policies (GDPR, CCPA, COPPA, LMT)
// potentially block passing geo based on GDPR
if gdprEnforced {
if gdprErr == nil {
if !auctionPermissions.PassGeo {
privacy.ScrubGeoAndDeviceIP(reqWrapper, ipConf)
}
} else {
privacy.ScrubGeoAndDeviceIP(reqWrapper, ipConf)
}
if gdprEnforced && (gdprErr != nil || !auctionPermissions.PassGeo) {
privacy.ScrubGeoAndDeviceIP(reqWrapper, ipConf)
}
// potentially block passing geo based on CCPA
if ccpaEnforcer.ShouldEnforce(bidderRequest.BidderName.String()) {
Expand Down
6 changes: 4 additions & 2 deletions ortb/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ func CloneUserAgent(s *openrtb2.UserAgent) *openrtb2.UserAgent {
c.Browsers = CloneBrandVersionSlice(s.Browsers)
c.Platform = CloneBrandVersion(s.Platform)

mobileCopy := &s.Mobile
c.Mobile = *mobileCopy
if s.Mobile != nil {
mobileCopy := *s.Mobile
c.Mobile = &mobileCopy
}
s.Ext = sliceutil.Clone(s.Ext)

return &c
Expand Down
95 changes: 76 additions & 19 deletions ortb/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,8 @@ func TestCloneDevice(t *testing.T) {
})

t.Run("populated", func(t *testing.T) {
var np *int8
var n int8
n = 1
np = &n
var n int8 = 1
np := &n
ct := adcom1.ConnectionWIFI

given := &openrtb2.Device{
Expand Down Expand Up @@ -630,16 +628,65 @@ func TestCloneInt8Pointer(t *testing.T) {
})

t.Run("populated", func(t *testing.T) {
var given *int8
var n int8
n = 1
given = &n
var n int8 = 1
given := &n
result := CloneInt8Pointer(given)
assert.Equal(t, given, result, "equality")
assert.NotSame(t, given, result, "pointer")
})
}

func TestCloneUserAgent(t *testing.T) {
t.Run("nil", func(t *testing.T) {
result := CloneUserAgent(nil)
assert.Nil(t, result)
})

t.Run("empty", func(t *testing.T) {
given := &openrtb2.UserAgent{}
result := CloneUserAgent(given)
assert.Empty(t, result)
assert.NotSame(t, given, result)
})

t.Run("populated", func(t *testing.T) {
var n int8 = 1
np := &n

given := &openrtb2.UserAgent{
Browsers: []openrtb2.BrandVersion{{Brand: "Apple"}},
Platform: &openrtb2.BrandVersion{Brand: "Apple"},
Mobile: np,
Architecture: "X86",
Bitness: "64",
Model: "iPad",
Source: adcom1.UASourceLowEntropy,
Ext: json.RawMessage(`{"anyField":1}`),
}
result := CloneUserAgent(given)
assert.Equal(t, given, result, "equality")
assert.NotSame(t, given, result, "pointer")
assert.NotSame(t, given.Browsers, result.Browsers, "browsers")
assert.NotSame(t, given.Platform, result.Platform, "platform")
assert.NotSame(t, given.Mobile, result.Mobile, "mobile")
assert.NotSame(t, given.Architecture, result.Architecture, "architecture")
assert.NotSame(t, given.Bitness, result.Bitness, "bitness")
assert.NotSame(t, given.Model, result.Model, "model")
assert.NotSame(t, given.Source, result.Source, "source")
assert.NotSame(t, given.Ext, result.Ext, "ext")
})

t.Run("assumptions", func(t *testing.T) {
assert.ElementsMatch(t, discoverPointerFields(reflect.TypeOf(openrtb2.UserAgent{})),
[]string{
"Browsers",
"Platform",
"Mobile",
"Ext",
})
})
}

func TestCloneBrandVersionSlice(t *testing.T) {
t.Run("nil", func(t *testing.T) {
result := CloneBrandVersionSlice(nil)
Expand Down Expand Up @@ -732,13 +779,19 @@ func TestCloneSource(t *testing.T) {
TID: "Tid",
PChain: "PChain",
SChain: &openrtb2.SupplyChain{
Complete: 1, Nodes: []openrtb2.SupplyChainNode{{ASI: "asi", Ext: json.RawMessage(`{"anyField":1}`)}}},
Complete: 1,
Nodes: []openrtb2.SupplyChainNode{
{ASI: "asi", Ext: json.RawMessage(`{"anyField":1}`)},
},
Ext: json.RawMessage(`{"anyField":2}`),
},
Ext: json.RawMessage(`{"anyField":1}`),
}
result := CloneSource(given)
assert.Equal(t, given, result, "equality")
assert.NotSame(t, given, result, "pointer")
assert.NotSame(t, given.SChain, result.SChain, "schain")
assert.NotSame(t, given.SChain.Ext, result.SChain.Ext, "schain.ext")
assert.NotSame(t, given.Ext, result.Ext, "ext")
})

Expand Down Expand Up @@ -766,7 +819,12 @@ func TestCloneSChain(t *testing.T) {

t.Run("populated", func(t *testing.T) {
given := &openrtb2.SupplyChain{
Complete: 1, Nodes: []openrtb2.SupplyChainNode{{ASI: "asi", Ext: json.RawMessage(`{"anyField":1}`)}}}
Complete: 1,
Nodes: []openrtb2.SupplyChainNode{
{ASI: "asi", Ext: json.RawMessage(`{"anyField":1}`)},
},
Ext: json.RawMessage(`{"anyField":1}`),
}
result := CloneSChain(given)
assert.Equal(t, given, result, "equality")
assert.NotSame(t, given, result, "pointer")
Expand All @@ -783,11 +841,9 @@ func TestCloneSChain(t *testing.T) {
})
}

func TestCloseSupplyChainNodes(t *testing.T) {
var np *int8
var n int8
n = 1
np = &n
func TestCloneSupplyChainNodes(t *testing.T) {
var n int8 = 1
np := &n
t.Run("nil", func(t *testing.T) {
result := CloneSupplyChainNodes(nil)
assert.Nil(t, result)
Expand Down Expand Up @@ -825,15 +881,16 @@ func TestCloseSupplyChainNodes(t *testing.T) {
assert.NotSame(t, given[1], result[1], "item1-pointer")
assert.NotSame(t, given[1].Ext, result[1].Ext, "item1-pointer-ext")
assert.NotSame(t, given[1].HP, result[1].HP, "item1-pointer-hp")
assert.NotSame(t, given[2], result[2], "item2-pointer")
assert.NotSame(t, given[2].Ext, result[2].Ext, "item2-pointer-ext")
assert.NotSame(t, given[2].HP, result[2].HP, "item2-pointer-hp")
})
}

func TestCloneSupplyChainNode(t *testing.T) {
t.Run("populated", func(t *testing.T) {
var np *int8
var n int8
n = 1
np = &n
var n int8 = 1
np := &n

given := openrtb2.SupplyChainNode{
ASI: "asi",
Expand Down
54 changes: 26 additions & 28 deletions privacy/scrubber.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type IPConf struct {
IPV4 config.IPv4
}

func ScrubDeviceIDs(reqWrapper *openrtb_ext.RequestWrapper) {
func scrubDeviceIDs(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.Device != nil {
reqWrapper.Device.DIDMD5 = ""
reqWrapper.Device.DIDSHA1 = ""
Expand All @@ -28,7 +28,7 @@ func ScrubDeviceIDs(reqWrapper *openrtb_ext.RequestWrapper) {
}
}

func ScrubUserIDs(reqWrapper *openrtb_ext.RequestWrapper) {
func scrubUserIDs(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.User != nil {
reqWrapper.User.Data = nil
reqWrapper.User.ID = ""
Expand All @@ -40,7 +40,7 @@ func ScrubUserIDs(reqWrapper *openrtb_ext.RequestWrapper) {
}
}

func ScrubUserDemographics(reqWrapper *openrtb_ext.RequestWrapper) {
func scrubUserDemographics(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.User != nil {
reqWrapper.User.BuyerUID = ""
reqWrapper.User.ID = ""
Expand All @@ -49,7 +49,7 @@ func ScrubUserDemographics(reqWrapper *openrtb_ext.RequestWrapper) {
}
}

func ScrubUserExt(reqWrapper *openrtb_ext.RequestWrapper, fieldName string) error {
func scrubUserExt(reqWrapper *openrtb_ext.RequestWrapper, fieldName string) error {
if reqWrapper.User != nil {
userExt, err := reqWrapper.GetUserExt()
if err != nil {
Expand All @@ -65,12 +65,12 @@ func ScrubUserExt(reqWrapper *openrtb_ext.RequestWrapper, fieldName string) erro
return nil
}

func ScrubEids(reqWrapper *openrtb_ext.RequestWrapper) error {
//transmitEids covers user.eids and user.ext.eids
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")
return scrubUserExt(reqWrapper, "eids")
}

func ScrubTID(reqWrapper *openrtb_ext.RequestWrapper) {
Expand All @@ -85,21 +85,19 @@ func ScrubTID(reqWrapper *openrtb_ext.RequestWrapper) {
reqWrapper.SetImp(impWrapper)
}

func ScrubGEO(reqWrapper *openrtb_ext.RequestWrapper) {
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 {
if reqWrapper.Device.Geo != nil {
reqWrapper.Device.Geo = scrubGeoPrecision(reqWrapper.Device.Geo)
}
if reqWrapper.Device != nil && reqWrapper.Device.Geo != nil {
reqWrapper.Device.Geo = scrubGeoPrecision(reqWrapper.Device.Geo)
}
}

func ScrubGeoFull(reqWrapper *openrtb_ext.RequestWrapper) {
func scrubGeoFull(reqWrapper *openrtb_ext.RequestWrapper) {
if reqWrapper.User != nil && reqWrapper.User.Geo != nil {
reqWrapper.User.Geo = &openrtb2.Geo{}
}
Expand All @@ -109,42 +107,42 @@ func ScrubGeoFull(reqWrapper *openrtb_ext.RequestWrapper) {

}

func ScrubDeviceIP(reqWrapper *openrtb_ext.RequestWrapper, ipConf IPConf) {
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)
scrubDeviceIDs(reqWrapper)
scrubDeviceIP(reqWrapper, ipConf)
scrubUserDemographics(reqWrapper)
scrubUserExt(reqWrapper, fieldName)

if scrubFullGeo {
ScrubGeoFull(reqWrapper)
scrubGeoFull(reqWrapper)
} else {
ScrubGEO(reqWrapper)
scrubGEO(reqWrapper)
}
}

func ScrubUserFPD(reqWrapper *openrtb_ext.RequestWrapper) {
ScrubDeviceIDs(reqWrapper)
ScrubUserIDs(reqWrapper)
ScrubUserExt(reqWrapper, "data")
scrubDeviceIDs(reqWrapper)
scrubUserIDs(reqWrapper)
scrubUserExt(reqWrapper, "data")
reqWrapper.User.EIDs = nil
}

func ScrubGdprID(reqWrapper *openrtb_ext.RequestWrapper) {
ScrubDeviceIDs(reqWrapper)
ScrubUserDemographics(reqWrapper)
ScrubUserExt(reqWrapper, "eids")
scrubDeviceIDs(reqWrapper)
scrubUserDemographics(reqWrapper)
scrubUserExt(reqWrapper, "eids")
}

func ScrubGeoAndDeviceIP(reqWrapper *openrtb_ext.RequestWrapper, ipConf IPConf) {
ScrubDeviceIP(reqWrapper, ipConf)
ScrubGEO(reqWrapper)
scrubDeviceIP(reqWrapper, ipConf)
scrubGEO(reqWrapper)
}

func scrubIP(ip string, ones, bits int) string {
Expand Down
Loading

0 comments on commit ea36a24

Please sign in to comment.