Repository navigation
Expand file tree
/
Copy pathutil.go
More file actions
90 lines (78 loc) · 2.92 KB
/
Copy pathutil.go
File metadata and controls
90 lines (78 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"crypto/subtle"
"errors"
"math/rand"
"net/http"
"regexp"
"strings"
"time"
"github.com/go-chi/chi/v5"
)
func RandomString(length int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
result := make([]byte, length)
for i := 0; i < length; i++ {
result[i] = chars[r.Intn(len(chars))]
}
return string(result)
}
// normalizeName canonicalizes a satellite or target name. Names are
// case-insensitive because viper lower-cases all map keys when reading the
// config file, so every lookup must use the lower-case form.
func normalizeName(name string) string {
return strings.ToLower(name)
}
// normalizeNames applies normalizeName to every element of a slice.
func normalizeNames(names []string) []string {
if names == nil {
return nil
}
out := make([]string, len(names))
for i, n := range names {
out[i] = normalizeName(n)
}
return out
}
// nameParam returns the normalized satellite/target name from a URL parameter.
func nameParam(r *http.Request, key string) string {
return normalizeName(chi.URLParam(r, key))
}
// ValidateIdentifier validates that an identifier (satellite name, target name, etc.)
// is not empty, within reasonable length, and contains only allowed characters.
// Allowed characters: alphanumeric, hyphen, underscore, dot
// Maximum length: 255 characters
func ValidateIdentifier(identifier string, identifierType string) error {
if identifier == "" {
return errors.New(identifierType + " cannot be empty")
}
if len(identifier) > 255 {
return errors.New(identifierType + " exceeds maximum length of 255 characters")
}
// Allow alphanumeric characters, hyphens, underscores, and dots
// Must not start or end with special characters
validPattern := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9]$|^[a-zA-Z0-9]$`)
if !validPattern.MatchString(identifier) {
return errors.New(identifierType + " contains invalid characters or format. Only alphanumeric, hyphen, underscore, and dot are allowed")
}
return nil
}
// SecureCompareStrings performs a constant-time comparison of two strings.
// This function is resistant to timing attacks and should be used for comparing
// sensitive values like secrets, tokens, and passwords.
// Returns true if the strings are equal, false otherwise.
func SecureCompareStrings(a, b string) bool {
// Convert strings to byte slices for constant-time comparison
// subtle.ConstantTimeCompare returns 1 if equal, 0 if not equal
return subtle.ConstantTimeCompare([]byte(a), []byte(b)) == 1
}
// CheckSecret verifies a client-provided secret against the configured one.
// Unlike SecureCompareStrings it fails closed: an unset (empty) configured
// secret or an empty provided secret never authenticates.
func CheckSecret(provided, expected string) bool {
if expected == "" || provided == "" {
return false
}
return SecureCompareStrings(provided, expected)
}