{"record":{"id":"9dc511a3e3bed169","repo":"JuliusBrussee/caveman","slug":"nonce-entropy-w","errorCode":null,"errorMessage":"nonce entropy: %w","messagePattern":"nonce entropy: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":80,"sourceCode":"\t}\n\tif runtimeenv.IsProduction() {\n\t\treturn nil, fmt.Errorf(\"secretbox: production requires CAVE_KMS_PROVIDER=scaleway\")\n\t}\n\tkeyBytes, err := loadKey()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tblock, err := aes.NewCipher(keyBytes)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"aes cipher: %w\", err)\n\t}\n\tgcm, err := cipher.NewGCM(block)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"aes-gcm: %w\", err)\n\t}\n\tnonce := make([]byte, gcm.NonceSize())\n\tif _, err := rand.Read(nonce); err != nil {\n\t\treturn nil, fmt.Errorf(\"nonce entropy: %w\", err)\n\t}\n\t// Seal appends the ciphertext+tag to nonce, so the returned slice is the\n\t// full nonce||ciphertext envelope.\n\treturn gcm.Seal(nonce, nonce, plaintext, nil), nil\n}\n\n// EncryptPayloadKey wraps an artifact data-encryption key. Production uses the\n// dedicated payload KEK; local development retains the same AES-GCM envelope as\n// other local secrets.\nfunc EncryptPayloadKey(plaintext []byte) ([]byte, error) {\n\tif useKMS() {\n\t\tctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)\n\t\tdefer cancel()\n\t\twrapped, err := kms.EncryptPayload(ctx, plaintext)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"secretbox: payload KMS encrypt: %w\", err)\n\t\t}\n\t\treturn wrapped, nil","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L62-L98","documentation":"Before sealing, secretbox.Encrypt fills the GCM nonce with crypto/rand.Read. If the OS cryptographic random source fails, the error is wrapped as 'nonce entropy'. Because a predictable or reused nonce breaks AES-GCM confidentiality/authenticity, the library correctly refuses to proceed rather than continuing with a weak nonce.","triggerScenarios":"Calling secretbox.Encrypt (local path, non-KMS) when crypto/rand cannot read gcm.NonceSize() (12) bytes — e.g. /dev/urandom unavailable or exhausted in a restricted container, getrandom(2) failing, or a seccomp/sandbox policy blocking the getrandom syscall.","commonSituations":"Minimal Docker images (distroless/scratch) missing entropy setup on old kernels; heavy sandboxing (gVisor, some CI sandboxes) that blocks getrandom; severely restricted chroots without /dev/urandom; VMs with entropy-starved kernels pre-modern-getrandom.","solutions":["Check the wrapped cause: errors.Unwrap to distinguish getrandom failure vs /dev/urandom open failure.","Ensure the runtime OS/kernel supports and permits getrandom(2); update seccomp/AppArmor profiles to allow it.","Use a base image with functioning /dev/urandom (not a stripped one) and a recent kernel.","Restart the host/container if the entropy pool is in a degenerate state, and monitor rand availability."],"exampleFix":"// before: docker run with seccomp blocking getrandom\ndocker run --security-opt seccomp:strict-old.json myapp\n\n// after: allow getrandom in the profile\n\"syscalls\": [{ \"names\": [\"getrandom\"], \"action\": \"SCMP_ACT_ALLOW\" }]","handlingStrategy":"try-catch","validationCode":"if _, err := rand.Read(make([]byte, 12)); err != nil {\n    log.Fatalf(\"crypto/rand unavailable at startup: %v\", err)\n}","typeGuard":null,"tryCatchPattern":"env, err := secretbox.Encrypt(pt)\nif err != nil {\n    var ne randReadError\n    if strings.Contains(err.Error(), \"nonce entropy\") {\n        // non-retryable until entropy source is fixed; crash loudly\n        log.Fatalf(\"entropy source broken: %v\", err)\n    }\n    return err\n}","preventionTips":["Do a one-time crypto/rand read during process startup health check.","Use container images with standard /dev/urandom and modern kernels supporting getrandom.","Review seccomp/AppArmor/sandbox profiles to ensure getrandom is allowed.","Never substitute math/rand or a fixed nonce; treat entropy failure as fatal."],"tags":["encryption","entropy","crypto","go","container"],"backgroundTag":"unexpected-system-failure","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"}