{"record":{"id":"892b04f59334e11e","repo":"JuliusBrussee/caveman","slug":"envelope-nonce-entropy-w","errorCode":null,"errorMessage":"envelope: nonce entropy: %w","messagePattern":"envelope: nonce entropy: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/envelope/envelope.go","lineNumber":84,"sourceCode":"\treturn seal(plaintext, schemeV2, aad, scopeHash)\n}\n\nfunc seal(plaintext []byte, scheme string, aad []byte, scopeHash string) (ciphertext []byte, metaJSON []byte, err error) {\n\tdataKey := make([]byte, 32)\n\tif _, err := rand.Read(dataKey); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: data key entropy: %w\", err)\n\t}\n\tblock, err := aes.NewCipher(dataKey)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: aes: %w\", err)\n\t}\n\tgcm, err := cipher.NewGCM(block)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: gcm: %w\", err)\n\t}\n\tnonce := make([]byte, gcm.NonceSize())\n\tif _, err := rand.Read(nonce); err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: nonce entropy: %w\", err)\n\t}\n\tciphertext = gcm.Seal(nonce, nonce, plaintext, aad)\n\n\twrapped, err := secretbox.EncryptPayloadKey(dataKey)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: wrap data key: %w\", err)\n\t}\n\tmeta := Metadata{Scheme: scheme, WrappedDataKey: base64.StdEncoding.EncodeToString(wrapped), ScopeHash: scopeHash}\n\tmetaJSON, err = json.Marshal(meta)\n\tif err != nil {\n\t\treturn nil, nil, fmt.Errorf(\"envelope: marshal metadata: %w\", err)\n\t}\n\treturn ciphertext, metaJSON, nil\n}\n\n// Open reverses Seal: it unwraps the data key from metadata and decrypts the\n// ciphertext. An unknown scheme fails closed.\nfunc Open(ciphertext []byte, metaJSON []byte) ([]byte, error) {","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/envelope/envelope.go#L66-L102","documentation":"seal draws the GCM nonce from crypto/rand and wraps a failed read as 'envelope: nonce entropy: %w'. Random nonces are mandatory for GCM security, so the library fails closed rather than sealing with a predictable or repeated nonce.","triggerScenarios":"Seal/SealForScope while the second rand.Read (nonce) fails — same OS entropy failure mode as the data key read.","commonSituations":"Host entropy exhaustion or getrandom failures in containers/sandboxes; degraded VM RNG; seccomp policies blocking getrandom.","solutions":["Inspect the wrapped cause for the OS RNG error.","Restore host entropy (fix /dev/urandom, seccomp, or getrandom availability).","Redeploy on a healthy host and retry the operation.","Update Go to the latest patch release if the platform has known crypto/rand defects."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"ciphertext, meta, err := envelope.Seal(plaintext, aad)\nif err != nil {\n    if strings.Contains(err.Error(), \"nonce entropy\") {\n        return fmt.Errorf(\"RNG failed during nonce generation; refusing to encrypt: %w\", err)\n    }\n    return err\n}","preventionTips":["Keep host RNG healthy (adequate entropy, unblocked getrandom).","Alert on any crypto/rand error — nonce reuse would be catastrophic for GCM.","Never cache or reuse nonces across Seal calls."],"tags":["crypto","nonce","entropy","gcm"],"backgroundTag":"internal-invariant-violation","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}