commit
8c4d463f1e
96 files changed
+3319
No files matched your search
@@ -0,0 +1,122 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/config"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testPanel(t *testing.T, api http.Handler) *Server {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
password := filepath.Join(dir, "password")
|
||||
key := filepath.Join(dir, "session.key")
|
||||
token := filepath.Join(dir, "control.token")
|
||||
for f, v := range map[string]string{password: "admin-test-password-long", key: strings.Repeat("a", 64), token: "generated-admin-token"} {
|
||||
if e := os.WriteFile(f, []byte(v), 0600); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
backend := httptest.NewServer(api)
|
||||
t.Cleanup(backend.Close)
|
||||
s, e := New(config.Config{Listen: "127.0.0.1:0", APIURL: backend.URL, TokenFile: token, PasswordFile: password, SessionKeyFile: key, Username: "admin"})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
return s
|
||||
}
|
||||
func TestLoginAndCSRF(t *testing.T) {
|
||||
s := testPanel(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"status":"ok"}`)
|
||||
}))
|
||||
h := s.Handler()
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, httptest.NewRequest("GET", "http://example.test/zones", nil))
|
||||
if w.Code != 303 || w.Header().Get("Location") != "/login" {
|
||||
t.Fatalf("route without login=%d %s", w.Code, w.Header().Get("Location"))
|
||||
}
|
||||
form := url.Values{"username": {"admin"}, "password": {"admin-test-password-long"}}
|
||||
w = httptest.NewRecorder()
|
||||
r := httptest.NewRequest("POST", "http://example.test/login", strings.NewReader(form.Encode()))
|
||||
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
h.ServeHTTP(w, r)
|
||||
if w.Code != 303 || len(w.Result().Cookies()) == 0 {
|
||||
t.Fatalf("login failed: %d", w.Code)
|
||||
}
|
||||
cookie := w.Result().Cookies()[0]
|
||||
r = httptest.NewRequest("POST", "http://example.test/policies", strings.NewReader("block_action=nxdomain"))
|
||||
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
r.AddCookie(cookie)
|
||||
w = httptest.NewRecorder()
|
||||
h.ServeHTTP(w, r)
|
||||
if w.Code != 403 {
|
||||
t.Fatalf("missing CSRF was accepted: %d", w.Code)
|
||||
}
|
||||
r = httptest.NewRequest("GET", "http://example.test/zones", nil)
|
||||
r.AddCookie(cookie)
|
||||
w = httptest.NewRecorder()
|
||||
h.ServeHTTP(w, r)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), "EasyDNS") {
|
||||
t.Fatalf("authenticated GET failed: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if strings.Contains(w.Body.String(), "API v1") {
|
||||
t.Fatal("visual artifact API v1 reappeared")
|
||||
}
|
||||
}
|
||||
func TestRecordEditConflictPreservesETag(t *testing.T) {
|
||||
var captured bool
|
||||
s := testPanel(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch {
|
||||
case strings.HasSuffix(r.URL.Path, "/records/111") && r.Method == "PUT":
|
||||
captured = true
|
||||
if r.Header.Get("If-Match") != "\"10\"" {
|
||||
t.Error("missing ETag match")
|
||||
}
|
||||
w.WriteHeader(412)
|
||||
_, _ = io.WriteString(w, `{"error":{"status":412,"message":"zone serial changed"}}`)
|
||||
case strings.HasPrefix(r.URL.Path, "/api/v1/zones/") && !strings.Contains(r.URL.Path, "/records"):
|
||||
_, _ = io.WriteString(w, `{"name":"example.com.","kind":"primary","serial":10,"loaded":true,"records":1}`)
|
||||
default:
|
||||
t.Logf("unexpected test request %s %s", r.Method, r.URL.Path)
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
}))
|
||||
form := url.Values{"name": {"www"}, "type": {"A"}, "ttl": {"300"}, "data": {"192.0.2.10"}, "etag": {"\"10\""}}
|
||||
r := httptest.NewRequest("POST", "http://example.test/zones/example.com./records/111", strings.NewReader(form.Encode()))
|
||||
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
sess := s.auth.makeSession()
|
||||
r.AddCookie(&http.Cookie{Name: sessionCookie, Value: sess})
|
||||
r.PostForm = form
|
||||
form.Set("_csrf", s.auth.csrf(strings.Join(strings.Split(sess, ".")[:2], ".")))
|
||||
r = httptest.NewRequest("POST", "http://example.test/zones/example.com./records/111", strings.NewReader(form.Encode()))
|
||||
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
r.AddCookie(&http.Cookie{Name: sessionCookie, Value: sess})
|
||||
w := httptest.NewRecorder()
|
||||
s.Handler().ServeHTTP(w, r)
|
||||
if !captured {
|
||||
t.Fatal("no PUT operation reached EasyDNS")
|
||||
}
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), "Zone changed") {
|
||||
t.Fatalf("missing conflict UI: status=%d %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
func TestNoAPISecretsInRenderedTemplates(t *testing.T) {
|
||||
s := testPanel(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{"status":"ok"}`) }))
|
||||
sess := s.auth.makeSession()
|
||||
r := httptest.NewRequest("GET", "http://example.test/", nil)
|
||||
r.AddCookie(&http.Cookie{Name: sessionCookie, Value: sess})
|
||||
w := httptest.NewRecorder()
|
||||
s.Handler().ServeHTTP(w, r)
|
||||
if strings.Contains(w.Body.String(), "generated-admin-token") {
|
||||
t.Fatal("generated API bearer leaked to browser")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user