{"record":{"id":"b1f1c466cbef9ab6","repo":"crowdsecurity/crowdsec","slug":"failed-to-generate-nonce-w","errorCode":null,"errorMessage":"failed to generate nonce: %w","messagePattern":"failed to generate nonce: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"pkg/appsec/challenge/crypto.go","lineNumber":145,"sourceCode":"\tgcm, err := cipher.NewGCM(block)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create GCM: %w\", err)\n\t}\n\n\t// Reject an over-limit envelope before marshaling it.\n\tmaxPlaintext := maxCookieLen/4*3 - 1 - gcm.NonceSize() - gcm.Overhead()\n\tif plaintextLen := cookiePlaintextFixedHeaderLen + len(reason) + proto.Size(envelope); plaintextLen > maxPlaintext {\n\t\treturn \"\", fmt.Errorf(\"%w: plaintext=%d > %d\", ErrCookieTooLarge, plaintextLen, maxPlaintext)\n\t}\n\n\tenvelopeBytes, err := proto.Marshal(envelope)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to marshal challenge cookie proto: %w\", err)\n\t}\n\n\tnonce := make([]byte, gcm.NonceSize())\n\tif _, err := rand.Read(nonce); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to generate nonce: %w\", err)\n\t}\n\n\t// Build the plaintext: not_after_be8 || flags || reason_len_be || reason || envelope\n\tplaintext := make([]byte, 0, cookiePlaintextFixedHeaderLen+len(reason)+len(envelopeBytes))\n\n\tvar notAfterBytes [8]byte\n\tbinary.BigEndian.PutUint64(notAfterBytes[:], uint64(notAfter))\n\tplaintext = append(plaintext, notAfterBytes[:]...)\n\n\tplaintext = append(plaintext, flags)\n\n\tvar reasonLenBytes [2]byte\n\tbinary.BigEndian.PutUint16(reasonLenBytes[:], uint16(len(reason)))\n\tplaintext = append(plaintext, reasonLenBytes[:]...)\n\n\tplaintext = append(plaintext, []byte(reason)...)\n\tplaintext = append(plaintext, envelopeBytes...)\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/appsec/challenge/crypto.go#L127-L163","documentation":"sealCookieV0 generates the random GCM nonce with crypto/rand.Read. This error means the OS CSPRNG failed, so the cookie cannot be sealed safely — without a fresh random nonce, AES-GCM nonce reuse would catastrophically break confidentiality and authenticity. The library fails closed.","triggerScenarios":"Calling sealCookieV0 (via SealAllowlistCookie or ValidateChallengeResponse) when crypto/rand.Read returns an error — e.g. getrandom(2) blocked by a seccomp/sandbox profile, fd exhaustion, or a broken /dev/urandom in the container.","commonSituations":"Containers or sandboxes with restrictive seccomp profiles blocking getrandom; severely resource-starved hosts; unusual kernels or restricted environments (some hardened container runtimes).","solutions":["Inspect the wrapped OS error (errno) to identify the CSPRNG failure cause.","Check the container/sandbox seccomp profile allows the getrandom syscall.","Verify /dev/urandom is available and the host kernel is sane.","Restart the process after fixing the environment; this is not retryable within the same call."],"exampleFix":"// seccomp profile (docker/containerd): ensure getrandom is permitted\n\"syscalls\": [\n  {\"names\": [\"getrandom\"], \"action\": \"SCMP_ACT_ALLOW\"}\n]","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := errors.Is(err, randErr); err != nil {\n    // log the OS-level cause; fix sandbox/CSPRNG; restart process\n    return fmt.Errorf(\"cookie sealing unavailable, entropy failure: %w\", err)\n}","preventionTips":["Ensure seccomp/sandbox profiles allow the getrandom syscall.","Monitor for crypto/rand failures in high-assurance environments.","Avoid running with a broken or exhausted /dev/urandom in containers.","Fail closed: never cache or reuse nonces to mask an entropy failure."],"tags":["appsec","crypto","entropy","csprng"],"backgroundTag":"csprng-failure","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}