{"id":"24ac6a0db0c92139","repo":"gofiber/fiber","slug":"fiber-encrypt-cookie-middleware-requires-key","errorCode":null,"errorMessage":"fiber: encrypt cookie middleware requires key","messagePattern":"fiber: encrypt cookie middleware requires key","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"middleware/encryptcookie/config.go","lineNumber":74,"sourceCode":"\t\tif cfg.Next == nil {\n\t\t\tcfg.Next = ConfigDefault.Next\n\t\t}\n\n\t\tif cfg.Except == nil {\n\t\t\tcfg.Except = ConfigDefault.Except\n\t\t}\n\n\t\tif cfg.Encryptor == nil {\n\t\t\tcfg.Encryptor = ConfigDefault.Encryptor\n\t\t}\n\n\t\tif cfg.Decryptor == nil {\n\t\t\tcfg.Decryptor = ConfigDefault.Decryptor\n\t\t}\n\t}\n\n\tif cfg.Key == \"\" {\n\t\tpanic(\"fiber: encrypt cookie middleware requires key\")\n\t}\n\n\tif err := validateKey(cfg.Key); err != nil {\n\t\tpanic(err)\n\t}\n\n\treturn cfg\n}\n","sourceCodeStart":56,"sourceCodeEnd":83,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/middleware/encryptcookie/config.go#L56-L83","documentation":"The encryptcookie middleware needs a symmetric key to AES-GCM encrypt/decrypt cookie values; an empty Key is unusable. configDefault (config.go:73-75) panics when cfg.Key == \"\" so the app fails fast at startup instead of silently passing cookies through unencrypted.","triggerScenarios":"Calling encryptcookie.New(encryptcookie.Config{}) with no Key, or passing a Config whose Key field is unset (zero value \"\"). Common when the key is sourced from an env var that is not set in the current environment.","commonSituations":"Forgetting to inject the ENCRYPT_COOKIE_KEY env var in a new deploy environment, or initializing the middleware in a test without setting Key.","solutions":["Generate a key with encryptcookie.GenerateKey(32) and pass it: encryptcookie.New(encryptcookie.Config{Key: key}).","Load Key from a secret manager or env var and fail app startup if it is empty before calling New.","Use the same key across all instances that share signed cookies; rotate by versioning the key."],"exampleFix":"// before\nencryptcookie.New(encryptcookie.Config{})\n\n// after\nkey := os.Getenv(\"COOKIE_ENCRYPT_KEY\")\nif key == \"\" { log.Fatal(\"COOKIE_ENCRYPT_KEY not set\") }\nencryptcookie.New(encryptcookie.Config{Key: key})","handlingStrategy":"validation","validationCode":"func resolveEncryptKey() string {\n    k := os.Getenv(\"COOKIE_ENCRYPT_KEY\")\n    if k == \"\" {\n        log.Fatal(\"COOKIE_ENCRYPT_KEY is not set; generate one with encryptcookie.GenerateKey(32)\")\n    }\n    return k\n}\n\ncfg := encryptcookie.Config{Key: resolveEncryptKey()}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fail fast at startup if the key env var is empty.","Store the key in a secret manager, not in source control.","Document the key requirement in your service's README."],"tags":["encryptcookie","security","config","key","cookies","panic"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}