Skip to content

Commit 761b557

Browse files
committed
fix(httpd): fail bearerauth if shared secret blank
1 parent 93b5632 commit 761b557

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

services/httpd/handler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,11 @@ func authenticate(inner func(http.ResponseWriter, *http.Request, meta.User), h *
15811581
return
15821582
}
15831583
case BearerAuthentication:
1584+
if h.Config.SharedSecret == "" {
1585+
atomic.AddInt64(&h.stats.AuthenticationFailures, 1)
1586+
h.httpError(w, "bearer auth disabled", http.StatusUnauthorized)
1587+
return
1588+
}
15841589
keyLookupFn := func(token *jwt.Token) (interface{}, error) {
15851590
// Check for expected signing method.
15861591
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {

services/httpd/handler_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,24 @@ func TestHandler_Query_Auth(t *testing.T) {
232232
t.Fatalf("unexpected body: %s", body)
233233
}
234234

235+
// Test that auth fails if shared secret is blank.
236+
origSecret := h.Config.SharedSecret
237+
h.Config.SharedSecret = ""
238+
token, _ = MustJWTToken("user1", h.Config.SharedSecret, false)
239+
signedToken, err = token.SignedString([]byte(h.Config.SharedSecret))
240+
if err != nil {
241+
t.Fatal(err)
242+
}
243+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", signedToken))
244+
w = httptest.NewRecorder()
245+
h.ServeHTTP(w, req)
246+
if w.Code != http.StatusUnauthorized {
247+
t.Fatalf("unexpected status: %d: %s", w.Code, w.Body.String())
248+
} else if body := strings.TrimSpace(w.Body.String()); body != `{"error":"bearer auth disabled"}` {
249+
t.Fatalf("unexpected body: %s", body)
250+
}
251+
h.Config.SharedSecret = origSecret
252+
235253
// Test the handler with valid user and password in the url and invalid in
236254
// basic auth (prioritize url).
237255
w = httptest.NewRecorder()

0 commit comments

Comments
 (0)