{"record":{"id":"eeef53faab0eb50e","repo":"JuliusBrussee/caveman","slug":"password-must-be-at-most-d-bytes","errorCode":null,"errorMessage":"password must be at most %d bytes","messagePattern":"password must be at most (.+?) bytes","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/security/keys.go","lineNumber":25,"sourceCode":"\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 {\n\tmac := hmac.New(sha256.New, []byte(pepper))\n\tmac.Write([]byte(full))\n\treturn hex.EncodeToString(mac.Sum(nil))","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/security/keys.go#L7-L43","documentation":"ValidatePassword rejects passwords longer than MaxPasswordBytes (1024). This upper bound prevents pathological inputs from being fed into key-derivation (which is deliberately expensive) and protects against memory/DoS abuse. Because the check is on bytes, a very long passphrase of multibyte characters can trip it even with a modest character count.","triggerScenarios":"Calling ValidatePassword with a string longer than 1024 bytes — pasted API keys/JWTs used as passwords, generated keys or hex dumps submitted into the password field, or fuzz/abuse traffic sending megabyte bodies.","commonSituations":"Users pasting a full secret or token into a 'passphrase' field; scripts passing entire files as passwords; clients without maxlength attributes on the password input.","solutions":["Cap the password field client-side (input maxlength / server-side length check) to 1024 bytes before calling ValidatePassword.","Detect and reject token-like inputs: if the 'password' looks like a JWT/API key, direct the user to the token field instead.","If long passphrases are legitimate, hash them (e.g. SHA-256) to a fixed size before validation per your policy — but prefer enforcing a sane passphrase limit.","Review request body limits at the HTTP layer so oversized payloads are rejected before reaching password validation."],"exampleFix":"// before\npw := readBody(r)\nif err := security.ValidatePassword(pw); err != nil { ... } // password must be at most 1024 bytes\n\n// after\nif len(pw) > security.MaxPasswordBytes {\n    http.Error(w, fmt.Sprintf(\"password must be at most %d bytes\", security.MaxPasswordBytes), http.StatusBadRequest)\n    return\n}\nif err := security.ValidatePassword(pw); err != nil { ... }","handlingStrategy":"validation","validationCode":"if len([]byte(password)) > security.MaxPasswordBytes {\n    return fmt.Errorf(\"password must be at most %d bytes\", security.MaxPasswordBytes)\n}","typeGuard":"func withinMaxLength(pw string) bool { return len(pw) <= security.MaxPasswordBytes }","tryCatchPattern":"if err := security.ValidatePassword(pw); err != nil {\n    if strings.Contains(err.Error(), \"at most\") {\n        return fmt.Errorf(\"input rejected as a password (too long) — did you paste a key or token?\")\n    }\n    return err\n}","preventionTips":["Set maxlength=1024 (bytes-aware) on password inputs.","Reject request bodies over a sane size at the HTTP layer.","Detect pasted JWT/API-key shapes and route them to token fields.","Log (without contents) oversized password attempts to spot abuse."],"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"}