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(sep-10): use 5 minutes grace period for challenge tx #256

Merged
merged 3 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
6 changes: 5 additions & 1 deletion sdk/lib/stellar/sep10.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class InvalidSep10ChallengeError < StandardError; end
class SEP10
include Stellar::DSL

# We use a small grace period for the challenge transaction time bounds
# to compensate possible clock drift on client's machine
GRACE_PERIOD = 5.minutes

# Helper method to create a valid {SEP0010}[https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md]
# challenge transaction which you can use for Stellar Web Authentication.
#
Expand Down Expand Up @@ -157,7 +161,7 @@ def self.read_challenge_tx(server:, challenge_xdr:, **options)
time_bounds = transaction.time_bounds
now = Time.now.to_i

if time_bounds.blank? || !now.between?(time_bounds.min_time, time_bounds.max_time)
if time_bounds.blank? || !now.between?(time_bounds.min_time - GRACE_PERIOD, time_bounds.max_time + GRACE_PERIOD)
raise InvalidSep10ChallengeError, "The transaction has expired"
end

Expand Down
42 changes: 30 additions & 12 deletions sdk/spec/lib/stellar/sep10_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,41 @@
expect { read_challenge }.to raise_invalid("is not signed by the server")
end

it "throws an error if transaction does not contain timeBounds" do
transaction.time_bounds = nil
describe "transaction time bounds" do
context "when transaction does not contain timeBounds" do
before { transaction.time_bounds = nil }

expect { read_challenge }.to raise_invalid("has expired")
end
it "throws an error" do
expect { read_challenge }.to raise_invalid("has expired")
end
end

it "throws an error if challenge is expired" do
transaction.time_bounds = Stellar::TimeBounds.new(min_time: 0, max_time: 5)
it "uses 5 minutes grace period for validation" do
now = Time.now.to_i

expect { read_challenge }.to raise_invalid("has expired")
end
transaction.time_bounds = Stellar::TimeBounds.new(min_time: now + 1.minute, max_time: now + 2.minutes)
expect { read_challenge }.not_to raise_error

transaction.time_bounds = Stellar::TimeBounds.new(min_time: now - 2.minutes, max_time: now - 1.minute)
expect { read_challenge }.not_to raise_error
end

it "throws an error if challenge is in the future" do
now = Time.now.to_i
transaction.time_bounds = Stellar::TimeBounds.new(min_time: now + 100, max_time: now + 500)
context "when challenge is expired beyond grace period" do
before { transaction.time_bounds = Stellar::TimeBounds.new(min_time: 0, max_time: 5) }

expect { read_challenge }.to raise_invalid("has expired")
it "throws an error if challenge is expired" do
expect { read_challenge }.to raise_invalid("has expired")
end
end

context "when challenge is in the future beyond grace period" do
it "throws an error" do
now = Time.now.to_i
transaction.time_bounds = Stellar::TimeBounds.new(min_time: now + 6.minutes, max_time: now + 7.minutes)

expect { read_challenge }.to raise_invalid("has expired")
end
end
end

it "throws an error if provided auth domain is wrong" do
Expand Down