Commit Graph

63 Commits

Author SHA1 Message Date
Igor Artamonov 4768065e72 faketls: skip ChangeCipherSpec records on read
Go / Lint (old) (push) Failing after 4m40s
Go / Lint (latest) (push) Failing after 4m40s
Go / Lint (old) (pull_request) Failing after 4m42s
Go / Lint (latest) (pull_request) Failing after 4m39s
The Read path treated every TLS record's payload as application data
and wrote it into readBuf — including the 1-byte payload (0x01) of
ChangeCipherSpec records. mtg sends those records intermittently as a
TLS-compat keep-alive; once one arrived inside the data stream it
desynced the obfuscated2 CTR keystream by one byte. From that point
on every MTProto message decrypted to garbage and the engine failed
with "decrypt: msg_key is invalid", forcibly closed the connection,
and looped.

The Go switch cases for ChangeCipherSpec and Application were both
empty (no fallthrough, no continue), so control reached the
o.readBuf.Write(rec.Data) call below the switch for both — exactly
the wrong behaviour for CCS.

Reshape the loop so that:
  - ChangeCipherSpec records are silently dropped
  - Application records are written to readBuf and returned
  - Handshake / unsupported types still error out

This matches tdlib's TlsTransport (CCS is skipped at the TLS framing
layer and never reaches the MTProto decoder).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 12:17:27 +03:00
Igor Artamonov 64bf6bfe90 faketls: emit GREASE bytes and a real padding extension
Go / Lint (old) (push) Failing after 4m39s
Go / Lint (latest) (push) Failing after 4m39s
Go / Lint (old) (pull_request) Failing after 4m39s
Go / Lint (latest) (pull_request) Failing after 4m42s
The ClientHello builder used a closure G(n) that was supposed to insert
two random GREASE bytes (RFC 8701, 0x?A?A pattern) at known positions
but expanded the buffer by zero. Every grease slot was therefore
omitted, and the trailing padding extension was written as a bare ext
id 0x0015 followed by raw zeros — its length field was never set.

Concretely, the old output looked structurally invalid to mtg's faketls
validator: the cipher list was off by two, supported_groups declared a
list_length larger than its body, and what should have been the padding
extension parsed as a stream of empty server_name extensions. mtg
responded with a fatal TLS Alert (description 50, decode_error) and
shut the connection.

Fix:
- generate seven distinct GREASE bytes per ClientHello, with the
  tdlib constraint grease[3] != grease[4]
- thread an io.Reader through writeClientHello so generation is
  deterministic in tests and keyed off the FakeTLS rand source in prod
- replace the trailing zero-pad with a proper padding extension whose
  length field is computed so the ClientHello is exactly 517 bytes

Add a regression test (structure_test.go) that feeds the result to
crypto/tls.Server: it must not return decode_error / malformed /
syntax errors. The previous output failed this; the new output passes.

The TestTLS golden vector is regenerated for the new layout.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 11:15:03 +03:00
Igor Artamonov aab48f0dbe faketls: include real record type and peek bytes on handshake errors
Go / Lint (old) (push) Failing after 4m43s
Go / Lint (latest) (push) Failing after 4m40s
Go / Lint (old) (pull_request) Failing after 4m40s
Go / Lint (latest) (pull_request) Failing after 4m43s
The previous error path used errors.Wrap(err, "unexpected record type")
inside type-mismatch branches where err was already nil. With
go-faster/errors that produced a wrapError with no cause and no detail,
making the user-visible message "unexpected record type" useless for
diagnostics — there was no way to tell what mtg actually sent.

Switch to errors.Errorf with the actual received byte and a 32-byte
hex peek of the read buffer. Also wrap the read-error path with the
same peek so a partial response is visible.

This is a diagnostic-only change; the parser semantics are unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 10:51:50 +03:00
Tulir Asokan 53dec19878 login: increase buffer for QR renewal 2026-04-10 22:58:16 +03:00
Tulir Asokan 60e3cf9c01 gotd/tgerr: reduce default flood wait max duration 2026-04-08 00:41:22 +03:00
Tulir Asokan 770b3b8d8c gotd: update to layer 224 2026-04-01 21:08:49 +03:00
Tulir Asokan 4a538f77ef gotd: log download response length 2026-03-29 13:11:33 +03:00
Tulir Asokan b43adb6bab gotd: update readme
[skip ci]
2026-03-28 16:58:28 +02:00
Tulir Asokan abae7b2854 gotd: assume any response is an ack 2026-03-28 16:50:51 +02:00
Tulir Asokan 472b9df44c gotd: fix infinite loop if server keeps replying with timeout to download request 2026-03-28 16:50:51 +02:00
Tulir Asokan bfe5999951 chatsync: merge post-login and takeout syncs and refactor everything 2026-03-19 13:13:01 +02:00
Tulir Asokan b1b5745033 gotd: add max duration and log for flood wait 2026-03-19 01:36:24 +02:00
Tulir Asokan 0e45edd1f4 gotd: always set field in logger 2026-03-04 01:59:56 +02:00
Tulir Asokan 7fb4539885 gotd: don't log uploaded bytes 2026-03-03 18:13:32 +02:00
Tulir Asokan 67adededff gotd/message: fix generators and update entity utilities 2026-03-03 15:16:44 +02:00
Tulir Asokan e5914196c5 gotd: update to layer 223 2026-03-03 15:13:10 +02:00
Tulir Asokan 189dbdfc52 gotd: move update dispatcher out of generator 2026-03-03 15:09:05 +02:00
Tulir Asokan 7738fc21f5 handletelegram,gotd: add missing log context 2026-03-03 14:34:02 +02:00
Tulir Asokan 4511c82cb0 gotd: only update server time offset once 2026-03-03 13:16:25 +02:00
Tulir Asokan 6af986ded5 gotd: add time synchronization 2026-02-26 18:24:51 +02:00
Tulir Asokan 52b2373528 dependencies: update 2026-02-16 15:41:08 +02:00
Tulir Asokan e597eace68 login: allow retrying phone codes and 2fa passwords (#131) 2026-02-10 16:49:49 +02:00
Tulir Asokan 78ba8e4d45 gotd: add missing getDifference retries 2026-01-07 16:11:41 +02:00
Tulir Asokan cac1f5acde gotd: retry auth transfers on AUTH_BYTES_INVALID error 2025-12-29 23:15:40 +02:00
Tulir Asokan 56fe704934 gotd: don't log file download responses 2025-12-29 23:15:40 +02:00
Tulir Asokan 7573e3d5a7 gotd: return fatal errors from all getDifference calls 2025-12-18 17:32:51 +02:00
Tulir Asokan a887f26023 gotd: retry non-fatal errors in getDifference calls 2025-12-18 16:48:03 +02:00
Tulir Asokan ced0a2d067 gotd: don't emit duplicate updates for channels 2025-12-18 16:36:23 +02:00
Tulir Asokan 20446d0d7d gotd: fix logging response payload 2025-12-16 17:33:43 +02:00
Tulir Asokan 1a2fd67ee9 gotd: remove extra wrapping in check participant error 2025-12-12 17:26:41 +02:00
Tulir Asokan d6a8e6a648 gotd: don't return run context error from channel state 2025-12-12 16:16:06 +02:00
Tulir Asokan 095bd65d51 gotd: add extra cache for left channels 2025-12-12 16:15:49 +02:00
Tulir Asokan cd9970055f gotd: fix channel membership check not doing anything 2025-12-12 16:15:26 +02:00
Tulir Asokan 3663f91c8a gotd: add more error wrapping around update loop 2025-12-12 16:04:40 +02:00
Tulir Asokan 0c3749a2ca gotd: don't start getDifference for left channels 2025-12-12 15:57:14 +02:00
Tulir Asokan ba4dd48d5a gotd: ensure user is member of channels before starting getDifference loop 2025-12-12 15:45:39 +02:00
Tulir Asokan 4775e67476 client: adjust start/stop logs 2025-12-11 15:29:42 +02:00
Tulir Asokan 43b230148b client: don't drop errors from client.Run() 2025-12-11 15:01:12 +02:00
Tulir Asokan d03260c4a7 gotd/updates: initialize channel state runctx immediately 2025-12-11 14:55:43 +02:00
Tulir Asokan de2e87ed52 client,gotd: remove unnecessary dispatcher wrapper 2025-12-11 14:07:48 +02:00
Tulir Asokan 581ba79c84 handletelegram,gotd: stop get difference polling after leaving channel 2025-12-11 13:56:48 +02:00
Tulir Asokan f80d6de818 gotd: use constants for error strings 2025-12-10 19:39:56 +02:00
Tulir Asokan 69fcbd30ce gotd: don't stop connection on channel error 2025-12-10 19:17:50 +02:00
Tulir Asokan 0e3b1b63a9 gotd/updates: stop listening to channel on ChannelForbidden/Invalid 2025-12-10 19:14:08 +02:00
Tulir Asokan 7f13284b59 gotd: remove redundant closures and improve logs on disconnect 2025-12-10 18:34:07 +02:00
Tulir Asokan 4268ee9909 gotd/transport: add default read/write deadlines 2025-12-10 18:33:37 +02:00
Tulir Asokan 14b3b1fed7 handletelegram: adjust some message handling code 2025-12-06 01:02:55 +02:00
Tulir Asokan 2cac8f8b4a client,gotd: refactor connection event handling
This might cause regressions if the onSession handler was load bearing
2025-12-04 14:53:35 +02:00
Tulir Asokan c83a361c0b gotd: reduce unnecessary debug logs 2025-12-04 14:44:13 +02:00
Tulir Asokan 04a10f361a gotd: skip broken manager test 2025-12-03 17:11:20 +02:00