cookieToPlaywrightOptionalCookie passes invalid expires values to Playwright #84

Closed
opened 2026-03-13 01:26:08 +00:00 by Claude · 2 comments
Collaborator

Bug

cookieToPlaywrightOptionalCookie() in playwright.go:130 unconditionally converts the cookie's Expires field to a unix timestamp:

Expires: playwright.Float(float64(cookie.Expires.Unix())),

Playwright's AddCookies() requires the expires field to be either:

  • -1 — session cookie (no expiry)
  • A positive unix timestamp in seconds

When a cookie has no expiry set (session cookie), cookie.Expires is the zero time.Time, and .Unix() returns -62135596800 — a large negative number that Playwright rejects.

This also affects cookies like Cloudflare's __cf_bm which may have an expires value that results in 0 or another non-positive timestamp.

Observed Error

WARN - skipping invalid cookie
  "error": "playwright: Cookie should have a valid expires, only -1 or a positive number for the unix timestamp in seconds is allowed"
  "host": "apnews.com"
  "name": "__cf_bm"

This warning fires on every browser initialization when cookies are loaded from the jar, which happens repeatedly during newsfeed polling.

Suggested Fix

In cookieToPlaywrightOptionalCookie(), sanitize the expires value:

func cookieToPlaywrightOptionalCookie(cookie Cookie) playwright.OptionalCookie {
	expires := float64(cookie.Expires.Unix())
	if cookie.Expires.IsZero() || expires <= 0 {
		expires = -1
	}

	oc := playwright.OptionalCookie{
		Name:     cookie.Name,
		Value:    cookie.Value,
		Domain:   playwright.String(cookie.Host),
		Path:     playwright.String(cookie.Path),
		Expires:  playwright.Float(expires),
		Secure:   playwright.Bool(cookie.Secure),
		HttpOnly: playwright.Bool(cookie.HttpOnly),
	}
	// ...
}

Location

  • playwright.go:124-138cookieToPlaywrightOptionalCookie()
  • browser_init.go:155-159 — where the warning is logged
## Bug `cookieToPlaywrightOptionalCookie()` in `playwright.go:130` unconditionally converts the cookie's `Expires` field to a unix timestamp: ```go Expires: playwright.Float(float64(cookie.Expires.Unix())), ``` Playwright's `AddCookies()` requires the `expires` field to be either: - `-1` — session cookie (no expiry) - A **positive** unix timestamp in seconds When a cookie has no expiry set (session cookie), `cookie.Expires` is the zero `time.Time`, and `.Unix()` returns `-62135596800` — a large negative number that Playwright rejects. This also affects cookies like Cloudflare's `__cf_bm` which may have an expires value that results in `0` or another non-positive timestamp. ## Observed Error ``` WARN - skipping invalid cookie "error": "playwright: Cookie should have a valid expires, only -1 or a positive number for the unix timestamp in seconds is allowed" "host": "apnews.com" "name": "__cf_bm" ``` This warning fires on every browser initialization when cookies are loaded from the jar, which happens repeatedly during newsfeed polling. ## Suggested Fix In `cookieToPlaywrightOptionalCookie()`, sanitize the expires value: ```go func cookieToPlaywrightOptionalCookie(cookie Cookie) playwright.OptionalCookie { expires := float64(cookie.Expires.Unix()) if cookie.Expires.IsZero() || expires <= 0 { expires = -1 } oc := playwright.OptionalCookie{ Name: cookie.Name, Value: cookie.Value, Domain: playwright.String(cookie.Host), Path: playwright.String(cookie.Path), Expires: playwright.Float(expires), Secure: playwright.Bool(cookie.Secure), HttpOnly: playwright.Bool(cookie.HttpOnly), } // ... } ``` ## Location - `playwright.go:124-138` — `cookieToPlaywrightOptionalCookie()` - `browser_init.go:155-159` — where the warning is logged
Author
Collaborator

Starting work on this. Plan: sanitize the expires value in cookieToPlaywrightOptionalCookie() so that zero/non-positive timestamps become -1 (session cookie), matching Playwright's requirements.

Starting work on this. Plan: sanitize the `expires` value in `cookieToPlaywrightOptionalCookie()` so that zero/non-positive timestamps become `-1` (session cookie), matching Playwright's requirements.
Author
Collaborator

Fixed in PR #85 (merged). The cookieToPlaywrightOptionalCookie() function now sets expires = -1 when cookie.Expires.IsZero() or the unix timestamp is non-positive, matching Playwright's requirements.

Fixed in PR #85 (merged). The `cookieToPlaywrightOptionalCookie()` function now sets `expires = -1` when `cookie.Expires.IsZero()` or the unix timestamp is non-positive, matching Playwright's requirements.
Sign in to join this conversation.