Skip to content

Commit

Permalink
feat: add parameter to disable path encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Paula Pasqualini committed Oct 6, 2022
1 parent dae48e4 commit 5be58cd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ client.path('/images/demo.png').to_url(w: 200)
#=> https://your-subdomain.imgix.net/images/demo.png?w=200&s=2eadddacaa9bba4b88900d245f03f51e
```

To disable path encoding, pass `disable_path_encoding: true` as an argument to the `Imgix#Path#to_url` function.

```rb
client = Imgix::Client.new(domain: 'your-subdomain.imgix.net', secure_url_token: 'your-token')

client.path('/images/demo.png').to_url
#=> https://your-subdomain.imgix.net/%5Bimages%5D/demo.png?s=270832685733a36ba02bd8ab9fd72df5

client.path('/images/demo.png').to_url(disable_path_encoding: true)
#=> https://your-subdomain.imgix.net/[images]/demo.png?s=ed6eb07e9eff3f6c8bbcc83fc4f63198

```

## Srcset Generation

The imgix gem allows for generation of custom `srcset` attributes, which can be invoked through `Imgix::Path#to_srcset`. By default, the `srcset` generated will allow for responsive size switching by building a list of image-width mappings.
Expand Down
28 changes: 16 additions & 12 deletions lib/imgix/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def initialize(prefix, secure_url_token, path = "/")
@options = {}
end

def to_url(opts = {})
sanitized_path = sanitize_path(@path)
def to_url(options: {}, **params)
sanitized_path = sanitize_path(@path, options: options)
prev_options = @options.dup
@options.merge!(opts)
@options.merge!(params)

current_path_and_params = path_and_params(sanitized_path)
url = @prefix + current_path_and_params
Expand Down Expand Up @@ -132,16 +132,20 @@ def method_missing(method, *args, &block)
# This includes " +?:#" characters. If a path is being used as a proxy, utf8
# encode everything. If it is not being used as proxy, leave certain chars, like
# "/", alone. Method assumes path is not already encoded.
def sanitize_path(path)
def sanitize_path(path, options: {})
# remove the leading "/", we'll add it back after encoding
path = path.slice(1, path.length) if Regexp.new('^/') =~ path
# if path is being used as a proxy, encode the entire thing
if /^https?/ =~ path
return encode_URI_Component(path)
else
# otherwise, encode only specific characters
return encode_URI(path)

if !options[:disable_path_encoding]
# if path is being used as a proxy, encode the entire thing
if /^https?/ =~ path
return encode_URI_Component(path)
else
# otherwise, encode only specific characters
return encode_URI(path)
end
end
return "/" + path
end

# URL encode the entire path
Expand Down Expand Up @@ -207,7 +211,7 @@ def build_srcset_pairs(options:, params:)

srcset_widths.each do |width|
params[:w] = width
srcset += "#{to_url(params)} #{width}w,\n"
srcset += "#{to_url(**params)} #{width}w,\n"
end

srcset[0..-3]
Expand All @@ -227,7 +231,7 @@ def build_dpr_srcset(options:, params:)

params[:q] = quality || DPR_QUALITY[ratio] unless disable_variable_quality

srcset += "#{to_url(params)} #{ratio}x,\n"
srcset += "#{to_url(**params)} #{ratio}x,\n"
end

srcset[0..-3]
Expand Down
12 changes: 9 additions & 3 deletions test/units/path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def test_creating_a_path
assert_equal "https://demo.imgix.net/images/demo.png?s=2c7c157eaf23b06a0deb2f60b81938c4", path.to_url
end

def test_disable_path_encoding
path = client.path("[images]/demo.png")
assert_equal "https://demo.imgix.net/%5Bimages%5D/demo.png?s=270832685733a36ba02bd8ab9fd72df5", path.to_url
assert_equal "https://demo.imgix.net/[images]/demo.png?s=ed6eb07e9eff3f6c8bbcc83fc4f63198", path.to_url(options: {disable_path_encoding: true})
end

def test_signing_path_with_param
url = "https://demo.imgix.net/images/demo.png?w=200&s=da421114ca238d1f4a927b889f67c34e"
path = client.path("/images/demo.png").w(200)
Expand Down Expand Up @@ -90,13 +96,13 @@ def test_path_with_multi_value_param_safely_encoded
end

def test_param_keys_are_escaped
ix_url = unsigned_client.path("demo.png").to_url({ "hello world" => "interesting" })
ix_url = unsigned_client.path("demo.png").to_url("hello world": "interesting")

assert_equal "https://demo.imgix.net/demo.png?hello%20world=interesting", ix_url
end

def test_param_values_are_escaped
ix_url = unsigned_client.path("demo.png").to_url({ hello_world: "/foo\"> <script>alert(\"hacked\")</script><" })
ix_url = unsigned_client.path("demo.png").to_url(hello_world: "/foo\"> <script>alert(\"hacked\")</script><")

assert_equal "https://demo.imgix.net/demo.png?hello_world=%2Ffoo%22%3E%20%3Cscript%3Ealert%28%22hacked%22%29%3C%2Fscript%3E%3C", ix_url
end
Expand All @@ -122,7 +128,7 @@ def test_unicode_path_variants_are_utf8_encoded
end

def test_base64_param_variants_are_base64_encoded
ix_url = unsigned_client.path("~text").to_url({txt64: "I cannøt belîév∑ it wors! 😱"})
ix_url = unsigned_client.path("~text").to_url(txt64: "I cannøt belîév∑ it wors! 😱")

assert_equal "https://demo.imgix.net/~text?txt64=SSBjYW5uw7h0IGJlbMOuw6l24oiRIGl0IHdvcu-jv3MhIPCfmLE", ix_url
end
Expand Down

0 comments on commit 5be58cd

Please sign in to comment.