package dnsprovider import ( "context" "encoding/json" "net/http" "net/http/httptest" "net/netip" "strconv" "testing" "time" ) func TestDNSManagerPreservesExistingRecordsAndSignsRequests(t *testing.T) { for _, tc := range []struct { name string ttl any want string }{{"numeric", 600, "600"}, {"string-low", "60", "600"}, {"string-high", "1800", "1800"}} { t.Run(tc.name, func(t *testing.T) { created := false deleted := "" stamp := time.Unix(1780000000, 0) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { t.Error("wrong method") } _ = r.ParseForm() if r.Form.Get("uid") != "12" || r.Form.Get("timestamp") != strconv.FormatInt(stamp.Unix(), 10) || r.Form.Get("sign") != md5Hex("12"+strconv.FormatInt(stamp.Unix(), 10)+"fixture-api-key") { t.Error("invalid API signature") } if r.Form.Get("key") != "" { t.Error("plaintext key transmitted") } switch r.URL.Path { case "/api/domain/7": _ = json.NewEncoder(w).Encode(map[string]any{"code": 0, "data": map[string]any{"recordLine": []map[string]any{{"id": "default", "name": "默认"}}, "minTTL": tc.ttl}}) case "/api/record/data/7": rows := []map[string]any{{"RecordId": "9", "Name": "_acme-challenge", "Type": "TXT", "Value": "proof"}} if created { rows = append(rows, map[string]any{"RecordId": "10", "Name": "_acme-challenge", "Type": "TXT", "Value": "proof"}) } _ = json.NewEncoder(w).Encode(map[string]any{"total": len(rows), "rows": rows}) case "/api/record/add/7": if r.Form.Get("line") != "default" || r.Form.Get("name") != "_acme-challenge" || r.Form.Get("ttl") != tc.want { t.Error("wrong record target") } created = true _, _ = w.Write([]byte(`{"code":0}`)) case "/api/record/delete/7": deleted = r.Form.Get("recordid") _, _ = w.Write([]byte(`{"code":0}`)) default: t.Error("unexpected endpoint", r.URL.Path) w.WriteHeader(404) } })) defer srv.Close() p := &dnsmgr{client: srv.Client(), zone: "example.com", credential: Credential{Endpoint: srv.URL, UserID: "12", APIKey: "fixture-api-key", ZoneID: "7"}, now: func() time.Time { return stamp }} id, e := p.Present(context.Background(), "_acme-challenge.example.com", "proof") if e != nil || id != "10" { t.Fatal(id, e) } if e = p.Cleanup(context.Background(), id); e != nil { t.Fatal(e) } if deleted != "10" { t.Fatal("deleted pre-existing record", deleted) } if _, e = p.Present(context.Background(), "outside.example.net", "proof"); e == nil { t.Fatal("zone escape accepted") } }) } } func TestWestAccountUsesReadOnlyDiscoveryAndExactRecordDeletion(t *testing.T) { deleted := "" stamp := time.Unix(1780000000, 0) srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _ = r.ParseForm() if r.Form.Get("token") != md5Hex("fixture-user"+"fixture-api-key"+strconv.FormatInt(stamp.UnixMilli(), 10)) { t.Error("bad account signature") } switch r.Form.Get("act") { case "adddnsrecord": if r.Form.Get("domain") != "example.com" || r.Form.Get("host") != "_acme-challenge" { t.Error("wrong zone") } _, _ = w.Write([]byte(`{"result":200,"data":{"id":55}}`)) case "deldnsrecord": deleted = r.Form.Get("id") _, _ = w.Write([]byte(`{"result":200,"data":{}}`)) default: t.Error("unexpected operation") w.WriteHeader(404) } })) defer srv.Close() p := &west{client: srv.Client(), baseURL: srv.URL, zone: "example.com", credential: Credential{Username: "fixture-user", APIKey: "fixture-api-key"}, now: func() time.Time { return stamp }} id, e := p.Present(context.Background(), "_acme-challenge.example.com", "proof") if e != nil || id != "55" { t.Fatal(id, e) } if e = p.Cleanup(context.Background(), id); e != nil { t.Fatal(e) } if deleted != "55" { t.Fatal(deleted) } } func TestDNSCustomEndpointCannotReachPrivateNetworks(t *testing.T) { for _, s := range []string{"http://dns.example.com", "https://127.0.0.1", "https://[::1]", "https://10.0.0.1", "https://dns.example.com:9443", "https://user:key@dns.example.com", "https://dns.example.com?a=b", "https://100.64.0.1", "https://[64:ff9b::7f00:1]"} { if _, e := publicEndpoint(s); e == nil { t.Error("unsafe endpoint", s) } } if _, e := publicEndpoint("https://dns.example.com/subdir"); e != nil { t.Fatal(e) } for _, s := range []string{"127.0.0.1", "::1", "::ffff:127.0.0.1", "192.168.1.1", "169.254.169.254", "100.64.0.1", "64:ff9b::7f00:1", "2002:7f00:1::"} { if publicIP(netip.MustParseAddr(s)) { t.Error("restricted IP accepted", s) } } if !publicIP(netip.MustParseAddr("1.1.1.1")) { t.Fatal("public DNS address rejected") } }