Skip to content

Commit 07f4e23

Browse files
committed
Fixing the reveiw comments
1 parent a431b3b commit 07f4e23

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

connection.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ import (
5656
)
5757

5858
var (
59-
connectionLogger = logger.New("connection")
59+
connectionLogger = logger.New("connection")
60+
asciiTotpRegex = regexp.MustCompile(`^[0-9]{6}$`) // precompiled: exactly 6 ASCII digits
6061
)
6162

6263
const (
@@ -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.
771772
func 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

0 commit comments

Comments
 (0)