Skip to content

Commit 0f10695

Browse files
fix: match api urls with version path (#1297)
1 parent 6666fb1 commit 0f10695

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

pkg/stripe/url.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
const (
99
// DefaultAPIBaseURL is the default base URL for API requests
1010
DefaultAPIBaseURL = "https://api.stripe.com"
11+
APIBaseURLRegexp = `^https:\/\/api\.stripe\.com\/v\d+$`
1112
qaAPIBaseURL = "https://qa-api.stripe.com"
1213
devAPIBaseURLRegexp = `http(s)?:\/\/[A-Za-z0-9\-]+api-mydev.dev.stripe.me`
1314

@@ -46,7 +47,7 @@ func isValid(url string, exactStrings []string, regexpStrings []string) bool {
4647
// ValidateAPIBaseURL returns an error if apiBaseURL isn't allowed
4748
func ValidateAPIBaseURL(apiBaseURL string) error {
4849
exactStrings := []string{DefaultAPIBaseURL, qaAPIBaseURL}
49-
regexpStrings := []string{devAPIBaseURLRegexp, localhostURLRegexp}
50+
regexpStrings := []string{APIBaseURLRegexp, devAPIBaseURLRegexp, localhostURLRegexp}
5051
if isValid(apiBaseURL, exactStrings, regexpStrings) {
5152
return nil
5253
}

pkg/stripe/url_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88

99
func TestValidateAPIBaseURLWorks(t *testing.T) {
1010
assert.Nil(t, ValidateAPIBaseURL("https://api.stripe.com"))
11+
assert.Nil(t, ValidateAPIBaseURL("https://api.stripe.com/v1"))
12+
assert.Nil(t, ValidateAPIBaseURL("https://api.stripe.com/v2"))
13+
assert.Nil(t, ValidateAPIBaseURL("https://api.stripe.com/v100"))
1114
assert.Nil(t, ValidateAPIBaseURL("https://qa-api.stripe.com"))
1215
assert.Nil(t, ValidateAPIBaseURL("http://foo-api-mydev.dev.stripe.me"))
1316
assert.Nil(t, ValidateAPIBaseURL("https://foo-lv5r9y--api-mydev.dev.stripe.me/"))
@@ -18,6 +21,7 @@ func TestValidateAPIBaseURLWorks(t *testing.T) {
1821
assert.ErrorIs(t, ValidateAPIBaseURL("https://unknowndomain"), errInvalidAPIBaseURL)
1922
assert.ErrorIs(t, ValidateAPIBaseURL("localhost"), errInvalidAPIBaseURL)
2023
assert.ErrorIs(t, ValidateAPIBaseURL("anything_else"), errInvalidAPIBaseURL)
24+
assert.ErrorIs(t, ValidateAPIBaseURL("https://api.stripe.com/v1.1"), errInvalidAPIBaseURL)
2125
}
2226

2327
func TestValidateDashboardBaseURLWorks(t *testing.T) {

0 commit comments

Comments
 (0)