-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvalidation.go
More file actions
105 lines (86 loc) · 3.19 KB
/
validation.go
File metadata and controls
105 lines (86 loc) · 3.19 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package oauth
import (
"fmt"
"net"
"net/url"
"strings"
)
// ValidateIssuerURL validates that an OIDC issuer URL is properly formatted.
// Enforces HTTPS for non-localhost URLs to prevent MITM attacks.
func ValidateIssuerURL(issuer string) error {
if issuer == "" {
return fmt.Errorf("issuer URL cannot be empty")
}
// Parse the URL
parsedURL, err := url.Parse(issuer)
if err != nil {
return fmt.Errorf("invalid issuer URL format: %w", err)
}
// Must be http or https scheme
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
return fmt.Errorf("issuer URL must use http or https scheme, got: %s", parsedURL.Scheme)
}
// Must have a host
if parsedURL.Host == "" {
return fmt.Errorf("issuer URL must have a host")
}
// Enforce HTTPS for non-localhost
if !isLocalhostHostname(parsedURL.Host) && parsedURL.Scheme != "https" {
return fmt.Errorf("issuer URL must use HTTPS for non-localhost hosts, got: %s", parsedURL.Scheme)
}
// Validate that the hostname is not an IP address (unless localhost)
// IP addresses in issuer URLs can be problematic for certificate validation
host := parsedURL.Hostname()
if net.ParseIP(host) != nil && !isLocalhostHostname(host) {
return fmt.Errorf("issuer URL hostname should not be a raw IP address (use a domain name instead), got: %s", host)
}
// Check for suspicious patterns
if strings.Contains(host, "..") || strings.HasPrefix(host, ".") {
return fmt.Errorf("issuer URL hostname contains invalid patterns")
}
return nil
}
// ValidateRedirectURI validates a redirect URI for security.
// Checks for proper URL format, scheme, and prevents open redirect vulnerabilities.
func ValidateRedirectURI(redirectURI string) error {
if redirectURI == "" {
return fmt.Errorf("redirect URI cannot be empty")
}
// Parse the URL
parsedURL, err := url.Parse(redirectURI)
if err != nil {
return fmt.Errorf("invalid redirect URI format: %w", err)
}
// Must be http or https scheme
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
return fmt.Errorf("redirect URI must use http or https scheme, got: %s", parsedURL.Scheme)
}
// Must have a host
if parsedURL.Host == "" {
return fmt.Errorf("redirect URI must have a host")
}
// Enforce HTTPS for non-localhost
if !isLocalhostHostname(parsedURL.Host) && parsedURL.Scheme != "https" {
return fmt.Errorf("redirect URI must use HTTPS for non-localhost hosts, got: %s (use https://)", parsedURL.Scheme)
}
// Prevent fragment in redirect URI (OAuth 2.0 security requirement)
if parsedURL.Fragment != "" {
return fmt.Errorf("redirect URI must not contain a fragment (per OAuth 2.0 spec)")
}
// Check for suspicious patterns in host
if strings.Contains(parsedURL.Host, "..") || strings.HasPrefix(parsedURL.Host, ".") {
return fmt.Errorf("redirect URI hostname contains invalid patterns")
}
return nil
}
// isLocalhostHostname checks if a hostname is localhost for development.
func isLocalhostHostname(host string) bool {
// Remove port if present
hostname, _, err := net.SplitHostPort(host)
if err != nil {
hostname = host
}
// Check for localhost variants
hostname = strings.ToLower(hostname)
return hostname == "localhost" || hostname == "127.0.0.1" || hostname == "::1"
}