-
Notifications
You must be signed in to change notification settings - Fork 278
/
helpers.go
215 lines (183 loc) · 5.15 KB
/
helpers.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package api
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"github.com/gobuffalo/uuid"
"github.com/netlify/gotrue/conf"
"github.com/netlify/gotrue/models"
"github.com/netlify/gotrue/storage"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func addRequestID(globalConfig *conf.GlobalConfiguration) middlewareHandler {
return func(w http.ResponseWriter, r *http.Request) (context.Context, error) {
id := ""
if globalConfig.API.RequestIDHeader != "" {
id = r.Header.Get(globalConfig.API.RequestIDHeader)
}
if id == "" {
uid, err := uuid.NewV4()
if err != nil {
return nil, err
}
id = uid.String()
}
ctx := r.Context()
ctx = withRequestID(ctx, id)
return ctx, nil
}
}
func sendJSON(w http.ResponseWriter, status int, obj interface{}) error {
w.Header().Set("Content-Type", "application/json")
b, err := json.Marshal(obj)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Error encoding json response: %v", obj))
}
w.WriteHeader(status)
_, err = w.Write(b)
return err
}
func getUserFromClaims(ctx context.Context, conn *storage.Connection) (*models.User, error) {
claims := getClaims(ctx)
if claims == nil {
return nil, errors.New("Invalid token")
}
if claims.Subject == "" {
return nil, errors.New("Invalid claim: id")
}
// System User
instanceID := getInstanceID(ctx)
if claims.Subject == models.SystemUserUUID.String() || claims.Subject == models.SystemUserID {
return models.NewSystemUser(instanceID, claims.Audience), nil
}
userID, err := uuid.FromString(claims.Subject)
if err != nil {
return nil, errors.New("Invalid user ID")
}
return models.FindUserByInstanceIDAndID(conn, instanceID, userID)
}
func (a *API) isAdmin(ctx context.Context, u *models.User, aud string) bool {
config := a.getConfig(ctx)
if aud == "" {
aud = config.JWT.Aud
}
return u.IsSuperAdmin || (aud == u.Aud && u.HasRole(config.JWT.AdminGroupName))
}
func (a *API) requestAud(ctx context.Context, r *http.Request) string {
config := a.getConfig(ctx)
// First check for an audience in the header
if aud := r.Header.Get(audHeaderName); aud != "" {
return aud
}
// Then check the token
claims := getClaims(ctx)
if claims != nil && claims.Audience != "" {
return claims.Audience
}
// Finally, return the default of none of the above methods are successful
return config.JWT.Aud
}
func (a *API) getReferrer(r *http.Request) string {
ctx := r.Context()
config := a.getConfig(ctx)
referrer := ""
if reqref := r.Referer(); reqref != "" {
base, berr := url.Parse(config.SiteURL)
refurl, rerr := url.Parse(reqref)
// As long as the referrer came from the site, we will redirect back there
if berr == nil && rerr == nil && base.Hostname() == refurl.Hostname() {
referrer = reqref
}
}
return referrer
}
var privateIPBlocks []*net.IPNet
func init() {
for _, cidr := range []string{
"127.0.0.0/8", // IPv4 loopback
"10.0.0.0/8", // RFC1918
"100.64.0.0/10", // RFC6598
"172.16.0.0/12", // RFC1918
"192.0.0.0/24", // RFC6890
"192.168.0.0/16", // RFC1918
"169.254.0.0/16", // RFC3927
"::1/128", // IPv6 loopback
"fe80::/10", // IPv6 link-local
"fc00::/7", // IPv6 unique local addr
} {
_, block, _ := net.ParseCIDR(cidr)
privateIPBlocks = append(privateIPBlocks, block)
}
}
func isPrivateIP(ip net.IP) bool {
for _, block := range privateIPBlocks {
if block.Contains(ip) {
return true
}
}
return false
}
func removeLocalhostFromPrivateIPBlock() *net.IPNet {
_, localhost, _ := net.ParseCIDR("127.0.0.0/8")
var localhostIndex int
for i := 0; i < len(privateIPBlocks); i++ {
if privateIPBlocks[i] == localhost {
localhostIndex = i
}
}
privateIPBlocks = append(privateIPBlocks[:localhostIndex], privateIPBlocks[localhostIndex+1:]...)
return localhost
}
func unshiftPrivateIPBlock(address *net.IPNet) {
privateIPBlocks = append([]*net.IPNet{address}, privateIPBlocks...)
}
type noLocalTransport struct {
inner http.RoundTripper
errlog logrus.FieldLogger
}
func (no noLocalTransport) RoundTrip(req *http.Request) (*http.Response, error) {
ctx, cancel := context.WithCancel(req.Context())
ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
ConnectStart: func(network, addr string) {
fmt.Printf("Checking network %v\n", addr)
host, _, err := net.SplitHostPort(addr)
if err != nil {
cancel()
fmt.Printf("Canceleing dur to error in addr parsing %v", err)
return
}
ip := net.ParseIP(host)
if ip == nil {
cancel()
fmt.Printf("Canceleing dur to error in ip parsing %v", host)
return
}
if isPrivateIP(ip) {
cancel()
fmt.Println("Canceleing dur to private ip range")
return
}
},
})
req = req.WithContext(ctx)
return no.inner.RoundTrip(req)
}
func SafeRoundtripper(trans http.RoundTripper, log logrus.FieldLogger) http.RoundTripper {
if trans == nil {
trans = http.DefaultTransport
}
ret := &noLocalTransport{
inner: trans,
errlog: log.WithField("transport", "local_blocker"),
}
return ret
}
func SafeHTTPClient(client *http.Client, log logrus.FieldLogger) *http.Client {
client.Transport = SafeRoundtripper(client.Transport, log)
return client
}