/
http.go
180 lines (158 loc) · 7.39 KB
/
http.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
package cli
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/NYTimes/gziphandler"
gcontext "github.com/gorilla/context"
"github.com/gorilla/mux"
"github.com/sourcegraph/sourcegraph/cmd/frontend/globals"
"github.com/sourcegraph/sourcegraph/cmd/frontend/hooks"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/app"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/app/assetsutil"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/auth"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/cli/middleware"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/httpapi/router"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/pkg/handlerutil"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/session"
"github.com/sourcegraph/sourcegraph/pkg/actor"
"github.com/sourcegraph/sourcegraph/pkg/conf"
"github.com/sourcegraph/sourcegraph/pkg/env"
tracepkg "github.com/sourcegraph/sourcegraph/pkg/trace"
)
// newExternalHTTPHandler creates and returns the HTTP handler that serves the app and API pages to
// external clients.
func newExternalHTTPHandler(ctx context.Context) (http.Handler, error) {
// Each auth middleware determines on a per-request basis whether it should be enabled (if not, it
// immediately delegates the request to the next middleware in the chain).
authMiddlewares := auth.AuthMiddleware()
// HTTP API handler.
apiHandler := httpapi.NewHandler(router.New(mux.NewRouter().PathPrefix("/.api/").Subrouter()))
apiHandler = authMiddlewares.API(apiHandler) // 🚨 SECURITY: auth middleware
// 🚨 SECURITY: The HTTP API should not accept cookies as authentication (except those with the
// X-Requested-With header). Doing so would open it up to CSRF attacks.
apiHandler = session.CookieMiddlewareWithCSRFSafety(apiHandler, corsAllowHeader, isTrustedOrigin) // API accepts cookies with special header
apiHandler = httpapi.AccessTokenAuthMiddleware(apiHandler) // API accepts access tokens
apiHandler = gziphandler.GzipHandler(apiHandler)
// App handler (HTML pages).
appHandler := app.NewHandler()
appHandler = handlerutil.CSRFMiddleware(appHandler, globals.ExternalURL.Scheme == "https") // after appAuthMiddleware because SAML IdP posts data to us w/o a CSRF token
appHandler = authMiddlewares.App(appHandler) // 🚨 SECURITY: auth middleware
appHandler = session.CookieMiddleware(appHandler) // app accepts cookies
// Mount handlers and assets.
sm := http.NewServeMux()
sm.Handle("/.api/", apiHandler)
sm.Handle("/", appHandler)
assetsutil.Mount(sm)
var h http.Handler = sm
// Wrap in middleware.
//
// 🚨 SECURITY: Auth middleware that must run before other auth middlewares.
h = auth.OverrideAuthMiddleware(h)
h = auth.ForbidAllRequestsMiddleware(h)
// 🚨 SECURITY: These all run before the auth handler, so the client is not yet authenticated.
if hooks.PreAuthMiddleware != nil {
h = hooks.PreAuthMiddleware(h)
}
h = healthCheckMiddleware(h)
h = tracepkg.Middleware(h)
h = middleware.SourcegraphComGoGetHandler(h)
h = middleware.BlackHole(h)
h = secureHeadersMiddleware(h)
h = gcontext.ClearHandler(h)
h = middleware.Trace(h)
return h, nil
}
func healthCheckMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/healthz", "/__version":
fmt.Fprintf(w, env.Version)
default:
next.ServeHTTP(w, r)
}
})
}
// newInternalHTTPHandler creates and returns the HTTP handler for the internal API (accessible to
// other internal services).
func newInternalHTTPHandler() http.Handler {
internalMux := http.NewServeMux()
internalMux.Handle("/.internal/", gziphandler.GzipHandler(
withInternalActor(
httpapi.NewInternalHandler(
router.NewInternal(mux.NewRouter().PathPrefix("/.internal/").Subrouter()),
),
),
))
return gcontext.ClearHandler(internalMux)
}
// withInternalActor wraps an existing HTTP handler by setting an internal actor in the HTTP request
// context.
//
// 🚨 SECURITY: This should *never* be called to wrap externally accessible handlers (i.e., only use
// for the internal endpoint), because internal requests will bypass repository permissions checks.
func withInternalActor(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rWithActor := r.WithContext(actor.WithActor(r.Context(), &actor.Actor{Internal: true}))
h.ServeHTTP(w, rWithActor)
})
}
// corsAllowHeader is the HTTP header that, if present (and assuming secureHeadersMiddleware is
// used), indicates that the incoming HTTP request is either same-origin or is from an allowed
// origin. See
// https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#Protecting_REST_Services:_Use_of_Custom_Request_Headers
// for more information on this technique.
const corsAllowHeader = "X-Requested-With"
// secureHeadersMiddleware adds and checks for HTTP security-related headers.
//
// 🚨 SECURITY: This handler is served to all clients, even on private servers to clients who have
// not authenticated. It must not reveal any sensitive information.
func secureHeadersMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// headers for security
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-XSS-Protection", "1; mode=block")
w.Header().Set("X-Frame-Options", "DENY")
// no cache by default
w.Header().Set("Cache-Control", "no-cache, max-age=0")
// CORS
// If the headerOrigin is the development or production Chrome Extension explicitly set the Allow-Control-Allow-Origin
// to the incoming header URL. Otherwise use the configured CORS origin.
headerOrigin := r.Header.Get("Origin")
isExtensionRequest := (headerOrigin == devExtension || headerOrigin == prodExtension) && !disableBrowserExtension
if corsOrigin := conf.Get().CorsOrigin; corsOrigin != "" || isExtensionRequest {
w.Header().Set("Access-Control-Allow-Credentials", "true")
allowOrigin := corsOrigin
if isExtensionRequest || isAllowedOrigin(headerOrigin, strings.Fields(corsOrigin)) {
allowOrigin = headerOrigin
}
w.Header().Set("Access-Control-Allow-Origin", allowOrigin)
if r.Method == "OPTIONS" {
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", corsAllowHeader+", X-Sourcegraph-Client, Content-Type")
w.WriteHeader(http.StatusOK)
return // do not invoke next handler
}
}
next.ServeHTTP(w, r)
})
}
// isTrustedOrigin returns whether the HTTP request's Origin is trusted to initiate authenticated
// cross-origin requests.
func isTrustedOrigin(r *http.Request) bool {
requestOrigin := r.Header.Get("Origin")
var isExtensionRequest bool
if !disableBrowserExtension {
isExtensionRequest = requestOrigin == devExtension || requestOrigin == prodExtension
}
var isCORSAllowedRequest bool
if corsOrigin := conf.Get().CorsOrigin; corsOrigin != "" {
isCORSAllowedRequest = isAllowedOrigin(requestOrigin, strings.Fields(corsOrigin))
}
if externalURL := strings.TrimSuffix(conf.Get().ExternalURL, "/"); externalURL != "" && requestOrigin == externalURL {
isCORSAllowedRequest = true
}
return isExtensionRequest || isCORSAllowedRequest
}