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

protocol/network: Fix deserialization of CookiePartitionKey #150

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions protocol/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ import (
// Map returns the headers decoded into a map.
func (n Headers) Map() (map[string]string, error) {
m := make(map[string]string)
if len(n) == 0 {
return m, nil
}
err := json.Unmarshal(n, &m)
return m, err
}

// UnmarhsalJSON implements json.Unmarshaler for CookiePartitionKey. The
// protocol defines CookiePartitionKey as an object but its return type
// can be string.
//
// https://chromium.googlesource.com/chromium/src/+/refs/heads/main/net/cookies/cookie_partition_key.cc
// https://chromium.googlesource.com/chromium/src/+/refs/heads/main/net/cookies/cookie_partition_key.h
func (k *CookiePartitionKey) UnmarshalJSON(data []byte) error {
type shadowed CookiePartitionKey
var v shadowed
if err := json.Unmarshal(data, &v); err != nil {
var s string
if err2 := json.Unmarshal(data, &s); err2 != nil {
return err
}

*k = CookiePartitionKey{
TopLevelSite: s,
HasCrossSiteAncestor: false,
}
return nil
}

*k = CookiePartitionKey(v)
return nil
}

func (k CookiePartitionKey) String() string {
return k.TopLevelSite
}
108 changes: 108 additions & 0 deletions protocol/network/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package network

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestHeaders_Map(t *testing.T) {
t.Parallel()

tests := []struct {
name string
n Headers
want map[string]string
wantErr bool
}{
{
name: "empty",
n: Headers{},
want: map[string]string{},
},
{
name: "valid",
n: Headers(`{"Content-Type":"application/json"}`),
want: map[string]string{"Content-Type": "application/json"},
},
{
name: "invalid",
n: Headers(`invalid`),
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.n.Map()
if tt.wantErr {
if err == nil {
t.Errorf("Headers.Map() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("Headers.Map() mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestCookiePartitionKey(t *testing.T) {
t.Parallel()
t.Run("UnmarshalJSON", func(t *testing.T) {
tests := []struct {
name string
data []byte
want CookiePartitionKey
wantErr bool
}{
{
name: "string",
data: []byte(`"example.com"`),
want: CookiePartitionKey{
TopLevelSite: "example.com",
HasCrossSiteAncestor: false,
},
wantErr: false,
},
{
name: "object",
data: []byte(`{"topLevelSite":"example.com","hasCrossSiteAncestor":true}`),
want: CookiePartitionKey{
TopLevelSite: "example.com",
HasCrossSiteAncestor: true,
},
wantErr: false,
},
{
name: "invalid",
data: []byte(`invalid`),
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

var k CookiePartitionKey
if err := k.UnmarshalJSON(tt.data); (err != nil) != tt.wantErr {
t.Errorf("CookiePartitionKey.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
if k != tt.want {
t.Errorf("CookiePartitionKey.UnmarshalJSON() = %v, want %v", k, tt.want)
}
})
}
})
t.Run("String", func(t *testing.T) {
t.Parallel()

k := CookiePartitionKey{
TopLevelSite: "example.com",
}
if got := k.String(); got != k.TopLevelSite {
t.Errorf("CookiePartitionKey.String() = %v, want %v", got, k.TopLevelSite)
}
})
}
Loading