Skip to content

Commit e6a8445

Browse files
fix: added validation check for user credentials (#228)
* fix: added validation check for user credentials * fix: added error code and more info in validate credentials * fix: added inline comments
1 parent 2fcaed4 commit e6a8445

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

client/client.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ import (
1616
"github.com/twilio/twilio-go/client/form"
1717
)
1818

19+
var alphanumericRegex *regexp.Regexp
20+
var delimitingRegex *regexp.Regexp
21+
22+
func init() {
23+
alphanumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]*$`)
24+
delimitingRegex = regexp.MustCompile(`\.\d+`)
25+
}
26+
1927
// Credentials store user authentication credentials.
2028
type Credentials struct {
2129
Username string
@@ -87,6 +95,26 @@ func (c *Client) doWithErr(req *http.Request) (*http.Response, error) {
8795
return res, nil
8896
}
8997

98+
// throws error if username and password contains special characters
99+
func (c *Client) validateCredentials() error {
100+
username, password := c.basicAuth()
101+
if !alphanumericRegex.MatchString(username) {
102+
return &TwilioRestError{
103+
Status: 400,
104+
Code: 21222,
105+
Message: "Invalid Username. Illegal chars",
106+
MoreInfo: "https://www.twilio.com/docs/errors/21222"}
107+
}
108+
if !alphanumericRegex.MatchString(password) {
109+
return &TwilioRestError{
110+
Status: 400,
111+
Code: 21224,
112+
Message: "Invalid Password. Illegal chars",
113+
MoreInfo: "https://www.twilio.com/docs/errors/21224"}
114+
}
115+
return nil
116+
}
117+
90118
// SendRequest verifies, constructs, and authorizes an HTTP request.
91119
func (c *Client) SendRequest(method string, rawURL string, data url.Values,
92120
headers map[string]interface{}) (*http.Response, error) {
@@ -101,8 +129,7 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
101129
if method == http.MethodGet {
102130
if data != nil {
103131
v, _ := form.EncodeToStringWith(data, delimiter, escapee, keepZeros)
104-
regex := regexp.MustCompile(`\.\d+`)
105-
s := regex.ReplaceAllString(v, "")
132+
s := delimitingRegex.ReplaceAllString(v, "")
106133

107134
u.RawQuery = s
108135
}
@@ -112,6 +139,11 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
112139
valueReader = strings.NewReader(data.Encode())
113140
}
114141

142+
credErr := c.validateCredentials()
143+
if credErr != nil {
144+
return nil, credErr
145+
}
146+
115147
req, err := http.NewRequest(method, u.String(), valueReader)
116148
if err != nil {
117149
return nil, err

client/client_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ func TestClient_SendRequestErrorWithDetails(t *testing.T) {
116116
assert.Equal(t, details, twilioError.Details)
117117
}
118118

119+
func TestClient_SendRequestUsernameError(t *testing.T) {
120+
newTestClient := NewClient("user1\nuser2", "pass")
121+
resp, err := newTestClient.SendRequest("GET", "http://example.org", nil, nil) //nolint:bodyclose
122+
twilioError := err.(*twilio.TwilioRestError)
123+
assert.Nil(t, resp)
124+
assert.Equal(t, 400, twilioError.Status)
125+
assert.Equal(t, 21222, twilioError.Code)
126+
assert.Equal(t, "https://www.twilio.com/docs/errors/21222", twilioError.MoreInfo)
127+
assert.Equal(t, "Invalid Username. Illegal chars", twilioError.Message)
128+
}
129+
130+
func TestClient_SendRequestPasswordError(t *testing.T) {
131+
newTestClient := NewClient("user1", "pass1\npass2")
132+
resp, err := newTestClient.SendRequest("GET", "http://example.org", nil, nil) //nolint:bodyclose
133+
twilioError := err.(*twilio.TwilioRestError)
134+
assert.Nil(t, resp)
135+
assert.Equal(t, 400, twilioError.Status)
136+
assert.Equal(t, 21224, twilioError.Code)
137+
assert.Equal(t, "https://www.twilio.com/docs/errors/21224", twilioError.MoreInfo)
138+
assert.Equal(t, "Invalid Password. Illegal chars", twilioError.Message)
139+
}
140+
119141
func TestClient_SendRequestWithRedirect(t *testing.T) {
120142
redirectServer := httptest.NewServer(http.HandlerFunc(
121143
func(writer http.ResponseWriter, request *http.Request) {

0 commit comments

Comments
 (0)