Repository navigation
fix(telegram): refuse a malformed connect code before it spends an attempt (TASK-157) - #1932
Merged
Merged
Conversation
…tempt (TASK-157) Two follow-ups to #1931 at the same call site. 1. Lowercase the code after joining it. Codes are minted lowercase (`crypto.randomBytes(16).toString('hex')`) and the lookup is exact, so a keyboard that capitalised one group refused a correct code. 2. Refuse anything that is not 32 hex characters BEFORE `registerEnableAttempt`. A minted code has a checkable shape, so a typo ("/commonly-enable 1964") currently spends one of the chat's five tries for nothing. The malformed path gets its own message and is left to the route's outer rate limiter; guessing stays metered because only a well-formed code can ever match. Order in the handler: usage -> shape -> attempt counter -> lookup. Witnesses assert the counter's value: a well-formed wrong code spends exactly one attempt, and four malformed shapes spend none. Fixture fallout from the shape gate is disclosed in the PR body.
This was referenced Sep 27, 2026
test(telegram): assert the expiry refusal's copy so the arm discriminates (TASK-157 follow-up)
#1934
Merged
lilyshen0722
added a commit
to thantiklermcirony/commonly
that referenced
this pull request
Oct 9, 2026
…hipped (Team-Commonly#1936) Records the live Telegram re-walk of 2026-09-27 (02:43–02:47Z): the code typed exactly as displayed, spaced and with a capitalised group, binds on the first try (Team-Commonly#1931, Team-Commonly#1932); a second gated pod relays its agent's line to the chat while a typed line goes only to the active pod; C3 and C5 inbound green. Marks the 7ccc6ac C1 red as fixed, TASK-151 as shipped (Team-Commonly#1929 on f5eb556) and TASK-155 as shipped (Team-Commonly#1933 on 9a6253f). Open: TASK-156. Claude-Session: https://claude.ai/code/session_01MCF2UK3EQ6z6EHdvxAkM44 Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Two follow-ups to #1931 at the same call site, per Vera 74610 (the shape check) and her 74613 ruling (the lowercase), ordered by connector-ops 74614.
crypto.randomBytes(16).toString('hex'),telegramConnectCode.mintConnectCode— the only mint path) and the lookup is an exact match, so a keyboard that capitalised one group of the displayed code refused a correct code.registerEnableAttempt(chatId)ran before the code was looked up, and a minted code has a checkable shape — 32 hex characters — so/commonly-enable 1964burned an attempt for nothing. That is the harm TASK-153's spec named and left in place.The change
Order in the handler, deliberately: usage message → shape refusal → attempt counter → lookup.
and at the call site
args.join('').toLowerCase()— join and strip as #1931 ships it, then lowercase, so the lowercase lands before the anchored shape test rather than after it.Guessing is not made free, and the comment says so: only a well-formed code can ever match a minted one, and a well-formed guess still spends an attempt. Malformed input is bounded by the route's outer limiter
telegramWebhookRateLimit(600/60s per Cloudflare IP,webhooks/telegram.ts:22-30) — a regex costs no database lookup. That limiter isskipped underNODE_ENV=test, so no witness here covers it and none pretends to (wren 74615): the three counter-value arms are the real coverage of this change.Fixture fallout — disclosed, because it is the finding
The pre-existing fixtures were mostly malformed codes, which is exactly why nobody noticed the counter being spent on typos:
refuses an expired codepassed for the wrong reason once the shape gate landed:'x'is malformed, so the refusal came from the shape gate before the lookup. The assertion (findByIdAndUpdatenot called) still held — a false pass that silently dropped the expiry branch's coverage. Fixture is now'e'.repeat(32).refuses a legacy code with no expiryusedabc123. Renamed torefuses a code with no expirywith a 32-hex fixture, because that is the only shape that can now reach the expiry branch (see the reachability note below).rate-limits attempts per chatguessed withguess${i}— not hex — so the counter never moved. Fixtures are now well-formed wrong codes.still refuses a spaced code that matches nothingusedzzzz zzzz …, which joins to 16 non-hex characters and was shape-refused before the lookup. It now uses a spaced, well-formed, unknown code, so it exercises the lookup path it names.telegram.webhook.test.js: both bind tests usedabc123, and would have failed. Fixtures are now 32-hex.Reachability shift, stated rather than glossed: a legacy 24-bit code (
abc123) is now refused by the shape gate with "That doesn't look like a connect code", not by the expiry branch with "Invalid or expired code". Both refuse; the message changes. The expiry branch keeps its coverage through a 32-hex code with no expiry, and the legacy shape is covered by the malformed arms below.Witnesses
Counter values, not just refusals — the row's discriminating pair:
registerEnableAttemptcalled once with the chat id) and reaches the lookup;1964(a fat-fingered first group),abc123(legacy), the joined code with one extra character, and the joined code one character short;Ledger
Baseline committed first (
b0f80a53), each arm reverted withgit checkout --, all green before and after:registerEnableAttempt.toLowerCase()dropped/[0-9a-f]{32}/)No survivors.
Verification
telegram.webhook.connectCode.test.js: 15/15;telegram.webhook.test.js+telegramConnectCode.test.js: 3 suites / 45 tests, exit 0, on Node 22.routes/webhooks/telegram.ts0 errors (its 4max-lenwarnings are pre-existing, not on the changed lines). Both touched.jstest files at their exact baseline profiles, measured withnpx eslint --stdin --stdin-filenameagainst the same files atHEAD~1rather than assumed, because that corpus is not CI-gated.Follows #1931 (
912e6a15). Gate: Vera.