{"record":{"id":"12a0b7867b632fc7","repo":"JuliusBrussee/caveman","slug":"secretbox-s-decrypted-to-an-empty-secret","errorCode":null,"errorMessage":"secretbox: %s decrypted to an empty secret","messagePattern":"secretbox: (.+?) decrypted to an empty secret","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":185,"sourceCode":"\tplain := strings.TrimSpace(os.Getenv(plaintextEnv))\n\tencoded := strings.TrimSpace(os.Getenv(ciphertextEnv))\n\tproduction := runtimeenv.IsProduction()\n\tif production && plain != \"\" {\n\t\treturn \"\", fmt.Errorf(\"secretbox: production refuses plaintext %s; use %s\", plaintextEnv, ciphertextEnv)\n\t}\n\tif encoded == \"\" {\n\t\treturn plain, nil\n\t}\n\twrapped, err := base64.StdEncoding.DecodeString(encoded)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"secretbox: %s is not valid base64\", ciphertextEnv)\n\t}\n\tdecrypted, err := Decrypt(wrapped)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"secretbox: decrypt %s: %w\", ciphertextEnv, err)\n\t}\n\tif len(decrypted) == 0 {\n\t\treturn \"\", fmt.Errorf(\"secretbox: %s decrypted to an empty secret\", ciphertextEnv)\n\t}\n\treturn string(decrypted), nil\n}\n","sourceCodeStart":167,"sourceCodeEnd":189,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L167-L189","documentation":"ResolveEnvironmentSecret decrypted the ciphertext env var successfully, but the resulting plaintext was empty (zero bytes). An empty secret is never meaningful, so the library rejects it to catch encryption mistakes early — most often someone encrypted an empty string or a whitespace-only value.","triggerScenarios":"Calling ResolveEnvironmentSecret where the ciphertextEnv decodes and decrypts cleanly but the stored plaintext was \"\" — e.g. secretbox.Encrypt([]byte(\"\")) output, or a secret that was blanked during a rotation and never repopulated.","commonSituations":"A CI script encrypting an unset variable (empty expansion) and publishing the resulting envelope; an operator rotating a token by clearing it first; a placeholder left in the secrets store.","solutions":["Set the real secret value before encrypting: verify len(secret) > 0 in the encryption script and regenerate the ciphertext.","Check the rotation/CI pipeline for an empty source variable (e.g. $TOKEN unset → encrypted empty string).","Re-run your secret provisioning tool and confirm the ciphertext env var now decrypts to the expected non-empty value.","Add a startup assertion that every required secret resolves to a non-empty string before serving traffic."],"exampleFix":"// before (script encrypted an unset var)\nenc=$(printf '%s' \"$API_KEY\" | ./encrypt)   # API_KEY empty\n\n// after\n: \"${API_KEY:?API_KEY must be set before encrypting}\"\nenc=$(printf '%s' \"$API_KEY\" | ./encrypt)\nplain, err := secretbox.ResolveEnvironmentSecret(\"\", \"API_KEY\") // ok, non-empty","handlingStrategy":"validation","validationCode":"val, err := secretbox.ResolveEnvironmentSecret(\"\", \"API_KEY\")\nif err == nil && val == \"\" {\n    return fmt.Errorf(\"API_KEY resolved to an empty value; re-provision the secret\")\n}","typeGuard":null,"tryCatchPattern":"val, err := secretbox.ResolveEnvironmentSecret(\"\", \"API_KEY\")\nif err != nil {\n    if strings.Contains(err.Error(), \"decrypted to an empty secret\") {\n        return fmt.Errorf(\"stored ciphertext for API_KEY is empty — regenerate it from the real token\")\n    }\n    return err\n}","preventionTips":["Guard encryption scripts with `: \"${SECRET:?unset}\"` so empty values never get encrypted.","Verify round-trip plaintext is non-empty before publishing ciphertext.","Treat 'secret cleared' as an incident: rotation must write the new value, not blank it.","Add startup assertions that every required secret is non-empty."],"tags":["go","secrets","validation","config"],"backgroundTag":"empty-required-field","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"}