package router import ( "net/http" "net/http/httptest" "strings" "testing" ) func TestTheAdminListenerRefusesWhatABrowserPageCouldSend(t *testing.T) { h := AdminGuard(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {})) do := func(method, host, ct string, hdr map[string]string) int { r := httptest.NewRequest(method, "/api/v1/models", strings.NewReader("{}")) r.Host = host if ct != "" { r.Header.Set("Content-Type", ct) } for k, v := range hdr { r.Header.Set(k, v) } w := httptest.NewRecorder() h.ServeHTTP(w, r) return w.Code } // What ctl and curl send. if got := do("POST", "127.0.0.1:9090", "application/json", nil); got != 200 { t.Errorf("ctl's request was refused: %d", got) } if got := do("GET", "localhost:9090", "", nil); got != 200 { t.Errorf("a GET through an ssh tunnel was refused: %d", got) } if got := do("DELETE", "[::1]:9090", "application/json", nil); got != 200 { t.Errorf("IPv6 loopback refused: %d", got) } // A page posting a form, or text/plain, needs no preflight. if got := do("POST", "127.0.0.1:9090", "text/plain", nil); got != http.StatusUnsupportedMediaType { t.Errorf("text/plain POST accepted: %d", got) } if got := do("POST", "127.0.0.1:9090", "", nil); got != http.StatusUnsupportedMediaType { t.Errorf("a bodiless POST accepted: %d", got) } // DNS rebinding: the page's own name, now resolving to 127.0.0.1. if got := do("GET", "evil.example:9090", "", nil); got != http.StatusMisdirectedRequest { t.Errorf("a rebound name was answered: %d", got) } // And anything a browser marks as coming from elsewhere. if got := do("POST", "127.0.0.1:9090", "application/json", map[string]string{"Sec-Fetch-Site": "cross-site"}); got != http.StatusForbidden { t.Errorf("cross-site fetch accepted: %d", got) } if got := do("POST", "127.0.0.1:9090", "application/json", map[string]string{"Origin": "https://evil.example"}); got != http.StatusForbidden { t.Errorf("foreign Origin accepted: %d", got) } }