Skip to content
File

Blob: gateway/config_auth_test.go

go58 lines
1package gateway
2 
3import (
4 "net/http"
5 "net/http/httptest"
6 "testing"
7 
8 "github.com/go-chi/chi/v5"
9 "github.com/stretchr/testify/require"
10)
11 
12func TestConfigurationUsesOperatorAuthenticationAndBrowserProtection(t *testing.T) {
13 for _, tc := range []struct {
14 name string
15 auth bool
16 origin string
17 site string
18 want int
19 }{
20 {name: "unauthenticated", want: http.StatusUnauthorized},
21 {name: "same origin", auth: true, origin: "https://operator.example.com", site: "same-origin", want: http.StatusNoContent},
22 {name: "command line", auth: true, want: http.StatusNoContent},
23 {name: "cross origin", auth: true, origin: "https://evil.example.net", site: "cross-site", want: http.StatusForbidden},
24 {name: "origin fallback", auth: true, origin: "https://evil.example.net", want: http.StatusForbidden},
25 } {
26 t.Run(tc.name, func(t *testing.T) {
27 called, proxied := false, false
28 pass := func(next http.Handler) http.Handler { return next }
29 apex := &apexServer{
30 authUser: "operator", authPass: "test-password", limiter: pass,
31 internalProxy: func(next http.Handler) http.Handler {
32 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33 proxied = true
34 next.ServeHTTP(w, r)
35 })
36 },
37 handlers: InternalHandlers{Configuration: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
38 called = true
39 w.WriteHeader(http.StatusNoContent)
40 })},
41 }
42 router := chi.NewRouter()
43 apex.Mount(router)
44 request := httptest.NewRequest(http.MethodPut, "https://operator.example.com/_internal/api/config/dns", nil)
45 request.Header.Set("Origin", tc.origin)
46 request.Header.Set("Sec-Fetch-Site", tc.site)
47 if tc.auth {
48 request.SetBasicAuth("operator", "test-password")
49 }
50 response := httptest.NewRecorder()
51 router.ServeHTTP(response, request)
52 require.Equal(t, tc.want, response.Code)
53 require.Equal(t, tc.want == http.StatusNoContent, called)
54 require.Equal(t, called, proxied, "browser check must precede internal proxying")
55 })
56 }
57}