Skip to content

The agent that grows with you

fix(feishu): match approval clicks against every operator id in Feishu lists

unsupportedpastels48b090c5e5fe
diff --git a/plugins/platforms/feishu/adapter.py b/plugins/platforms/feishu/adapter.py
index 371e6fb6f94..557a10abc74 100644
--- a/plugins/platforms/feishu/adapter.py
+++ b/plugins/platforms/feishu/adapter.py
@@ -2231,7 +2231,9 @@ class FeishuAdapter(BasePlatformAdapter):
future.add_done_callback(self._log_background_failure)
return True
- def _is_interactive_operator_authorized(self, open_id: str, *, user_id: str = "", chat_id: str = "") -> bool:
+ def _is_interactive_operator_authorized(
+ self, open_id: str, *, user_id: str = "", union_id: str = "", chat_id: str = "",
+ ) -> bool:
"""Return whether this card-action operator may answer gated prompts.
Feishu admins / FEISHU_ALLOWED_USERS decide when set. With neither (setup's pairing-mode
@@ -2243,7 +2245,9 @@ class FeishuAdapter(BasePlatformAdapter):
return False
allowed_ids = set(self._admins) | set(self._allowed_group_users)
if allowed_ids:
- return "*" in allowed_ids or normalized in allowed_ids
+ # Lists may name a user by any id intake accepts (_sender_identity).
+ operator_ids = {normalized, str(user_id or "").strip(), str(union_id or "").strip()} - {""}
+ return "*" in allowed_ids or bool(operator_ids & allowed_ids)
# SessionSource.user_id is the tenant user_id, else open_id (_resolve_sender_profile).
principal = str(user_id or "").strip() or normalized
# getattr: object.__new__ test doubles never ran BasePlatformAdapter.__init__. Feishu chat
@@ -2272,18 +2276,19 @@ class FeishuAdapter(BasePlatformAdapter):
def _validate_card_action(
self, *, event: Any, state: dict[str, str], label: str, ident: Any,
- ) -> Optional[tuple[str, str, str, str]]:
+ ) -> Optional[tuple[str, str, str, str, str]]:
"""Shared operator/chat checks for approval + update-prompt clicks.
- Returns ``(open_id, callback_chat_id, user_name, user_id)`` or None (already logged).
+ Returns ``(open_id, callback_chat_id, user_name, user_id, union_id)`` or None (already logged).
"""
operator = getattr(event, "operator", None)
open_id = str(getattr(operator, "open_id", "") or "")
user_id = str(getattr(operator, "user_id", "") or "")
+ union_id = str(getattr(operator, "union_id", "") or "")
callback_chat_id = str(getattr(getattr(event, "context", None), "open_chat_id", "") or "")
expected_chat_id = str(state.get("chat_id", "") or "")
if not self._is_interactive_operator_authorized(
- open_id, user_id=user_id, chat_id=expected_chat_id or callback_chat_id,
+ open_id, user_id=user_id, union_id=union_id, chat_id=expected_chat_id or callback_chat_id,
):
logger.warning("[Feishu] Unauthorized %s click by %s", label, open_id or "<unknown>")
return None
@@ -2293,7 +2298,7 @@ class FeishuAdapter(BasePlatformAdapter):
label.capitalize(), ident, expected_chat_id, callback_chat_id,
)
return None
- return open_id, callback_chat_id, self._get_cached_sender_name(open_id) or open_id, user_id
+ return open_id, callback_chat_id, self._get_cached_sender_name(open_id) or open_id, user_id, union_id
def _handle_approval_card_action(self, *, event: Any, action_value: dict[str, Any], loop: Any) -> Any:
"""Schedule approval resolution and build the synchronous callback response."""
@@ -2309,10 +2314,10 @@ class FeishuAdapter(BasePlatformAdapter):
checked = self._validate_card_action(event=event, state=state, label="approval", ident=approval_id)
if checked is None:
return self._card_response()
- open_id, chat_id, user_name, user_id = checked
+ open_id, chat_id, user_name, user_id, union_id = checked
coro = self._resolve_approval(
approval_id=approval_id, choice=choice, user_name=user_name, open_id=open_id, chat_id=chat_id,
- user_id=user_id,
+ user_id=user_id, union_id=union_id,
)
if not self._submit_on_loop(loop, coro):
return self._card_response()
@@ -2335,9 +2340,9 @@ class FeishuAdapter(BasePlatformAdapter):
checked = self._validate_card_action(event=event, state=state, label="update prompt", ident=prompt_id)
if checked is None:
return self._card_response()
- open_id, chat_id, user_name, user_id = checked
+ open_id, chat_id, user_name, user_id, union_id = checked
coro = self._resolve_update_prompt(
- prompt_id, answer, user_name, open_id=open_id, chat_id=chat_id, user_id=user_id,
+ prompt_id, answer, user_name, open_id=open_id, chat_id=chat_id, user_id=user_id, union_id=union_id,
)
if not self._submit_on_loop(loop, coro):
return self._card_response()
@@ -2345,7 +2350,7 @@ class FeishuAdapter(BasePlatformAdapter):
def _pop_validated_prompt_state(
self, *, states: dict[int, dict[str, str]], ident: Any, label: str, open_id: str, chat_id: str,
- unauthorized_fmt: str, operator_repr: str, user_id: str = "",
+ unauthorized_fmt: str, operator_repr: str, user_id: str = "", union_id: str = "",
) -> Optional[dict[str, str]]:
"""Re-validate on the loop thread (state may have changed since the callback) and pop."""
state = states.get(ident)
@@ -2353,7 +2358,7 @@ class FeishuAdapter(BasePlatformAdapter):
logger.debug("[Feishu] %s %s already resolved or unknown", label, ident)
return None
if not self._is_interactive_operator_authorized(
- open_id, user_id=user_id, chat_id=str(state.get("chat_id", "") or "") or chat_id,
+ open_id, user_id=user_id, union_id=union_id, chat_id=str(state.get("chat_id", "") or "") or chat_id,
):
logger.warning(unauthorized_fmt, operator_repr, ident)
return None
@@ -2368,13 +2373,13 @@ class FeishuAdapter(BasePlatformAdapter):
async def _resolve_approval(
self, approval_id: Any, choice: str, user_name: str, *, open_id: str = "", chat_id: str = "",
- user_id: str = "",
+ user_id: str = "", union_id: str = "",
) -> None:
"""Pop approval state and unblock the waiting agent thread."""
state = self._pop_validated_prompt_state(
states=self._approval_state, ident=approval_id, label="Approval", open_id=open_id, chat_id=chat_id,
unauthorized_fmt="[Feishu] Unauthorized approval click by %s for approval %s",
- operator_repr=open_id or "<unknown>", user_id=user_id,
+ operator_repr=open_id or "<unknown>", user_id=user_id, union_id=union_id,
)
if not state:
return
@@ -2403,13 +2408,13 @@ class FeishuAdapter(BasePlatformAdapter):
async def _resolve_update_prompt(
self, prompt_id: Any, answer: str, user_name: str, *, open_id: str = "", chat_id: str = "",
- user_id: str = "",
+ user_id: str = "", union_id: str = "",
) -> None:
"""Persist an update prompt answer for the detached update process."""
state = self._pop_validated_prompt_state(
states=self._update_prompt_state, ident=prompt_id, label="Update prompt", open_id=open_id,
chat_id=chat_id, unauthorized_fmt="[Feishu] Unauthorized update prompt click by %s for prompt %s",
- operator_repr=open_id, user_id=user_id,
+ operator_repr=open_id, user_id=user_id, union_id=union_id,
)
if not state:
return
diff --git a/tests/gateway/test_feishu_card_action_gateway_authz.py b/tests/gateway/test_feishu_card_action_gateway_authz.py
index c47a6f9d08c..a0b2d24643f 100644
--- a/tests/gateway/test_feishu_card_action_gateway_authz.py
+++ b/tests/gateway/test_feishu_card_action_gateway_authz.py
@@ -80,10 +80,10 @@ class _GatewayCheck:
_gateway_check = _GatewayCheck
-def _approval_click(open_id, *, user_id=None, chat_id="oc_group"):
+def _approval_click(open_id, *, user_id=None, union_id=None, chat_id="oc_group"):
return SimpleNamespace(event=SimpleNamespace(
token="tok", context=SimpleNamespace(open_chat_id=chat_id),
- operator=SimpleNamespace(open_id=open_id, user_id=user_id),
+ operator=SimpleNamespace(open_id=open_id, user_id=user_id, union_id=union_id),
action=SimpleNamespace(tag="button", value={"hermes_action": "approve_once", "approval_id": 1}),
))
@@ -216,3 +216,57 @@ class TestFeishuAllowlistUnchanged:
assert response.card is None
submit.assert_not_called()
+
+
+class TestFeishuAllowlistMatchesEveryOperatorId:
+ """A Feishu list may name a user by any id intake accepts (open_id / user_id / union_id)."""
+
+ @pytest.mark.parametrize("click_ids", [{"user_id": "u_tenant"}, {"union_id": "on_union"}])
+ def test_allowed_users_listed_by_other_id_can_approve(self, click_ids):
+ adapter = _make_adapter(allowed={"u_tenant", "on_union"})
+
+ with patch("asyncio.run_coroutine_threadsafe", side_effect=_close_submitted_coro) as submit:
+ response = adapter._on_card_action_trigger(_approval_click("ou_x", **click_ids))
+
+ assert response.card is not None
+ submit.assert_called_once()
+
+ def test_admin_listed_by_tenant_user_id_can_approve(self):
+ adapter = _make_adapter(admins={"u_admin"})
+
+ with patch("asyncio.run_coroutine_threadsafe", side_effect=_close_submitted_coro) as submit:
+ response = adapter._on_card_action_trigger(_approval_click("ou_y", user_id="u_admin"))
+
+ assert response.card is not None
+ submit.assert_called_once()
+
+ def test_unlisted_ids_still_refused(self):
+ adapter = _make_adapter(allowed={"u_tenant"}, admins={"u_admin"})
+
+ with patch("asyncio.run_coroutine_threadsafe") as submit:
+ response = adapter._on_card_action_trigger(
+ _approval_click("ou_z", user_id="u_other", union_id="on_other"),
+ )
+
+ assert response.card is None
+ submit.assert_not_called()
+ assert 1 in adapter._approval_state
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("click_ids", [{"user_id": "u_tenant"}, {"union_id": "on_union"}])
+ async def test_loop_side_recheck_matches_other_ids(self, click_ids):
+ adapter = _make_adapter(allowed={"u_tenant", "on_union"})
+
+ with patch("tools.approval.resolve_gateway_approval", return_value=1) as resolve:
+ await adapter._resolve_approval(1, "once", "Alice", open_id="ou_x", chat_id="oc_group", **click_ids)
+
+ resolve.assert_called_once_with("sess-1", "once")
+
+ @pytest.mark.asyncio
+ async def test_update_prompt_recheck_matches_tenant_user_id(self, tmp_path, monkeypatch):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ adapter = _make_adapter(allowed={"u_tenant"})
+
+ await adapter._resolve_update_prompt(2, "y", "Alice", open_id="ou_x", chat_id="oc_group", user_id="u_tenant")
+
+ assert 2 not in adapter._update_prompt_state