// Tes validasi FQDN untuk nilai record DNS dan hostname nameserver // (Bug 2 v0.6.1): label pendek seperti "ns1" harus ditolak — PowerDNS // mengualifikasikannya relatif terhadap zona dan merusak NS/SOA. package server import ( "bytes" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "github.com/maspend/hostora/internal/store" ) func TestValidateDNSRecordFQDN(t *testing.T) { accept := []struct{ typ, value string }{ {"NS", "ns1.hostora.xyz"}, {"NS", "ns1.hostora.xyz."}, {"NS", "NS1.HOSTORA.XYZ"}, {"CNAME", "www.contoh.com"}, {"CNAME", "cdn.contoh.com."}, {"MX", "mail.contoh.com"}, {"MX", "mail.contoh.com."}, {"SRV", "10 60 5060 sip.contoh.com"}, {"SRV", "10 60 5060 sip.contoh.com."}, } for _, c := range accept { prio := 0 if c.typ == "MX" { prio = 10 } if err := validateDNSRecord(c.typ, "@", c.value, prio); err != nil { t.Errorf("validateDNSRecord(%s, %q): mau lolos, dapat %v", c.typ, c.value, err) } } reject := []struct{ typ, value string }{ {"NS", "ns1"}, {"NS", "ns1."}, {"NS", ""}, {"NS", "ns1..hostora.xyz"}, {"NS", "ns1 hostora.xyz"}, {"CNAME", "cdn"}, {"CNAME", "cdn."}, {"CNAME", ""}, {"MX", "mail"}, {"MX", "mail."}, {"MX", ""}, {"SRV", "10 60 5060 sip"}, {"SRV", "10 60 5060 sip."}, } for _, c := range reject { prio := 0 if c.typ == "MX" { prio = 10 } err := validateDNSRecord(c.typ, "@", c.value, prio) if err == nil { t.Errorf("validateDNSRecord(%s, %q): mau ditolak, malah lolos", c.typ, c.value) continue } if !strings.Contains(err.Error(), "hostname lengkap") { t.Errorf("validateDNSRecord(%s, %q): pesan %q tidak menyebut hostname lengkap", c.typ, c.value, err.Error()) } } } // seedZoneA membuat zona a.com kosong di store (untuk pengujian handler // record/nameserver zona). func seedZoneA(t *testing.T, srv *Server) { t.Helper() if err := srv.store.Update(func(d *store.Data) error { d.DNSZones = append(d.DNSZones, store.DNSZone{Domain: "a.com", Records: []store.DNSRecord{}}) return nil }); err != nil { t.Fatalf("seed zone: %v", err) } } // doZone memanggil handler zona dengan PathValue domain terisi. func doZone(t *testing.T, h http.HandlerFunc, method, domain string, body any, cookie *http.Cookie) *httptest.ResponseRecorder { t.Helper() var buf bytes.Buffer if body != nil { if err := json.NewEncoder(&buf).Encode(body); err != nil { t.Fatalf("encode: %v", err) } } req := httptest.NewRequest(method, "/api/dns/"+domain, &buf) req.SetPathValue("domain", domain) if cookie != nil { req.AddCookie(cookie) } rec := httptest.NewRecorder() h(rec, req) return rec } // POST record NS dengan nilai label pendek harus 400; FQDN lolos 201. func TestAddRecordRejectsShortNS(t *testing.T) { srv := newTestServer(t) admin, _, _ := seedTenant(t, srv) seedZoneA(t, srv) post := func(value string) *httptest.ResponseRecorder { return doZone(t, srv.handleDNSRecords, http.MethodPost, "a.com", map[string]any{"type": "NS", "name": "@", "value": value, "ttl": 3600}, admin) } for _, bad := range []string{"ns1", "ns1.", ""} { rec := post(bad) if rec.Code != http.StatusBadRequest { t.Errorf("POST NS %q: got %d, want 400 (%s)", bad, rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), "hostname lengkap") { t.Errorf("POST NS %q: pesan tidak menyebut hostname lengkap: %s", bad, rec.Body.String()) } } rec := post("ns1.hostora.xyz") if rec.Code != http.StatusCreated { t.Errorf("POST NS ns1.hostora.xyz: got %d, want 201 (%s)", rec.Code, rec.Body.String()) } } // PUT /api/nameservers (global) harus menolak hostname label pendek. func TestPutNameserversRejectsShortHostname(t *testing.T) { srv := newTestServer(t) admin, _, _ := seedTenant(t, srv) bad := doJSON(t, srv.handleNameservers, http.MethodPut, "/api/nameservers", map[string]any{"nameservers": []map[string]any{{"hostname": "ns1", "is_default": true}}}, admin, "") if bad.Code != http.StatusBadRequest { t.Errorf("PUT nameserver ns1: got %d, want 400 (%s)", bad.Code, bad.Body.String()) } ok := doJSON(t, srv.handleNameservers, http.MethodPut, "/api/nameservers", map[string]any{"nameservers": []map[string]any{{"hostname": "ns1.hostora.xyz", "ipv4": "203.0.113.10", "is_default": true}}}, admin, "") if ok.Code != http.StatusOK { t.Errorf("PUT nameserver ns1.hostora.xyz: got %d, want 200 (%s)", ok.Code, ok.Body.String()) } } // PUT /api/dns/{domain}/nameservers harus menolak label pendek — // TERMASUK bila hostname itu ada di daftar global (celah bypass lama: // entri global dari sebelum validasi diperketat bisa berisi "ns1"). func TestPutZoneNameserversRejectsShortHostname(t *testing.T) { srv := newTestServer(t) admin, _, _ := seedTenant(t, srv) seedZoneA(t, srv) put := func(hosts []string) *httptest.ResponseRecorder { return doZone(t, srv.handleZoneNameservers, http.MethodPut, "a.com", map[string]any{"nameservers": hosts}, admin) } if rec := put([]string{"ns1"}); rec.Code != http.StatusBadRequest { t.Errorf("PUT zone NS ns1: got %d, want 400 (%s)", rec.Code, rec.Body.String()) } if rec := put([]string{"ns1.hostora.xyz", "ns2.hostora.xyz"}); rec.Code != http.StatusOK { t.Errorf("PUT zone NS FQDN: got %d, want 200 (%s)", rec.Code, rec.Body.String()) } // Celah bypass: tanam entri global "ns1" langsung di store // (seolah tersimpan sebelum validasi diperketat), lalu coba pakai // sebagai override zona — tetap harus 400. if err := srv.store.Update(func(d *store.Data) error { d.Nameservers = []store.Nameserver{{Hostname: "ns1", IsDefault: true}} return nil }); err != nil { t.Fatalf("seed nameserver jelek: %v", err) } if rec := put([]string{"ns1"}); rec.Code != http.StatusBadRequest { t.Errorf("PUT zone NS ns1 (ada di global): got %d, want 400 (%s)", rec.Code, rec.Body.String()) } }