-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
65 lines (57 loc) · 1.34 KB
/
validate.go
File metadata and controls
65 lines (57 loc) · 1.34 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
package main
import (
"net"
"strings"
"unicode/utf8"
)
func validEmail(email string) bool {
if len(email) == 0 || len(email) > 254 {
return false
}
parts := strings.SplitN(email, "@", 2)
return len(parts) == 2 && len(parts[0]) > 0 && strings.Contains(parts[1], ".")
}
// maskEmail returns "*@domain" for event logging (never log full addresses).
func maskEmail(email string) string {
if i := strings.LastIndex(email, "@"); i >= 0 {
return "*@" + email[i+1:]
}
return ""
}
func validPassword(password string) bool {
n := utf8.RuneCountInString(normalizePassword(password))
return n >= 8 && n <= 64
}
func validLabel(s string) bool {
if len(s) == 0 || len(s) > 32 {
return false
}
for _, c := range s {
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-') {
return false
}
}
return true
}
func validEndpoint(s string) bool {
if len(s) == 0 || len(s) > 253 {
return false
}
i := strings.LastIndex(s, ":")
return i > 0 && i < len(s)-1
}
func validAllowedIPs(s string) bool {
_, _, err := net.ParseCIDR(s)
return err == nil
}
func validWGPubkey(s string) bool {
if len(s) != 44 || s[43] != '=' {
return false
}
for _, c := range s[:43] {
if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '+' || c == '/') {
return false
}
}
return true
}