Skip to content

Commit

Permalink
Refactor code flow, tests and errors in cookies
Browse files Browse the repository at this point in the history
Co-authored-by: Ankur Agarwal <[email protected]>
  • Loading branch information
inancgumus and ankur22 committed Sep 8, 2023
1 parent 4a81880 commit 258b482
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
19 changes: 10 additions & 9 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func (b *BrowserContext) Cookies(urls ...string) ([]*api.Cookie, error) {
// filter cookies by the provided URLs.
cookies, err = filterCookies(cookies, urls...)
if err != nil {
return nil, fmt.Errorf("retrieving cookies: %w", err)
return nil, fmt.Errorf("filtering cookies: %w", err)
}
if len(cookies) == 0 {
return nil, nil
Expand All @@ -568,7 +568,7 @@ func filterCookies(cookies []*api.Cookie, urls ...string) ([]*api.Cookie, error)

purls, err := parseURLs(urls...)
if err != nil {
return nil, fmt.Errorf("filtering by URL: %w", err)
return nil, fmt.Errorf("parsing urls: %w", err)
}

// the following algorithm is like a sorting algorithm,
Expand All @@ -595,7 +595,7 @@ func filterCookies(cookies []*api.Cookie, urls ...string) ([]*api.Cookie, error)
var keep bool

for _, uri := range purls {
if shouldFilterCookie(c, uri) {
if shouldKeepCookie(c, uri) {
keep = true
break
}
Expand All @@ -617,10 +617,10 @@ func filterCookies(cookies []*api.Cookie, urls ...string) ([]*api.Cookie, error)
return cookies[:n], nil
}

// shouldFilterCookie determines whether a cookie should be kept,
// shouldKeepCookie determines whether a cookie should be kept,
// based on its compatibility with a specific URL.
// Returns true if the cookie should be kept, false otherwise.
func shouldFilterCookie(c *api.Cookie, uri *url.URL) bool {
func shouldKeepCookie(c *api.Cookie, uri *url.URL) bool {
// Ensure consistent domain formatting for easier comparison.
// A leading dot means the cookie is valid across subdomains.
// For example, if the domain is example.com, then adding a
Expand All @@ -633,20 +633,21 @@ func shouldFilterCookie(c *api.Cookie, uri *url.URL) bool {
// Confirm that the cookie's domain is a suffix of the URL's
// hostname, emulating how a browser would scope cookies to
// specific domains.
if !strings.HasSuffix("."+uri.Hostname(), domain) {
if !strings.HasSuffix(domain, "."+uri.Hostname()) {
return false
}
// Follow RFC 6265 for cookies: an empty or missing path should
// be treated as "/".
//
// See: https://datatracker.ietf.org/doc/html/rfc6265#section-5.1.4
if uri.Path == "" {
uri.Path = "/"
path := c.Path
if path == "" {
path = "/"
}
// Ensure that the cookie applies to the specific path of the
// URL, emulating how a browser would scope cookies to specific
// paths within a domain.
if !strings.HasPrefix(uri.Path, c.Path) {
if !strings.HasPrefix(path, uri.Path) {
return false
}
// Emulate browser behavior: Don't include secure cookies when
Expand Down
32 changes: 26 additions & 6 deletions common/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestNewBrowserContext(t *testing.T) {
}

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

tests := map[string]struct {
filterByURLs []string
cookies []*api.Cookie
Expand Down Expand Up @@ -134,13 +136,16 @@ func TestFilterCookies(t *testing.T) {
"https://sub.foo.com",
},
cookies: []*api.Cookie{
{
Domain: "sub.foo.com",
},
{
Domain: ".foo.com",
},
},
wantCookies: []*api.Cookie{
{
Domain: ".foo.com",
Domain: "sub.foo.com",
},
},
},
Expand Down Expand Up @@ -214,6 +219,23 @@ func TestFilterCookies(t *testing.T) {
},
},
},
"allow_secure_cookie_on_localhost": {
filterByURLs: []string{
"http://localhost",
},
cookies: []*api.Cookie{
{
Domain: "localhost",
Secure: true,
},
},
wantCookies: []*api.Cookie{
{
Domain: "localhost",
Secure: true,
},
},
},
"disallow_secure_cookie_on_http": {
filterByURLs: []string{
"http://foo.com",
Expand Down Expand Up @@ -264,7 +286,10 @@ func TestFilterCookies(t *testing.T) {
},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()

cookies, err := filterCookies(
tt.cookies,
tt.filterByURLs...,
Expand All @@ -276,11 +301,6 @@ func TestFilterCookies(t *testing.T) {
}
require.NoError(t, err)

assert.Lenf(t,
cookies, len(tt.wantCookies),
"incorrect number of cookies filtered. filter: %#v", tt.filterByURLs,
)

assert.Equalf(t,
tt.wantCookies, cookies,
"incorrect cookies filtered by the filter %#v", tt.filterByURLs,
Expand Down
8 changes: 0 additions & 8 deletions tests/browser_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ func TestBrowserContextAddCookies(t *testing.T) {
require.NoErrorf(t,
err, "failed to get cookies from the browser context",
)
require.Lenf(t,
tt.wantCookiesToSet, len(cookies),
"incorrect number of cookies received from the browser context",
)
assert.Equalf(t,
tt.wantCookiesToSet, cookies,
"incorrect cookies received from the browser context",
Expand Down Expand Up @@ -586,10 +582,6 @@ func TestBrowserContextCookies(t *testing.T) {
require.NoErrorf(t,
err, "failed to get cookies from the browser context",
)
assert.Lenf(t,
cookies, len(tt.wantContextCookies),
"incorrect number of cookies received from the browser context",
)
assert.Equalf(t,
tt.wantContextCookies, cookies,
"incorrect cookies received from the browser context",
Expand Down

0 comments on commit 258b482

Please sign in to comment.