Merge pull request #1855 from badgerbees/fix/telegram-group-id-validation
fix(identity): support negative integers in isNumeric for Telegram group IDs
This commit is contained in:
commit
e6ea9c4ff3
2 changed files with 20 additions and 3 deletions
|
|
@ -94,13 +94,18 @@ func MatchAllowed(sender bus.SenderInfo, allowed string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// isNumeric returns true if s consists entirely of digits.
|
// isNumeric returns true if s consists entirely of digits, allowing for an optional leading minus sign
|
||||||
|
// (required for Telegram group/channel IDs like -1001234567890).
|
||||||
func isNumeric(s string) bool {
|
func isNumeric(s string) bool {
|
||||||
if s == "" {
|
if s == "" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, r := range s {
|
start := 0
|
||||||
if r < '0' || r > '9' {
|
if s[0] == '-' && len(s) > 1 {
|
||||||
|
start = 1
|
||||||
|
}
|
||||||
|
for i := start; i < len(s); i++ {
|
||||||
|
if s[i] < '0' || s[i] > '9' {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,15 @@ func TestMatchAllowed(t *testing.T) {
|
||||||
allowed: "654321",
|
allowed: "654321",
|
||||||
want: false,
|
want: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "negative numeric ID matches PlatformID",
|
||||||
|
sender: bus.SenderInfo{
|
||||||
|
Platform: "telegram",
|
||||||
|
PlatformID: "-1001234567890",
|
||||||
|
},
|
||||||
|
allowed: "-1001234567890",
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
// Username matching
|
// Username matching
|
||||||
{
|
{
|
||||||
name: "@username matches Username",
|
name: "@username matches Username",
|
||||||
|
|
@ -238,6 +247,9 @@ func TestIsNumeric(t *testing.T) {
|
||||||
{"abc", false},
|
{"abc", false},
|
||||||
{"12a34", false},
|
{"12a34", false},
|
||||||
{"telegram", false},
|
{"telegram", false},
|
||||||
|
{"-1001234567890", true},
|
||||||
|
{"-", false},
|
||||||
|
{"-12a34", false},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue