Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit 5763b65

Browse files
authored
Merge pull request #2255 from njhale/fix-gen-secret-sugar
Fix edge case in range syntax sugar
2 parents 87ccaa2 + ef36bde commit 5763b65

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

pkg/secrets/generate.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/rand"
55
"math/big"
66
"sort"
7+
"unicode"
78

89
"k8s.io/apimachinery/pkg/util/sets"
910
)
@@ -48,23 +49,18 @@ func inflateRanges(characterSet string) string {
4849
)
4950
for i := 0; i < len(runeSet); i++ {
5051
cur := runeSet[i]
51-
if alphanumeric(cur) {
52-
// Alphanumeric character detected
53-
if i+2 < len(runeSet) && runeSet[i+1] == '-' && alphanumeric(runeSet[i+2]) {
54-
// Range detected, convert to full set of characters
55-
start, end := cur, runeSet[i+2]
56-
if start > end {
57-
// Swap start and end if they're out of order
58-
start, end = end, start
59-
}
60-
52+
// Alphanumeric character detected
53+
if alphanumeric(cur) && (i+2 < len(runeSet) && runeSet[i+1] == '-' && alphanumeric(runeSet[i+2])) {
54+
// Range detected, convert to full set of characters
55+
start, end := cur, runeSet[i+2]
56+
if start <= end && !mixedRange(start, end) {
6157
for c := start; c <= end; c++ {
6258
if alphanumeric(c) {
6359
inflated.Insert(c)
6460
}
6561
}
6662

67-
// Skip the next two characters since we've already processed them
63+
// Skip the next two characters since they were part of the range
6864
i += 2
6965
continue
7066
}
@@ -83,6 +79,19 @@ func inflateRanges(characterSet string) string {
8379

8480
// alphanumeric returns true IFF the given rune is alphanumeric; e.g. [A-z0-9] .
8581
func alphanumeric(r rune) bool {
86-
cv := int(r)
87-
return (cv >= int('A') && cv <= int('Z')) || (cv >= int('a') && cv <= int('z')) || (cv >= int('0') && cv <= int('9'))
82+
return (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9')
83+
}
84+
85+
// mixedRange returns true IFF the given runes don't have the same casing or aren't both numbers.
86+
func mixedRange(a, b rune) bool {
87+
switch {
88+
case unicode.IsNumber(a):
89+
return !unicode.IsNumber(b)
90+
case unicode.IsUpper(a):
91+
return !unicode.IsUpper(b)
92+
case unicode.IsLower(a):
93+
return !unicode.IsLower(b)
94+
}
95+
96+
return true
8897
}

pkg/secrets/generate_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func TestInflateRanges(t *testing.T) {
1919
name: "-",
2020
want: "-",
2121
},
22+
{
23+
name: "0-a",
24+
want: "-0a",
25+
},
2226
{
2327
name: "A-",
2428
want: "-A",
@@ -33,22 +37,30 @@ func TestInflateRanges(t *testing.T) {
3337
},
3438
{
3539
name: "Z-A",
36-
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
40+
want: "-AZ",
3741
},
3842
{
3943
name: "A-z",
40-
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
44+
want: "-Az",
4145
},
4246
{
4347
name: "z-A",
44-
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
48+
want: "-Az",
4549
},
4650
{
47-
name: "a-z",
48-
want: "abcdefghijklmnopqrstuvwxyz",
51+
name: "a-Z",
52+
want: "-Za",
53+
},
54+
{
55+
name: "Z-a",
56+
want: "-Za",
4957
},
5058
{
5159
name: "z-a",
60+
want: "-az",
61+
},
62+
{
63+
name: "a-z",
5264
want: "abcdefghijklmnopqrstuvwxyz",
5365
},
5466
{
@@ -57,7 +69,7 @@ func TestInflateRanges(t *testing.T) {
5769
},
5870
{
5971
name: "9-0",
60-
want: "0123456789",
72+
want: "-09",
6173
},
6274
{
6375
name: "0-9A-Z",

0 commit comments

Comments
 (0)