Skip to content

Your lightweight open-source personal agent.

fix(feishu): preserve QR query values and registration expiry

Xubin Ren4535496064c0
diff --git a/nanobot/channels/feishu/runtime.py b/nanobot/channels/feishu/runtime.py
index a99e71ced..41c840e51 100644
--- a/nanobot/channels/feishu/runtime.py
+++ b/nanobot/channels/feishu/runtime.py
@@ -18,6 +18,7 @@ from datetime import UTC, datetime
from functools import partial
from pathlib import Path
from typing import TYPE_CHECKING, Any, TypedDict, cast
+from urllib.parse import urlsplit, urlunsplit
from rich.console import Console
from rich.markup import escape
@@ -546,15 +547,17 @@ def _begin_registration(domain: str = "feishu") -> _RegistrationStart:
qr_url = res.get("verification_uri_complete", "")
if not isinstance(qr_url, str) or not qr_url:
raise RuntimeError("Feishu / Lark registration did not return a login URL")
- # The registration endpoint returns a /page/launcher URL, but that page
- # rejects these device codes instantly ("Link expired") even when scanned
- # seconds after issuance. /page/cli is the page used by other agent
- # implementations of this flow and works for both feishu.cn and
- # larksuite.com tenants, so rewrite the host page and keep the user_code.
- if "/page/launcher?" in qr_url:
- qr_url = qr_url.replace("/page/launcher?", "/page/cli?")
+ # PersonalAgent codes use /page/cli. Rewrite only the known landing page,
+ # preserving opaque query values (including user_code) and the fragment.
+ parsed = urlsplit(qr_url)
+ if (
+ parsed.scheme == "https"
+ and parsed.netloc in {"open.feishu.cn", "open.larksuite.com"}
+ and parsed.path == "/page/launcher"
+ ):
+ qr_url = urlunsplit(parsed._replace(path="/page/cli"))
interval = res.get("interval")
- expire_in = res.get("expire_in")
+ expire_in = res.get("expires_in", res.get("expire_in"))
return {
"device_code": device_code,
"qr_url": qr_url,
diff --git a/tests/channels/test_feishu_login_url.py b/tests/channels/test_feishu_login_url.py
index 11c276f5c..528977d2c 100644
--- a/tests/channels/test_feishu_login_url.py
+++ b/tests/channels/test_feishu_login_url.py
@@ -42,3 +42,40 @@ def test_cli_url_passed_through_unchanged() -> None:
return_value=_begin_response(uri)):
start = feishu_runtime._begin_registration("feishu")
assert start["qr_url"] == uri
+
+
+ "https://open.feishu.cn/page/cli?return_to=/page/launcher?user_code=OTHER",
+ "https://open.feishu.cn/other/page/launcher?user_code=ABCD",
+ "https://other.example/page/launcher?user_code=ABCD",
+])
+def test_other_urls_and_embedded_query_values_are_not_rewritten(uri):
+ with patch.object(feishu_runtime, "_post_registration", return_value=_begin_response(uri)):
+ assert feishu_runtime._begin_registration()["qr_url"] == uri
+
+
[email protected]("domain,host", [
+ ("feishu", "open.feishu.cn"), ("lark", "open.larksuite.com"),
+])
+def test_launcher_rewrite_preserves_opaque_query_and_fragment(domain, host):
+ suffix = "?user_code=AB%2BCD&return_to=/page/launcher?other=1#confirm"
+ uri = f"https://{host}/page/launcher{suffix}"
+ with patch.object(feishu_runtime, "_post_registration", return_value=_begin_response(uri)):
+ start = feishu_runtime._begin_registration(domain)
+ assert start["qr_url"] == f"https://{host}/page/cli{suffix}"
+ assert start["device_code"] == "dc-test"
+ assert start["interval"] == 5
+
+
[email protected]("ttl_fields,expected", [
+ ({"expires_in": 3600}, 3600),
+ ({"expire_in": 1200}, 1200),
+ ({"expires_in": 3600, "expire_in": 1200}, 3600),
+ ({}, 600),
+])
+def test_registration_uses_server_expiry_with_legacy_fallback(ttl_fields, expected):
+ response = _begin_response("https://open.feishu.cn/page/launcher?user_code=ABCD")
+ response.pop("expire_in")
+ response.update(ttl_fields)
+ with patch.object(feishu_runtime, "_post_registration", return_value=response):
+ assert feishu_runtime._begin_registration()["expire_in"] == expected