@@ -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.
2028type 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.
91119func (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
0 commit comments