-
Notifications
You must be signed in to change notification settings - Fork 26
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
Validate the JSON data in load #36
Conversation
Check to make sure that the loaded JSON actually contains data in the keys we are going to use before we use them, to avoid runtime exceptions on Nil.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ashleym1972, thank for you contribution. Would you mind adding a test for this.
lib/u2f/register_response.rb
Outdated
@@ -20,6 +20,10 @@ def self.load_from_json(json) | |||
raise RegistrationError, code: data['errorCode'] | |||
end | |||
|
|||
if data['clientData'].blank? || data['registrationData'].blank? | |||
raise RegistrationError, code: 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put the code in a constant, for clarity
When trying to sign response make sue the data is correct and raise a known error rather than a method missing error on bad data.
1 similar comment
Looks good to me. @mastahyeti any thoughts? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems okay. Validating a JSON schema would be more robust, but would probably be overkill. Also, can you add a similar check to ClientData.load_from_json
while you're at it?
lib/u2f/sign_response.rb
Outdated
@@ -4,6 +4,10 @@ class SignResponse | |||
|
|||
def self.load_from_json(json) | |||
data = ::JSON.parse(json) | |||
if data['clientData'].nil? || data['keyHandle'].nil? || data['signatureData'].nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you doing a #nil?
check in one place and just checking that the key exists in the other?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure which one people here prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whichever, but it would be nice to be consistent if the intent is the same.
lib/u2f/register_response.rb
Outdated
@@ -20,6 +21,10 @@ def self.load_from_json(json) | |||
raise RegistrationError, code: data['errorCode'] | |||
end | |||
|
|||
if !data.key?('clientData') || !data.key?('registrationData') | |||
raise RegistrationError, code: BAD_REQUEST |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These error codes normally come from the browser. It might make more sense to pass a message:
argument instead of a code:
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surprisingly that is not my use case. Additionally, the Rails / browser mind set in this library is a very bad idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error codes are specific to the JavaScript API (here). It would make more sense to specify an error message here than to reuse the codes from the JS API.
1 similar comment
What's still missing here? |
Sorry dropped the ball on this one. Merging |
Thanks @wallin - this library is great. |
Check to make sure that the loaded JSON actually contains data in the keys we are going to use before we use them, to avoid runtime exceptions on Nil.