The vulnerability exists in the PKI authentication realm of Elasticsearch, where the user's identity (principal) is extracted from a client X.509 certificate. The root cause is the use of a regular expression on the string representation of the certificate's Subject Distinguished Name (DN) within the getPrincipalFromSubjectDN function. This method is fundamentally insecure because the string representation of a DN is not guaranteed to be unambiguous and can be manipulated by an attacker who can control the fields of a certificate to be signed by a trusted Certificate Authority.
The patch addresses this by introducing a new, safer method of principal extraction. A new class, RdnFieldExtractor, is added, which parses the DER-encoded ASN.1 structure of the DN to extract a specific attribute (RDN) by its Object Identifier (OID). This is a robust method that is not susceptible to the ambiguities of string representation. The PkiRealm is modified to use this new extractor when configured via new settings (username_rdn_oid or username_rdn_name), falling back to the old, vulnerable regex-based method only for backward compatibility. The key functions authenticate and token in PkiRealm were updated to use this new, safer logic. An attacker could exploit this by presenting a crafted certificate, causing the vulnerable getPrincipalFromSubjectDN function to be called, leading to user impersonation.