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

privacy: Potential JSON injection #1304

Merged
merged 4 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 8 additions & 4 deletions privacy/ccpa/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ func (p Policy) Write(req *openrtb.BidRequest) error {
req.Regs = &openrtb.Regs{}
}

var err error

if req.Regs.Ext == nil {
req.Regs.Ext = json.RawMessage(`{"us_privacy":"` + p.Value + `"}`)
return nil
req.Regs.Ext, err = json.Marshal(openrtb_ext.ExtRegs{USPrivacy: p.Value})
djcsdy marked this conversation as resolved.
Show resolved Hide resolved
return err
}

var err error
req.Regs.Ext, err = jsonparser.Set(req.Regs.Ext, []byte(`"`+p.Value+`"`), "us_privacy")
jsonString, err := json.Marshal(p.Value)
djcsdy marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
req.Regs.Ext, err = jsonparser.Set(req.Regs.Ext, jsonString, "us_privacy")
}
return err
}

Expand Down
37 changes: 37 additions & 0 deletions privacy/ccpa/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ func TestRead(t *testing.T) {
},
expectedError: true,
},
{
description: "Injection Attack",
request: &openrtb.BidRequest{
Regs: &openrtb.Regs{
Ext: json.RawMessage(`{"us_privacy":"1YYY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""}`),
},
},
expectedPolicy: Policy{
Value: "1YYY\"},\"oops\":\"malicious\",\"p\":{\"p\":\"",
},
},
}

for _, test := range testCases {
Expand Down Expand Up @@ -138,6 +149,32 @@ func TestWrite(t *testing.T) {
Ext: json.RawMessage(`malformed`)}},
expectedError: true,
},
{
description: "Injection Attack With Nil Request Regs Object",
policy: Policy{Value: "1YYY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""},
request: &openrtb.BidRequest{},
expected: &openrtb.BidRequest{Regs: &openrtb.Regs{
Ext: json.RawMessage(`{"us_privacy":"1YYY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""}`),
}},
},
{
description: "Injection Attack With Nil Request Regs Ext Object",
policy: Policy{Value: "1YYY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""},
request: &openrtb.BidRequest{Regs: &openrtb.Regs{}},
expected: &openrtb.BidRequest{Regs: &openrtb.Regs{
Ext: json.RawMessage(`{"us_privacy":"1YYY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""}`),
}},
},
{
description: "Injection Attack With Existing Request Regs Ext Object",
policy: Policy{Value: "1YYY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""},
request: &openrtb.BidRequest{Regs: &openrtb.Regs{
Ext: json.RawMessage(`{"existing":"any"}`),
}},
expected: &openrtb.BidRequest{Regs: &openrtb.Regs{
Ext: json.RawMessage(`{"existing":"any","us_privacy":"1YYY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""}`),
}},
},
}

for _, test := range testCases {
Expand Down
13 changes: 9 additions & 4 deletions privacy/gdpr/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gdpr

import (
"encoding/json"
"github.com/prebid/prebid-server/openrtb_ext"

"github.com/buger/jsonparser"
"github.com/mxmCherry/openrtb"
Expand All @@ -24,13 +25,17 @@ func (p Policy) Write(req *openrtb.BidRequest) error {
req.User = &openrtb.User{}
}

var err error

if req.User.Ext == nil {
req.User.Ext = json.RawMessage(`{"consent":"` + p.Consent + `"}`)
return nil
req.User.Ext, err = json.Marshal(openrtb_ext.ExtUser{Consent: p.Consent})
return err
}

var err error
req.User.Ext, err = jsonparser.Set(req.User.Ext, []byte(`"`+p.Consent+`"`), "consent")
jsonString, err := json.Marshal(p.Consent)
if err == nil {
req.User.Ext, err = jsonparser.Set(req.User.Ext, jsonString, "consent")
}
return err
}

Expand Down
26 changes: 26 additions & 0 deletions privacy/gdpr/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ func TestWrite(t *testing.T) {
Ext: json.RawMessage(`malformed`)}},
expectedError: true,
},
{
description: "Injection Attack With Nil Request User Object",
policy: Policy{Consent: "BONV8oqONXwgmADACHENAO7pqzAAppY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""},
request: &openrtb.BidRequest{},
expected: &openrtb.BidRequest{User: &openrtb.User{
Ext: json.RawMessage(`{"consent":"BONV8oqONXwgmADACHENAO7pqzAAppY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""}`),
}},
},
{
description: "Injection Attack With Nil Request User Ext Object",
policy: Policy{Consent: "BONV8oqONXwgmADACHENAO7pqzAAppY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""},
request: &openrtb.BidRequest{User: &openrtb.User{}},
expected: &openrtb.BidRequest{User: &openrtb.User{
Ext: json.RawMessage(`{"consent":"BONV8oqONXwgmADACHENAO7pqzAAppY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""}`),
}},
},
{
description: "Injection Attack With Existing Request User Ext Object",
policy: Policy{Consent: "BONV8oqONXwgmADACHENAO7pqzAAppY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""},
request: &openrtb.BidRequest{User: &openrtb.User{
Ext: json.RawMessage(`{"existing":"any"}`),
}},
expected: &openrtb.BidRequest{User: &openrtb.User{
Ext: json.RawMessage(`{"existing":"any","consent":"BONV8oqONXwgmADACHENAO7pqzAAppY\"},\"oops\":\"malicious\",\"p\":{\"p\":\""}`),
}},
},
}

for _, test := range testCases {
Expand Down