Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(auth): escape LDAP direct bind username in DN (#5949)

zhanggaoyuan3cdfbc22ff17
diff --git a/openviking/server/auth/plugins/ldap.py b/openviking/server/auth/plugins/ldap.py
index 3efe5d66c..2a74982d3 100644
--- a/openviking/server/auth/plugins/ldap.py
+++ b/openviking/server/auth/plugins/ldap.py
@@ -10,6 +10,7 @@ from __future__ import annotations
import asyncio
import logging
+import re
import sys
from typing import Any, Dict, List, Optional, Tuple
@@ -43,6 +44,43 @@ except ImportError:
_LDAP_NETWORK_TIMEOUT = 10.0
_LDAP_OPERATION_TIMEOUT = 10.0
+_DN_VALUE_ESCAPES = {
+ "\x00": r"\00",
+ '"': r'\"',
+ "+": r"\+",
+ ",": r"\,",
+ ";": r"\;",
+ "<": r"\<",
+ "=": r"\=",
+ ">": r"\>",
+ "\\": r"\\",
+}
+_DN_ESCAPE_RE = re.compile(r"\\([0-9A-Fa-f]{2}|[\s\S])")
+_FIRST_RDN_RE = re.compile(r"(?:\\.|[^,])*")
+
+
+def _escape_dn_value(value: str) -> str:
+ """Escape a single LDAP DN attribute value per RFC 4514."""
+ escaped = "".join(_DN_VALUE_ESCAPES.get(char, char) for char in value)
+ if not escaped:
+ return escaped
+ if value[0] in {" ", "#"}:
+ escaped = "\\" + escaped
+ if len(value) > 1 and value[-1] == " ":
+ escaped = escaped[:-1] + r"\ "
+ return escaped
+
+
+def _split_first_rdn(dn: str) -> str:
+ return _FIRST_RDN_RE.match(dn)[0]
+
+
+def _unescape_dn_value(value: str) -> str:
+ return _DN_ESCAPE_RE.sub(
+ lambda match: chr(int(match[1], 16)) if len(match[1]) == 2 else match[1],
+ value,
+ )
+
def _apply_ldap_connection_options(conn: Any) -> None:
"""Apply standard connection options including network timeouts."""
@@ -63,10 +101,11 @@ def _format_user_dn(pattern: str, username: str) -> str:
(``uid=%s,ou=users,dc=example,dc=com``).
* ``"%(username)s"`` / ``"%(user)s"`` mapping substitution.
"""
+ safe_username = _escape_dn_value(username)
if "%(" in pattern:
- return pattern % {"username": username, "user": username}
+ return pattern % {"username": safe_username, "user": safe_username}
# Only substitute a single %s; treat %% as an escaped %.
- return pattern % (username,)
+ return pattern % (safe_username,)
class LDAPAuthPlugin(AuthPlugin):
@@ -369,11 +408,11 @@ class LDAPAuthPlugin(AuthPlugin):
"""Parse the first RDN of a DN, returning (attr_name, value)."""
if not dn:
return None, None
- first = dn.split(",", 1)[0]
+ first = _split_first_rdn(dn)
if "=" not in first:
return None, None
attr, _, value = first.partition("=")
- return attr.strip(), value.strip()
+ return attr.strip(), _unescape_dn_value(value)
@classmethod
def _attrs_from_dn(cls, dn: str) -> Dict[str, List[bytes]]:
diff --git a/tests/server/test_ldap_auth.py b/tests/server/test_ldap_auth.py
new file mode 100644
index 000000000..1c8d71c41
--- /dev/null
+++ b/tests/server/test_ldap_auth.py
@@ -0,0 +1,31 @@
+import pytest
+
+from openviking.server.auth.plugins.ldap import LDAPAuthPlugin, _format_user_dn
+
+
+ "pattern",
+ [
+ "uid=%(username)s,ou=users,dc=example,dc=com",
+ "uid=%s,ou=users,dc=example,dc=com",
+ ],
+)
+def test_format_user_dn_escapes_username(pattern: str) -> None:
+ dn = _format_user_dn(
+ pattern,
+ "victim,ou=attacker",
+ )
+
+ assert dn == r"uid=victim\,ou\=attacker,ou=users,dc=example,dc=com"
+
+
+def test_attrs_from_dn_round_trips_escaped_rdn_value() -> None:
+ username = "victim,ou=attacker"
+
+ dn = _format_user_dn(
+ "uid=%s,ou=users,dc=example,dc=com",
+ username,
+ )
+
+ assert dn == r"uid=victim\,ou\=attacker,ou=users,dc=example,dc=com"
+ assert LDAPAuthPlugin._attrs_from_dn(dn) == {"uid": [username.encode()]}