From b0381b12e825c0a9309e060a7286c4c6d04dad34 Mon Sep 17 00:00:00 2001 From: Hugo Blanc Date: Sat, 15 Jul 2023 21:09:33 +0200 Subject: [PATCH] fix: check response size before accessing the array this should partially solve #9 --- cloudflare/cloudflare.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/cloudflare/cloudflare.go b/cloudflare/cloudflare.go index e5b7392..4ef42bf 100644 --- a/cloudflare/cloudflare.go +++ b/cloudflare/cloudflare.go @@ -13,7 +13,10 @@ const ( ActiveCertificate = "active" ) -var ErrEmptyResponse = errors.New("cloudflare returned nothing") +var ( + ErrEmptyResponse = errors.New("cloudflare returned nothing") + ErrNoResult = errors.New("cloudflare did not return any result in the response") +) type Credentials struct { Token string @@ -65,6 +68,10 @@ func GetZoneID(name string, credz Credentials) (string, error) { return "", ErrEmptyResponse } + if len(holder.Result) < 1 { + return "", ErrNoResult + } + return holder.Result[0].ID, nil } @@ -108,6 +115,10 @@ func GetTXTValues(id string, credz Credentials) ([]ValidationRecords, error) { return []ValidationRecords{}, ErrEmptyResponse } + if len(holder.Result) < 1 { + return []ValidationRecords{}, ErrNoResult + } + return holder.Result[0].ValidationRecords, nil } @@ -150,6 +161,10 @@ func GetCertificatePacksStatus(id string, credz Credentials) (string, error) { return "", ErrEmptyResponse } + if len(holder.Result) < 1 { + return "", ErrNoResult + } + switch holder.Result[0].Status { case "active": return ActiveCertificate, nil