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

feat: add client data origin validation #15

Merged
merged 3 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: add client data origin validation
  • Loading branch information
brauliomartinezlm committed May 23, 2018
commit af83f77eae055472ab45b9984ac1c73067c4fa65
7 changes: 6 additions & 1 deletion lib/webauthn/authenticator_attestation_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ def initialize(attestation_object:, client_data_json:)
@client_data_json = client_data_json
end

def valid?(original_challenge)
def valid?(original_challenge, original_origin)
valid_type? &&
valid_challenge?(original_challenge) &&
valid_origin?(original_origin) &&
authenticator_data.valid? &&
user_present? &&
valid_attestation_statement?
Expand All @@ -34,6 +35,10 @@ def valid_challenge?(original_challenge)
Base64.urlsafe_decode64(client_data.challenge) == Base64.urlsafe_decode64(original_challenge)
end

def valid_origin?(original_origin)
client_data.origin == original_origin
end

def valid_attestation_statement?
if attestation_format == ATTESTATION_FORMAT_NONE
true
Expand Down
4 changes: 4 additions & 0 deletions lib/webauthn/client_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def challenge
data["challenge"]
end

def origin
data["origin"]
end

private

attr_reader :client_data_json
Expand Down
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "bundler/setup"
require "webauthn"
require "cbor"

require "byebug"

Expand Down Expand Up @@ -39,3 +40,11 @@ def seeds
}
}
end

def hash_to_encoded_json(hash)
Base64.urlsafe_encode64(hash.to_json)
end

def hash_to_encoded_cbor(hash)
Base64.urlsafe_encode64(CBOR.encode(hash))
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! 👏

47 changes: 45 additions & 2 deletions spec/webauthn/authenticator_attestation_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
client_data_json: response[:client_data_json]
)

expect(response.valid?(original_challenge)).to eq(true)
expect(response.valid?(original_challenge, "http:https://localhost:3000")).to eq(true)
end

it "returns user-friendly error if no client data received" do
Expand All @@ -22,7 +22,50 @@
)

expect {
response.valid?("")
response.valid?("", "")
}.to raise_exception(RuntimeError, "Missing client_data_json")
end

describe "origin validation" do
let(:base_url) { "http:https://localhost" }
Copy link
Contributor

Choose a reason for hiding this comment

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

What about calling this original_origin instead of base_url? Feel like it might help understandability.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good 👍

let(:challenge) { Base64.urlsafe_encode64(SecureRandom.random_bytes(16)) }
Copy link
Contributor

Choose a reason for hiding this comment

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

Same reasoning here, original_challenge instead of challenge?

let(:client_data_json) { hash_to_encoded_json(challenge: challenge,
clientExtensions: {},
hashAlgorithm: "SHA-256",
origin: origin,
type: "webauthn.create") }
let(:auth_data) { [73, 150, 13, 229, 136, 14, 140, 104, 116, 52, 23, 15, 100,
118, 96, 91, 143, 228, 174, 185, 162, 134, 50, 199, 153,
92, 243, 186, 131, 29, 151, 99, 65, 0, 0, 0, 0].pack('c*') }
let(:attestation_object) { hash_to_encoded_cbor(fmt: "none",
attStmt: {},
authData: auth_data) }

context "matches the default one" do
let(:origin) { "http:https://localhost" }

it "is valid" do
response = WebAuthn::AuthenticatorAttestationResponse.new(
attestation_object: attestation_object,
client_data_json: client_data_json
)

expect(response.valid?(challenge, base_url)).to be_truthy
end
end

context "doesn't match the default one" do
let(:origin) { "http:https://invalid" }

it "isn't valid" do
response = WebAuthn::AuthenticatorAttestationResponse.new(
attestation_object: attestation_object,
client_data_json: client_data_json
)

expect(response.valid?(challenge, base_url)).to be_falsy
end
end
end

Copy link
Contributor

Choose a reason for hiding this comment

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

There's already a .rubocop.yml in place with just a few cops enabled, very minimal. The goal was to keep some kind of consistency from the start.

Do you mind checking if bundle exec rubocop is green for this branch? If you have an opinion about changing any of the enabled cops let me know :-)

Copy link
Member Author

Choose a reason for hiding this comment

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

Didn't know about it :). Will address!

end