Repository navigation
Accept any valid LDAP DN in simp config's DN items - #239
Conversation
The base DN item only accepted values starting with lowercase dc=, so
it rejected DC=example,DC=com (the form simplib::ldap::domain_to_dn()
produces by default) and valid base DNs such as o=Example. The bind,
sync and root DN items only accepted a lowercase cn=. Because the
regexes used ^, they also passed a bare dc= or a multi-line value with
a matching second line.
Add Utils.validate_ldap_dn, which checks the whole string as
comma-separated attribute=value RDNs with case-insensitive attribute
types, and use it for the base DN. The bind, sync and root DNs keep a
leading-cn= check, now case-insensitive and anchored to the whole
string, since their recommended values end in a %{hiera(...)}
interpolation rather than an RDN.
Fixes simp#194
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
||
| def validate(x) | ||
| (x.to_s =~ %r{^dc=}) ? true : false | ||
| Simp::Cli::Config::Utils.validate_ldap_dn(x) |
There was a problem hiding this comment.
Base DN validation now accepts non-dc= DNs (e.g. o=Example) even when cli::is_simp_ldap_server is true, but simp_openldap's templates/etc/openldap/default.ldif.erb builds the suffix entry as dc: <%= @_suffix.split(',').first.split(/dc=/i).last %> with objectClass: domain. With o=Example that writes dn: o=Example / dc: o=Example, an invalid root entry, so the LDAP server bootstrap fails. The removed negative spec (cn=hostAuth,... rejected) was the only guard on this, and nothing replaces it. Consider keeping a leading-dc= requirement (case-insensitive) when this host is the SIMP LDAP server, or validating against what simp_openldap can actually bootstrap.
There was a problem hiding this comment.
Good catch, thanks. In 715377e, when cli::is_simp_ldap_server is true, the base DN must also start with dc= (case-insensitive), and the error message says why. A host that isn't the SIMP LDAP server still accepts any valid DN, such as o=Example. The spec now covers both cases, including the cn=hostAuth,... rejection that had been dropped.
|
|
||
| def validate(x) | ||
| (x.to_s =~ %r{^cn=}) ? true : false | ||
| x.to_s.match?(%r{\Acn=[^,\n]+(?:,.+)?\z}i) |
There was a problem hiding this comment.
%r{\Acn=[^,\n]+(?:,.+)?\z}i checks only the first RDN. Everything after the first comma is unchecked, so cn=a,garbage, cn=a,,, cn=a, and cn= ,dc=x all validate (checked in Ruby 3.4). That contradicts the changelog line "Malformed values such as an empty dc= ... are rejected", and the same regex is in sync_dn.rb:18 and root_dn.rb:32. A typo in the bind/sync/root DN tail gets past simp config and fails later at sssd/openldap bind time.
There was a problem hiding this comment.
Fixed in 715377e. The bind, sync and root items now use Utils.validate_ldap_cn_dn (see your comment below), which validates the whole DN. cn=a,garbage, cn=a,,, cn=a, and cn= ,dc=x are all rejected now and covered by specs.
| return false unless dn.is_a?(String) | ||
|
|
||
| attr = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)} | ||
| value = %r{(?:[^,+\\\n]|\\.)+} |
There was a problem hiding this comment.
value = %r{(?:[^,+\\\n]|\\.)+} allows the characters RFC 4514 requires to be escaped (;, ", <, >, a leading # or space) and an unescaped =. So dc=example;dc=com, dc=example dc=com (missing comma) and dc=#zz,dc=com all return true. A missing or wrong separator, which is the most common base DN typo, is treated as one RDN with an odd value, and the error only shows up later when sssd/openldap search under the wrong base.
There was a problem hiding this comment.
Agreed. In 715377e, values follow RFC 4514: ", +, ,, ;, <, > and \ must be escaped (with a backslash or \XX hex), as must a leading # or space and a trailing space. I also reject an unescaped =. RFC 4514 doesn't require escaping it, but in a base DN it almost always means a missing comma. dc=example;dc=com, dc=example dc=com and dc=#zz,dc=com now fail. Escaped forms such as cn=\#1 and cn=\3Cx\3E pass.
| return false unless dn.is_a?(String) | ||
|
|
||
| attr = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)} | ||
| value = %r{(?:[^,+\\\n]|\\.)+} |
There was a problem hiding this comment.
Whitespace handling is inconsistent. The value class excludes \n but not \r, tabs or trailing spaces, so "dc=example\r" (an answers file with CRLF line endings) and "dc=example,dc=com " pass and get written to hiera with the stray character. Meanwhile "dc=example\n", which the old ^dc= check accepted (for example from a YAML | block scalar), is now a hard ValidationError. Consider stripping surrounding whitespace before matching and rejecting all control characters.
There was a problem hiding this comment.
In 715377e, control characters (CR, LF and tab included) are rejected anywhere, and so is unescaped leading or trailing whitespace. "dc=example\r" and "dc=example,dc=com " now fail. I chose to reject rather than strip: an item's validate can't change @value, so stripping only for the check would still write the stray characters to Hiera. Interactive answers aren't affected, because HighLine strips whitespace by default. That leaves "dc=example\n" from a YAML | block as a clear validation error instead of a newline in base_dn, which I think is the better trade.
|
|
||
| def validate(x) | ||
| (x.to_s =~ %r{^cn=}) ? true : false | ||
| x.to_s.match?(%r{\Acn=[^,\n]+(?:,.+)?\z}i) |
There was a problem hiding this comment.
This hand-written cn regex is copied into bind_dn, sync_dn and root_dn, separate from the new Utils.validate_ldap_dn. A fix at a lower level would avoid both the copies and the unchecked tail: a single Utils.validate_ldap_dn_with_cn(x) that replaces each %{...} hiera interpolation with a placeholder RDN (or validates the interpolation with the existing Utils.validate_hiera_lookup), runs validate_ldap_dn on the result, and checks for a leading cn= RDN. That validates the whole DN, keeps the recommended defaults valid, and puts the rule in one place.
There was a problem hiding this comment.
Done in 715377e. Utils.validate_ldap_cn_dn replaces each %{...} interpolation with a placeholder RDN (dc=interpolated), then requires a leading cn= and runs validate_ldap_dn on the result. The three copies are gone. An interpolation inside a value (cn=%{...}) doesn't validate, because the placeholder adds an unescaped =. None of SIMP's defaults do that.
| value = %r{(?:[^,+\\\n]|\\.)+} | ||
| ava = %r{#{attr}=#{value}} | ||
| rdn = %r{#{ava}(?:\+#{ava})*} | ||
| dn.match?(%r{\A#{rdn}(?: *, *#{rdn})*\z}) |
There was a problem hiding this comment.
Five regexes are interpolated and compiled again on every call (attr, value, ava, rdn and the final anchored regex). simp config calls this validator again on each re-prompt and on answers-file loads. Hoisting them to frozen class constants (e.g. LDAP_DN_REGEX = %r{...}o.freeze) costs nothing and makes the grammar easier to read and reuse from the cn-DN validators.
There was a problem hiding this comment.
Done in 715377e. The grammar is a set of frozen constants (LDAP_DN_ATTR, LDAP_DN_ESCAPE, LDAP_DN_PLAIN, LDAP_DN_VALUE, LDAP_DN_RDN, LDAP_DN_REGEX), with the rules documented above them. validate_ldap_cn_dn reuses them.
- Values follow RFC 4514: ", +, comma, ;, <, >, \ and an unescaped =
must be escaped, as must a leading # or space and a trailing space,
and control characters (CR, LF, tab) are rejected. A ; or missing
comma between RDNs no longer passes as one odd value.
- The grammar is built once as frozen constants.
- Utils.validate_ldap_cn_dn replaces the copied cn= regexes in the bind,
sync and root DN items. It substitutes a placeholder RDN for each
%{...} Hiera interpolation, then validates the whole DN and requires a
leading cn= RDN, so the tail after the first comma is checked too.
- When this host is the SIMP LDAP server, the base DN must still start
with dc=, because simp_openldap names the suffix entry's domain object
after its first RDN.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes #194
simp configvalidated LDAP DNs with case-sensitive prefix regexes:simp_options::ldap::base_dnhad to start with lowercasedc=. That rejectedDC=example,DC=com, which is the formsimplib::ldap::domain_to_dn()produces by default, and valid base DNs such aso=Example. Passing either in an answers file or as akey=valueargument madesimp configfail with aValidationError.cn=.^, so a baredc=or a multi-line value with a matching second line passed.Changes
Simp::Cli::Config::Utils.validate_ldap_dn: checks the whole string against an RFC 4514 grammar, built once as frozen constants:",+,,,;,<,>,\and=must be escaped (\or\XXhex), as must a leading#or space and a trailing space;+) and spaces around the commas between RDNs are allowed.=isn't special in RFC 4514, but in a base DN an unescaped one almost always means a missing comma.Utils.validate_ldap_cn_dn: for the bind, sync and root DNs. It replaces each%{...}Hiera interpolation with a placeholder RDN, requires a leadingcn=(any case), and validates the whole DN. Their recommended values, such ascn=hostAuth,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}, still validate.Base DN: any valid DN, except that the SIMP LDAP server's base DN (
cli::is_simp_ldap_servertrue) must still start withdc=. simp_openldap names the suffix entry'sdomainobject after its first RDN.8.0.0 changelog: one new bullet.
Apart from the malformed values that passed before by accident, this only widens what the validators accept: an empty
dc=, a trailing comma, a;or missing comma between RDNs, a stray\ror trailing space, or a multi-line string. That includes"dc=example\n", which the old^dc=check accepted. A newline inbase_dnwas never usable, so it's now a validation error. Interactive answers are unaffected because HighLine strips whitespace.Tests
DC=/CN=,o=Example, escapes, multi-valued RDNs and OIDs, every malformed case from review, the SIMP LDAP serverdc=rule, and each item's hiera-interpolated recommended value.rake specwith the OpenVox Ruby: 1557 examples, 0 failures. rubocop is clean.🤖 Generated with Claude Code