{"record":{"id":"f96691bdaab6f780","repo":"grafana/k6","slug":"if-cookie-url-is-not-provided-both-domain-and-pat","errorCode":null,"errorMessage":"if cookie URL is not provided, both domain and path must be specified: %#v","messagePattern":"if cookie URL is not provided, both domain and path must be specified: %#v","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/browser_context.go","lineNumber":474,"sourceCode":"\tb.logger.Debugf(\"BrowserContext:AddCookies\", \"bctxid:%v\", b.id)\n\n\t// skip work if no cookies provided.\n\tif len(cookies) == 0 {\n\t\treturn fmt.Errorf(\"no cookies provided\")\n\t}\n\n\tcookiesToSet := make([]*network.CookieParam, 0, len(cookies))\n\tfor _, c := range cookies {\n\t\tif c.Name == \"\" {\n\t\t\treturn fmt.Errorf(\"cookie name must be set: %#v\", c)\n\t\t}\n\t\tif c.Value == \"\" {\n\t\t\treturn fmt.Errorf(\"cookie value must be set: %#v\", c)\n\t\t}\n\t\t// if URL is not set, both Domain and Path must be provided\n\t\tif c.URL == \"\" && (c.Domain == \"\" || c.Path == \"\") {\n\t\t\tconst msg = \"if cookie URL is not provided, both domain and path must be specified: %#v\"\n\t\t\treturn fmt.Errorf(msg, c)\n\t\t}\n\t\t// calculate the cookie expiration date, session cookie if not set.\n\t\tvar ts *cdp.TimeSinceEpoch\n\t\tif c.Expires > 0 {\n\t\t\tt := cdp.TimeSinceEpoch(time.Unix(c.Expires, 0))\n\t\t\tts = &t\n\t\t}\n\t\tcookiesToSet = append(cookiesToSet, &network.CookieParam{\n\t\t\tName:     c.Name,\n\t\t\tValue:    c.Value,\n\t\t\tDomain:   c.Domain,\n\t\t\tPath:     c.Path,\n\t\t\tURL:      c.URL,\n\t\t\tExpires:  ts,\n\t\t\tHTTPOnly: c.HTTPOnly,\n\t\t\tSecure:   c.Secure,\n\t\t\tSameSite: network.CookieSameSite(c.SameSite),\n\t\t})","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/browser_context.go#L456-L492","documentation":"Validation error from BrowserContext.AddCookies: a cookie provided neither a url nor the domain+path pair. CDP cookie semantics require either an exact URL to scope the cookie, or explicit domain and path; k6 enforces this before the CDP call and rejects the whole request if any cookie violates it.","triggerScenarios":"Calling browserContext.addCookies([{ name: 'sid', value: '1', domain: 'example.com' }]) (path missing), or with only url empty and only one of domain/path set, or with neither field at all.","commonSituations":"Porting Playwright cookie fixtures that use url only and accidentally dropping it; assuming domain alone is enough because that is common in other tools; building cookies from document.cookie strings (which lack domain/path scoping info) without adding them back.","solutions":["Prefer the simplest form: give the cookie a url, e.g. { name, value, url: 'https://example.com' }.","Alternatively provide both domain and path explicitly, e.g. { name, value, domain: 'example.com', path: '/' }.","When importing cookies from a HAR or another context, make sure each entry carries url or domain+path before passing it in."],"exampleFix":"// before\ncontext.addCookies([{ name: 'sid', value: '1', domain: 'example.com' }]); // no path, no url\n\n// after\ncontext.addCookies([{ name: 'sid', value: '1', url: 'https://example.com' }]);\n// or: { name: 'sid', value: '1', domain: 'example.com', path: '/' }","handlingStrategy":"validation","validationCode":"function isValidCookieScoping(c) {\n  const hasUrl = typeof c.url === 'string' && c.url !== '';\n  const hasDomainAndPath =\n    typeof c.domain === 'string' && c.domain !== '' &&\n    typeof c.path === 'string' && c.path !== '';\n  return hasUrl || hasDomainAndPath;\n}\nif (!cookies.every(isValidCookieScoping)) {\n  throw new Error('each cookie needs \"url\" or both \"domain\" and \"path\"');\n}\ncontext.addCookies(cookies);","typeGuard":"function isScopedCookie(c) {\n  return (c.url != null && c.url !== '') ||\n         (c.domain != null && c.domain !== '' && c.path != null && c.path !== '');\n}","tryCatchPattern":null,"preventionTips":["Standardize on the url form ({ name, value, url }) unless you need cross-path scoping.","When exporting cookies from another context, carry url (or domain+path) through the pipeline.","Add a pre-flight validator for cookie arrays in shared test helpers."],"tags":["cookies","validation","browser-context"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}