Skip to content

Commit

Permalink
validate APIBase
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc committed Jan 2, 2025
1 parent 81f1205 commit 3366c74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
6 changes: 1 addition & 5 deletions httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/mailgun/errors"
)

var validURL = regexp.MustCompile(`/v[1-5].*`)
var invalidURL = regexp.MustCompile(`/v\d+.*`)

type httpRequest struct {
URL string
Expand Down Expand Up @@ -370,10 +370,6 @@ func (r *httpRequest) generateUrlWithParameters() (string, error) {
return "", err
}

if !validURL.MatchString(uri.Path) {
return "", errors.New(`APIBase must end with a /v1, /v2, /v3 or /v4; SetAPIBase("https://host/v3")`)
}

q := uri.Query()
if len(r.Parameters) > 0 {
for name, values := range r.Parameters {
Expand Down
14 changes: 11 additions & 3 deletions mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type Mailgun interface {
APIKey() string
HTTPClient() *http.Client
SetHTTPClient(client *http.Client)
SetAPIBase(url string)
SetAPIBase(url string) error
AddOverrideHeader(k string, v string)

// Send attempts to queue a message (see CommonMessage, NewMessage, and its methods) for delivery.
Expand Down Expand Up @@ -276,7 +276,10 @@ func NewMailgunFromEnv() (*MailgunImpl, error) {

url := os.Getenv("MG_URL")
if url != "" {
mg.SetAPIBase(url)
err := mg.SetAPIBase(url)
if err != nil {
return nil, err
}
}

webhookSigningKey := os.Getenv("MG_WEBHOOK_SIGNING_KEY")
Expand Down Expand Up @@ -342,8 +345,13 @@ func (mg *MailgunImpl) RemoveOnBehalfOfSubaccount() {
//
// // Set a custom base API
// mg.SetAPIBase("https://localhost")
func (mg *MailgunImpl) SetAPIBase(address string) {
func (mg *MailgunImpl) SetAPIBase(address string) error {
if invalidURL.MatchString(address) {
return errors.New(`APIBase must not contain a version; SetAPIBase("https://host")`)
}

mg.apiBase = address
return nil
}

// AddOverrideHeader allows the user to specify additional headers that will be included in the HTTP request
Expand Down
24 changes: 10 additions & 14 deletions mailgun_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mailgun_test

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -29,11 +28,8 @@ func TestMailgun(t *testing.T) {

func TestInvalidBaseAPI(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase("https://localhost")

ctx := context.Background()
_, err := mg.GetDomain(ctx, "unknown.domain")
assert.EqualError(t, err, `APIBase must end with a /v1, /v2, /v3 or /v4; SetAPIBase("https://host/v3")`)
err := mg.SetAPIBase("https://localhost/v3")
assert.EqualError(t, err, `APIBase must not contain a version; SetAPIBase("https://host")`)
}

func TestValidBaseAPI(t *testing.T) {
Expand All @@ -47,17 +43,17 @@ func TestValidBaseAPI(t *testing.T) {
}))

apiBases := []string{
fmt.Sprintf("%s/v3", testServer.URL),
fmt.Sprintf("%s/proxy/v3", testServer.URL),
mailgun.APIBase,
mailgun.APIBaseEU,
fmt.Sprintf("%s", testServer.URL),
}

for _, apiBase := range apiBases {
mg := mailgun.NewMailgun(testKey)
mg.SetAPIBase(apiBase)

ctx := context.Background()
_, err := mg.GetDomain(ctx, "unknown.domain")
require.NoError(t, err)
t.Run(apiBase, func(t *testing.T) {
mg := mailgun.NewMailgun(testKey)
err := mg.SetAPIBase(apiBase)
require.NoError(t, err)
})
}
}

Expand Down

0 comments on commit 3366c74

Please sign in to comment.