{"record":{"id":"4fbfe93c494d9d14","repo":"beego/beego","slug":"decode-invalid-value-format","errorCode":null,"errorMessage":"Decode: invalid value format","messagePattern":"Decode: invalid value format","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/web/session/sess_utils.go","lineNumber":152,"sourceCode":"\tsig := h.Sum(nil)\n\t// Append mac, remove name.\n\tb = append(b, sig...)[len(name)+1:]\n\t// 4. Encode to base64.\n\tb = encode(b)\n\t// Done.\n\treturn string(b), nil\n}\n\nfunc decodeCookie(block cipher.Block, hashKey, name, value string, gcmaxlifetime int64) (map[interface{}]interface{}, error) {\n\t// 1. Decode from base64.\n\tb, err := decode([]byte(value))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\t// 2. Verify MAC. Value is \"date|value|mac\".\n\tparts := bytes.SplitN(b, []byte(\"|\"), 3)\n\tif len(parts) != 3 {\n\t\treturn nil, errors.New(\"Decode: invalid value format\")\n\t}\n\n\tb = append([]byte(name+\"|\"), b[:len(b)-len(parts[2])]...)\n\th := hmac.New(sha256.New, []byte(hashKey))\n\th.Write(b)\n\tsig := h.Sum(nil)\n\tif len(sig) != len(parts[2]) || subtle.ConstantTimeCompare(sig, parts[2]) != 1 {\n\t\treturn nil, errors.New(\"Decode: the value is not valid\")\n\t}\n\t// 3. Verify date ranges.\n\tvar t1 int64\n\tif t1, err = strconv.ParseInt(string(parts[0]), 10, 64); err != nil {\n\t\treturn nil, errors.New(\"Decode: invalid timestamp\")\n\t}\n\tt2 := time.Now().UTC().Unix()\n\tif t1 > t2 {\n\t\treturn nil, errors.New(\"Decode: timestamp is too new\")\n\t}","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/beego/beego/blob/939cfde380bb9f15844ad633b84f037f7da21584/server/web/session/sess_utils.go#L134-L170","documentation":"decodeCookie (server/web/session/sess_utils.go:152) base64-decodes the cookie value and splits on '|' expecting exactly three parts: date|value|mac. Any other shape means the value was not produced by beego's encodeCookie, so its format is declared invalid before MAC or timestamp checks run.","triggerScenarios":"A foreign or hand-crafted cookie arriving under the session cookie name; a value mangled so a '|' disappeared (double URL-decoding, truncation at a gateway); switching session providers while old-format cookies linger in browsers.","commonSituations":"Another framework (or an older beego layout) using the same cookie name on the same domain; proxy rewrites corrupting the value; manual cookie editing during debugging.","solutions":["Clear the session cookie client-side and obtain a fresh one","Use a distinct SessionName per app/environment so foreign values never reach decodeCookie","Avoid rewriting Cookie headers in proxies; pass them through verbatim"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"kv, err := decodeCookie(block, hashKey, name, value, gcmaxlifetime)\nif err != nil {\n    switch {\n    case strings.Contains(err.Error(), \"invalid value format\"):\n        // foreign or mangled cookie: discard, do not 500\n        clearSessionCookie(w, name)\n        kv = map[interface{}]interface{}{}\n    default:\n        return err\n    }\n}","preventionTips":["Reserve the session cookie name exclusively for one beego app","Pass Cookie headers through proxies untouched","After changing session providers or formats, plan for old cookies to fail once"],"tags":["cookie","session","validation"],"backgroundTag":null,"analyzedSha":"939cfde380bb9f15844ad633b84f037f7da21584","analyzedAt":"2026-08-15T17:22:06.535Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}