-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Validate base URLs * Fix lint error * allow 127.0.0.1 for tests * cleanup
- Loading branch information
1 parent
2206d0a
commit 94c1bee
Showing
12 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package stripe | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
) | ||
|
||
const ( | ||
// DefaultAPIBaseURL is the default base URL for API requests | ||
DefaultAPIBaseURL = "https://api.stripe.com" | ||
qaAPIBaseURL = "https://qa-api.stripe.com" | ||
devAPIBaseURLRegexp = `http(s)?:\/\/[A-Za-z0-9\-]+api-mydev.dev.stripe.me` | ||
|
||
// DefaultFilesAPIBaseURL is the default base URL for Files API requsts | ||
DefaultFilesAPIBaseURL = "https://files.stripe.com" | ||
|
||
// DefaultDashboardBaseURL is the default base URL for dashboard requests | ||
DefaultDashboardBaseURL = "https://dashboard.stripe.com" | ||
qaDashboardBaseURL = "https://qa-dashboard.stripe.com" | ||
devDashboardBaseURLRegexp = `http(s)?:\/\/[A-Za-z0-9\-]+manage-mydev\.dev\.stripe\.me` | ||
|
||
// localhostURLRegexp is used in tests | ||
localhostURLRegexp = `http:\/\/127\.0\.0\.1(:[0-9]+)?` | ||
) | ||
|
||
var ( | ||
errInvalidAPIBaseURL = errors.New("invalid API base URL") | ||
errInvalidDashboardBaseURL = errors.New("invalid dashboard base URL") | ||
) | ||
|
||
func isValid(url string, exactStrings []string, regexpStrings []string) bool { | ||
for _, s := range exactStrings { | ||
if url == s { | ||
return true | ||
} | ||
} | ||
for _, r := range regexpStrings { | ||
matched, err := regexp.Match(r, []byte(url)) | ||
if err == nil && matched { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// ValidateAPIBaseURL returns an error if apiBaseURL isn't allowed | ||
func ValidateAPIBaseURL(apiBaseURL string) error { | ||
exactStrings := []string{DefaultAPIBaseURL, qaAPIBaseURL} | ||
regexpStrings := []string{devAPIBaseURLRegexp, localhostURLRegexp} | ||
if isValid(apiBaseURL, exactStrings, regexpStrings) { | ||
return nil | ||
} | ||
return errInvalidAPIBaseURL | ||
} | ||
|
||
// ValidateDashboardBaseURL returns an error if dashboardBaseURL isn't allowed | ||
func ValidateDashboardBaseURL(dashboardBaseURL string) error { | ||
exactStrings := []string{DefaultDashboardBaseURL, qaDashboardBaseURL} | ||
regexpStrings := []string{devDashboardBaseURLRegexp, localhostURLRegexp} | ||
if isValid(dashboardBaseURL, exactStrings, regexpStrings) { | ||
return nil | ||
} | ||
return errInvalidDashboardBaseURL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package stripe | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateAPIBaseURLWorks(t *testing.T) { | ||
assert.Nil(t, ValidateAPIBaseURL("https://api.stripe.com")) | ||
assert.Nil(t, ValidateAPIBaseURL("https://qa-api.stripe.com")) | ||
assert.Nil(t, ValidateAPIBaseURL("http://foo-api-mydev.dev.stripe.me")) | ||
assert.Nil(t, ValidateAPIBaseURL("https://foo-lv5r9y--api-mydev.dev.stripe.me/")) | ||
assert.Nil(t, ValidateAPIBaseURL("http://127.0.0.1")) | ||
assert.Nil(t, ValidateAPIBaseURL("http://127.0.0.1:1337")) | ||
|
||
assert.ErrorIs(t, ValidateAPIBaseURL("https://example.com"), errInvalidAPIBaseURL) | ||
assert.ErrorIs(t, ValidateAPIBaseURL("https://unknowndomain"), errInvalidAPIBaseURL) | ||
assert.ErrorIs(t, ValidateAPIBaseURL("localhost"), errInvalidAPIBaseURL) | ||
assert.ErrorIs(t, ValidateAPIBaseURL("anything_else"), errInvalidAPIBaseURL) | ||
} | ||
|
||
func TestValidateDashboardBaseURLWorks(t *testing.T) { | ||
assert.Nil(t, ValidateDashboardBaseURL("https://dashboard.stripe.com")) | ||
assert.Nil(t, ValidateDashboardBaseURL("https://qa-dashboard.stripe.com")) | ||
assert.Nil(t, ValidateDashboardBaseURL("http://foo-manage-mydev.dev.stripe.me")) | ||
assert.Nil(t, ValidateDashboardBaseURL("https://foo-lv5r9y--manage-mydev.dev.stripe.me/")) | ||
assert.Nil(t, ValidateDashboardBaseURL("http://127.0.0.1")) | ||
assert.Nil(t, ValidateDashboardBaseURL("http://127.0.0.1:1337")) | ||
|
||
assert.ErrorIs(t, ValidateDashboardBaseURL("https://example.com"), errInvalidDashboardBaseURL) | ||
assert.ErrorIs(t, ValidateDashboardBaseURL("https://unknowndomain"), errInvalidDashboardBaseURL) | ||
assert.ErrorIs(t, ValidateDashboardBaseURL("localhost"), errInvalidDashboardBaseURL) | ||
assert.ErrorIs(t, ValidateDashboardBaseURL("anything_else"), errInvalidDashboardBaseURL) | ||
} |