Skip to content

Write safety: delete only ids in the named chat, strict E.164 for typed phones, date entities, safe kick - #69

Open
erfnzdeh wants to merge 4 commits into
mainfrom
fix/write-safety
Open

erfnzdeh wants to merge 4 commits into
mainfrom
fix/write-safety

Conversation

@erfnzdeh

Copy link
Copy Markdown
Collaborator

Closes #59

What was wrong

Line numbers at 2f303da.

  • message delete could delete messages in other chats. tlgr/ops/message.py:1637 called client.delete_messages(peer, ids, revoke=revoke). For a private chat or basic group Telethon sends messages.deleteMessages(id=[...]) with no peer, and those ids are account-wide, so a wrong id deleted a message in whatever chat held it, for both sides (revoke is the default). Ranges (message delete @alice 100-120) made it likely: in a DM most ids in a range belong to other DMs. tlgr/ops/call.py:1591 (call log delete) had the same shape.
  • Typed phones got a guessed country code. e164() (tlgr/ops/contact.py:133) prefixed + to any digits, so 9123456789 (an Iranian mobile without its trunk 0) became +91 India and 2125551234 became +212 Morocco. It was used for user input in resolve phone (tlgr/ops/resolve.py:331), contact add --phone (contact.py:776), contact remove --phone (contact.py:950) and vCard/CSV import/sync (contact.py:411, :426). +phone peer refs (tlgr/models/peer.py:341) accepted +0....
  • Date entities were dropped. tl_entities (tlgr/ops/_send.py:189) built MessageEntityFormattedDate(offset, length) without the required date; the TypeError was swallowed at :217. message_entities (tlgr/ops/_serialize.py:342) dropped the date and flags on read, and four places that copy a MessageEntity field by field would have dropped them again.
  • chat member remove could lift a ban. tlgr/ops/chat_member.py:573 banned then unbanned (:605) with no membership check, so kicking an already banned user unbanned them. There was no self guard, a cancel between the two calls could leave the ban in place, and a failed unban surfaced as a bare RPC error.

Changes

  • ops/_common.py: ids_outside_chat() fetches the ids back through the peer (get_messages returns None for an id from another chat, 100 per call, paced by the bulk bucket) and returns the ones that are not messages of the chat. Channels and supergroups are skipped. message delete refuses typed ids that are not in the chat (NOT_FOUND naming them, nothing deleted). Ids that only came from a range are skipped and listed in a new skipped field, with a warning. call log delete checks every chat before deleting anything. Scheduled deletes are per peer and unchanged.
  • models/peer.py: parse_e164() for typed numbers. It needs a leading +, a country code that does not start with 0, and 7 to 15 digits. Spaces, dots, brackets, dashes, RTL direction marks and Persian/Arabic-Indic digits are tolerated. 0912..., 00 98..., 8 999... and bare digits are refused, and the message says how to write the number. Used by resolve phone, contact add/remove --phone (remove checks every number before deleting anything) and +phone peer refs. e164() stays, documented as the formatter for phones the server returns (and for contact cards inside messages).
  • Import/sync: rows whose number is not E.164 are skipped and returned in a new rejected list (line, value, name, reason) with a warning; the rest still import. A file with no usable rows is a USAGE error that names the rejected lines. vCard 4 tel: values are accepted. contact sync --delete-missing --apply refuses when any row was rejected, because a skipped 0912... row may be exactly the +98912... contact the diff would delete (deleteByPhones is irreversible).
  • MessageEntity gains date, date_unix and the six display flags (relative, short_time, long_time, short_date, long_date, day_of_week). These match Telethon 1.44's MessageEntityFormattedDate(offset, length, date, ...). On send, date_unix or date (anything parse_dt takes) is passed. A formatted_date without a date is a USAGE error, not a silent drop. On read, core.text.entity_from_tl keeps them, and _serialize.message_entities now uses it. The copy sites (split_text, spoiler shifting, _recount) use msgspec.structs.replace, so a future field is not lost either.
  • chat member remove: every named user is checked against this account before anyone is removed (refused with "run tlgr chat leave"). In a channel or supergroup the member is looked up first: someone already banned stays banned and someone not in the chat is left alone (removed: false, already: true). --purge/--messages/--report still apply. Ban plus unban run under asyncio.shield. A failed unban raises under the cause's code (for example RATE_LIMITED with wait_seconds), says the user is now BANNED, lists who was removed before, and hints the chat member unban command.
  • Test fake: delete_messages now models Telegram's account-wide id space for non-channel peers, and call log entries also live in the DM history.

Tests

Test Reproduces Fails on main?
test_ops_message::test_delete_refuses_an_id_from_another_private_chat id 201 "in @alice" deletes Bob's message yes
test_ops_message::test_delete_in_a_basic_group_checks_ids_too basic group delete reaching a DM message yes
test_ops_message::test_a_range_skips_ids_that_belong_to_other_chats 102-105 where 104 is Bob's yes
test_ops_message::test_a_channel_delete_is_not_checked channels keep the old path no (guard)
test_ops_call::test_a_row_naming_another_chats_message_is_refused call log delete with a foreign row yes
test_models::TestParseE164 (17 cases) accepted and refused forms, incl. the +91/+212 cases yes
test_models::test_a_phone_ref_with_a_zero_country_code_is_refused +0912... peer ref yes
test_ops_contacts::test_a_phone_without_a_plus_is_refused_before_any_lookup resolve phone 912 345 6789 looked up as +91 yes
test_ops_contacts::test_a_phone_without_a_country_code_is_refused_not_guessed contact add --phone 2125551234 imports +212 yes
test_ops_contacts::test_a_local_phone_is_refused_before_anything_is_deleted contact remove with a local number yes
test_ops_contacts::test_a_local_number_is_reported_and_the_rest_imported CSV with one local row yes
test_ops_contacts::test_a_vcard_with_only_a_local_number_is_rejected vCard with 0912..., plus a vCard 4 tel: value yes
test_ops_contacts::test_a_file_of_only_local_numbers_is_a_usage_error nothing usable in the file yes
test_ops_contacts::test_sync_refuses_to_delete_when_a_row_was_rejected --delete-missing --apply with an unreadable row yes
test_ops_message::test_a_formatted_date_entity_is_sent_with_its_date date chip dropped on send; checks bytes(entity) serialises yes
test_ops_message::test_a_formatted_date_entity_without_a_date_is_a_usage_error silent drop of a chip with no date yes
test_ops_message::test_a_received_date_chip_keeps_its_date read side drops date and flags yes
test_text::test_a_date_chip_survives_json_and_splits field-by-field copies in split_text yes
test_ops_chat_admin::test_kicking_an_already_banned_user_does_not_unban_them kick lifts an existing ban yes
test_ops_chat_admin::test_kicking_a_non_member_changes_nothing ban+unban sent for a non-member yes
test_ops_chat_admin::test_kicking_yourself_is_refused_before_anyone_is_removed me in the list, Bob kicked first yes
test_ops_chat_admin::test_kicking_yourself_by_username_is_refused_too own account by @username yes
test_ops_chat_admin::test_a_failed_unban_says_the_user_is_now_banned flood on the unban step yes

Not changed, and why

  • message thread disable (message.py:1396) deletes in the linked discussion group, which is always a supergroup, using an id the server returned. No cross-chat risk, so it is not checked.
  • Contact cards inside messages (contact add --from-message) keep the lenient formatter: the number comes from the server, not from the caller.
  • Kicking a restricted member who is still in the chat clears their restriction, the same as before and as telegram-mcp does. Restoring the old mask after the kick is possible but I could not verify how the server treats restrictions on a non-member.
  • Basic-group kicks (messages.deleteChatUser) are a true removal with no ban step, so they are unchanged.
  • I did not add the --date-chip convenience flag from the report. --entities with a formatted_date now works.

Not verified

  • Against real Telegram: the delete guard relies on get_messages(peer, ids=...) returning None for an id from another chat. Telethon 1.44's _IDsIter does that by comparing peer_id. A message deleted in the meantime also comes back None and is refused the same way.
  • That the server accepts every combination of the date flags (it has ENTITY_DATE_FORMAT_INVALID for bad ones). The flags are passed through as given.

make check: pass (lint, typecheck, 13689 tests passed, docs, parity; coverage 84%)

In private chats and basic groups messages.deleteMessages takes bare ids
and Telethon drops the peer, so a wrong id or a range in a DM deleted
messages in other chats, for both sides. Fetch the ids back through the
peer first: a typed id that is not a message of the chat is refused and
nothing is deleted, ids that only came from a range are skipped and
listed in skipped. call log delete checks every chat before deleting
anything. Channels and supergroups are unchanged.
…ountry

e164() prefixed + to any digits, so an Iranian 9123456789 typed without
its trunk 0 became +91 (India) and a US 2125551234 became +212 (Morocco),
and the lookup or import reached a stranger. Add a strict parse_e164 for
user input (leading +, country code not starting with 0, 7 to 15 digits,
separators, direction marks and Persian digits tolerated) and use it in
resolve phone, contact add/remove --phone and +phone peer refs. vCard and
CSV import and sync skip rows they cannot read and list them in
rejected; sync --delete-missing --apply refuses when a row was rejected.
e164() stays for phones the server returns.
tl_entities built MessageEntityFormattedDate without its required date,
the constructor raised TypeError and the entity was dropped silently, and
the read side dropped the date and flags too. MessageEntity gains date,
date_unix and the six display flags; tl_entities passes them (a date chip
without a date is now a USAGE error, not a silent drop), received
entities keep them, and every place that copies an entity uses
structs.replace so new fields are not lost when text is split or shifted.
… failed unban

A kick is ban then unban, and the unban cleared any ban already in
place, so kicking a banned user let them back in. In a channel or
supergroup the member is now looked up first: an already banned user
stays banned and a non-member is left alone (already: true). Naming
this account is refused before anyone is removed. The ban and unban run
under asyncio.shield, and a failed unban raises an error under the
cause's code saying the user is now banned, with the unban command.

This branch has not been deployed

No deployments
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.

message delete can remove messages in other chats; typed phones get the wrong country code; date entities dropped

1 participant