Skip to content

Commit f192353

Browse files
authored
Merge pull request from GHSA-xg2h-wx96-xgxr
Remove unnecessary checks on a value that is already definitely an alphanum
2 parents 864fea7 + c707277 commit f192353

4 files changed

+76
-45
lines changed

cryptorandomstringutils.go

+2-23
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"math"
2323
"math/big"
24-
"regexp"
2524
"unicode"
2625
)
2726

@@ -99,27 +98,7 @@ Returns:
9998
error - an error stemming from an invalid parameter within underlying function, CryptoRandom(...)
10099
*/
101100
func CryptoRandomAlphaNumeric(count int) (string, error) {
102-
if count == 0 {
103-
return "", nil
104-
}
105-
RandomString, err := CryptoRandom(count, 0, 0, true, true)
106-
if err != nil {
107-
return "", fmt.Errorf("Error: %s", err)
108-
}
109-
match, err := regexp.MatchString("([0-9]+)", RandomString)
110-
if err != nil {
111-
panic(err)
112-
}
113-
114-
if !match {
115-
//Get the position between 0 and the length of the string-1 to insert a random number
116-
position := getCryptoRandomInt(count)
117-
//Insert a random number between [0-9] in the position
118-
RandomString = RandomString[:position] + string('0' + getCryptoRandomInt(10)) + RandomString[position + 1:]
119-
return RandomString, err
120-
}
121-
return RandomString, err
122-
101+
return CryptoRandom(count, 0, 0, true, true)
123102
}
124103

125104
/*
@@ -204,7 +183,7 @@ func CryptoRandom(count int, start int, end int, letters bool, numbers bool, cha
204183
if chars == nil {
205184
ch = rune(getCryptoRandomInt(gap) + int64(start))
206185
} else {
207-
ch = chars[getCryptoRandomInt(gap) + int64(start)]
186+
ch = chars[getCryptoRandomInt(gap)+int64(start)]
208187
}
209188

210189
if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {

cryptorandomstringutils_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package goutils
22

33
import (
4+
"regexp"
5+
"strconv"
46
"testing"
57
"unicode/utf8"
68
)
@@ -74,3 +76,37 @@ func TestCryptoRandomAlphaNumeric(t *testing.T) {
7476
}
7577
}
7678
}
79+
80+
func TestCryptoRandAlphaNumeric_FuzzOnlyNumeric(t *testing.T) {
81+
82+
// Testing for a reported regression in which some versions produced
83+
// a predictably small set of chars.
84+
iters := 1000
85+
charlen := 0
86+
for i := 0; i < 16; i++ {
87+
numOnly := 0
88+
charlen++
89+
for i := 0; i < iters; i++ {
90+
out, err := CryptoRandomAlphaNumeric(charlen)
91+
if err != nil {
92+
t.Fatal("func failed to produce a random thinger")
93+
}
94+
if _, err := strconv.Atoi(out); err == nil {
95+
numOnly++
96+
}
97+
98+
m, err := regexp.MatchString("^[0-9a-zA-Z]+$", out)
99+
if err != nil {
100+
t.Fatal(err)
101+
}
102+
if !m {
103+
t.Fatal("Character is not alphanum")
104+
}
105+
}
106+
107+
if numOnly == iters {
108+
t.Fatalf("Got %d numeric-only random sequences", numOnly)
109+
}
110+
}
111+
112+
}

randomstringutils.go

+2-22
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"math"
2222
"math/rand"
23-
"regexp"
2423
"time"
2524
"unicode"
2625
)
@@ -75,12 +74,10 @@ func RandomNumeric(count int) (string, error) {
7574

7675
/*
7776
RandomAlphabetic creates a random string whose length is the number of characters specified.
78-
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
77+
Characters will be chosen from the set of alphabetic characters.
7978
8079
Parameters:
8180
count - the length of random string to create
82-
letters - if true, generated string may include alphabetic characters
83-
numbers - if true, generated string may include numeric characters
8481
8582
Returns:
8683
string - the random string
@@ -102,24 +99,7 @@ Returns:
10299
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
103100
*/
104101
func RandomAlphaNumeric(count int) (string, error) {
105-
RandomString, err := Random(count, 0, 0, true, true)
106-
if err != nil {
107-
return "", fmt.Errorf("Error: %s", err)
108-
}
109-
match, err := regexp.MatchString("([0-9]+)", RandomString)
110-
if err != nil {
111-
panic(err)
112-
}
113-
114-
if !match {
115-
//Get the position between 0 and the length of the string-1 to insert a random number
116-
position := rand.Intn(count)
117-
//Insert a random number between [0-9] in the position
118-
RandomString = RandomString[:position] + string('0'+rand.Intn(10)) + RandomString[position+1:]
119-
return RandomString, err
120-
}
121-
return RandomString, err
122-
102+
return Random(count, 0, 0, true, true)
123103
}
124104

125105
/*

randomstringutils_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package goutils
33
import (
44
"fmt"
55
"math/rand"
6+
"regexp"
7+
"strconv"
68
"testing"
79
)
810

@@ -76,3 +78,37 @@ func ExampleRandomSeed() {
7678
// H_I;E
7779
// 2b2ca
7880
}
81+
82+
func TestRandAlphaNumeric_FuzzOnlyNumeric(t *testing.T) {
83+
84+
// Testing for a reported regression in which some versions produced
85+
// a predictably small set of chars.
86+
iters := 1000
87+
charlen := 0
88+
for i := 0; i < 16; i++ {
89+
numOnly := 0
90+
charlen++
91+
for i := 0; i < iters; i++ {
92+
out, err := RandomAlphaNumeric(charlen)
93+
if err != nil {
94+
t.Fatal("func failed to produce a random thinger")
95+
}
96+
if _, err := strconv.Atoi(out); err == nil {
97+
numOnly++
98+
}
99+
100+
m, err := regexp.MatchString("^[0-9a-zA-Z]+$", out)
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
if !m {
105+
t.Fatal("Character is not alphanum")
106+
}
107+
}
108+
109+
if numOnly == iters {
110+
t.Fatalf("Got %d numeric-only random sequences", numOnly)
111+
}
112+
}
113+
114+
}

0 commit comments

Comments
 (0)