fix: validate AD email match to prevent wrong requester fallback

This commit is contained in:
Денис Кривоченко
2026-07-23 10:18:42 +07:00
parent 44117f90b3
commit d812d9b2b2
+14 -3
View File
@@ -73,16 +73,27 @@ sd_sessions = {}
def get_ad_user_sync(user_id: str):
"""Ищем по email/UPN (search_by_user_id), НЕ по sAMAccountName.
user_id из TrueConf — обычно email вида 'login@tcs.sibcem.ru'.
Функция сама заменит @tcs.sibcem.ru на @sibcem.ru и найдёт правильную учётку."""
Функция сама заменит @tcs.sibcem.ru на @sibcem.ru и найдёт правильную учётку.
ВАЖНО: если найденный пользователь не совпадает с запрошенным по email — возвращаем None."""
try:
from utils.ad_search import search_by_user_id
entries = search_by_user_id(user_id, ["displayName", "mail", "l", "userAccountControl"])
if entries:
if not entries:
return None
user = entries[0]
mail = user.mail.value if 'mail' in user else None
if not mail:
return None
# Валидация: извлекаем логин из user_id и из mail, сравниваем
req_login = user_id.split("@")[0] if "@" in user_id else user_id
mail_login = mail.split("@")[0] if "@" in mail else mail
if req_login.lower() != mail_login.lower():
logger.info(f"AD user mismatch: requested login={req_login}, found mail={mail} (login={mail_login})")
return None
uac = user.userAccountControl.value if 'userAccountControl' in user else 0
return {
"name": user.displayName.value if 'displayName' in user else user_id,
"mail": user.mail.value if 'mail' in user else None,
"mail": mail,
"city": user.l.value if 'l' in user else "Кемерово",
"is_disabled": bool(uac & 2)
}