This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
forked from jmhodges/howsmyssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
allow.go
269 lines (237 loc) · 6.69 KB
/
allow.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"encoding/json"
"expvar"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"cloud.google.com/go/logging"
topk "github.com/dgryski/go-topk"
"golang.org/x/net/publicsuffix"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
)
type originAllower struct {
m map[string]struct{}
ns *expvar.Map
hostname string
gclog logClient
mu *sync.RWMutex
topKAllDomains *topk.Stream
topKOfflistDomains *topk.Stream
}
type logClient interface {
Log(logging.Entry)
Flush()
}
func newOriginAllower(blockedDomains []string, hostname string, gclog logClient, ns *expvar.Map) *originAllower {
mu := &sync.RWMutex{}
topKAllDomains := topk.New(100)
topKOfflistDomains := topk.New(100)
lifetime := new(expvar.Map).Init()
ns.Set("lifetime", lifetime)
lifetime.Set("top_all_domains", expvar.Func(func() interface{} {
mu.RLock()
defer mu.RUnlock()
return topKAllDomains.Keys()
}))
lifetime.Set("top_offlist_domains", expvar.Func(func() interface{} {
mu.RLock()
defer mu.RUnlock()
return topKOfflistDomains.Keys()
}))
oa := &originAllower{
m: make(map[string]struct{}),
ns: ns,
hostname: hostname,
gclog: gclog,
mu: mu,
topKAllDomains: topKAllDomains,
topKOfflistDomains: topKOfflistDomains,
}
for _, d := range blockedDomains {
oa.m[d] = struct{}{}
}
return oa
}
func (oa *originAllower) Allow(r *http.Request) (string, bool) {
origin := r.Header.Get("Origin")
referrer := r.Header.Get("Referer")
apiKey := r.FormValue("key")
userAgent := r.Header.Get("User-Agent")
remoteIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Printf("error splitting %#v as host:port: %s", r.RemoteAddr, err)
remoteIP = "0.0.0.0"
}
entry := &apiLogEntry{
DetectedDomain: "",
DetectedFullDomain: "",
Allowed: false,
APIKey: apiKey,
Headers: headers{
Origin: origin,
Referrer: referrer,
UserAgent: userAgent,
},
}
defer func() {
go oa.countRequest(entry, r, remoteIP)
}()
if origin == "" && referrer == "" {
entry.Allowed = true
return "", true
}
if origin != "" {
domain, fullDomain, ok := oa.checkDomain(origin)
entry.DetectedDomain = domain
entry.DetectedFullDomain = fullDomain
entry.Allowed = ok
if !ok {
entry.RejectionReason = rejectionConfig
}
return domain, ok
}
if referrer != "" {
domain, fullDomain, ok := oa.checkDomain(referrer)
entry.DetectedDomain = domain
entry.DetectedFullDomain = fullDomain
entry.Allowed = ok
if !ok {
entry.RejectionReason = rejectionConfig
}
return domain, ok
}
return "", false
}
// checkDomain checks if the detected domain from the request headers and
// whether domain is allowed to make requests against howsmyssl's API.
func (oa *originAllower) checkDomain(d string) (string, string, bool) {
domain, fullDomain, err := effectiveDomain(d)
if err != nil {
// TODO(jmhodges): replace this len check with false when we use top-k
return "", "", len(oa.m) == 0
}
_, isBlocked := oa.m[domain]
// TODO(jmhodges): remove this len check when we use top-k
return domain, fullDomain, !isBlocked || len(oa.m) == 0
}
func (oa *originAllower) countRequest(entry *apiLogEntry, r *http.Request, remoteIP string) {
oa.gclog.Log(logging.Entry{
Payload: entry,
HTTPRequest: &logging.HTTPRequest{Request: r, RemoteIP: remoteIP},
Labels: map[string]string{
"server_hostname": oa.hostname,
"app": "howsmyssl",
},
})
if entry.DetectedDomain == "" {
return
}
oa.mu.Lock()
defer oa.mu.Unlock()
oa.topKAllDomains.Insert(entry.DetectedDomain, 1)
if !entry.Allowed {
oa.topKOfflistDomains.Insert(entry.DetectedDomain, 1)
}
}
func effectiveDomain(str string) (string, string, error) {
u, err := url.Parse(str)
if err != nil {
return "", "", err
}
host := u.Host
if host == "" {
return "", "", fmt.Errorf("unparsable domain string %#v", str)
}
i := strings.Index(host, ":")
if i >= 0 {
host = host[:i]
}
if host == "localhost" {
return "localhost", "localhost", nil
}
d, err := publicsuffix.EffectiveTLDPlusOne(host)
if err != nil {
return "", "", err
}
return d, host, nil
}
func loadOriginsConfig(fp string) *originsConfig {
f, err := os.Open(fp)
if err != nil {
log.Fatalf("unable to open origins config file %#v: %s", fp, err)
}
defer f.Close()
jc := &originsConfig{}
err = json.NewDecoder(f).Decode(jc)
if err != nil {
log.Fatalf("unable to parse origins config file %#v: %s", fp, err)
}
for _, a := range jc.BlockedOrigins {
if strings.HasPrefix(a, "http://") || strings.HasPrefix(a, "https://") {
log.Fatalf("origins config file (%#v) should have only domains without the leading scheme. For example, %#v should not have the protocol scheme at its beginning.", fp, a)
}
if strings.Contains(a, "/") {
log.Fatalf("origins config file (%#v) should have only domains without a path after it. For example, %#v should not have a trailing path.", fp, a)
}
}
return jc
}
type originsConfig struct {
// BlockedOrigins are domains that are not to be allowed as referrers to the
// API. They should not have a scheme or path, but only the domain, as in
// "example.com".
BlockedOrigins []string `json:"blocked_origins"`
}
type rejectionReason string
const rejectionConfig = rejectionReason("config")
type apiLogEntry struct {
DetectedDomain string `json:"detected_domain"`
DetectedFullDomain string `json:"detected_full_domain"`
Allowed bool `json:"allowed"`
APIKey string `json:"api_key"`
RejectionReason rejectionReason `json:"rejection_reason"`
Headers headers `json:"headers"`
}
type headers struct {
Origin string `json:"origin"`
Referrer string `json:"referrer"`
UserAgent string `json:"user_agent"`
}
func loadGoogleServiceAccount(fp string) *googleConfig {
bs, err := ioutil.ReadFile(fp)
if err != nil {
log.Fatalf("unable to read Google service account config %#v: %s", fp, err)
}
c := &googleConfig{}
err = json.Unmarshal(bs, c)
if err != nil {
log.Fatalf("unable to parse project ID from Google service account config %#v: %s", fp, err)
}
if c.ProjectID == "" {
log.Fatalf("blank project ID in Google service account config %#v: %s", fp, err)
}
jwtConf, err := google.JWTConfigFromJSON(bs, logging.WriteScope)
if err != nil {
log.Fatalf("unable to parse Google service account config %#v: %s", fp, err)
}
c.conf = jwtConf
return c
}
type googleConfig struct {
ProjectID string `json:"project_id"`
conf *jwt.Config
}
var _ logClient = nullLogClient{}
type nullLogClient struct{}
func (n nullLogClient) Log(e logging.Entry) {
}
func (n nullLogClient) Flush() {
}