ory/hydra · error

cookiex: cannot create AEAD

Error message

cookiex: cannot create AEAD

What it means

Returned by Codec.seal when constructing the AEAD cipher from the codec's 32-byte key fails. Keys are already fixed-size arrays derived by HKDF at construction time, so this only fires if the underlying AEAD implementation rejects the key — an environmental/library-level failure, not bad input.

Source

Thrown at oryx/cookiex/cookiex.go:142

// aad binds a ciphertext to this codec's purpose and the cookie name, so a
// sealed value cannot be replayed as a different cookie or in a different
// context, even under the same key.
func (c *Codec[T]) aad(name string) []byte {
	return []byte(aadPrefix + "|" + c.purpose + "|" + name)
}

func (c *Codec[T]) seal(name string, value T) (string, error) {
	payload, err := json.Marshal(value)
	if err != nil {
		return "", errors.Wrap(err, "cookiex: cannot marshal cookie value")
	}
	plaintext, err := json.Marshal(envelope{IssuedAt: c.now().Unix(), Values: payload})
	if err != nil {
		return "", errors.Wrap(err, "cookiex: cannot marshal envelope")
	}
	a, err := aead.New(c.keys[0])
	if err != nil {
		return "", errors.Wrap(err, "cookiex: cannot create AEAD")
	}
	// The nonce is prepended to the ciphertext. AEADs that manage the nonce
	// internally report a nonce size of zero, so this also covers them.
	nonce := make([]byte, a.NonceSize())
	if _, err := rand.Read(nonce); err != nil {
		return "", errors.Wrap(err, "cookiex: cannot generate nonce")
	}
	sealed := a.Seal(nonce, nonce, plaintext, c.aad(name))
	return formatPrefix + base64.RawURLEncoding.EncodeToString(sealed), nil
}

func (c *Codec[T]) open(name, value string) (T, error) {
	var zero T
	raw, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(value, formatPrefix))
	if err != nil {
		return zero, errors.WithStack(ErrInvalidCookie)
	}
	for _, key := range c.keys {

View on GitHub (pinned to 4174065ffb)

Solutions

  1. Check the wrapped error for the cipher-library failure reason
  2. Verify the codec was built via New so keys are correctly HKDF-derived 32-byte arrays
  3. Treat as non-recoverable for this request; signal a server-side error
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at oryx/cookiex/cookiex.go:142 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ory/hydra@4174065ffb (2026-09-03). Data as JSON: /api/errors/7931b513c4d16cf1. Report an issue: GitHub.