cloudflare/cloudflared · error

message is too short to contain a nonce

Error message

message is too short to contain a nonce

What it means

This error comes from token.Encrypter.Decrypt when the ciphertext buffer is shorter than 24 bytes, the size of a NaCl secretbox/nonce. Messages produced by Encrypt embed the nonce as the first 24 bytes of the payload, so anything shorter cannot possibly contain one and decryption aborts immediately.

Source

Thrown at token/encrypt.go:72

	if err != nil {
		return nil, err
	}
	return &Encrypter{privateKey: key, publicKey: pubKey}, nil
}

// PublicKey returns a base64 encoded public key. Useful for transport (like in HTTP requests)
func (e *Encrypter) PublicKey() string {
	return base64.URLEncoding.EncodeToString(e.publicKey[:])
}

// Decrypt data that was encrypted using our publicKey. It will use our privateKey and the sender's publicKey to decrypt
// data is an encrypted buffer of data, mostly like from the Encrypt function. Messages contain the nonce data on the front
// of the message.
// senderPublicKey is a base64 encoded version of the sender's public key (most likely from the PublicKey function).
// The return value is the decrypted buffer or an error.
func (e *Encrypter) Decrypt(data []byte, senderPublicKey string) ([]byte, error) {
	if len(data) < 24 {
		return nil, errors.New("message is too short to contain a nonce")
	}
	var decryptNonce [24]byte
	copy(decryptNonce[:], data[:24]) // we pull the nonce from the front of the actual message.
	pubKey, err := e.decodePublicKey(senderPublicKey)
	if err != nil {
		return nil, err
	}
	decrypted, ok := box.Open(nil, data[24:], &decryptNonce, pubKey, e.privateKey)
	if !ok {
		return nil, errors.New("failed to decrypt message")
	}
	return decrypted, nil
}

// decodePublicKey will base64 decode the provided key to the box representation
func (e *Encrypter) decodePublicKey(key string) (*[32]byte, error) {
	pub, err := base64.URLEncoding.DecodeString(key)
	if err != nil {

View on GitHub (pinned to 2253eeeb25)

Solutions

  1. Verify the input is the full output of Encrypt (nonce prefix + ciphertext) and that no truncation happened during storage or base64 round-trips.
  2. Check len(data) >= 24 before calling Decrypt and report a clear error about the corrupted/short message.
  3. Confirm the same encryption format/version produced the data — old records without a 24-byte nonce prefix must be re-encrypted.
  4. Ensure base64 decoding uses the same encoding (standard vs URL-safe, with/without padding) that Encrypt's output was stored with.

Example fix

// before
plain, err := encrypter.Decrypt(shortBlob, senderPub)
// after
if len(shortBlob) < 24 {
    return nil, fmt.Errorf("encrypted token too short (%d bytes): missing nonce", len(shortBlob))
}
plain, err := encrypter.Decrypt(shortBlob, senderPub)
Defensive patterns

Strategy: validation

Validate before calling

if len(data) < 24 {
    return nil, fmt.Errorf("ciphertext too short (%d bytes): nonce missing", len(data))
}
plain, err := e.Decrypt(data, senderPublicKey)

Type guard

func hasNoncePrefix(data []byte) bool { return len(data) >= 24 }

Try / catch

plain, err := e.Decrypt(data, senderPub)
if err != nil && strings.Contains(err.Error(), "too short to contain a nonce") {
    // treat record as corrupt; re-fetch or re-encrypt
}

Prevention

When it happens

Trigger: Calling e.Decrypt(data, senderPublicKey) with len(data) < 24 — e.g. an empty or truncated message, passing the plaintext instead of the ciphertext, a base64 payload that was decoded incorrectly, or a store that clipped the record.

Common situations: Reading an encrypted token from a file/database that was written by an older format without the nonce prefix; copy-paste or base64 encoding/decoding mistakes dropping bytes; partially-written files; decrypting a non-encrypted value by mistake.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of cloudflare/cloudflared@2253eeeb25 (2026-09-06). Data as JSON: /api/errors/0a710c0dbebe128d. Report an issue: GitHub.