Commit Graph

2672 Commits

Author SHA1 Message Date
Igor Artamonov ccb349f3d2 obfuscated2: only XOR bytes actually delivered on Read
Go / Lint (old) (push) Failing after 4m40s
Go / Lint (latest) (push) Failing after 4m39s
Go / Lint (old) (pull_request) Failing after 4m41s
Go / Lint (latest) (pull_request) Failing after 4m40s
Read called XORKeyStream(b, b) — XOR-ing the entire caller buffer even
when the underlying transport returned fewer bytes. AES-CTR's keystream
position is then advanced by len(b), but the peer only consumed n
bytes' worth of keystream. After a single short read the two
keystreams diverge for the lifetime of the connection, every
subsequent MTProto message decrypts to garbage, and the engine fails
with "consume message: decrypt: msg_key is invalid".

The faketls layer makes short reads routine: each Read returns at most
one TLS Application record's payload, regardless of how big the caller
buffer is. So in practice the stream desynced almost immediately on
high-traffic clients (active supergroups, post-relogin catch-up) and
intermittently on quiet ones.

Match the upstream gotd/td fix and only XOR the n bytes that came out
of the transport. Add a regression test (chunkConn delivers ciphertext
in 7-byte chunks; client reads through Obfuscated2.Read with a 128-byte
buffer; plaintext must round-trip).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 12:33:44 +03:00
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
Igor Artamonov b00e2d8955 connector: hex-decode mtproxy secret
Go / Lint (old) (push) Failing after 5m14s
Go / Lint (latest) (push) Failing after 5m19s
Go / Lint (old) (pull_request) Failing after 5m14s
Go / Lint (latest) (pull_request) Failing after 4m40s
dcs.MTProxy expects raw secret bytes. Carrying them verbatim through a
YAML string field is impossible: real secrets contain bytes >= 0x80
(faketls starts with 0xee, secured with 0xdd) which cannot survive a
unicode string round-trip, so the value reached the bridge corrupted or
empty (gotd then logged "invalid secret").

Accept the standard hex form printed by mtg/MTProxy tooling
(e.g. "ee" + 16-byte secret + cloak domain hex) and decode it before
handing the bytes to gotd.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 10:14:58 +03:00
Tulir Asokan e3bb26aee1 handlematrix: allow bridging cached custom emoji reactions with any scheme 2026-04-30 16:55:03 +03:00
Tulir Asokan 7c2c72bbde imagepack: implement listing interface 2026-04-30 15:49:10 +03:00
Tulir Asokan 2ffbde7448 .github: add another item to bug report template 2026-04-30 13:23:26 +03:00
Tulir Asokan 2a0da7801a imagepack: move emoji shortcodes to go-util 2026-04-30 12:24:08 +03:00
Tulir Asokan eaf387abfe imagepack: switch to bridgev2 API for importing 2026-04-29 18:01:36 +03:00
Tulir Asokan 64d80c3d1d imagepack: populate cache when importing pack 2026-04-29 16:18:52 +03:00
Tulir Asokan c78b1abd2d imagepack: use emoji shortcode as fallback when importing packs 2026-04-29 14:51:38 +03:00
Tulir Asokan 12f900a7bd dependencies: update mautrix-go 2026-04-29 09:10:02 +03:00
Tulir Asokan cdb77f938a tomatrix: include external_url field in messages 2026-04-28 22:01:54 +03:00
Tulir Asokan 5a1a478992 matrixfmt: convert matrix.to links in other direction too 2026-04-28 21:46:07 +03:00
Tulir Asokan d2a06ebbbe capabilities: mark lottie and webm as allowed sticker formats 2026-04-28 16:09:13 +03:00
Tulir Asokan e6243d8935 imagepack: switch to new shared metadata field 2026-04-27 20:24:10 +03:00
Tulir Asokan 9e1c42a992 matrixfmt: fix trimming all-space entity string 2026-04-27 20:24:10 +03:00
Tulir Asokan 6eacf38d74 tomatrix: use extra field in info for custom fields 2026-04-27 20:24:10 +03:00
Gerardo Rodriguez 65fcf712d3 client: treat pool.ErrConnDead as transient in onPing (#1066) 2026-04-24 13:58:43 +03:00
Tulir Asokan 8512cfe6a6 commands/imagepack: include pack metadata in sticker info 2026-04-23 14:26:52 +03:00
Tulir Asokan 7a6d1bf17a dependencies: update mautrix-go 2026-04-20 23:22:06 +03:00
Tulir Asokan 18f831553d changelog: update 2026-04-20 16:51:51 +03:00
Tulir Asokan dce0c4dbe1 handletelegram: add support for updateBotMessageReaction
Fixes #1064
2026-04-19 17:30:20 +03:00
Tulir Asokan ac2a2c2980 legacymigrate: fix mx_room_state migration on sqlite 2026-04-16 23:11:15 +03:00
Tulir Asokan b9f0962881 Bump version to v26.04 v0.2604.0 2026-04-16 16:45:33 +03:00
lavacat d7864fcd3a client: add initial proxy support (#1062) 2026-04-16 13:14:29 +03:00
Tulir Asokan 0f0b21b22c dependencies: update mautrix-go 2026-04-15 15:32:24 +03:00
Tulir Asokan 1dd11f1086 dependencies: update mautrix-go 2026-04-14 20:09:17 +03:00
Tulir Asokan 3f155672a7 login: always set update handler 2026-04-14 17:26:33 +03:00
Tulir Asokan b761f04621 dependencies: update mautrix-go 2026-04-14 14:39:13 +03:00
Tulir Asokan 95db7a6d0d tomatrix: fix default file names 2026-04-13 16:48:19 +03:00
Tulir Asokan 8b3707b0ee handlematrix: redact previous sponsored message when sending new one 2026-04-13 12:24:02 +03:00
Tulir Asokan 4d46c5ee7c tomatrix: use m.image for bridging document images 2026-04-12 01:03:06 +03:00
Tulir Asokan 009ce8c0d3 handlematrix: remove unnecessary nil checks 2026-04-11 19:58:39 +03:00
Tulir Asokan a06b7d607d handlematrix: add video document attribute 2026-04-11 19:56:45 +03:00
Tulir Asokan 659b0f82ae dependencies: update mautrix-go 2026-04-10 23:14:46 +03:00
Tulir Asokan 53dec19878 login: increase buffer for QR renewal 2026-04-10 22:58:16 +03:00
Tulir Asokan a5b1927acb handletelegram: don't sync empty reactions on new messages 2026-04-10 20:33:31 +03:00
Tulir Asokan 0988de1267 tomatrix: consistently add extensions for all files 2026-04-10 20:19:16 +03:00
Tulir Asokan 5c1975808a tomatrix: add extension to unnamed documents from telegram 2026-04-10 20:01:30 +03:00
Tulir Asokan 2b79d842a4 dependencies: update mautrix-go 2026-04-10 20:01:17 +03:00
Tulir Asokan 6e08539d57 dependencies: update mautrix-go 2026-04-10 17:15:49 +03:00
Tulir Asokan 634cec5ba9 tomatrix: avoid multipart messages 2026-04-10 14:43:37 +03:00
Tulir Asokan 506e13f6b8 commands: remove extra dots 2026-04-10 13:21:29 +03:00
Tulir Asokan e55e596d68 commands/join: allow different invite hash lengths 2026-04-10 13:20:48 +03:00
Tulir Asokan 11495e6e7e client: fix handling mentions of non-logged-in users 2026-04-09 23:32:34 +03:00
Tulir Asokan 53574754be dependencies: update mautrix-go again 2026-04-09 22:21:06 +03:00
Tulir Asokan 3f725b37b9 dependencies: update mautrix-go 2026-04-09 21:55:08 +03:00
Tulir Asokan 2ad36b6366 legacymigrate: recreate mx_room_state on sqlite only 2026-04-09 13:15:47 +03:00