{"record":{"id":"0362c793d57a68f7","repo":"JuliusBrussee/caveman","slug":"secretbox-production-refuses-plaintext-s-use-s","errorCode":null,"errorMessage":"secretbox: production refuses plaintext %s; use %s","messagePattern":"secretbox: production refuses plaintext (.+?); use (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/secretbox/secretbox.go","lineNumber":171,"sourceCode":"\t\treturn plaintext, nil\n\t}\n\treturn Decrypt(envelope)\n}\n\nfunc useKMS() bool {\n\treturn strings.EqualFold(strings.TrimSpace(os.Getenv(\"CAVE_KMS_PROVIDER\")), kms.ProviderScaleway)\n}\n\n// ResolveEnvironmentSecret loads a boot-time secret. In production plaintext\n// environment variables are rejected: operators must provide a base64-encoded\n// secretbox/KMS envelope in ciphertextEnv. Local development may continue using\n// plaintextEnv. An entirely absent optional secret returns an empty string.\nfunc ResolveEnvironmentSecret(plaintextEnv, ciphertextEnv string) (string, error) {\n\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":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/secretbox/secretbox.go#L153-L189","documentation":"ResolveEnvironmentSecret enforces that in production (per runtimeenv.IsProduction) secrets are supplied only as encrypted values in the ciphertext env var; a non-empty plaintext env var is rejected outright to prevent secrets leaking through insecure channels. The error names both variables so the operator knows which pair to fix.","triggerScenarios":"Calling ResolveEnvironmentSecret(plaintextEnv, ciphertextEnv) while the process is in production mode and os.Getenv(plaintextEnv) is set to a non-empty, non-whitespace value — regardless of whether the ciphertext var is also set.","commonSituations":"Copying a docker-compose / dev .env file (which sets plain env vars) to a production deployment; a platform injecting legacy plaintext vars; someone setting the plaintext var as a 'fallback' during an incident.","solutions":["Remove the plaintext env var from the production environment and set only the ciphertext variable with a secretbox-encrypted, base64 value.","If local testing is intended, run with the non-production runtime environment flag so plaintext is permitted.","Audit your deployment manifest / IaC for legacy plaintext secret keys and delete them from production templates.","Encrypt the value with secretbox.Encrypt, base64-encode it, and place it in the ciphertextEnv variable instead."],"exampleFix":"// before (prod manifest)\nenv:\n  APP_TOKEN: \"sk-live-abc123\"          # plaintext\n  APP_TOKEN_CIPHERTEXT: \"<base64>\"\n\n// after\nenv:\n  # APP_TOKEN removed for production\n  APP_TOKEN_CIPHERTEXT: \"<base64 secretbox envelope>\"","handlingStrategy":"validation","validationCode":"if runtimeenv.IsProduction() && strings.TrimSpace(os.Getenv(plaintextEnv)) != \"\" {\n    return fmt.Errorf(\"%s must not be set in production; provide %s instead\", plaintextEnv, ciphertextEnv)\n}","typeGuard":null,"tryCatchPattern":"val, err := secretbox.ResolveEnvironmentSecret(\"APP_TOKEN\", \"APP_TOKEN_CIPHERTEXT\")\nif err != nil {\n    if strings.Contains(err.Error(), \"production refuses plaintext\") {\n        log.Fatalf(\"deployment misconfiguration: %v\", err)\n    }\n    return err\n}","preventionTips":["Keep plaintext secret vars only in dev-only .env files excluded from prod images/manifests.","Lint IaC templates to forbid known plaintext secret keys in production.","Encrypt every prod secret and set only the *_CIPHERTEXT variable.","Run a startup config audit that fails fast when plaintext vars are present in prod."],"tags":["go","secrets","config","production"],"backgroundTag":"plaintext-secret-in-production","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"}