/

CVE-2026-40165

Severity:

HIGH RISK

(8.7)

Authentication Bypass via XML Injection in authentik

CVE-2026-40165

Deferred

Vulnerability type

Category:

Authentication Bypass via XML Injection

CWE:

CWE-91, CWE-287, CWE-436

Impact:

Account Takeover / Authentication Bypass

Severity:

HIGH RISK

(8.7)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

authentik's SAML NameID handling allowed XML comments to be injected into the NameID value, causing authentik to interpret only the portion before the comment.

In deployments using a SAML Source where users can influence their NameID, this parsing flaw could allow an attacker to impersonate other accounts and gain unauthorized access.

Affected context

This vulnerability affects authentik instances configured with a SAML Source where:

  • The SAML assertion's NameID is used to identify or map users (e.g., username or email).
  • XML Signing is enabled, so assertions are accepted as cryptographically valid.
  • Users can control or influence their NameID value at the SAML identity provider (IdP).

Under these conditions, authentik's NameID extraction logic may misinterpret a crafted NameID containing XML comments, leading to incorrect user mapping and potential account compromise.

Exploit preconditions

  • Attack surface: SAML authentication endpoint of the authentik instance.
  • Required access:
  • A valid account on the SAML Source (IdP) that is trusted by authentik.
  • Ability to modify or influence the NameID value (often username or email) in the IdP profile or configuration.
  • Trigger point: Processing of a SAML assertion during login where the NameID element contains an attacker-crafted value with an embedded XML comment.
  • Why exploitation is possible:
  • The NameID parsing logic does not robustly handle comments within the XML text.
  • Authentik treats only the part of the NameID before the XML comment as the effective identifier, enabling an attacker to craft a NameID that maps to a different user's identifier.
  • XML signature validation confirms the assertion's integrity, but does not prevent semantic manipulation of the NameID content.

Why this happens

The core issue stems from improper handling of XML content within security-sensitive fields, specifically the NameID in a SAML assertion.

Instead of treating the NameID as a simple, fully-validated string, authentik's extraction logic allowed XML comments to alter how the value was interpreted, effectively truncating the identifier at the comment boundary.

This maps to:

  • CWE-91: XML Injection – XML structures (comments) injected into data fields are processed in a way that changes application logic.
  • CWE-287: Improper Authentication – Authentication decisions are made based on an incorrectly parsed identity value.
  • CWE-436: Interpretation Conflict – A mismatch between how the IdP/user intends the identifier to be interpreted and how authentik actually parses it, leading to security-relevant discrepancies.

The abstract insecure pattern is: relying on XML-parsed identity attributes without normalizing and validating them as opaque, unambiguous strings before using them in authentication and account-mapping logic.

Potential impact

  • Attacker may impersonate other user accounts within the authentik-managed applications.
  • Attacker could gain access to higher-privileged accounts if those accounts are mappable via crafted NameID values.
  • Compromise of sensitive data and resources accessible to the impersonated users.
  • Potential escalation to broader system compromise depending on the permissions of targeted accounts.
  • Undermining of trust in SAML-based single sign-on for the affected environment.

General mitigation guidance

Effective mitigation focuses on treating identity attributes from SAML as opaque, fully-normalized strings and hardening XML handling:

  • Normalize and sanitize NameID and similar attributes by stripping or rejecting XML markup constructs, including comments, before use in authentication or user mapping.
  • Enforce strict schema validation for SAML assertions, disallowing unexpected constructs within identifiers and ensuring NameID content matches a constrained pattern (e.g., RFC-compliant email or username format).
  • Use SAML and XML libraries that provide safe, defensive parsing modes and emphasize treating data-bearing elements as text rather than as mixed XML content.
  • Implement robust, deterministic account-linking rules that do not rely on ambiguous or partially parsed identity attributes.
  • Apply least-privilege principles to mapped accounts and limit the blast radius of any single account compromise.
  • Include defense-in-depth checks (such as additional attribute verification or step-up authentication) before granting access to high-privilege roles.

How teams detect issues like this

Detection commonly involves both code-level and configuration-level analysis:

  • Static analysis patterns:
  • Flag use of XML parsing APIs that allow comments or mixed content in fields that drive authentication or authorization.
  • Identify locations where NameID or other identity attributes are parsed or split based on XML structure rather than treated as plain strings.
  • Code review signals:
  • Authentication logic that assumes XML-parsed text nodes are safe identifiers without normalization or validation.
  • Complex handling of SAML NameID or attributes that includes string concatenation, trimming, or manual parsing of XML constructs.
  • Inconsistent interpretation of the same identity attribute between different components (e.g., IdP configuration vs SP mapping logic).
  • Policy and runtime monitoring:
  • Enforce security guidelines for SSO integration, requiring schema validation and normalization of identity attributes.
  • Log and review authentication events for unusual patterns, such as multiple distinct users apparently sharing similar identifiers or unexpected account linkages.
  • Automated SAST/IAST tools can be configured to detect risky XML processing in authentication paths, and security tests can include malformed SAML assertions to exercise parsing behavior.

Sources

SourceLink
github.comgithub.com/goauthentik/authentik/commit/47dec5c6b7fb4a62bfad2ae8bddf002bde7ba774
github.comgithub.com/goauthentik/authentik/releases/tag/version%2F2025.12.5
github.comgithub.com/goauthentik/authentik/security/advisories/GHSA-9wj8-xv4r-qwrp

Frequently asked

questions

Exploitability is limited to authentik instances that use a SAML Source and accept NameID-based identification, where the attacker has an account on the SAML IdP and can influence their NameID. Within that scope, a crafted assertion can often be used to impersonate other users.

The attacker needs a legitimate account on the trusted SAML identity provider and the ability to modify their own NameID (for example, by editing their username or email attribute in the IdP, or via administrative misconfiguration). Network access to the authentik SAML endpoint is also required.

The safest pattern is to treat SAML NameID and similar attributes as opaque, strictly validated strings: disallow XML comments and markup in identity values, normalize the data before use, validate it against expected formats, and ensure that account mapping logic operates only on these sanitized values.

Future prevention involves adopting secure XML processing practices for all federation protocols: use hardened XML/SAML libraries, enforce strict schemas, avoid interpreting identity attributes as structured XML, add security-focused reviews for authentication and SSO code paths, and incorporate negative tests with malformed or adversarial SAML assertions into CI pipelines.

Find out what's already
exploitable in your codebase.