Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TransmitPreciseGeo activity handling in modules #3348

Merged
merged 9 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
activityControl = privacy.NewActivityControl(&account.Privacy)

hookExecutor.SetActivityControl(activityControl)
hookExecutor.SetAccount(account)

secGPC := r.Header.Get("Sec-GPC")

Expand Down
1 change: 1 addition & 0 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
activityControl = privacy.NewActivityControl(&account.Privacy)

hookExecutor.SetActivityControl(activityControl)
hookExecutor.SetAccount(account)

ctx := context.Background()

Expand Down
33 changes: 21 additions & 12 deletions hooks/hookexecution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hookexecution
import (
"context"
"fmt"
"github.com/prebid/prebid-server/v2/config"
"github.com/prebid/prebid-server/v2/ortb"
"strings"
"sync"
Expand Down Expand Up @@ -68,7 +69,7 @@ func executeGroup[H any, P any](

for _, hook := range group.Hooks {
mCtx := executionCtx.getModuleContext(hook.Module)
newPayload := handleModuleActivities(hook.Code, executionCtx.activityControl, payload)
newPayload := handleModuleActivities(hook.Code, executionCtx.activityControl, payload, executionCtx.account.Privacy)
wg.Add(1)
go func(hw hooks.HookWrapper[H], moduleCtx hookstage.ModuleInvocationContext) {
defer wg.Done()
Expand Down Expand Up @@ -315,28 +316,36 @@ func handleHookMutations[P any](
return payload
}

func handleModuleActivities[P any](hookCode string, activityControl privacy.ActivityControl, payload P) P {
func handleModuleActivities[P any](hookCode string, activityControl privacy.ActivityControl, payload P, ap config.AccountPrivacy) P {
payloadData, ok := any(&payload).(hookstage.RequestUpdater)
if !ok {
return payload
}

scopeGeneral := privacy.Component{Type: privacy.ComponentTypeGeneral, Name: hookCode}
transmitUserFPDActivityAllowed := activityControl.Allow(privacy.ActivityTransmitUserFPD, scopeGeneral, privacy.ActivityRequest{})
transmitPreciseGeoActivityAllowed := activityControl.Allow(privacy.ActivityTransmitPreciseGeo, scopeGeneral, privacy.ActivityRequest{})

if !transmitUserFPDActivityAllowed {
// changes need to be applied to new payload and leave original payload unchanged
bidderReq := payloadData.GetBidderRequestPayload()
if transmitUserFPDActivityAllowed && transmitPreciseGeoActivityAllowed {
return payload
}

bidderReqCopy := ortb.CloneBidderReq(bidderReq.BidRequest)
// changes need to be applied to new payload and leave original payload unchanged
bidderReq := payloadData.GetBidderRequestPayload()

privacy.ScrubUserFPD(bidderReqCopy)
bidderReqCopy := ortb.CloneBidderReq(bidderReq.BidRequest)

var newPayload = payload
var np = any(&newPayload).(hookstage.RequestUpdater)
np.SetBidderRequestPayload(bidderReqCopy)
return newPayload
if !transmitUserFPDActivityAllowed {
privacy.ScrubUserFPD(bidderReqCopy)
}
if !transmitPreciseGeoActivityAllowed {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice there should always be an account if we've gotten this far but perhaps we should add a check here to make sure the account is not nil just to be safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, in practice we will always have an account because we call hookExecutor.SetAccount(account) in auction and amp_auction.
In order to add nil check for account, what do we expect to do if for some reason account is nil?

  1. skip activity execution: privacy.ScrubGeoAndDeviceIP(bidderReqCopy, ipConf)
  2. set default masking values(which also should be already set) and execute activity with default values:
    account.Privacy.IPv6Config.AnonKeepBits = iputil.IPv6DefaultMaskingBitSize and
    account.Privacy.IPv4Config.AnonKeepBits = iputil.IPv4DefaultMaskingBitSize

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest option 2 so that we still scrub given the sensitive nature.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

ipConf := privacy.IPConf{IPV6: ap.IPv6Config, IPV4: ap.IPv4Config}
privacy.ScrubGeoAndDeviceIP(bidderReqCopy, ipConf)
}

return payload
var newPayload = payload
var np = any(&newPayload).(hookstage.RequestUpdater)
np.SetBidderRequestPayload(bidderReqCopy)
return newPayload

}
89 changes: 86 additions & 3 deletions hooks/hookexecution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
"testing"
)

const (
testIpv6 = "1111:2222:3333:4444:5555:6666:7777:8888"
testIPv6Scubbed = "1111:2222::"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nitpick: typo Scrubbed

testIPv6ScrubBytes = 32
)

func TestHandleModuleActivitiesBidderRequestPayload(t *testing.T) {

testCases := []struct {
Expand Down Expand Up @@ -49,13 +55,44 @@ func TestHandleModuleActivitiesBidderRequestPayload(t *testing.T) {
}},
},
},
{
description: "payload should change when transmitPreciseGeo is blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.BidderRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", false),
expectedPayloadData: hookstage.BidderRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIPv6Scubbed},
},
}},
},
{
description: "payload should not change when transmitPreciseGeo is not blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.BidderRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", true),
expectedPayloadData: hookstage.BidderRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
},
}
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
//check input payload didn't change
origInPayloadData := test.inPayloadData
activityControl := privacy.NewActivityControl(test.privacyConfig)
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData)
accountPrivacy := config.AccountPrivacy{IPv6Config: config.IPv6{testIPv6ScrubBytes}}
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData, accountPrivacy)
assert.Equal(t, test.expectedPayloadData.Request.BidRequest, newPayload.Request.BidRequest)
assert.Equal(t, origInPayloadData, test.inPayloadData)
})
Expand Down Expand Up @@ -101,13 +138,45 @@ func TestHandleModuleActivitiesProcessedAuctionRequestPayload(t *testing.T) {
}},
},
},

{
description: "payload should change when transmitPreciseGeo is blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.ProcessedAuctionRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", false),
expectedPayloadData: hookstage.ProcessedAuctionRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIPv6Scubbed},
}},
},
},
{
description: "payload should not change when transmitPreciseGeo is not blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.ProcessedAuctionRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", true),
expectedPayloadData: hookstage.ProcessedAuctionRequestPayload{
Request: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{
Device: &openrtb2.Device{IPv6: testIpv6},
}},
},
},
}
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
//check input payload didn't change
origInPayloadData := test.inPayloadData
activityControl := privacy.NewActivityControl(test.privacyConfig)
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData)
accountPrivacy := config.AccountPrivacy{IPv6Config: config.IPv6{testIPv6ScrubBytes}}
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData, accountPrivacy)
assert.Equal(t, test.expectedPayloadData.Request.BidRequest, newPayload.Request.BidRequest)
assert.Equal(t, origInPayloadData, test.inPayloadData)
})
Expand Down Expand Up @@ -137,13 +206,27 @@ func TestHandleModuleActivitiesNoBidderRequestPayload(t *testing.T) {
privacyConfig: getTransmitUFPDActivityConfig("foo", true),
expectedPayloadData: hookstage.RawAuctionRequestPayload{},
},
{
description: "payload should change when transmitPreciseGeo is blocked by activity",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the test cases in this test have descriptions that indicate the payload should change but it doesn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, description fixed. Payload should NOT change in any of these cases, because hookstage.RawAuctionRequestPayload doesn't implement hookstage.RequestUpdater

hookCode: "foo",
inPayloadData: hookstage.RawAuctionRequestPayload{},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", false),
expectedPayloadData: hookstage.RawAuctionRequestPayload{},
},
{
description: "payload should not change when transmitPreciseGeo is not blocked by activity",
hookCode: "foo",
inPayloadData: hookstage.RawAuctionRequestPayload{},
privacyConfig: getTransmitPreciseGeoActivityConfig("foo", true),
expectedPayloadData: hookstage.RawAuctionRequestPayload{},
},
}
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
//check input payload didn't change
origInPayloadData := test.inPayloadData
activityControl := privacy.NewActivityControl(test.privacyConfig)
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData)
newPayload := handleModuleActivities(test.hookCode, activityControl, test.inPayloadData, config.AccountPrivacy{})
assert.Equal(t, test.expectedPayloadData, newPayload)
assert.Equal(t, origInPayloadData, test.inPayloadData)
})
Expand Down
1 change: 1 addition & 0 deletions hooks/hookexecution/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewHookExecutor(builder hooks.ExecutionPlanBuilder, endpoint string, me met
stageOutcomes: []StageOutcome{},
moduleContexts: &moduleContexts{ctxs: make(map[string]hookstage.ModuleContext)},
metricEngine: me,
account: &config.Account{Privacy: config.AccountPrivacy{}},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the motivation for creating an empty account here instead of just keeping it as nil until an account is set on the hookExecutor using SetAccount? It seems like an attempt to avoid nil checks when executing each stage but I'm not sure that's a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. I changed handleModuleActivities function signature to accept account *config.Account instead of AccountPrivacy.

}
}

Expand Down
29 changes: 23 additions & 6 deletions hooks/hookexecution/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ func TestExecuteRawAuctionStage(t *testing.T) {
t.Run(test.description, func(t *testing.T) {
exec := NewHookExecutor(test.givenPlanBuilder, EndpointAuction, &metricsConfig.NilMetricsEngine{})

privacyConfig := getTransmitUFPDActivityConfig("foo", false)
privacyConfig := getModuleActivities("foo", false, false)
ac := privacy.NewActivityControl(privacyConfig)
exec.SetActivityControl(ac)
exec.SetAccount(test.givenAccount)
Expand Down Expand Up @@ -902,7 +902,7 @@ func TestExecuteProcessedAuctionStage(t *testing.T) {
t.Run(test.description, func(ti *testing.T) {
exec := NewHookExecutor(test.givenPlanBuilder, EndpointAuction, &metricsConfig.NilMetricsEngine{})

privacyConfig := getTransmitUFPDActivityConfig("foo", false)
privacyConfig := getModuleActivities("foo", false, false)
ac := privacy.NewActivityControl(privacyConfig)
exec.SetActivityControl(ac)
exec.SetAccount(test.givenAccount)
Expand Down Expand Up @@ -1179,7 +1179,7 @@ func TestExecuteBidderRequestStage(t *testing.T) {
for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
exec := NewHookExecutor(test.givenPlanBuilder, EndpointAuction, &metricsConfig.NilMetricsEngine{})
privacyConfig := getTransmitUFPDActivityConfig("foo", false)
privacyConfig := getModuleActivities("foo", false, false)
ac := privacy.NewActivityControl(privacyConfig)
exec.SetActivityControl(ac)
exec.SetAccount(test.givenAccount)
Expand All @@ -1200,6 +1200,15 @@ func TestExecuteBidderRequestStage(t *testing.T) {
}
}

func getModuleActivities(componentName string, allowTransmitUserFPD, allowTransmitPreciseGeo bool) *config.AccountPrivacy {
return &config.AccountPrivacy{
AllowActivities: &config.AllowActivities{
TransmitUserFPD: buildDefaultActivityConfig(componentName, allowTransmitUserFPD),
TransmitPreciseGeo: buildDefaultActivityConfig(componentName, allowTransmitPreciseGeo),
},
}
}

func getTransmitUFPDActivityConfig(componentName string, allow bool) *config.AccountPrivacy {
return &config.AccountPrivacy{
AllowActivities: &config.AllowActivities{
Expand All @@ -1208,6 +1217,14 @@ func getTransmitUFPDActivityConfig(componentName string, allow bool) *config.Acc
}
}

func getTransmitPreciseGeoActivityConfig(componentName string, allow bool) *config.AccountPrivacy {
return &config.AccountPrivacy{
AllowActivities: &config.AllowActivities{
TransmitPreciseGeo: buildDefaultActivityConfig(componentName, allow),
},
}
}

func buildDefaultActivityConfig(componentName string, allow bool) config.Activity {
return config.Activity{
Default: ptrutil.ToPtr(true),
Expand Down Expand Up @@ -1427,7 +1444,7 @@ func TestExecuteRawBidderResponseStage(t *testing.T) {
t.Run(test.description, func(ti *testing.T) {
exec := NewHookExecutor(test.givenPlanBuilder, EndpointAuction, &metricsConfig.NilMetricsEngine{})

privacyConfig := getTransmitUFPDActivityConfig("foo", false)
privacyConfig := getModuleActivities("foo", false, false)
ac := privacy.NewActivityControl(privacyConfig)
exec.SetActivityControl(ac)
exec.SetAccount(test.givenAccount)
Expand Down Expand Up @@ -1710,7 +1727,7 @@ func TestExecuteAllProcessedBidResponsesStage(t *testing.T) {
t.Run(test.description, func(t *testing.T) {
exec := NewHookExecutor(test.givenPlanBuilder, EndpointAuction, &metricsConfig.NilMetricsEngine{})

privacyConfig := getTransmitUFPDActivityConfig("foo", false)
privacyConfig := getModuleActivities("foo", false, false)
ac := privacy.NewActivityControl(privacyConfig)
exec.SetActivityControl(ac)
exec.SetAccount(test.givenAccount)
Expand Down Expand Up @@ -1963,7 +1980,7 @@ func TestExecuteAuctionResponseStage(t *testing.T) {
t.Run(test.description, func(t *testing.T) {
exec := NewHookExecutor(test.givenPlanBuilder, EndpointAuction, &metricsConfig.NilMetricsEngine{})

privacyConfig := getTransmitUFPDActivityConfig("foo", false)
privacyConfig := getModuleActivities("foo", false, false)
ac := privacy.NewActivityControl(privacyConfig)
exec.SetActivityControl(ac)
exec.SetAccount(test.givenAccount)
Expand Down