-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathrequest.go
More file actions
158 lines (128 loc) · 3.7 KB
/
request.go
File metadata and controls
158 lines (128 loc) · 3.7 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package utilities
import (
"bytes"
"io"
"net"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/supabase/auth/internal/conf"
"github.com/supabase/auth/internal/sbff"
)
func getIPAddressWithXFF(r *http.Request) string {
if r.Header != nil {
xForwardedFor := r.Header.Get("X-Forwarded-For")
if xForwardedFor != "" {
ips := strings.Split(xForwardedFor, ",")
for i := range ips {
ips[i] = strings.TrimSpace(ips[i])
}
for _, ip := range ips {
if ip != "" {
parsed := net.ParseIP(ip)
if parsed == nil {
continue
}
return parsed.String()
}
}
}
}
ipPort := r.RemoteAddr
ip, _, err := net.SplitHostPort(ipPort)
if err != nil {
return ipPort
}
return ip
}
// GetIPAddress returns the real IP address of the HTTP request.
func GetIPAddress(r *http.Request) string {
if sbffAddr, ok := sbff.GetIPAddress(r); ok {
return sbffAddr
}
return getIPAddressWithXFF(r)
}
// GetBodyBytes reads the whole request body properly into a byte array.
func GetBodyBytes(req *http.Request) ([]byte, error) {
if req.Body == nil || req.Body == http.NoBody {
return nil, nil
}
originalBody := req.Body
defer SafeClose(originalBody)
buf, err := io.ReadAll(originalBody)
if err != nil {
return nil, err
}
req.Body = io.NopCloser(bytes.NewReader(buf))
return buf, nil
}
func GetReferrer(r *http.Request, config *conf.GlobalConfiguration) string {
// try get redirect url from query or post data first
reqref := getRedirectTo(r)
if IsRedirectURLValid(config, reqref) {
return reqref
}
// instead try referrer header value
reqref = r.Referer()
if IsRedirectURLValid(config, reqref) {
return reqref
}
return config.SiteURL
}
var decimalIPAddressPattern = regexp.MustCompile("^[0-9]+$")
var regularHostname = regexp.MustCompile("^[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?$")
func IsRedirectURLValid(config *conf.GlobalConfiguration, redirectURL string) bool {
if redirectURL == "" {
return false
}
base, berr := url.Parse(config.SiteURL)
refurl, rerr := url.Parse(redirectURL)
// As long as the referrer came from the site, we will redirect back there
if berr == nil && rerr == nil && base.Hostname() == refurl.Hostname() {
// ensure scheme hasn't changed; most browsers also check this but double check here
if base.Scheme == refurl.Scheme {
// Per RFC 8252 Section 7.3, native apps using a localhost redirect URI
// MUST be allowed to use variable port numbers, so skip the port check
// for loopback addresses.
if base.Port() == refurl.Port() || isLocalhost(refurl.Hostname()) {
return true
}
}
}
if rerr != nil {
// redirect URL is for some reason invalid
return false
}
scheme := strings.TrimSuffix(strings.ToLower(refurl.Scheme), ":")
isHTTP := scheme == "http" || scheme == "https"
if decimalIPAddressPattern.MatchString(refurl.Hostname()) {
// IP address in decimal form also not allowed in redirects!
return false
} else if ip := net.ParseIP(refurl.Hostname()); ip != nil {
return ip.IsLoopback()
} else if isHTTP && !regularHostname.MatchString(refurl.Hostname()) {
// hostname uses characters that are not typically used
return false
}
// For case when user came from mobile app or other permitted resource - redirect back
for _, pattern := range config.URIAllowListMap {
// only match without the fragment
matchAgainst, _, _ := strings.Cut(redirectURL, "#")
if pattern.Match(matchAgainst) {
return true
}
}
return false
}
// getRedirectTo tries extract redirect url from header or from query params
func getRedirectTo(r *http.Request) (reqref string) {
reqref = r.Header.Get("redirect_to")
if reqref != "" {
return
}
if err := r.ParseForm(); err == nil {
reqref = r.Form.Get("redirect_to")
}
return
}