Skip to content

Commit f5a3664

Browse files
committed
Merge pull request #861 from didacte/bugfix/s3-presigned-default-expires
Ensure S3 presigned default expires time is not changing
2 parents 109ab53 + 3a09755 commit f5a3664

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)