Skip to content

Commit 15990be

Browse files
author
Dave Sims
committed
Test for invalid URL strings as well as bad schemes in ReferralChaser
1 parent 02cef0b commit 15990be

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

lib/github/ldap/referral_chaser.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ def search(options)
5353
unless referral_entries.empty?
5454
entry = referral_entries.first
5555
referral_string = entry[:search_referrals].first
56-
referral = Referral.new(referral_string, admin_user, admin_password, port)
57-
search_results = referral.search(options)
56+
if GitHub::Ldap::URL.valid?(referral_string)
57+
referral = Referral.new(referral_string, admin_user, admin_password, port)
58+
search_results = referral.search(options)
59+
end
5860
end
5961

60-
search_results
62+
Array(search_results)
6163
end
6264

6365
private

lib/github/ldap/url.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,20 @@ class URL
4949
# ldap://dc4.ghe.local:456/CN=Maggie,DC=dc4,DC=ghe,DC=local?cn,mail?base?(cn=Charlie)
5050
#
5151
def initialize(url_string)
52-
@uri = URI(url_string)
53-
unless ["ldap", "ldaps"].include?(@uri.scheme)
54-
raise InvalidSchemeException.new("Invalid scheme: #{@uri.scheme}")
52+
if !self.class.valid?(url_string)
53+
raise InvalidLdapURLException.new("Invalid LDAP URL: #{url_string}")
5554
end
55+
@uri = URI(url_string)
5656
@dn = URI.unescape(@uri.path.sub(/^\//, ""))
5757
if @uri.query
5858
@attributes, @scope, @filter = @uri.query.split("?")
5959
end
6060
end
6161

62+
def self.valid?(url_string)
63+
url_string =~ URI::regexp && ["ldap", "ldaps"].include?(URI(url_string).scheme)
64+
end
65+
6266
# Maps the returned scope value from the URL to one of Net::LDAP::Scopes
6367
#
6468
# The URL scope value can be one of:
@@ -76,7 +80,7 @@ def net_ldap_scope
7680
Net::LDAP::SearchScopes[SCOPES[scope]]
7781
end
7882

79-
class InvalidSchemeException < Exception; end
83+
class InvalidLdapURLException < Exception; end
8084
end
8185
end
8286
end

test/referral_chaser_test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ def setup
88
end
99

1010
def test_creates_referral_with_connection_credentials
11-
@ldap.expects(:search).yields({ search_referrals: ["referral string"]}).returns([])
11+
@ldap.expects(:search).yields({ search_referrals: ["ldap://dc1.ghe.local/"]}).returns([])
1212

1313
referral = mock("GitHub::Ldap::ReferralChaser::Referral")
1414
referral.stubs(:search).returns([])
1515

1616
GitHub::Ldap::ReferralChaser::Referral.expects(:new)
17-
.with("referral string", "uid=admin,dc=github,dc=com", "passworD1", options[:port])
17+
.with("ldap://dc1.ghe.local/", "uid=admin,dc=github,dc=com", "passworD1", options[:port])
1818
.returns(referral)
1919

2020
@chaser.search({})
@@ -81,6 +81,13 @@ def test_returns_referral_search_results
8181
assert_equal(["result", "result"], results)
8282
end
8383

84+
def test_handle_blank_url_string_in_referral
85+
@mock_connection.expects(:search).yields({ search_referrals: [""] })
86+
87+
results = @chaser.search({})
88+
assert_equal([], results)
89+
end
90+
8491
def test_returns_referral_search_results
8592
@ldap.expects(:search).yields({ foo: ["not a referral"] })
8693

test/url_test.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,17 @@ def test_simple_url
3535
end
3636

3737
def test_invalid_scheme
38-
ex = assert_raises(GitHub::Ldap::URL::InvalidSchemeException) do
38+
ex = assert_raises(GitHub::Ldap::URL::InvalidLdapURLException) do
3939
GitHub::Ldap::URL.new("http://dc4.ghe.local")
4040
end
41-
assert_equal("Invalid scheme: http", ex.message)
41+
assert_equal("Invalid LDAP URL: http://dc4.ghe.local", ex.message)
42+
end
43+
44+
def test_invalid_url
45+
ex = assert_raises(GitHub::Ldap::URL::InvalidLdapURLException) do
46+
GitHub::Ldap::URL.new("not a url")
47+
end
48+
assert_equal("Invalid LDAP URL: not a url", ex.message)
4249
end
4350

4451
def test_parse_dn

0 commit comments

Comments
 (0)