fix(pairing): accept spaced approval codes

Port from qwibitai/nanoclaw#3282: normalize visual whitespace in DM pairing codes while preserving exact-match rejection for chatter-wrapped input.
This commit is contained in:
Teknium
2026-08-19 03:19:36 -07:00
parent d228013832
commit 2db1f25f2c
2 changed files with 22 additions and 1 deletions

View File

@@ -464,7 +464,9 @@ class PairingStore:
"""
with self._lock:
self._cleanup_expired(platform)
code = code.upper().strip()
# Chat UIs insert visual spacing between code characters; strip all
# whitespace, then match exactly (surrounding words still fail). #89937
code = "".join(str(code or "").upper().split())
# Before the lookup, or an already-issued valid code would bypass lockout.
if self._is_locked_out(platform):
return None

View File

@@ -275,6 +275,25 @@ class TestApprovalFlow:
assert result["user_id"] == "user1"
assert result["user_name"] == "Alice"
def test_approve_code_with_internal_spacing(self, tmp_path):
with patch("gateway.pairing.PAIRING_DIR", tmp_path):
store = PairingStore()
code = store.generate_code("telegram", "user1", "Alice")
spaced_code = " ".join(code)
result = store.approve_code("telegram", f" {spaced_code} ")
assert isinstance(result, dict)
assert result["user_id"] == "user1"
assert result["user_name"] == "Alice"
def test_approve_code_with_words_still_fails(self, tmp_path):
with patch("gateway.pairing.PAIRING_DIR", tmp_path):
store = PairingStore()
code = store.generate_code("telegram", "user1", "Alice")
result = store.approve_code("telegram", f"code {code}")
assert result is None
def test_approved_user_is_approved(self, tmp_path):
with patch("gateway.pairing.PAIRING_DIR", tmp_path):
store = PairingStore()