File tree Expand file tree Collapse file tree 1 file changed +14
-8
lines changed
Expand file tree Collapse file tree 1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -56,7 +56,8 @@ import (
5656)
5757
5858var (
59- connectionLogger = logger .New ("connection" )
59+ connectionLogger = logger .New ("connection" )
60+ asciiTotpRegex = regexp .MustCompile (`^[0-9]{6}$` ) // precompiled: exactly 6 ASCII digits
6061)
6162
6263const (
@@ -769,13 +770,18 @@ func (v *connection) authSendOAuthAccessToken() error {
769770// validateTOTP ensures the TOTP string is a 1-6 digit numeric code.
770771// Returns an error if blank, non-numeric, or longer than 6 digits.
771772func validateTOTP (t string ) error {
772- if t == "" {
773- return fmt .Errorf ("Invalid TOTP: Cannot be empty" )
774- }
775- if ! regexp .MustCompile (`^\d+$` ).MatchString (t ) {
776- return fmt .Errorf ("Invalid TOTP: contains non-numeric characters" )
777- }
778- if len (t ) > 6 {
773+ // Enforce exactly six ASCII digits. Avoid \d which matches Unicode digits.
774+ if ! asciiTotpRegex .MatchString (t ) {
775+ if t == "" {
776+ return fmt .Errorf ("Invalid TOTP: cannot be empty" )
777+ }
778+ // Provide more granular feedback for common cases.
779+ for _ , ch := range t {
780+ if ch < '0' || ch > '9' { // Non-ASCII digit
781+ return fmt .Errorf ("Invalid TOTP: contains non-numeric characters" )
782+ }
783+ }
784+ // All chars are digits but length wrong
779785 return fmt .Errorf ("Invalid TOTP: must be 6 digits" )
780786 }
781787 return nil
You can’t perform that action at this time.
0 commit comments