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

Fix edge case in range syntax sugar #2255

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions pkg/secrets/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/rand"
"math/big"
"sort"
"unicode"

"k8s.io/apimachinery/pkg/util/sets"
)
Expand Down Expand Up @@ -48,23 +49,18 @@ func inflateRanges(characterSet string) string {
)
for i := 0; i < len(runeSet); i++ {
cur := runeSet[i]
if alphanumeric(cur) {
// Alphanumeric character detected
if i+2 < len(runeSet) && runeSet[i+1] == '-' && alphanumeric(runeSet[i+2]) {
// Range detected, convert to full set of characters
start, end := cur, runeSet[i+2]
if start > end {
// Swap start and end if they're out of order
start, end = end, start
}

// Alphanumeric character detected
if alphanumeric(cur) && (i+2 < len(runeSet) && runeSet[i+1] == '-' && alphanumeric(runeSet[i+2])) {
// Range detected, convert to full set of characters
start, end := cur, runeSet[i+2]
if start <= end && !mixedRange(start, end) {
for c := start; c <= end; c++ {
if alphanumeric(c) {
inflated.Insert(c)
}
}

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

// alphanumeric returns true IFF the given rune is alphanumeric; e.g. [A-z0-9] .
func alphanumeric(r rune) bool {
cv := int(r)
return (cv >= int('A') && cv <= int('Z')) || (cv >= int('a') && cv <= int('z')) || (cv >= int('0') && cv <= int('9'))
return (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9')
}

// mixedRange returns true IFF the given runes don't have the same casing or aren't both numbers.
func mixedRange(a, b rune) bool {
switch {
case unicode.IsNumber(a):
return !unicode.IsNumber(b)
case unicode.IsUpper(a):
return !unicode.IsUpper(b)
case unicode.IsLower(a):
return !unicode.IsLower(b)
}

return true
}
24 changes: 18 additions & 6 deletions pkg/secrets/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func TestInflateRanges(t *testing.T) {
name: "-",
want: "-",
},
{
name: "0-a",
want: "-0a",
},
{
name: "A-",
want: "-A",
Expand All @@ -33,22 +37,30 @@ func TestInflateRanges(t *testing.T) {
},
{
name: "Z-A",
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
want: "-AZ",
},
{
name: "A-z",
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
want: "-Az",
},
{
name: "z-A",
want: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
want: "-Az",
},
{
name: "a-z",
want: "abcdefghijklmnopqrstuvwxyz",
name: "a-Z",
want: "-Za",
},
{
name: "Z-a",
want: "-Za",
},
{
name: "z-a",
want: "-az",
},
{
name: "a-z",
want: "abcdefghijklmnopqrstuvwxyz",
},
{
Expand All @@ -57,7 +69,7 @@ func TestInflateRanges(t *testing.T) {
},
{
name: "9-0",
want: "0123456789",
want: "-09",
},
{
name: "0-9A-Z",
Expand Down