Skip to content

Net::LDAP::DN.escape() throws when attempting to escape attr value beginning with # #398

Closed
@bgraves-lo

Description

@bgraves-lo

Net::LDAP::DN.escape() is meant to adhere to https://datatracker.ietf.org/doc/html/rfc2253#section-2.4, which defines the convention for escaping attribute values.

Here's the related code:

# http://tools.ietf.org/html/rfc2253 section 2.4 lists these exceptions
# for dn values. All of the following must be escaped in any normal string
# using a single backslash ('\') as escape.
ESCAPES = {
',' => ',',
'+' => '+',
'"' => '"',
'\\' => '\\',
'<' => '<',
'>' => '>',
';' => ';',
}
# 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
# string.
ESCAPE_RE = Regexp.new("(^ |^#| $|[" +
ESCAPES.keys.map { |e| Regexp.escape(e) }.join +
"])")
##
# Escape a string for use in a DN value
def self.escape(string)
string.gsub(ESCAPE_RE) { |char| "\\" + ESCAPES[char] }
end

The code properly escapes the special characters included in the ESCAPES hash, handling this case from the RFC:

    o   one of the characters ",", "+", """, "\", "<", ">" or ";"

But the problem occurs with the special cases involving '#' and space:

    o   a space or "#" character occurring at the beginning of the
        string

    o   a space character occurring at the end of the string

Space and '#' aren't included in that hash, so if ESCAPE_RE matches '^#', for instance, the lookup of ESCAPES['#'] returns nil, which causes "\\" + ESCAPES[char] to throw a TypeError (no implicit conversion of nil into String).

A potential workaround:

  def self.escape(string)
    string.gsub(ESCAPE_RE) { |char| "\\" + (ESCAPES[char] || char) }
  end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions