Skip to content

Commit a98add6

Browse files
author
Amir Tocker
committed
ACL should be escaped by the API.
1 parent 83a0373 commit a98add6

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

lib/cloudinary/auth_token.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def generate_auth_token(options = {})
2121
duration = options[:duration]
2222
url = options[:url]
2323
start = Time.new.getgm.to_i if start == 'now'
24-
unless expiration
25-
if duration
24+
if expiration.nil? || expiration == 0
25+
if !(duration.nil? || duration == 0)
2626
expiration = (start || Time.new.getgm.to_i) + duration
2727
else
2828
throw 'Must provide either expiration or duration'
@@ -33,17 +33,19 @@ def generate_auth_token(options = {})
3333
token << "ip=#{ip}" if ip
3434
token << "st=#{start}" if start
3535
token << "exp=#{expiration}"
36-
token << "acl=#{acl}" if acl
36+
token << "acl=#{escape_to_lower(acl)}" if acl
3737
to_sign = token.clone
38-
if url
39-
url = CGI::escape(url).gsub(/%../){|h| h.downcase}
40-
to_sign << "url=#{url}"
41-
end
38+
to_sign << "url=#{escape_to_lower(url)}" if url
4239
auth = digest(to_sign.join(SEPARATOR), key)
4340
token << "hmac=#{auth}"
4441
"#{name}=#{token.join(SEPARATOR)}"
4542
end
4643

44+
# escape URI pattern using lowercase hex. For example "/" -> "%2f".
45+
def escape_to_lower(url)
46+
CGI::escape(url).gsub(/%../) { |h| h.downcase }
47+
end
48+
4749
private
4850

4951
def digest(message, key)

lib/cloudinary/utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def self.unsigned_download_url(source, options = {})
320320
if options[:auth_token] == false
321321
auth_token = false
322322
else
323-
auth_token = Cloudinary.config.auth_token.to_h.merge { options[:auth_token].to_h }
323+
auth_token = Cloudinary.config.auth_token.to_h.merge( options[:auth_token].to_h )
324324
end
325325

326326
original_source = source

spec/auth_token_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@url_backup = ENV["CLOUDINARY_URL"]
1111
end
1212
before do
13-
Cloudinary.config_from_url "cloudinary://a:b@test123?load_strategies=false"
13+
Cloudinary.config_from_url "cloudinary://a:b@test123"
1414
Cloudinary.config.auth_token = { :key => KEY, :duration => 300, :start_time => 11111111 }
1515
end
1616
after do
@@ -19,7 +19,7 @@
1919
end
2020
it "should generate with start and duration" do
2121
token = Cloudinary::Utils.generate_auth_token :start_time => 1111111111, :acl => "/image/*", :duration => 300
22-
expect(token).to eq '__cld_token__=st=1111111111~exp=1111111411~acl=/image/*~hmac=0854e8b6b6a46471a80b2dc28c69bd352d977a67d031755cc6f3486c121b43af'
22+
expect(token).to eq '__cld_token__=st=1111111111~exp=1111111411~acl=%2fimage%2f%2a~hmac=0d5b0c9c1485ee162c459879fe62e06caa23bc26fec92d58bd100f2e1592eac6'
2323
end
2424

2525
describe "authenticated url" do
@@ -58,14 +58,18 @@
5858
expect(url).to eq("http://test123-res.cloudinary.com/image/authenticated/v1486020273/sample.jpg?__cld_token__=st=11111111~exp=11111411~hmac=8db0d753ee7bbb9e2eaf8698ca3797436ba4c20e31f44527e43b6a6e995cfdb3")
5959

6060
end
61+
it "should throw if expiration and duration are not provided" do
62+
token = { :key => KEY, :expiration => 0, :duration => 0 }
63+
expect{Cloudinary::Utils.generate_auth_token(token)}.to raise_exception
64+
end
6165
end
6266
describe "authentication token" do
6367
it "should generate token string" do
6468
user = "foobar" # we can't rely on the default "now" value in tests
6569
tokenOptions = { :key => KEY, :duration => 300, :acl => "/*/t_#{user}" }
6670
tokenOptions[:start_time] = 222222222 # we can't rely on the default "now" value in tests
6771
cookieToken = Cloudinary::Utils.generate_auth_token tokenOptions
68-
expect(cookieToken).to eq("__cld_token__=st=222222222~exp=222222522~acl=/*/t_foobar~hmac=eb5e2266c8ec9573f696025f075b92998080347e1c12ac39a26c94d7d712704a")
72+
expect(cookieToken).to eq("__cld_token__=st=222222222~exp=222222522~acl=%2f%2a%2ft_foobar~hmac=1284376353c1c43d6f6a98f2813c5596f4ff6f34d837cd853fd8c3c9e7f8428c")
6973

7074
end
7175
end

0 commit comments

Comments
 (0)