@@ -265,7 +265,7 @@ func ShouldCacheFile(reqFile string) bool {
265
265
}
266
266
267
267
func (h * Handler ) serveHTML (resp http.ResponseWriter , request * http.Request , reqPath string , state htmlState ) bool {
268
- if data , err := h .renderHTMLWithState (resp , request , reqPath , state ); err == nil {
268
+ if data , err := h .renderHTMLWithState (request , reqPath , state ); err == nil {
269
269
if reqPath == "" {
270
270
// Pass "index.html" to the ServeContent so the ServeContent sets the right content headers.
271
271
reqPath = "index.html"
@@ -278,7 +278,7 @@ func (h *Handler) serveHTML(resp http.ResponseWriter, request *http.Request, req
278
278
279
279
// renderWithState will render the file using the given nonce if the file exists
280
280
// as a template. If it does not, it will return an error.
281
- func (h * Handler ) renderHTMLWithState (rw http. ResponseWriter , r * http.Request , filePath string , state htmlState ) ([]byte , error ) {
281
+ func (h * Handler ) renderHTMLWithState (r * http.Request , filePath string , state htmlState ) ([]byte , error ) {
282
282
var buf bytes.Buffer
283
283
if filePath == "" {
284
284
filePath = "index.html"
@@ -290,7 +290,11 @@ func (h *Handler) renderHTMLWithState(rw http.ResponseWriter, r *http.Request, f
290
290
291
291
// Cookies are sent when requesting HTML, so we can get the user
292
292
// and pre-populate the state for the frontend to reduce requests.
293
- apiKey , actor , _ := httpmw .ExtractAPIKey (rw , r , httpmw.ExtractAPIKeyConfig {
293
+ // We use a noop response writer because we don't want to write
294
+ // anything to the response and break the HTML, an error means we
295
+ // simply don't pre-populate the state.
296
+ noopRW := noopResponseWriter {}
297
+ apiKey , actor , ok := httpmw .ExtractAPIKey (noopRW , r , httpmw.ExtractAPIKeyConfig {
294
298
Optional : true ,
295
299
DB : h .opts .Database ,
296
300
OAuth2Configs : h .opts .OAuth2Configs ,
@@ -300,7 +304,7 @@ func (h *Handler) renderHTMLWithState(rw http.ResponseWriter, r *http.Request, f
300
304
RedirectToLogin : false ,
301
305
SessionTokenFunc : nil ,
302
306
})
303
- if apiKey != nil && actor != nil {
307
+ if ok && apiKey != nil && actor != nil {
304
308
ctx := dbauthz .As (r .Context (), actor .Actor )
305
309
306
310
var eg errgroup.Group
@@ -392,6 +396,13 @@ func (h *Handler) renderHTMLWithState(rw http.ResponseWriter, r *http.Request, f
392
396
return buf .Bytes (), nil
393
397
}
394
398
399
+ // noopResponseWriter is a response writer that does nothing.
400
+ type noopResponseWriter struct {}
401
+
402
+ func (noopResponseWriter ) Header () http.Header { return http.Header {} }
403
+ func (noopResponseWriter ) Write (p []byte ) (int , error ) { return len (p ), nil }
404
+ func (noopResponseWriter ) WriteHeader (int ) {}
405
+
395
406
// secureHeaders is only needed for statically served files. We do not need this for api endpoints.
396
407
// It adds various headers to enforce browser security features.
397
408
func secureHeaders () * secure.Secure {
0 commit comments