Skip to content

PKI: enforce_hostnames=false does not allow underscores in DNS SANs #31925

@aloeenmae

Description

@aloeenmae

Describe the bug

The enforce_hostnames=false role parameter does not allow underscores in DNS SANs, contradicting the official PKI API documentation which states:

enforce_hostnames (bool: true) - Specifies if only valid host names are allowed for CNs, DNS SANs, and the host part of email addresses.
The documentation implies that setting enforce_hostnames=false should relax hostname validation (including the "no underscores" rule) for DNS SANs. In practice, DNS SANs containing underscores are always rejected regardless of this setting:

  • In v1.20.3 (and earlier): the invalid SAN is silently dropped from the issued certificate with no warning or error.
  • In the current main branch: the request errors out with "subject alternate name <name> is not a valid DNS name and cannot be included as a SAN".
    Neither behavior matches the documentation's promise.
    Root cause: the hostnameRegex in issue_common.go does not allow underscores, and the regex check at SAN extraction is independent of role.EnforceHostnames. The EnforceHostnames flag is only consulted later in ValidateNames, which is after the underscore SAN has already been stripped or rejected.

To Reproduce

Steps to reproduce the behavior:

  1. Enable PKI and generate a root CA
  2. Create a role with the most permissive settings:
allow_any_name=true
enforce_hostnames=false
allow_wildcard_certificates=true
max_ttl=168h
  1. Verify role settings:
vault read pki/roles/test -format=json | jq '.data | {enforce_hostnames, allow_any_name}'
{
  "enforce_hostnames": false,
  "allow_any_name": true
}
  1. Issue a certificate with an underscore in alt_names:
vault write pki/issue/test
common_name="test.example.com"
alt_names="with-hyphen.example.com,with_underscore.example.com"
-format=json | jq -r '.data.certificate' | openssl x509 -noout -text | grep -A3 "Subject Alternative Name"

output:

            X509v3 Subject Alternative Name:
                DNS:test.example.com, DNS:with-hyphen.example.com

Expected behavior

With enforce_hostnames=false (and allow_any_name=true, allow_wildcard_certificates=true), DNS SANs containing underscores should be accepted and included in the issued certificate, per the documentation.
Alternatively, if this is intentional, the documentation should be updated to clarify that hostname format validation (specifically the hostnameRegex check at SAN extraction) is always enforced on DNS SANs regardless of enforce_hostnames.

Environment:

  • Vault Server Version (retrieve with vault status): Vault v1.20.3
  • Vault CLI Version (retrieve with vault version): v1.20.3
  • Server Operating System/Architecture: Linux amd64

Additional context

  • Underscores in the CN work correctly (fixed in PKI Secrets Engine: underscores not permitted in common_name value #3876 for Vault 0.9.4), but that fix was not extended to DNS SANs.

  • The current main branch changed the behavior from silent drop to explicit error (see issue_common.go), but the core issue — that enforce_hostnames=false does not actually relax DNS SAN hostname validation as documented — remains unaddressed.

  • Workaround: names with underscores can be placed in the CN (which is then automatically copied to SANs only if it's a valid hostname, so underscore-containing CNs don't appear in SANs either), or passed via uri_sans with a URI scheme prefix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugUsed to indicate a potential bugsecret/pki

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions