diff --git a/provider/pihole/clientV6.go b/provider/pihole/clientV6.go index ab616ddcf..dfb4d8ee4 100644 --- a/provider/pihole/clientV6.go +++ b/provider/pihole/clientV6.go @@ -337,7 +337,15 @@ func (p *piholeClientV6) retrieveNewToken(ctx context.Context) error { log.Debugf("Fetching new token from %s", apiUrl) // Define the JSON payload - jsonData := []byte(`{"password":"` + p.cfg.Password + `"}`) + body := struct { + Password string `json:"password"` + }{ + Password: p.cfg.Password, + } + jsonData, err := json.Marshal(body) + if err != nil { + return err + } req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiUrl, bytes.NewBuffer(jsonData)) if err != nil { diff --git a/provider/pihole/clientV6_test.go b/provider/pihole/clientV6_test.go index 34be0e28b..66c5ee095 100644 --- a/provider/pihole/clientV6_test.go +++ b/provider/pihole/clientV6_test.go @@ -132,7 +132,7 @@ func TestNewPiholeClientV6(t *testing.T) { w.Header().Set("Content-Type", "application/json") - if requestData["password"] != "correct" { + if requestData["password"] != "correct" && requestData["password"] != "correct\\with\"special'characters" { // Return unsuccessful authentication response w.WriteHeader(http.StatusUnauthorized) _, err = w.Write([]byte(`{ @@ -185,7 +185,22 @@ func TestNewPiholeClientV6(t *testing.T) { t.Fatal(err) } if cl.(*piholeClientV6).token != "supersecret" { - t.Error("Parsed invalid token from login response:", cl.(*piholeClient).token) + t.Error("Parsed invalid token from login response:", cl.(*piholeClientV6).token) + } + + // Test correct password with special characters + cl, err = newPiholeClientV6( + PiholeConfig{ + Server: srvr.URL, + APIVersion: "6", + Password: "correct\\with\"special'characters", + }, + ) + if err != nil { + t.Fatal(err) + } + if cl.(*piholeClientV6).token != "supersecret" { + t.Error("Parsed invalid token from login response:", cl.(*piholeClientV6).token) } }