Skip to content

Added exception for Twitter and OAuth missing options #3676

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

Merged
merged 2 commits into from
Mar 28, 2017

Conversation

montymxb
Copy link
Contributor

Bringing over a patch for a noted issue in a PR from the php sdk.

It looks like while attempting to utilize third party authentication via twitter there is the possibility for the server to experience an error. This can be caused if the server is either missing it's auth configuration option entirely or just the twitter component.

Taking a look at src/Adapters/Auth/twitter.js under validateAuthData on line 9 you can see where an options object (from the server config) of undefined may be passed unchecked into the constructor of OAuth.

var client = new OAuth(options);

Internally OAuth won't attempt to discern whether it's been passed a valid object or not, and will attempt to extract the properties regardless. This ends up with a TypeError being thrown with the message Cannot read property 'consumer_key' of undefined when access is attempted on the first property.

Ultimately the exception is noted in the server logs but the client receives the following response:

{"code":1,"message":"Internal server error."}

This is particularly troublesome as this is a valid response to most SDKs, in this case to the php sdk. Without any error property in sight the sdk will proceed to return the results for further use as legitimate data.

While the SDKs could attempt to screen for this type of response it's not very specific or helpful without the server logs. This PR attempts to address that by adding a check and exception throw in both twitter.js and in OAuth1Client.js.

The exception thrown in OAuth is more general, simply indicating that no options were passed for OAuth. This is mostly to prevent unintended usage in the future.

However, for the sdks this response may not be specific enough for say, a missing twitter auth. In this regard twitter.validateAuthData can check and throw a specific exception 'Twitter auth configuration missing' upon a missing options parameter. This exception is more pointed towards notifying the SDK (and it's developers) that their server is missing the required configuration. This not only plugs up the current internal server error, but, ideally, promotes correction of it as well.

Tests have been updated to account for this behavior in both cases. Also a little typo fix in TwitterAuth.spec.js, missing semi-colon :).

@montymxb montymxb changed the title Added exception for Twitter and OAuth missing configuration information Added exception for Twitter and OAuth missing options Mar 28, 2017

var OAuth = function(options) {
if(!options) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'No options passed to OAuth');
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be a -1 don't you think? As it's still a server internal error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea that makes sense, considering case there's no way to fire the exception unless you call it without the proper object.

@@ -5,6 +5,9 @@ var logger = require('../../logger').default;

// Returns a promise that fulfills iff this user id is valid.
function validateAuthData(authData, options) {
if(!options) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Twitter auth configuration missing');
Copy link
Contributor

Choose a reason for hiding this comment

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

same here, it should probably be a -1 as it's a server misconfiguration. easy to spot with the message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea this will work as well. I took a quick look at all the error codes seeing if there was something more relevant for a misconfig, nothing looks to fit the bill. -1 it is!

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, -1 is a go to error for server misconfiguration.

@montymxb
Copy link
Contributor Author

montymxb commented Mar 28, 2017

@flovilmart updated with new error codes. Noted that INTERNAL_SERVER_ERROR is a code 1 instead of a -1, just using the provided definition.

@flovilmart
Copy link
Contributor

Good call :)

@montymxb
Copy link
Contributor Author

No problem! Let me know if you need those commits squashed or anything else before this goes in.

@flovilmart
Copy link
Contributor

We squash on merge! It's ready to go!

@flovilmart flovilmart merged commit e01b417 into parse-community:master Mar 28, 2017
@facebook-github-bot
Copy link

@montymxb updated the pull request - view changes

@flovilmart
Copy link
Contributor

Thanks again for the quick fix!

@montymxb
Copy link
Contributor Author

Excellent! No problem and thanks for the quick response @flovilmart

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