{"record":{"id":"2de44e12c8e2d45a","repo":"qax-os/excelize","slug":"errpasswordlengthinvalid","errorCode":"ErrPasswordLengthInvalid","errorMessage":"password length invalid","messagePattern":"password length invalid","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"errors.go","lineNumber":129,"sourceCode":"\tErrMaxGraphicNameLength = fmt.Errorf(\"the name length exceeds the %d characters limit\", MaxGraphicNameLength)\n\t// ErrOptionsUnzipSizeLimit defined the error message for receiving\n\t// invalid UnzipSizeLimit and UnzipXMLSizeLimit.\n\tErrOptionsUnzipSizeLimit = errors.New(\"the value of UnzipSizeLimit should be greater than or equal to UnzipXMLSizeLimit\")\n\t// ErrOutlineLevel defined the error message on receive an invalid outline\n\t// level number.\n\tErrOutlineLevel = errors.New(\"invalid outline level\")\n\t// ErrPageSetupAdjustTo defined the error message for receiving a page setup\n\t// adjust to value exceeds limit.\n\tErrPageSetupAdjustTo = errors.New(\"adjust to value must be an integer from 0 to 400\")\n\t// ErrParameterInvalid defined the error message on receive the invalid\n\t// parameter.\n\tErrParameterInvalid = errors.New(\"parameter is invalid\")\n\t// ErrParameterRequired defined the error message on receive the empty\n\t// parameter.\n\tErrParameterRequired = errors.New(\"parameter is required\")\n\t// ErrPasswordLengthInvalid defined the error message on invalid password\n\t// length.\n\tErrPasswordLengthInvalid = errors.New(\"password length invalid\")\n\t// ErrPivotTableShowValuesAsBaseField defined the error message on enable\n\t// this kind of \"show values as\" type requires a base field.\n\tErrPivotTableShowValuesAsBaseField = errors.New(\"this kind of show values as type requires a base field\")\n\t// ErrPivotTableShowValuesAsBaseItem defined the error message on enable\n\t// this kind of \"show values as\" type and base field requires a base item.\n\tErrPivotTableShowValuesAsBaseItem = errors.New(\"this kind of show values as type and base field requires a base item\")\n\t// ErrPivotTableClassicLayout defined the error message on enable\n\t// ClassicLayout and CompactData in the same time.\n\tErrPivotTableClassicLayout = errors.New(\"cannot enable ClassicLayout and CompactData in the same time\")\n\t// ErrSave defined the error message for saving file.\n\tErrSave = errors.New(\"no path defined for file, consider File.WriteTo or File.Write\")\n\t// ErrSheetIdx defined the error message on receive the invalid worksheet\n\t// index.\n\tErrSheetIdx = errors.New(\"invalid worksheet index\")\n\t// ErrSheetNameBlank defined the error message on receive the blank sheet\n\t// name.\n\tErrSheetNameBlank = errors.New(\"the sheet name can not be blank\")\n\t// ErrSheetNameInvalid defined the error message on receive the sheet name","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/qax-os/excelize/blob/f2483381fbfbe432a6baa98d48a1c277183f3285/errors.go#L111-L147","documentation":"ErrPasswordLengthInvalid is returned when a password is empty or exceeds MaxFieldLength UTF-16 code units. It is raised in standardKeyEncryption (file encryption) and genISOPasswdHash (sheet/workbook protection hash generation).","triggerScenarios":"Calling File.Encrypt/SaveAs with encryption where the password is empty or too long; File.ProtectSheet or ProtectWorkbook with a password of length 0 or greater than MaxFieldLength (measured with countUTF16String).","commonSituations":"Empty password variables from un-set env vars or config keys; very long generated tokens used as passwords; multi-byte characters pushing the UTF-16 length over the limit.","solutions":["Supply a non-empty password within MaxFieldLength UTF-16 units","Check the password length in your code before calling Encrypt/ProtectSheet/ProtectWorkbook","Trim or hash long secrets to a bounded value before use"],"exampleFix":"// before\npassword := os.Getenv(\"XLSX_PASS\") // may be empty\nerr := f.ProtectSheet(\"Sheet1\", &excelize.SheetProtectionOptions{Password: password})\n// after\npassword := os.Getenv(\"XLSX_PASS\")\nif len(password) == 0 || len(utf16.Encode([]rune(password))) > 255 {\n    return errors.New(\"password length invalid\")\n}\nerr := f.ProtectSheet(\"Sheet1\", &excelize.SheetProtectionOptions{Password: password})","handlingStrategy":"validation","validationCode":"func validPassword(p string) bool {\n    n := len(utf16.Encode([]rune(p)))\n    return n >= 1 && n <= 255 // within MaxFieldLength\n}","typeGuard":"func validPassword(p string) bool { return len(utf16.Encode([]rune(p))) >= 1 && len(utf16.Encode([]rune(p))) <= 255 }","tryCatchPattern":"if err := f.ProtectSheet(sheet, opts); errors.Is(err, excelize.ErrPasswordLengthInvalid) { // prompt for a new password }","preventionTips":["Check password length in UTF-16 units before encrypting or protecting","Never use empty strings from unset env vars as passwords","Hash or truncate overly long secrets to a bounded length"],"tags":["go","excelize","password","encryption"],"backgroundTag":"invalid-password-length","analyzedSha":"f2483381fbfbe432a6baa98d48a1c277183f3285","analyzedAt":"2026-09-02T01:26:19.299Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}