Commit 14ed20d6 authored by Michael Kozono's avatar Michael Kozono

Resolve Rubocop offenses

Disabling some for now since this is based on `Net::LDAP::DN`.
parent 45ab20dc
...@@ -37,16 +37,16 @@ module Gitlab ...@@ -37,16 +37,16 @@ module Gitlab
buffer = StringIO.new buffer = StringIO.new
args.each_index do |index| args.each_index do |index|
buffer << "=" if index % 2 == 1 buffer << "=" if index.odd?
buffer << "," if index % 2 == 0 && index != 0 buffer << "," if index.even? && index != 0
arg = args[index].downcase arg = args[index].downcase
if index < args.length - 1 || index % 2 == 1 buffer << if index < args.length - 1 || index.odd?
buffer << self.class.escape(arg) self.class.escape(arg)
else else
buffer << arg arg
end end
end end
@dn = buffer.string @dn = buffer.string
...@@ -55,6 +55,9 @@ module Gitlab ...@@ -55,6 +55,9 @@ module Gitlab
## ##
# Parse a DN into key value pairs using ASN from # Parse a DN into key value pairs using ASN from
# http://tools.ietf.org/html/rfc2253 section 3. # http://tools.ietf.org/html/rfc2253 section 3.
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def each_pair def each_pair
state = :key state = :key
key = StringIO.new key = StringIO.new
...@@ -98,7 +101,7 @@ module Gitlab ...@@ -98,7 +101,7 @@ module Gitlab
state = :key state = :key
yield key.string.strip, value.string.rstrip yield key.string.strip, value.string.rstrip
key = StringIO.new key = StringIO.new
value = StringIO.new; value = StringIO.new
else else
state = :value_normal state = :value_normal
value << char value << char
...@@ -110,7 +113,7 @@ module Gitlab ...@@ -110,7 +113,7 @@ module Gitlab
state = :key state = :key
yield key.string.strip, value.string.rstrip yield key.string.strip, value.string.rstrip
key = StringIO.new key = StringIO.new
value = StringIO.new; value = StringIO.new
when '+' then raise(UnsupportedDnFormatError, "Multivalued RDNs are not supported") when '+' then raise(UnsupportedDnFormatError, "Multivalued RDNs are not supported")
else value << char else value << char
end end
...@@ -119,8 +122,12 @@ module Gitlab ...@@ -119,8 +122,12 @@ module Gitlab
when '0'..'9', 'a'..'f' then when '0'..'9', 'a'..'f' then
state = :value_normal_escape_hex state = :value_normal_escape_hex
hex_buffer = char hex_buffer = char
when /\s/ then state = :value_normal_escape_whitespace; value << char when /\s/ then
else state = :value_normal; value << char state = :value_normal_escape_whitespace
value << char
else
state = :value_normal
value << char
end end
when :value_normal_escape_hex then when :value_normal_escape_hex then
case char case char
...@@ -136,7 +143,7 @@ module Gitlab ...@@ -136,7 +143,7 @@ module Gitlab
state = :key state = :key
yield key.string.strip, value.string # Don't strip trailing escaped space! yield key.string.strip, value.string # Don't strip trailing escaped space!
key = StringIO.new key = StringIO.new
value = StringIO.new; value = StringIO.new
when '+' then raise(UnsupportedDnFormatError, "Multivalued RDNs are not supported") when '+' then raise(UnsupportedDnFormatError, "Multivalued RDNs are not supported")
else value << char else value << char
end end
...@@ -152,7 +159,7 @@ module Gitlab ...@@ -152,7 +159,7 @@ module Gitlab
state = :value_quoted_escape_hex state = :value_quoted_escape_hex
hex_buffer = char hex_buffer = char
else else
state = :value_quoted; state = :value_quoted
value << char value << char
end end
when :value_quoted_escape_hex then when :value_quoted_escape_hex then
...@@ -172,7 +179,7 @@ module Gitlab ...@@ -172,7 +179,7 @@ module Gitlab
state = :key state = :key
yield key.string.strip, value.string.rstrip yield key.string.strip, value.string.rstrip
key = StringIO.new key = StringIO.new
value = StringIO.new; value = StringIO.new
else raise(MalformedDnError, "Expected the first character of a hex pair, but got \"#{char}\"") else raise(MalformedDnError, "Expected the first character of a hex pair, but got \"#{char}\"")
end end
when :value_hexstring_hex then when :value_hexstring_hex then
...@@ -189,7 +196,7 @@ module Gitlab ...@@ -189,7 +196,7 @@ module Gitlab
state = :key state = :key
yield key.string.strip, value.string.rstrip yield key.string.strip, value.string.rstrip
key = StringIO.new key = StringIO.new
value = StringIO.new; value = StringIO.new
else raise(MalformedDnError, "Expected the end of an attribute value, but got \"#{char}\"") else raise(MalformedDnError, "Expected the end of an attribute value, but got \"#{char}\"")
end end
else raise "Fell out of state machine" else raise "Fell out of state machine"
...@@ -228,13 +235,13 @@ module Gitlab ...@@ -228,13 +235,13 @@ module Gitlab
# using a single backslash ('\') as escape. The space character is left # using a single backslash ('\') as escape. The space character is left
# out here because in a "normalized" string, spaces should only be escaped # out here because in a "normalized" string, spaces should only be escaped
# if necessary (i.e. leading or trailing space). # if necessary (i.e. leading or trailing space).
NORMAL_ESCAPES = [',', '+', '"', '\\', '<', '>', ';', '='] NORMAL_ESCAPES = [',', '+', '"', '\\', '<', '>', ';', '='].freeze
# The following must be represented as escaped hex # The following must be represented as escaped hex
HEX_ESCAPES = { HEX_ESCAPES = {
"\n" => '\0a', "\n" => '\0a',
"\r" => '\0d' "\r" => '\0d'
} }.freeze
# Compiled character class regexp using the keys from the above hash, and # Compiled character class regexp using the keys from the above hash, and
# checking for a space or # at the start, or space at the end, of the # checking for a space or # at the start, or space at the end, of the
...@@ -257,6 +264,7 @@ module Gitlab ...@@ -257,6 +264,7 @@ module Gitlab
## ##
# Proxy all other requests to the string object, because a DN is mainly # Proxy all other requests to the string object, because a DN is mainly
# used within the library as a string # used within the library as a string
# rubocop:disable GitlabSecurity/PublicSend
def method_missing(method, *args, &block) def method_missing(method, *args, &block)
@dn.send(method, *args, &block) @dn.send(method, *args, &block)
end end
......
...@@ -48,7 +48,7 @@ describe Gitlab::LDAP::DN do ...@@ -48,7 +48,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'uid=john smith+telephonenumber=+1 555-555-5555,ou=people,dc=example,dc=com' } let(:given) { 'uid=john smith+telephonenumber=+1 555-555-5555,ou=people,dc=example,dc=com' }
it 'raises UnsupportedDnFormatError' do it 'raises UnsupportedDnFormatError' do
expect{ subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError) expect { subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError)
end end
end end
...@@ -57,7 +57,7 @@ describe Gitlab::LDAP::DN do ...@@ -57,7 +57,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'uid = John Smith + telephoneNumber = + 1 555-555-5555 , ou = People,dc=example,dc=com' } let(:given) { 'uid = John Smith + telephoneNumber = + 1 555-555-5555 , ou = People,dc=example,dc=com' }
it 'raises UnsupportedDnFormatError' do it 'raises UnsupportedDnFormatError' do
expect{ subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError) expect { subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError)
end end
end end
...@@ -65,7 +65,7 @@ describe Gitlab::LDAP::DN do ...@@ -65,7 +65,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'uid = John Smith + telephoneNumber = +1 555-555-5555 , ou = People,dc=example,dc=com' } let(:given) { 'uid = John Smith + telephoneNumber = +1 555-555-5555 , ou = People,dc=example,dc=com' }
it 'raises UnsupportedDnFormatError' do it 'raises UnsupportedDnFormatError' do
expect{ subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError) expect { subject }.to raise_error(Gitlab::LDAP::UnsupportedDnFormatError)
end end
end end
end end
...@@ -77,7 +77,7 @@ describe Gitlab::LDAP::DN do ...@@ -77,7 +77,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'uid=John Smith,' } let(:given) { 'uid=John Smith,' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'DN string ended unexpectedly') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'DN string ended unexpectedly')
end end
end end
...@@ -85,7 +85,7 @@ describe Gitlab::LDAP::DN do ...@@ -85,7 +85,7 @@ describe Gitlab::LDAP::DN do
let(:given) { '0.9.2342.19200300.100.1.25=#aa aa' } let(:given) { '0.9.2342.19200300.100.1.25=#aa aa' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, "Expected the end of an attribute value, but got \"a\"") expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, "Expected the end of an attribute value, but got \"a\"")
end end
end end
...@@ -93,7 +93,7 @@ describe Gitlab::LDAP::DN do ...@@ -93,7 +93,7 @@ describe Gitlab::LDAP::DN do
let(:given) { '0.9.2342.19200300.100.1.25=#aaXaaa' } let(:given) { '0.9.2342.19200300.100.1.25=#aaXaaa' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, "Expected the first character of a hex pair, but got \"x\"") expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, "Expected the first character of a hex pair, but got \"x\"")
end end
end end
...@@ -101,7 +101,7 @@ describe Gitlab::LDAP::DN do ...@@ -101,7 +101,7 @@ describe Gitlab::LDAP::DN do
let(:given) { '0.9.2342.19200300.100.1.25=#aaaYaa' } let(:given) { '0.9.2342.19200300.100.1.25=#aaaYaa' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, "Expected the second character of a hex pair, but got \"y\"") expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, "Expected the second character of a hex pair, but got \"y\"")
end end
end end
...@@ -109,7 +109,7 @@ describe Gitlab::LDAP::DN do ...@@ -109,7 +109,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'uid="Sebasti\\cX\\a1n"' } let(:given) { 'uid="Sebasti\\cX\\a1n"' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, "Expected the second character of a hex pair inside a double quoted value, but got \"x\"") expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, "Expected the second character of a hex pair inside a double quoted value, but got \"x\"")
end end
end end
...@@ -117,7 +117,7 @@ describe Gitlab::LDAP::DN do ...@@ -117,7 +117,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'John' } let(:given) { 'John' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'DN string ended unexpectedly') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'DN string ended unexpectedly')
end end
end end
...@@ -125,7 +125,7 @@ describe Gitlab::LDAP::DN do ...@@ -125,7 +125,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'cn="James' } let(:given) { 'cn="James' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'DN string ended unexpectedly') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'DN string ended unexpectedly')
end end
end end
...@@ -133,7 +133,7 @@ describe Gitlab::LDAP::DN do ...@@ -133,7 +133,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'cn=J\ames' } let(:given) { 'cn=J\ames' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Invalid escaped hex code "\am"') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Invalid escaped hex code "\am"')
end end
end end
...@@ -141,7 +141,7 @@ describe Gitlab::LDAP::DN do ...@@ -141,7 +141,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'cn=\\' } let(:given) { 'cn=\\' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'DN string ended unexpectedly') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'DN string ended unexpectedly')
end end
end end
...@@ -149,7 +149,7 @@ describe Gitlab::LDAP::DN do ...@@ -149,7 +149,7 @@ describe Gitlab::LDAP::DN do
let(:given) { '1.2.d=Value' } let(:given) { '1.2.d=Value' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Unrecognized RDN OID attribute type name character "d"') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Unrecognized RDN OID attribute type name character "d"')
end end
end end
...@@ -157,7 +157,7 @@ describe Gitlab::LDAP::DN do ...@@ -157,7 +157,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'd1.2=Value' } let(:given) { 'd1.2=Value' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Unrecognized RDN attribute type name character "."') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Unrecognized RDN attribute type name character "."')
end end
end end
...@@ -165,7 +165,7 @@ describe Gitlab::LDAP::DN do ...@@ -165,7 +165,7 @@ describe Gitlab::LDAP::DN do
let(:given) { ' -uid=John Smith' } let(:given) { ' -uid=John Smith' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Unrecognized first character of an RDN attribute type name "-"') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Unrecognized first character of an RDN attribute type name "-"')
end end
end end
...@@ -173,7 +173,7 @@ describe Gitlab::LDAP::DN do ...@@ -173,7 +173,7 @@ describe Gitlab::LDAP::DN do
let(:given) { 'uid\\=john' } let(:given) { 'uid\\=john' }
it 'raises MalformedDnError' do it 'raises MalformedDnError' do
expect{ subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Unrecognized RDN attribute type name character "\\"') expect { subject }.to raise_error(Gitlab::LDAP::MalformedDnError, 'Unrecognized RDN attribute type name character "\\"')
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment