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

Net::LDAP::DN - Retain trailing spaces in RDN values in DNs #412

Merged

Conversation

TomSellers
Copy link
Contributor

@TomSellers TomSellers commented Feb 16, 2023

Net::LDAP::DN.each_pair and Net::LDAP::DN.to_a are stripping trailing spaces in the values they return. The comment for each_pair refers to RFC 2253 Section 3 for the rules on parsing the DN into key value pairs. It allows for spaces in values. In Section 2.4, which is just prior, it states how to escape leading and trailing spaces in values.

Net::LDAP::DN retains leading and trailing spaces and escapes as expected when calling to_s however when calling each_pair or to_a it is removing the trailing spaces.

For example:
Original data: CN=Foo.bar.baz, OU=Foo \ ,OU=\ Bar, O=Baz
to_s output: CN=Foo.bar.baz, OU=Foo \ ,OU=\ Bar, O=Baz
to_a output:

CN: 'Foo.bar.baz'
OU: 'Foo'                <- This should be 'Foo    '
OU: '  Bar'
O: 'Baz'

This PR contains changes to change this behavior and tests to verify the results.

Before changes

irb
irb(main):001:0> require 'net/ldap'
=> true
irb(main):002:0> require 'net/ldap/dn'
=> true

# Check version
irb(main):003:0> Net::LDAP::VERSION
=> "0.17.1"

# create the object using the test string
irb(main):004:0> test = Net::LDAP::DN.new('CN=Foo.bar.baz, OU=Foo   \ ,OU=\  Bar, O=Baz')
=> #<Net::LDAP::DN:0x000000015bb4b138 @dn="CN=Foo.bar.baz, OU=Foo   \\ ,OU=\\  Bar, O=Baz">

# verify that the spaces exist and are escaped as expected in `to_s`
irb(main):005:0> test.to_s
=> "CN=Foo.bar.baz, OU=Foo   \\ ,OU=\\  Bar, O=Baz"

# verify that the values returned by `to_a` retain the spaces
# Failed - missing trailing spaces in `Foo    `
irb(main):007:0> test.to_a
=> ["CN", "Foo.bar.baz", "OU", "Foo", "OU", "  Bar", "O", "Baz"]

# verify that the values returned by each_pair retain the spaces
# Failed - missing trailing spaces in `Foo    `
irb(main):006:0> test.each_pair { |k, value| puts(k +": '" + value +"'") }
CN: 'Foo.bar.baz'
OU: 'Foo'
OU: '  Bar'
O: 'Baz'
=> nil

After changes

$ irb
irb(main):001:0> require 'net/ldap'
=> true
irb(main):002:0> require 'net/ldap/dn'
=> true

# create the object using the test string
irb(main):005:0> test = Net::LDAP::DN.new('CN=Foo.bar.baz, OU=Foo   \ ,OU=\  Bar, O=Baz')
=> #<Net::LDAP::DN:0x000000012fad3d58 @dn="CN=Foo.bar.baz, OU=Foo   \\ ,OU=\\  Bar, O=Baz">

# verify that the spaces exist and are escaped as expected in `to_s`
irb(main):006:0> test.to_s
=> "CN=Foo.bar.baz, OU=Foo   \\ ,OU=\\  Bar, O=Baz"

# verify that the values returned by `to_a` retain the spaces
irb(main):008:0> test.to_a
=> ["CN", "Foo.bar.baz", "OU", "Foo    ", "OU", "  Bar", "O", "Baz"]

# verify that the values returned by each_pair retain the spaces
irb(main):007:0> test.each_pair { |k, value| puts(k +": '" + value +"'") }
CN: 'Foo.bar.baz'
OU: 'Foo    '
OU: '  Bar'
O: 'Baz'
=> nil

@TomSellers
Copy link
Contributor Author

TomSellers commented Feb 24, 2023

Note, the jruby-9.2 test failure is due to that version of JRuby not being compatible with the version of bundler that is being used.

ERROR:  Error installing bundler:
	The last version of bundler (>= 0) to support your Ruby & RubyGems was 2.3.26. Try installing it with `gem install bundler -v 2.3.26`
	bundler requires Ruby version >= 2.6.0. The current ruby version is 2.5.8.0.
1
Error: Process completed with exit code 1.

JRuby 9.2.x is compatible with Ruby 2.5.x , while JRuby 9.3.x is compatible with Ruby 2.6.x.

@TomSellers
Copy link
Contributor Author

@HarlemSquirrel Would it be possible to have this fix included in the next release?

@HarlemSquirrel
Copy link
Member

@TomSellers Thanks for this. It looks great! Could you please update the branch from master so CI can run?

@HarlemSquirrel HarlemSquirrel self-assigned this Apr 19, 2023
Copy link
Member

@HarlemSquirrel HarlemSquirrel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me. Please update from the latest master branch so CI can run.

@TomSellers
Copy link
Contributor Author

TomSellers commented Apr 19, 2023

@HarlemSquirrel Triggered, the workflow is waiting on approval.

@HarlemSquirrel HarlemSquirrel merged commit 3d03e60 into ruby-ldap:master Apr 19, 2023
@TomSellers TomSellers deleted the retain_spaces_in_RDN_values branch April 19, 2023 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants