Skip to content

Accept any valid LDAP DN in simp config's DN items - #239

Merged
silug merged 2 commits into
simp:masterfrom
silug:ldap-dn-validation
Nov 18, 2026
Merged

silug merged 2 commits into
simp:masterfrom
silug:ldap-dn-validation

Conversation

@silug

@silug silug commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #194

simp config validated LDAP DNs with case-sensitive prefix regexes:

  • simp_options::ldap::base_dn had to start with lowercase dc=. That rejected DC=example,DC=com, which is the form simplib::ldap::domain_to_dn() produces by default, and valid base DNs such as o=Example. Passing either in an answers file or as a key=value argument made simp config fail with a ValidationError.
  • The bind, sync and root DNs had to start with lowercase cn=.
  • The regexes anchored with ^, so a bare dc= 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:

    • attribute types are case-insensitive names or numeric OIDs, and values are non-empty;
    • ", +, ,, ;, <, >, \ and = must be escaped (\ or \XX hex), as must a leading # or space and a trailing space;
    • control characters (CR, LF, tab) are rejected;
    • multi-valued RDNs (+) 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 leading cn= (any case), and validates the whole DN. Their recommended values, such as cn=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_server true) must still start with dc=. simp_openldap names the suffix entry's domain object 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 \r or trailing space, or a multi-line string. That includes "dc=example\n", which the old ^dc= check accepted. A newline in base_dn was never usable, so it's now a validation error. Interactive answers are unaffected because HighLine strips whitespace.

Tests

  • New specs cover uppercase DC=/CN=, o=Example, escapes, multi-valued RDNs and OIDs, every malformed case from review, the SIMP LDAP server dc= rule, and each item's hiera-interpolated recommended value.
  • rake spec with the OpenVox Ruby: 1557 examples, 0 failures. rubocop is clean.

🤖 Generated with Claude Code

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

%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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread lib/simp/cli/config/utils.rb Outdated
return false unless dn.is_a?(String)

attr = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)}
value = %r{(?:[^,+\\\n]|\\.)+}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@silug silug Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread lib/simp/cli/config/utils.rb Outdated
return false unless dn.is_a?(String)

attr = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)}
value = %r{(?:[^,+\\\n]|\\.)+}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@silug silug Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread lib/simp/cli/config/utils.rb Outdated
value = %r{(?:[^,+\\\n]|\\.)+}
ava = %r{#{attr}=#{value}}
rdn = %r{#{ava}(?:\+#{ava})*}
dn.match?(%r{\A#{rdn}(?: *, *#{rdn})*\z})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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>
@simp-automation simp-automation Bot moved this from New to In Review in Org Triage Nov 17, 2026
@silug
silug merged commit 1a249bd into simp:master Nov 18, 2026
6 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in Org Triage Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Relax LDAP DN validation

2 participants