Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added validation check for user credentials #228

Merged
merged 4 commits into from
Feb 9, 2024

Conversation

tiwarishubham635
Copy link
Contributor

@tiwarishubham635 tiwarishubham635 commented Feb 7, 2024

Fixes

Added check for username and Password to be alphanumeric

Checklist

  • I acknowledge that all my contributions will be made under the project's license
  • I have made a material change to the repo (functionality, testing, spelling, grammar)
  • I have read the Contribution Guidelines and my PR follows them
  • I have titled the PR appropriately
  • I have updated my branch with the main branch
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation about the functionality in the appropriate .md file
  • I have added inline documentation to the code I modified

If you have questions, please file a support ticket, or create a GitHub Issue in this repository.

client/client.go Outdated
@@ -87,6 +87,24 @@ func (c *Client) doWithErr(req *http.Request) (*http.Response, error) {
return res, nil
}

func isAlphanumeric(word string) bool {
return regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(word)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not very efficient to do this every single time someone calls isAlphanumeric - make the regexp a package level variable that gets initialized one time.

Note, I believe this will also pass if word is the empty string. It would be pretty easy to add some basic smoke tests for this function.

client/client.go Outdated
username, password := c.basicAuth()
err := &TwilioRestError{Status: 400}
if !isAlphanumeric(username) {
err.Message = "Invalid Username"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be pretty vague and confusing for end users - if I get "Invalid Username" it might not be obvious to me where the problem is, especially if e.g. the user is pulling a username from an environment variable or a web form.

Can we make the error clearer about what the problem is?

@@ -112,6 +130,11 @@ func (c *Client) SendRequest(method string, rawURL string, data url.Values,
valueReader = strings.NewReader(data.Encode())
}

credErr := c.validateCredentials()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that there are some Twilio endpoints that allow data to be retrieved without a valid username and password so it would be great to know if there is a product spec for this.

Status: 400,
Code: 21222,
Message: "Invalid Username. Illegal chars",
MoreInfo: "https://www.twilio.com/docs/errors/21222"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added error codes and more info from error page

Copy link

sonarqubecloud bot commented Feb 9, 2024

@tiwarishubham635 tiwarishubham635 merged commit e6a8445 into main Feb 9, 2024
16 checks passed
@tiwarishubham635 tiwarishubham635 deleted the validate_credentials branch February 9, 2024 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants