{"record":{"id":"f781ef3f64ce91e9","repo":"ory/hydra","slug":"cookiex-cannot-marshal-envelope","errorCode":null,"errorMessage":"cookiex: cannot marshal envelope","messagePattern":"cookiex: cannot marshal envelope","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"oryx/cookiex/cookiex.go","lineNumber":138,"sourceCode":"\tIssuedAt int64           `json:\"iat\"`\n\tValues   json.RawMessage `json:\"v\"`\n}\n\n// aad binds a ciphertext to this codec's purpose and the cookie name, so a\n// sealed value cannot be replayed as a different cookie or in a different\n// context, even under the same key.\nfunc (c *Codec[T]) aad(name string) []byte {\n\treturn []byte(aadPrefix + \"|\" + c.purpose + \"|\" + name)\n}\n\nfunc (c *Codec[T]) seal(name string, value T) (string, error) {\n\tpayload, err := json.Marshal(value)\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"cookiex: cannot marshal cookie value\")\n\t}\n\tplaintext, err := json.Marshal(envelope{IssuedAt: c.now().Unix(), Values: payload})\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"cookiex: cannot marshal envelope\")\n\t}\n\ta, err := aead.New(c.keys[0])\n\tif err != nil {\n\t\treturn \"\", errors.Wrap(err, \"cookiex: cannot create AEAD\")\n\t}\n\t// The nonce is prepended to the ciphertext. AEADs that manage the nonce\n\t// internally report a nonce size of zero, so this also covers them.\n\tnonce := make([]byte, a.NonceSize())\n\tif _, err := rand.Read(nonce); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"cookiex: cannot generate nonce\")\n\t}\n\tsealed := a.Seal(nonce, nonce, plaintext, c.aad(name))\n\treturn formatPrefix + base64.RawURLEncoding.EncodeToString(sealed), nil\n}\n\nfunc (c *Codec[T]) open(name, value string) (T, error) {\n\tvar zero T\n\traw, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(value, formatPrefix))","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/ory/hydra/blob/4174065ffb052799890f7480f5360a877a67ffc1/oryx/cookiex/cookiex.go#L120-L156","documentation":"Returned by Codec.seal when json.Marshal of the internal envelope{IssuedAt, Values} fails. Because Values is json.RawMessage produced by the preceding successful marshal, this error is practically unreachable and would indicate memory corruption or an invalid RawMessage injected into the envelope.","triggerScenarios":"Thrown at oryx/cookiex/cookiex.go:138 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect the wrapped error; if reachable, the RawMessage payload was mutated between marshals","Ensure the payload type T does not produce invalid raw JSON fragments","Treat as an internal invariant violation and fail the Set call"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"4174065ffb052799890f7480f5360a877a67ffc1","analyzedAt":"2026-09-03T14:52:41.581Z","contentChangedAt":"2026-09-03T14:52:41.581Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}