{"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/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/browser/common/browser_context.go#L456-L492","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Provide a url per cookie — the simplest valid form: { name, value, url: 'https://example.com/path' }","Or provide both domain and path explicitly: { name, value, domain: 'example.com', path: '/' }","When copying cookies from ctx.cookies(), carry over domain and path unchanged instead of rebuilding objects"],"exampleFix":"// before\nctx.addCookies([{ name: 'session', value: 'abc' }]); // no url, no domain/path\n\n// after\nctx.addCookies([{ name: 'session', value: 'abc', url: 'https://example.com' }]);\n// or: { name: 'session', value: 'abc', domain: 'example.com', path: '/' }","handlingStrategy":"validation","validationCode":"function isValidCookieScoping(c) {\n  return (typeof c.url === 'string' && c.url !== '')\n    || ((typeof c.domain === 'string' && c.domain !== '')\n        && (typeof c.path === 'string' && c.path !== ''));\n}\nconst cookies = [{ name: 'session', value: 'abc' }].map((c) => ({\n  ...c,\n  url: 'https://example.com', // ensure scoping is always present\n}));\nif (cookies.every(isValidCookieScoping)) ctx.addCookies(cookies);","typeGuard":"const isScopedCookie = (c) =>\n  c != null && typeof c === 'object'\n  && ((typeof c.url === 'string' && c.url) !== ''\n      || (Boolean(c.domain) && Boolean(c.path)));","tryCatchPattern":null,"preventionTips":["Default to giving each cookie a url — simplest valid scoping","Otherwise always set both domain and path (path: '/' is typical)","When copying cookies via ctx.cookies(), keep their domain/path fields intact"],"tags":["browser","cookies","validation","domain"],"backgroundTag":"cookie-validation-failed","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}