Skip to content

fix(telegram): refuse a malformed connect code before it spends an attempt (TASK-157) - #1932

Merged
lilyshen0722 merged 1 commit into
mainfrom
kai/task157-enable-shape-gate
Sep 27, 2026
Merged

lilyshen0722 merged 1 commit into
mainfrom
kai/task157-enable-shape-gate

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

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.

  1. A capitalised code was refused. Codes are minted lowercase (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.
  2. A typo spent one of five tries. registerEnableAttempt(chatId) ran before the code was looked up, and a minted code has a checkable shape — 32 hex characters — so /commonly-enable 1964 burned 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.

const CONNECT_CODE_SHAPE = /^[0-9a-f]{32}$/;          // mintConnectCode's only shape
...
if (!code) { /* usage, unchanged */ }
if (!CONNECT_CODE_SHAPE.test(code)) {                 // typo, so no attempt spent
  await telegramService.sendMessage(botToken, chatId,
    "That doesn't look like a connect code — copy it from Commonly.");
  return;
}
if (!registerEnableAttempt(chatId)) { /* too many attempts */ }
const integration = await Integration.findOne({ 'config.connectCode': code, ... });

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 is skipped under NODE_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 code passed 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 (findByIdAndUpdate not 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 expiry used abc123. Renamed to refuses a code with no expiry with 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 chat guessed with guess${i} — not hex — so the counter never moved. Fixtures are now well-formed wrong codes.
  • still refuses a spaced code that matches nothing used zzzz 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.
  • sibling telegram.webhook.test.js: both bind tests used abc123, 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:

  • a well-formed wrong code spends exactly 1 attempt (registerEnableAttempt called once with the chat id) and reaches the lookup;
  • four malformed shapes spend 0 and get the shape message, with no lookup: 1964 (a fat-fingered first group), abc123 (legacy), the joined code with one extra character, and the joined code one character short;
  • a capitalised, spaced code binds — the lookup carries the lowercased joined code;
  • the spaced displayed form from TASK-153 still binds, and one displayed command still spends one attempt.

Ledger

Baseline committed first (b0f80a53), each arm reverted with git checkout --, all green before and after:

mutation red
M1 shape check moved after registerEnableAttempt the four malformed arms (they spend an attempt and never see the shape message) — the mutation the row names
M2 .toLowerCase() dropped the capitalised witness alone
M3 shape check removed entirely the four malformed arms
M4 regex anchors dropped (/[0-9a-f]{32}/) the 33-character arm alone — the anchors are load-bearing, not decoration

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.
  • eslint: routes/webhooks/telegram.ts 0 errors (its 4 max-len warnings are pre-existing, not on the changed lines). Both touched .js test files at their exact baseline profiles, measured with npx eslint --stdin --stdin-filename against the same files at HEAD~1 rather than assumed, because that corpus is not CI-gated.

Follows #1931 (912e6a15). Gate: Vera.

…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.
@lilyshen0722
lilyshen0722 added this pull request to the merge queue Sep 27, 2026
Merged via the queue into main with commit c941626 Sep 27, 2026
14 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant