grafana/k6 · error

if cookie URL is not provided, both domain and path must be…

Error message

if cookie URL is not provided, both domain and path must be specified: %#v

What it means

Client-side validation in AddCookies mirroring CDP's Network.setCookie rules: if a cookie has no URL, BOTH Domain and Path must be set. A cookie with only a domain, only a path, or neither is rejected with the full cookie printed via %#v. No CDP call is made.

Solutions

  1. Provide a url per cookie — the simplest valid form: { name, value, url: 'https://example.com/path' }
  2. Or provide both domain and path explicitly: { name, value, domain: 'example.com', path: '/' }
  3. When copying cookies from ctx.cookies(), carry over domain and path unchanged instead of rebuilding objects

Example fix

// before
ctx.addCookies([{ name: 'session', value: 'abc' }]); // no url, no domain/path

// after
ctx.addCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);
// or: { name: 'session', value: 'abc', domain: 'example.com', path: '/' }
Defensive patterns

Strategy: validation

Validate before calling

function isValidCookieScoping(c) {
  return (typeof c.url === 'string' && c.url !== '')
    || ((typeof c.domain === 'string' && c.domain !== '')
        && (typeof c.path === 'string' && c.path !== ''));
}
const cookies = [{ name: 'session', value: 'abc' }].map((c) => ({
  ...c,
  url: 'https://example.com', // ensure scoping is always present
}));
if (cookies.every(isValidCookieScoping)) ctx.addCookies(cookies);

Type guard

const isScopedCookie = (c) =>
  c != null && typeof c === 'object'
  && ((typeof c.url === 'string' && c.url) !== ''
      || (Boolean(c.domain) && Boolean(c.path)));

Prevention

When it happens

Trigger: browserContext.addCookies([{ name: 'n', value: 'v' }]) with neither url nor domain/path; cookies parsed from document.cookie strings (which carry no domain/path attributes); only domain: 'example.com' provided without path.

Common situations: Porting cookies captured from document.cookie or from another context's cookies() output and dropping fields; assuming the browser defaults path to '/' — k6 requires you to be explicit.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/f96691bdaab6f780. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/browser_context.go:474

	b.logger.Debugf("BrowserContext:AddCookies", "bctxid:%v", b.id)

	// skip work if no cookies provided.
	if len(cookies) == 0 {
		return fmt.Errorf("no cookies provided")
	}

	cookiesToSet := make([]*network.CookieParam, 0, len(cookies))
	for _, c := range cookies {
		if c.Name == "" {
			return fmt.Errorf("cookie name must be set: %#v", c)
		}
		if c.Value == "" {
			return fmt.Errorf("cookie value must be set: %#v", c)
		}
		// if URL is not set, both Domain and Path must be provided
		if c.URL == "" && (c.Domain == "" || c.Path == "") {
			const msg = "if cookie URL is not provided, both domain and path must be specified: %#v"
			return fmt.Errorf(msg, c)
		}
		// calculate the cookie expiration date, session cookie if not set.
		var ts *cdp.TimeSinceEpoch
		if c.Expires > 0 {
			t := cdp.TimeSinceEpoch(time.Unix(c.Expires, 0))
			ts = &t
		}
		cookiesToSet = append(cookiesToSet, &network.CookieParam{
			Name:     c.Name,
			Value:    c.Value,
			Domain:   c.Domain,
			Path:     c.Path,
			URL:      c.URL,
			Expires:  ts,
			HTTPOnly: c.HTTPOnly,
			Secure:   c.Secure,
			SameSite: network.CookieSameSite(c.SameSite),
		})

View on GitHub (pinned to 01ffac6f24)