{"record":{"id":"fe305a4197d2aea4","repo":"ory/kratos","slug":"key-s-does-not-exist-in-cookie-v","errorCode":null,"errorMessage":"key %s does not exist in cookie: %+v","messagePattern":"key (.+?) does not exist in cookie: %\\+v","errorType":"error_code","errorClass":"errors.Errorf","httpStatus":null,"severity":"error","filePath":"x/cookie.go","lineNumber":31,"sourceCode":"\n// SessionPersistValues adds values to the session store and persists the changes.\nfunc SessionPersistValues(w http.ResponseWriter, r *http.Request, s sessions.StoreExact, id string, values map[string]interface{}) error {\n\t// The error does not matter because in the worst case we're re-writing the session cookie.\n\tcookie, _ := s.Get(r, id)\n\tfor k, v := range values {\n\t\tcookie.Values[k] = v\n\t}\n\n\treturn errors.WithStack(cookie.Save(r, w))\n}\n\n// SessionGetString returns a string for the given id and key or an error if the session is invalid,\n// the key does not exist, or the key value is not a string.\nfunc SessionGetString(r *http.Request, s sessions.StoreExact, id string, key interface{}) (string, error) {\n\tcheck := func(v map[interface{}]interface{}) (string, error) {\n\t\tvv, ok := v[key]\n\t\tif !ok {\n\t\t\treturn \"\", errors.Errorf(\"key %s does not exist in cookie: %+v\", key, id)\n\t\t} else if vvv, ok := vv.(string); !ok {\n\t\t\treturn \"\", errors.Errorf(\"value of key %s is not of type string in cookie\", key)\n\t\t} else {\n\t\t\treturn vvv, nil\n\t\t}\n\t}\n\n\tvar exactErr error\n\tcookie, err := s.GetExact(r, id, func(s *sessions.Session) bool {\n\t\t_, exactErr = check(s.Values)\n\t\treturn exactErr == nil\n\t})\n\tif err != nil {\n\t\treturn \"\", err\n\t} else if exactErr != nil {\n\t\treturn \"\", exactErr\n\t}\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/ory/kratos/blob/b86338da04a040247a07f46100a86dcfb3875909/x/cookie.go#L13-L49","documentation":"x.SessionGetString reads a value out of a gorilla/sessions cookie store by session id and key. When the cookie's value map does not contain the requested key, this error is returned. It means the cookie exists and decodes, but the expected field is missing.","triggerScenarios":"Calling x.SessionGetString with a key that was never stored (or already expired/evicted) in the session cookie identified by id: check(map) finds v[key] missing.","commonSituations":"Reading a flash/session value after the flash message was consumed, mismatched session key names between writer and reader (e.g. \"user_id\" vs \"userId\"), or an old cookie created before a code change added the key.","solutions":["Ensure the key is set with the exact same name before reading (check the session.Set/flash call site).","Handle the error gracefully: treat missing keys as \"no value\" and redirect to a fresh flow (e.g. re-authentication) instead of failing.","Have users clear cookies / start a new session if stale cookies from an older schema are involved.","Log the cookie contents to verify which keys actually exist before reading."],"exampleFix":"// before\nval, err := x.SessionGetString(r, store, \"sid\", \"userId\")\n// after\nval, err := x.SessionGetString(r, store, \"sid\", \"identity_id\") // key must match the one used at session.Set","handlingStrategy":"try-catch","validationCode":"sess, err := store.Get(r, id)\nif err == nil {\n  if _, ok := sess.Values[key]; !ok { /* key absent — handle before calling SessionGetString */ }\n}","typeGuard":"func cookieHasKey(sess *sessions.Session, key interface{}) bool {\n  _, ok := sess.Values[key]\n  return ok\n}","tryCatchPattern":"val, err := x.SessionGetString(r, store, id, key)\nif err != nil {\n  log.WithError(err).Info(\"session cookie missing key; starting fresh flow\")\n  http.Redirect(w, r, startFlowURL, http.StatusSeeOther)\n  return\n}","preventionTips":["Define session keys as shared constants used by both writer and reader.","Treat missing cookie keys as expected (flash values are consumed once).","Invalidate stale cookies after schema/key changes (rotate cookie name).","Log available keys when debugging unexpected misses."],"tags":["session","cookie","missing-key","gorilla-sessions"],"backgroundTag":"missing-config-key","analyzedSha":"b86338da04a040247a07f46100a86dcfb3875909","analyzedAt":"2026-09-07T15:58:15.934Z","contentChangedAt":"2026-09-07T15:58:15.934Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}