Skip to content

Commit 3a09755

Browse files
committed
Ensure S3 presigned default expires time is not changing
It occurred to us that signatures end up invalid randomly because the default signature time is computed twice and we can end up with a policy that was signed for a policy with an expiration time 1 second earlier. To be specific, the policy is computed twice inside the `fields` method which uses the `formation_expiration` twice too, which in turn computes `Time.now` at two different times. @see marcel/aws-s3#54 (similar issue)
1 parent 109ab53 commit 3a09755

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

lib/aws/s3/presigned_post.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def initialize(bucket, opts = {})
207207
@content_length = range_value(opts[:content_length])
208208
@conditions = opts[:conditions] || {}
209209
@ignored_fields = [opts[:ignore]].flatten.compact
210-
@expires = opts[:expires]
210+
@expires = opts[:expires] || Time.now.utc + 60*60
211211

212212
super
213213

@@ -397,17 +397,16 @@ def with_condition(field, condition)
397397
# @api private
398398
private
399399
def format_expiration
400-
time = expires || Time.now.utc + 60*60
401400
time =
402-
case time
401+
case expires
403402
when Time
404-
time
403+
expires
405404
when DateTime
406-
Time.parse(time.to_s)
405+
Time.parse(expires.to_s)
407406
when Integer
408-
(Time.now + time)
407+
(Time.now + expires)
409408
when String
410-
Time.parse(time)
409+
Time.parse(expires)
411410
end
412411
time.utc.iso8601
413412
end

spec/aws/s3/presigned_post_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,17 @@ def policy_conditions(post)
352352
policy["expiration"].should == "2011-05-25T01:51:04Z"
353353
end
354354

355+
it "should reuse the default expire set during initialize" do
356+
now = Time.parse("2011-05-24T17:54:04-07:00Z")
357+
Time.stub(:now).and_return(now)
358+
policy["expiration"].should == "2011-05-25T01:54:04Z"
359+
360+
later = Time.parse("2011-05-24T17:54:05-07:00Z")
361+
Time.stub(:now).and_return(later)
362+
later_policy = JSON.load(Base64.decode64(post.policy))
363+
later_policy["expiration"].should == "2011-05-25T01:54:04Z"
364+
end
365+
355366
context 'when :expires is provided' do
356367

357368
it 'should support Time' do

0 commit comments

Comments
 (0)