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

Add domain validation during Client initialization #42

Merged
merged 5 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions imgix.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require 'imgix/version'
Gem::Specification.new do |spec|
spec.name = 'imgix'
spec.version = Imgix::VERSION
spec.authors = ['Kelly Sutton', 'Sam Soffes', 'Ryan LeFevre', 'Antony Denyer', 'Paul Straw']
spec.email = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
spec.authors = ['Kelly Sutton', 'Sam Soffes', 'Ryan LeFevre', 'Antony Denyer', 'Paul Straw', 'Sherwin Heydarbeygi']
spec.email = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
spec.description = 'Easily create and sign imgix URLs.'
spec.summary = 'Official Ruby Gem for easily creating and signing imgix URLs.'
spec.homepage = 'https://github.com/imgix/imgix-rb'
Expand Down
1 change: 1 addition & 0 deletions lib/imgix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

module Imgix
STRATEGIES = [:crc, :cycle]
DOMAIN_REGEX = /^(?:[a-z\d\-_]{1,62}\.){0,125}(?:[a-z\d](?:\-(?=\-*[a-z\d])|[a-z]|\d){0,62}\.)[a-z\d]{1,63}$/i
end
7 changes: 6 additions & 1 deletion lib/imgix/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def prefix(path)
def get_host(path)
host = host_for_crc(path) if @shard_strategy == :crc
host = host_for_cycle if @shard_strategy == :cycle
host.gsub("http://","").gsub("https://","").chomp("/")
host
end

def host_for_crc(path)
Expand All @@ -75,6 +75,11 @@ def validate_hosts!
unless @hosts.length > 0
raise ArgumentError, "The :host or :hosts option must be specified"
end
@hosts.each do |host|
if host.match(DOMAIN_REGEX) == nil
raise ArgumentError, "Domains must be passed in as fully-qualified domain names and should not include a protocol or any path element, i.e. \"example.imgix.net\"."
end
end
end

end
Expand Down
30 changes: 12 additions & 18 deletions test/units/domains_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ def test_cycling_choosing_domain_in_order
assert_equal 'https://demos-1.imgix.net/bridge.png?s=0233fd6de51f20f11cff6b452b7a9a05', path.to_url
end

def test_strips_out_protocol
client = Imgix::Client.new(host: "http://demos-1.imgix.net",
secure_url_token: '10adc394',
include_library_param: false)

path = client.path('/bridge.png')
assert_equal 'https://demos-1.imgix.net/bridge.png?s=0233fd6de51f20f11cff6b452b7a9a05', path.to_url
end

def test_strips_out_trailing_slash
client = Imgix::Client.new(host: "http://demos-1.imgix.net/",
secure_url_token: '10adc394',
include_library_param: false)

path = client.path('/bridge.png')
assert_equal 'https://demos-1.imgix.net/bridge.png?s=0233fd6de51f20f11cff6b452b7a9a05', path.to_url
end

def test_with_full_paths
client = Imgix::Client.new(hosts: [
"demos-1.imgix.net",
Expand All @@ -73,4 +55,16 @@ def test_with_full_paths
path = 'https://google.com/cats.gif'
assert_equal "https://demos-1.imgix.net/#{CGI.escape(path)}?s=e686099fbba86fc2b8141d3c1ff60605", client.path(path).to_url
end

def test_invalid_domain_append_slash
assert_raises(ArgumentError) {Imgix::Client.new(hosts: ["assets.imgix.net/"])}
end

def test_invalid_domain_prepend_scheme
assert_raises(ArgumentError) {Imgix::Client.new(hosts: ["https://assets.imgix.net"])}
end

def test_invalid_domain_append_dash
assert_raises(ArgumentError) {Imgix::Client.new(hosts: ["assets.imgix.net-"])}
end
end