{"record":{"id":"328c8043736894e7","repo":"JuliusBrussee/caveman","slug":"password-must-be-at-least-d-bytes","errorCode":null,"errorMessage":"password must be at least %d bytes","messagePattern":"password must be at least (.+?) bytes","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/security/keys.go","lineNumber":22,"sourceCode":"\t\"crypto/hmac\"\n\t\"crypto/rand\"\n\t\"crypto/sha256\"\n\t\"encoding/base64\"\n\t\"encoding/hex\"\n\t\"fmt\"\n\t\"strings\"\n\n\t\"golang.org/x/crypto/argon2\"\n)\n\nconst (\n\tMinPasswordBytes = 12\n\tMaxPasswordBytes = 1024\n)\n\nfunc ValidatePassword(password string) error {\n\tif len(password) < MinPasswordBytes {\n\t\treturn fmt.Errorf(\"password must be at least %d bytes\", MinPasswordBytes)\n\t}\n\tif len(password) > MaxPasswordBytes {\n\t\treturn fmt.Errorf(\"password must be at most %d bytes\", MaxPasswordBytes)\n\t}\n\treturn nil\n}\n\nfunc GenerateProjectKey() (string, string, error) {\n\traw := make([]byte, 32)\n\tif _, err := rand.Read(raw); err != nil {\n\t\treturn \"\", \"\", err\n\t}\n\tsecret := base64.RawURLEncoding.EncodeToString(raw)\n\tfull := \"cave_live_\" + secret[:12] + \"_\" + secret[12:]\n\treturn full, secret[:12], nil\n}\n\nfunc HashProjectKey(pepper, full string) string {","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/security/keys.go#L4-L40","documentation":"ValidatePassword enforces the shared platform password policy: passwords must be at least MinPasswordBytes (12) bytes in length. The check uses raw byte length (len(password)), so multibyte UTF-8 characters count as multiple bytes. The error names the required minimum so callers can surface it directly to users.","triggerScenarios":"Calling ValidatePassword with a string whose byte length is < 12 — e.g. \"hunter2\", \"Password1!\" (10 chars), or a 12-character string of multibyte characters that would pass a character count but the check is on bytes so it actually passes — specifically any input under 12 bytes.","commonSituations":"User signup/login forms accepting short passwords before validation; generated passwords from an old policy; test fixtures with placeholder passwords; trimming a password to fit a legacy 8-char system.","solutions":["Require the user to supply a password of at least 12 bytes before calling ValidatePassword or the API.","Update password-generation logic to emit >= 12 bytes (e.g. 16+ random bytes base64).","Fix UI/form minimum-length validation to match MinPasswordBytes so users get feedback before the API call.","If you must accept shorter legacy values, migrate them: force a reset and store only policy-compliant passwords."],"exampleFix":"// before\npw := r.FormValue(\"password\") // \"secret12\"\nif err := security.ValidatePassword(pw); err != nil { ... } // password must be at least 12 bytes\n\n// after\npw := r.FormValue(\"password\")\nif len(pw) < security.MinPasswordBytes {\n    http.Error(w, fmt.Sprintf(\"password must be at least %d characters\", security.MinPasswordBytes), http.StatusBadRequest)\n    return\n}\nif err := security.ValidatePassword(pw); err != nil { ... }","handlingStrategy":"validation","validationCode":"if len([]byte(password)) < security.MinPasswordBytes {\n    return fmt.Errorf(\"choose a password of at least %d characters\", security.MinPasswordBytes)\n}","typeGuard":"func meetsMinLength(pw string) bool { return len(pw) >= security.MinPasswordBytes }","tryCatchPattern":"if err := security.ValidatePassword(pw); err != nil {\n    if strings.Contains(err.Error(), \"at least\") {\n        return fmt.Errorf(\"password too weak: %v\", err)\n    }\n    return err\n}","preventionTips":["Mirror MinPasswordBytes in every client-side form validator.","Generate passwords/keys at 16+ bytes so the floor is never brushed.","Enforce the policy at account-creation and change-password endpoints alike.","Force resets for legacy passwords shorter than the policy."],"tags":["go","password","validation","security-policy"],"backgroundTag":"value-out-of-range","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"}