bug: updateCookies ignores error from page.Context().Cookies() #7

Closed
opened 2026-02-14 16:05:50 +00:00 by Claude · 2 comments
Collaborator

Parent: #1

Description

In playwright.go:138-153, updateCookies() calls page.Context().Cookies(page.URL()) and assigns both cookies and err, but err is not checked before iterating cookies:

func (b playWrightBrowser) updateCookies(_ context.Context, page playwright.Page) error {
    if b.cookieJar != nil {
        cookies, err := page.Context().Cookies(page.URL())
        // BUG: err is not checked here — if Cookies() fails, cookies may be nil/empty
        // and err is only used in the loop below for Set()

        for _, cookie := range cookies {
            err = b.cookieJar.Set(playwrightCookieToCookie(cookie))
            if err != nil {
                return fmt.Errorf("error setting cookie in cookie jar: %w", err)
            }
        }
    }
    return nil
}

If Cookies() returns an error, it's silently swallowed and the cookie jar won't be updated.

Fix

Add if err != nil { return fmt.Errorf("error getting cookies: %w", err) } after the Cookies() call.

**Parent:** #1 ## Description In `playwright.go:138-153`, `updateCookies()` calls `page.Context().Cookies(page.URL())` and assigns both `cookies` and `err`, but `err` is not checked before iterating `cookies`: ```go func (b playWrightBrowser) updateCookies(_ context.Context, page playwright.Page) error { if b.cookieJar != nil { cookies, err := page.Context().Cookies(page.URL()) // BUG: err is not checked here — if Cookies() fails, cookies may be nil/empty // and err is only used in the loop below for Set() for _, cookie := range cookies { err = b.cookieJar.Set(playwrightCookieToCookie(cookie)) if err != nil { return fmt.Errorf("error setting cookie in cookie jar: %w", err) } } } return nil } ``` If `Cookies()` returns an error, it's silently swallowed and the cookie jar won't be updated. ## Fix Add `if err != nil { return fmt.Errorf("error getting cookies: %w", err) }` after the `Cookies()` call.
Claude added the bugpriority/mediumtype/task labels 2026-02-14 16:06:15 +00:00
Author
Collaborator

Starting work on this as part of PR 6 (also includes #18). Will add error check for page.Context().Cookies() in playwright.go:140.

Starting work on this as part of PR 6 (also includes #18). Will add error check for `page.Context().Cookies()` in `playwright.go:140`.
Author
Collaborator

Work finished. PR: #37 (merged)

Added error check for page.Context().Cookies() before iterating.

Work finished. PR: #37 (merged) Added error check for `page.Context().Cookies()` before iterating.
Sign in to join this conversation.