forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cors.go
67 lines (54 loc) · 1.71 KB
/
cors.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
package http
import (
"fmt"
"net/http"
"strings"
"github.com/hashicorp/vault/helper/strutil"
"github.com/hashicorp/vault/vault"
)
var allowedMethods = []string{
http.MethodDelete,
http.MethodGet,
http.MethodOptions,
http.MethodPost,
http.MethodPut,
"LIST", // LIST is not an official HTTP method, but Vault supports it.
}
func wrapCORSHandler(h http.Handler, core *vault.Core) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
corsConf := core.CORSConfig()
// If CORS is not enabled or if no Origin header is present (i.e. the request
// is from the Vault CLI. A browser will always send an Origin header), then
// just return a 204.
if !corsConf.IsEnabled() {
h.ServeHTTP(w, req)
return
}
origin := req.Header.Get("Origin")
requestMethod := req.Header.Get("Access-Control-Request-Method")
if origin == "" {
h.ServeHTTP(w, req)
return
}
// Return a 403 if the origin is not allowed to make cross-origin requests.
if !corsConf.IsValidOrigin(origin) {
respondError(w, http.StatusForbidden, fmt.Errorf("origin not allowed"))
return
}
if req.Method == http.MethodOptions && !strutil.StrListContains(allowedMethods, requestMethod) {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Vary", "Origin")
// apply headers for preflight requests
if req.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Methods", strings.Join(allowedMethods, ","))
w.Header().Set("Access-Control-Allow-Headers", strings.Join(corsConf.AllowedHeaders, ","))
w.Header().Set("Access-Control-Max-Age", "300")
return
}
h.ServeHTTP(w, req)
return
})
}