From 58fc05e007270a639d197bdb2834ca55c7e19cea Mon Sep 17 00:00:00 2001 From: Steven Pritchard Date: Tue, 17 Nov 2026 20:01:32 +0000 Subject: [PATCH 1/2] Accept any valid LDAP DN in simp config's DN items 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 #194 Co-Authored-By: Claude Opus 5.5 --- build/rubygem-simp-cli.spec | 5 +++ .../items/data/simp_options_ldap_base_dn.rb | 5 +-- .../items/data/simp_options_ldap_bind_dn.rb | 4 +-- .../items/data/simp_options_ldap_root_dn.rb | 4 +-- .../items/data/simp_options_ldap_sync_dn.rb | 4 +-- lib/simp/cli/config/utils.rb | 15 +++++++++ .../data/simp_options_ldap_base_dn_spec.rb | 7 +++- .../data/simp_options_ldap_bind_dn_spec.rb | 10 ++++++ .../data/simp_options_ldap_root_dn_spec.rb | 10 ++++++ .../data/simp_options_ldap_sync_dn_spec.rb | 10 ++++++ spec/lib/simp/cli/config/utils_spec.rb | 32 +++++++++++++++++++ 11 files changed, 97 insertions(+), 9 deletions(-) diff --git a/build/rubygem-simp-cli.spec b/build/rubygem-simp-cli.spec index f39047fb..797cde7f 100644 --- a/build/rubygem-simp-cli.spec +++ b/build/rubygem-simp-cli.spec @@ -233,6 +233,11 @@ EOM options, e.g. `LDAP: {}`) instead of the deprecated `sssd::domains` Array, and omits the key when there are no domains. Requires simp/sssd 11.0.0 or later; the item class `SssdDomains` is now `SssdDomainsEntries` +- `simp config` accepts any valid LDAP DN for `simp_options::ldap::base_dn` + (e.g. `DC=example,DC=com`, the form `simplib::ldap::domain_to_dn()` + produces, or `o=Example`), and accepts `CN=` as well as `cn=` for the + bind, sync and root DNs. Malformed values such as an empty `dc=` or a + multi-line string are rejected * Fri May 20 2022 Trevor Vaughan - 7.0.1 - Always set the hostname to the one specified during `simp config` in the apply diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb index 0a1d5b42..ed9ec944 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../item' +require_relative '../../utils' require_relative 'cli_is_simp_ldap_server' require_relative 'cli_network_hostname' @@ -28,11 +29,11 @@ def get_recommended_value end def validate(x) - (x.to_s =~ %r{^dc=}) ? true : false + Simp::Cli::Config::Utils.validate_ldap_dn(x) end def not_valid_message - "Valid LDAP Base Distinguished Name must begin with 'dc='" + "Invalid LDAP Base Distinguished Name. Use a DN such as 'dc=example,dc=com'" end end end diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb index cc12979e..dd4a1e7e 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb @@ -15,11 +15,11 @@ def initialize(puppet_env_info = DEFAULT_PUPPET_ENV_INFO) end def validate(x) - (x.to_s =~ %r{^cn=}) ? true : false + x.to_s.match?(%r{\Acn=[^,\n]+(?:,.+)?\z}i) end def not_valid_message - "Valid LDAP Bind Distinguished Name must begin with 'cn='" + "Valid LDAP Bind Distinguished Name must begin with a 'cn=' RDN" end def get_recommended_value diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb index e1ea0ccc..f09674ad 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb @@ -25,11 +25,11 @@ def get_recommended_value end def not_valid_message - "Valid LDAP Root Distinguished Name must begin with 'cn='" + "Valid LDAP Root Distinguished Name must begin with a 'cn=' RDN" end def validate(x) - (x.to_s =~ %r{^cn=}) ? true : false + x.to_s.match?(%r{\Acn=[^,\n]+(?:,.+)?\z}i) end end end diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb index 70519164..efe76b94 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb @@ -15,11 +15,11 @@ def initialize(puppet_env_info = DEFAULT_PUPPET_ENV_INFO) end def validate(x) - (x.to_s =~ %r{^cn=}) ? true : false + x.to_s.match?(%r{\Acn=[^,\n]+(?:,.+)?\z}i) end def not_valid_message - "Valid LDAP Sync Distinguished Name must begin with 'cn='" + "Valid LDAP Sync Distinguished Name must begin with a 'cn=' RDN" end def get_recommended_value diff --git a/lib/simp/cli/config/utils.rb b/lib/simp/cli/config/utils.rb index cf80ec00..23246bcb 100644 --- a/lib/simp/cli/config/utils.rb +++ b/lib/simp/cli/config/utils.rb @@ -41,6 +41,21 @@ def validate_fqdn(fqdn) ((fqdn =~ regex) ? true : false) end + # Validate an LDAP Distinguished Name: one or more comma-separated + # RDNs, each `attribute=value` (multi-valued RDNs joined with `+`). + # Attribute types are case-insensitive, and may be names (`dc`, `DC`, + # `o`, `ou`, ...) or numeric OIDs. Values must be non-empty; a `,`, `+` + # or `\` inside a value must be escaped with a backslash. + def validate_ldap_dn(dn) + return false unless dn.is_a?(String) + + attr = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)} + value = %r{(?:[^,+\\\n]|\\.)+} + ava = %r{#{attr}=#{value}} + rdn = %r{#{ava}(?:\+#{ava})*} + dn.match?(%r{\A#{rdn}(?: *, *#{rdn})*\z}) + end + def validate_ip(ip) return false unless ip.respond_to?(:=~) diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb index 73a1cf4a..46adba3e 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb @@ -44,10 +44,15 @@ describe '#validate' do it 'validates ldap_base_dns' do expect(@ci.validate('dc=tasty,dc=bacon')).to be true + expect(@ci.validate('DC=tasty,DC=bacon')).to be true + expect(@ci.validate('o=Bacon')).to be true + expect(@ci.validate('ou=Hosts,dc=tasty,dc=bacon')).to be true end it "doesn't validate bad ldap_base_dns" do - expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be false + expect(@ci.validate('dc=')).to be false + expect(@ci.validate("x\ndc=tasty,dc=bacon")).to be false + expect(@ci.validate('tasty.bacon')).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false end diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb index 55b4cd03..cba54414 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb @@ -34,8 +34,18 @@ expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be true end + it 'validates an uppercase CN' do + expect(@ci.validate('CN=hostAuth,OU=Hosts,DC=tasty,DC=bacon')).to be true + end + + it 'validates the hiera-interpolated recommended value' do + expect(@ci.validate("cn=hostAuth,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}")).to be true + end + it "doesn't validate bad ldap_bind_dns" do expect(@ci.validate('dc=tasty,dc=bacon')).to be false + expect(@ci.validate('cn=')).to be false + expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false end diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb index e9e89c81..d4ce9a5f 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb @@ -34,8 +34,18 @@ expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be true end + it 'validates an uppercase CN' do + expect(@ci.validate('CN=LDAPAdmin,OU=Hosts,DC=tasty,DC=bacon')).to be true + end + + it 'validates the hiera-interpolated recommended value' do + expect(@ci.validate("cn=LDAPAdmin,ou=People,%{hiera('simp_options::ldap::base_dn')}")).to be true + end + it "doesn't validate bad ldap_root_dns" do expect(@ci.validate('dc=tasty,dc=bacon')).to be false + expect(@ci.validate('cn=')).to be false + expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false end diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb index bf8c7d33..99d726f8 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb @@ -34,7 +34,17 @@ expect(@ci.validate('cn=LDAPSync,ou=Hosts,dc=tasty,dc=bacon')).to be true end + it 'validates an uppercase CN' do + expect(@ci.validate('CN=LDAPSync,OU=Hosts,DC=tasty,DC=bacon')).to be true + end + + it 'validates the hiera-interpolated recommended value' do + expect(@ci.validate("cn=LDAPSync,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}")).to be true + end + it "doesn't validate bad ldap_sync_dns" do + expect(@ci.validate('cn=')).to be false + expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false end diff --git a/spec/lib/simp/cli/config/utils_spec.rb b/spec/lib/simp/cli/config/utils_spec.rb index 8ec51af1..1242ede4 100644 --- a/spec/lib/simp/cli/config/utils_spec.rb +++ b/spec/lib/simp/cli/config/utils_spec.rb @@ -37,6 +37,38 @@ end end + describe '.validate_ldap_dn' do + it 'validates DNs regardless of attribute case or type' do + [ + 'dc=example,dc=com', + 'DC=example,DC=com', + 'o=Example', + 'ou=People, dc=example,dc=com', + 'cn=Doe\\, John,ou=People,dc=example,dc=com', + 'cn=a+uid=b,dc=example,dc=com', + '2.5.4.3=a,dc=example', + ].each do |dn| + expect(described_class.validate_ldap_dn(dn)).to be(true), dn + end + end + + it "doesn't validate malformed DNs" do + [ + 'dc=', + '=example', + 'dc=example,', + 'dc=example,,dc=com', + 'example.com', + "x\ndc=example", + "dc=example\n", + nil, + false, + ].each do |dn| + expect(described_class.validate_ldap_dn(dn)).to be(false), dn.inspect + end + end + end + describe '.validate_fqdn' do it 'validates good FQDNs' do expect(described_class.validate_fqdn('simp.dev')).to be true From 715377e05f5035058fa21f6d52f6ef72799cad9f Mon Sep 17 00:00:00 2001 From: Steven Pritchard Date: Tue, 17 Nov 2026 20:14:59 +0000 Subject: [PATCH 2/2] Address review: stricter DN grammar, full cn DN check, dc= for SIMP LDAP - 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 --- build/rubygem-simp-cli.spec | 12 +++--- .../items/data/simp_options_ldap_base_dn.rb | 19 +++++++- .../items/data/simp_options_ldap_bind_dn.rb | 5 ++- .../items/data/simp_options_ldap_root_dn.rb | 5 ++- .../items/data/simp_options_ldap_sync_dn.rb | 5 ++- lib/simp/cli/config/utils.rb | 43 ++++++++++++++----- .../data/simp_options_ldap_base_dn_spec.rb | 15 +++++++ .../data/simp_options_ldap_bind_dn_spec.rb | 1 + .../data/simp_options_ldap_root_dn_spec.rb | 1 + .../data/simp_options_ldap_sync_dn_spec.rb | 1 + spec/lib/simp/cli/config/utils_spec.rb | 36 ++++++++++++++++ 11 files changed, 120 insertions(+), 23 deletions(-) diff --git a/build/rubygem-simp-cli.spec b/build/rubygem-simp-cli.spec index 797cde7f..175057c5 100644 --- a/build/rubygem-simp-cli.spec +++ b/build/rubygem-simp-cli.spec @@ -233,11 +233,13 @@ EOM options, e.g. `LDAP: {}`) instead of the deprecated `sssd::domains` Array, and omits the key when there are no domains. Requires simp/sssd 11.0.0 or later; the item class `SssdDomains` is now `SssdDomainsEntries` -- `simp config` accepts any valid LDAP DN for `simp_options::ldap::base_dn` - (e.g. `DC=example,DC=com`, the form `simplib::ldap::domain_to_dn()` - produces, or `o=Example`), and accepts `CN=` as well as `cn=` for the - bind, sync and root DNs. Malformed values such as an empty `dc=` or a - multi-line string are rejected +- `simp config` validates LDAP DNs as whole DNs with case-insensitive + attribute types. `simp_options::ldap::base_dn` accepts any valid DN (e.g. + `DC=example,DC=com`, the form `simplib::ldap::domain_to_dn()` produces), + except that the SIMP LDAP server's base DN must still start with `dc=`. + The bind, sync and root DNs accept `CN=` as well as `cn=`. Malformed DNs, + such as an empty value, a `;` or missing comma between RDNs, or stray + whitespace or line breaks, are rejected * Fri May 20 2022 Trevor Vaughan - 7.0.1 - Always set the hostname to the one specified during `simp config` in the apply diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb index ed9ec944..007f5210 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb @@ -28,12 +28,27 @@ def get_recommended_value end end + # simp_openldap creates the suffix entry as a `domain` object named by + # its first RDN, so when this host is the SIMP LDAP server, the base DN + # must start with `dc=`. def validate(x) - Simp::Cli::Config::Utils.validate_ldap_dn(x) + return false unless Simp::Cli::Config::Utils.validate_ldap_dn(x) + return true unless simp_ldap_server? + + x.match?(%r{\Adc=}i) end def not_valid_message - "Invalid LDAP Base Distinguished Name. Use a DN such as 'dc=example,dc=com'" + msg = "Invalid LDAP Base Distinguished Name. Use a DN such as 'dc=example,dc=com'" + msg += "; the SIMP LDAP server's base DN must start with 'dc='" if simp_ldap_server? + msg + end + + private + + def simp_ldap_server? + @config_items.key?('cli::is_simp_ldap_server') && + @config_items.fetch('cli::is_simp_ldap_server').value end end end diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb index dd4a1e7e..1906cd89 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../item' +require_relative '../../utils' require_relative 'cli_is_simp_ldap_server' module Simp; end @@ -15,11 +16,11 @@ def initialize(puppet_env_info = DEFAULT_PUPPET_ENV_INFO) end def validate(x) - x.to_s.match?(%r{\Acn=[^,\n]+(?:,.+)?\z}i) + Simp::Cli::Config::Utils.validate_ldap_cn_dn(x) end def not_valid_message - "Valid LDAP Bind Distinguished Name must begin with a 'cn=' RDN" + "Valid LDAP Bind Distinguished Name must be a DN that begins with a 'cn=' RDN" end def get_recommended_value diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb index f09674ad..58bd5789 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../item' +require_relative '../../utils' require_relative 'cli_is_simp_ldap_server' module Simp; end @@ -25,11 +26,11 @@ def get_recommended_value end def not_valid_message - "Valid LDAP Root Distinguished Name must begin with a 'cn=' RDN" + "Valid LDAP Root Distinguished Name must be a DN that begins with a 'cn=' RDN" end def validate(x) - x.to_s.match?(%r{\Acn=[^,\n]+(?:,.+)?\z}i) + Simp::Cli::Config::Utils.validate_ldap_cn_dn(x) end end end diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb index efe76b94..bf7ee376 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../item' +require_relative '../../utils' require_relative 'cli_is_simp_ldap_server' module Simp; end @@ -15,11 +16,11 @@ def initialize(puppet_env_info = DEFAULT_PUPPET_ENV_INFO) end def validate(x) - x.to_s.match?(%r{\Acn=[^,\n]+(?:,.+)?\z}i) + Simp::Cli::Config::Utils.validate_ldap_cn_dn(x) end def not_valid_message - "Valid LDAP Sync Distinguished Name must begin with a 'cn=' RDN" + "Valid LDAP Sync Distinguished Name must be a DN that begins with a 'cn=' RDN" end def get_recommended_value diff --git a/lib/simp/cli/config/utils.rb b/lib/simp/cli/config/utils.rb index 23246bcb..5ba4e66d 100644 --- a/lib/simp/cli/config/utils.rb +++ b/lib/simp/cli/config/utils.rb @@ -14,6 +14,26 @@ class Simp::Cli::Config::Utils SHA512_CRYPT_UNSUPPORTED_MSG = "This system's crypt(3) does not support SHA-512 password hashes" + # LDAP Distinguished Name grammar (RFC 4514), used by validate_ldap_dn. + # + # * Attribute types are names (case-insensitive) or numeric OIDs. + # * A value is non-empty. `"`, `+`, `,`, `;`, `<`, `=`, `>` and `\` must be + # escaped with a backslash (or written as `\XX` hex pairs), as must a + # leading `#` or space and a trailing space. `=` isn't special in RFC 4514, + # but an unescaped one almost always means a missing comma. + # * Control characters (including CR, LF and tab) are never allowed. + # * Multi-valued RDNs are joined with `+`; spaces around the `,` between + # RDNs are accepted. + LDAP_DN_ATTR = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)}.freeze + LDAP_DN_ESCAPE = %r{\\(?:[ "\#+,;<=>\\]|\h\h)}.freeze + LDAP_DN_PLAIN = %r{[^\p{Cntrl} "+,;<=>\\]}.freeze + LDAP_DN_VALUE = %r{(?:#{LDAP_DN_ESCAPE}|(?!\#)#{LDAP_DN_PLAIN})(?:(?:#{LDAP_DN_ESCAPE}|#{LDAP_DN_PLAIN}| )*(?:#{LDAP_DN_ESCAPE}|#{LDAP_DN_PLAIN}))?}.freeze + LDAP_DN_RDN = %r{#{LDAP_DN_ATTR}=#{LDAP_DN_VALUE}(?:\+#{LDAP_DN_ATTR}=#{LDAP_DN_VALUE})*}.freeze + LDAP_DN_REGEX = %r{\A#{LDAP_DN_RDN}(?: *, *#{LDAP_DN_RDN})*\z}.freeze + + # A Hiera interpolation such as %{hiera('simp_options::ldap::base_dn')} + HIERA_INTERPOLATION_REGEX = %r{%\{[^{}\p{Cntrl}]+\}}.freeze + class << self def validate_domain(domain) # From Simplib::Domain custom type from pupmod-simp-simplib @@ -41,19 +61,22 @@ def validate_fqdn(fqdn) ((fqdn =~ regex) ? true : false) end - # Validate an LDAP Distinguished Name: one or more comma-separated - # RDNs, each `attribute=value` (multi-valued RDNs joined with `+`). - # Attribute types are case-insensitive, and may be names (`dc`, `DC`, - # `o`, `ou`, ...) or numeric OIDs. Values must be non-empty; a `,`, `+` - # or `\` inside a value must be escaped with a backslash. + # Validate an LDAP Distinguished Name against LDAP_DN_REGEX. def validate_ldap_dn(dn) + dn.is_a?(String) && LDAP_DN_REGEX.match?(dn) + end + + # Validate an LDAP DN whose first RDN is `cn=...` (e.g. a bind DN). + # + # A whole RDN may be a Hiera interpolation, as in SIMP's recommended + # values (`cn=hostAuth,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}`). + # Each interpolation is replaced with a placeholder RDN before the DN + # is validated, so it can't hide a malformed DN around it. + def validate_ldap_cn_dn(dn) return false unless dn.is_a?(String) - attr = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)} - value = %r{(?:[^,+\\\n]|\\.)+} - ava = %r{#{attr}=#{value}} - rdn = %r{#{ava}(?:\+#{ava})*} - dn.match?(%r{\A#{rdn}(?: *, *#{rdn})*\z}) + resolved = dn.gsub(HIERA_INTERPOLATION_REGEX, 'dc=interpolated') + resolved.match?(%r{\Acn=}i) && validate_ldap_dn(resolved) end def validate_ip(ip) diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb index 46adba3e..16fd7583 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb @@ -42,6 +42,21 @@ end describe '#validate' do + context 'when this host is the SIMP LDAP server' do + before :each do + item = Simp::Cli::Config::Item::CliIsSimpLdapServer.new + item.value = true + @ci.config_items[item.key] = item + end + + it 'requires a leading dc= RDN' do + expect(@ci.validate('DC=tasty,DC=bacon')).to be true + expect(@ci.validate('o=Bacon')).to be false + expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be false + expect(@ci.not_valid_message).to include("must start with 'dc='") + end + end + it 'validates ldap_base_dns' do expect(@ci.validate('dc=tasty,dc=bacon')).to be true expect(@ci.validate('DC=tasty,DC=bacon')).to be true diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb index cba54414..4a3ddede 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb @@ -45,6 +45,7 @@ it "doesn't validate bad ldap_bind_dns" do expect(@ci.validate('dc=tasty,dc=bacon')).to be false expect(@ci.validate('cn=')).to be false + expect(@ci.validate('cn=a,garbage')).to be false expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb index d4ce9a5f..2495803f 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb @@ -45,6 +45,7 @@ it "doesn't validate bad ldap_root_dns" do expect(@ci.validate('dc=tasty,dc=bacon')).to be false expect(@ci.validate('cn=')).to be false + expect(@ci.validate('cn=a,garbage')).to be false expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb index 99d726f8..ea77f0e6 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb @@ -44,6 +44,7 @@ it "doesn't validate bad ldap_sync_dns" do expect(@ci.validate('cn=')).to be false + expect(@ci.validate('cn=a,garbage')).to be false expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false diff --git a/spec/lib/simp/cli/config/utils_spec.rb b/spec/lib/simp/cli/config/utils_spec.rb index 1242ede4..e3fe5103 100644 --- a/spec/lib/simp/cli/config/utils_spec.rb +++ b/spec/lib/simp/cli/config/utils_spec.rb @@ -47,6 +47,9 @@ 'cn=Doe\\, John,ou=People,dc=example,dc=com', 'cn=a+uid=b,dc=example,dc=com', '2.5.4.3=a,dc=example', + 'cn=Some Name,dc=example', + 'cn=\\#1,dc=example', + 'cn=\\3Cx\\3E,dc=example', ].each do |dn| expect(described_class.validate_ldap_dn(dn)).to be(true), dn end @@ -61,6 +64,13 @@ 'example.com', "x\ndc=example", "dc=example\n", + "dc=example\r", + 'dc=example,dc=com ', + 'dc= example', + 'dc=example;dc=com', + 'dc=example dc=com', + 'dc=#zz,dc=com', + 'cn=a