Session Security
Status: encrypted durable default and atomic domain storage are implemented; explicit key-rotation tooling remains future work.
Storage Choices
Section titled “Storage Choices”InMemorySessionStorage stores a copied session mapping in process memory and is intended for tests, throwaway clients and future ephemeral workflows.
EncryptedSQLiteSessionStorage is the durable default for local sessions. It stores independently encrypted auth, peers, update_state and metadata domains for SessionRecord values or one encrypted payload domain for arbitrary mappings and fails closed unless a key is provided through the constructor or MINIPROTO_SESSION_KEY.
Default Client Storage And Migration
Section titled “Default Client Storage And Migration”When ClientConfig.session_storage is omitted, Client constructs EncryptedSQLiteSessionStorage for the relative path miniproto.session.sqlite. It validates the constructor or MINIPROTO_SESSION_KEY key before any network connection and does not create an empty SQLite file during construction. The file is created only when normal session storage work first needs it.
This is an Alpha breaking change for callers that previously relied on Client(ClientConfig(api_id=..., api_hash=...)) being ephemeral. Set MINIPROTO_SESSION_KEY through a secret manager before creating the client and pass a unique absolute session_path for every account or deployment. Two clients that use the default relative path from the same working directory share one session file and must not represent different accounts.
An explicit session_storage always takes precedence over session_path. Use InMemorySessionStorage() explicitly for a test or throwaway workflow:
from miniproto import Client, ClientConfig, InMemorySessionStorage
client = Client(ClientConfig(api_id=12345, api_hash="...", session_storage=InMemorySessionStorage()))Session Keys
Section titled “Session Keys”Provide at least 16 bytes of key material; 32 bytes or more is recommended. The key may be passed as bytes or str:
from miniproto import Client, ClientConfig, EncryptedSQLiteSessionStorage
storage = EncryptedSQLiteSessionStorage("client.session.sqlite", key="use-a-long-random-secret-value")client = Client(ClientConfig(api_id=12345, api_hash="...", session_storage=storage))For deployments, load MINIPROTO_SESSION_KEY from the platform secret manager instead of committing it to configuration files:
$env:MINIPROTO_SESSION_KEY = "use-a-long-random-secret-value"Encrypted Envelope
Section titled “Encrypted Envelope”An application mapping can use the reserved __miniproto_type__ key. These mappings use an escaped payload representation that requires an updated reader.
Each SQLite database (including named siblings) has a random 32-byte salt. PBKDF2-HMAC-SHA256 derives key material from the supplied secret and that salt. Separate subkeys verify the database key and encrypt its rows. Each changed domain receives a fresh 12-byte AES-256-GCM nonce. The authentication tag covers the version and domain name along with the ciphertext. Unchanged domain rows retain their ciphertext. Copy or back up the entire SQLite database to preserve its salt and encrypted rows.
Version-1 single-record and split-domain archives and version-2 domain archives remain readable. A successful save or mutation migrates the archive in one transaction. After migration, readers accept only version-3 envelopes. Restoring a complete backup also restores its session state and update cursors.
Reads and mutations raise SessionEnvelopeError when authentication fails. An explicit save() with a verified database key can replace damaged version-3 rows. Use clear() to discard an unreadable session.
On POSIX systems the backend creates the main SQLite file with owner-only 0600 permissions and reapplies that mode before writing an existing file. Keep the encryption key and backups protected, and restrict access to the service account on every platform.
Atomic Mutations
Section titled “Atomic Mutations”Use await storage.mutate(transform) for every read-modify-write operation. The transform is synchronous, receives an isolated Mapping[str, Any] | None and returns a SessionRecord, arbitrary mapping or None to clear the record. The returned committed mapping is also isolated. Do not await, perform network I/O, log session material or retain the provided mapping from inside the transform.
Built-in backends serialize load, save, mutate, clear and close operations. SQLite holds one backend threading.RLock across BEGIN IMMEDIATE, legacy/domain loading, decryption, the transform, canonical plaintext comparison, changed-domain encryption and writes and commit. Any transform, encryption or SQLite failure rolls the entire transaction back. A clear that wins the lock before a queued mutation makes that mutation observe None; close is idempotent and later load, save, mutate and clear calls fail with SessionStorageError.
domain_revisions() returns the local change counters for auth, peers, update_state, metadata and payload. Saving a loaded record mapping or returning it from a mutation keeps its four original domains while the top-level field set is intact. Editing only metadata advances only its counter. An arbitrary mapping saved to empty storage uses the single payload domain.
Loads, unchanged writes, failed transactions and physical migration of unchanged legacy data do not advance counters.
Custom Storage Migration
Section titled “Custom Storage Migration”The library is Alpha and SessionStorage requires both mutate() and domain_revisions(). Custom backends must provide the same isolation, committed-result, revision, clear/close ordering and post-close failure semantics as the built-in backends. A custom mutate() implemented as an unlocked load() followed by save() is not conformant because concurrent tasks can erase unrelated auth, peer, cursor, salt or media-DC changes. Keep transforms synchronous and fetch all network data before entering the mutation.
Persisted Data
Section titled “Persisted Data”SessionRecord stores authorization keys, data-center options, account identity, update state, peer information and metadata.
Portable Session Strings
Section titled “Portable Session Strings”from miniproto import Client, ClientConfig
async def export_for_backup(client: Client, passphrase: str) -> str: session = await client.export_session_string(format="miniproto", passphrase=passphrase) return str(session)
async def import_backup(config: ClientConfig, session: str, passphrase: str) -> None: client = Client(config) try: await client.import_session_string(session, format="auto", passphrase=passphrase) finally: await client.disconnect()export_session_string(record_or_mapping, ...) and import_session_string(value, ...) provide synchronous conversion for a typed SessionRecord or an already loaded record mapping. Generic storage is asynchronous, so use await Client.export_session_string(...) to export the client’s current storage and await Client.import_session_string(...) to import into it:
portable = await client.export_session_string(passphrase="load this from a secret manager")
replacement = Client(ClientConfig(api_id=12345, api_hash="...", session_storage=InMemorySessionStorage()))await replacement.import_session_string(portable, passphrase="load this from a secret manager")Import requires a disconnected client and serializes with connect/disconnect. It refuses any existing storage value unless replace=True is explicit, before draining old account resources. Replacement then closes retained senders, drops runtime key/salt state and account-derived limits, and resets a failed startup’s update cursor and queued events while retaining subscriber objects. The old runtime bot token is discarded. A successful disconnect() permanently closes storage, create a new client afterwards. Explicit range caches remain caller-owned, clear them under the media ownership contract. SessionString is a str subclass and its repr plus miniproto’s redaction helpers hide its contents, but direct string access, encoding, serialization, process memory and a deliberately printed value still contain the credential.
Native strings use the mp1: prefix and canonical unpadded URL-safe base64. The bearer mode preserves the complete typed record and adds a SHA-256 corruption checksum, but it is plaintext and provides no confidentiality or attacker-resistant authenticity. The protected mode derives a 256-bit key from a non-empty passphrase with Scrypt (n=2**17, r=8, p=1), uses a fresh 16-byte salt and 12-byte nonce, encrypts with AES-256-GCM and authenticates the fixed header and KDF/cipher parameters as associated data. Imports also accept the original scrypt cost (n=2**14). A protected string never embeds its passphrase or derived key. Both modes enforce fixed input/payload/collection limits, strict base64, exact lengths, known flags and algorithms, duplicate-field rejection and authenticated or checksummed decoding before constructing a record. Wrong passphrases and tampering fail with non-secret SessionEnvelopeError messages.
New native exports include each peer alias’s observation times. Earlier mp1: strings remain readable, aliases without those times require a fresh username resolution while account credentials and numeric peer references remain available. This adds peer metadata without changing the native envelope or session-record version.
All session strings are bearer credentials. Anyone who obtains one with its passphrase, when applicable, can operate the authorized Telegram account. Store the string and passphrase separately where practical, use the protected native format for portable backups, never pass either through command-line arguments or logs and rotate the underlying Telegram authorization after suspected disclosure. Protection only covers the serialized value at rest; it cannot protect a compromised process that imports it.
Telethon v1 compatibility
Section titled “Telethon v1 compatibility”The telethon format implements Telethon 1.44’s version-1 IPv4 and IPv6 layouts from the tagged source without importing Telethon at runtime. Import and export preserve only active DC ID, server IP, port and the 256-byte auth key. Telethon strings do not contain API ID/hash, account identity, update cursors, peers, salts, media-DC authorizations or miniproto metadata. Configure the original API credentials yourself. Imported records are marked for one updates.getState bootstrap when the client next connects. Telethon v2 removed this format and is not a compatibility target.
Pyrogram compatibility
Section titled “Pyrogram compatibility”The pyrogram format implements Pyrogram 2.0’s current >BI?256sQ? layout and imports both legacy >B?256sI? and >B?256sQ? layouts from the pinned upstream storage definitions without importing Pyrogram at runtime. Import preserves DC ID, test-mode flag, auth key, user ID and bot/user kind; the modern layout also preserves API ID. Pyrogram strings contain no server endpoint, API hash, update cursor, peer cache, salts, media-DC authorizations or miniproto metadata. Miniproto uses its normal production/test DC endpoint configuration and performs one updates.getState bootstrap after connection.
Export emits only the modern Pyrogram layout and requires a 256-byte auth key, account identity, positive API ID and explicit test-mode value. Client.export_session_string(format="pyrogram") supplies the API ID and test mode from ClientConfig; record-level export uses explicitly supplied values or compatible metadata from a modern Pyrogram import. Client import validates an encoded API ID and test-mode value against ClientConfig and a configured bot token against an imported user account. A deliberate migration with known mismatches must pass allow_mismatch=True; the override does not rewrite the imported credential.
Redaction
Section titled “Redaction”Secret-like values are redacted by key name before rendering helper output or RPC errors. The protected names include api_hash, phone, auth_key, session_key, bot_token, password, proxy, raw_session and case-insensitive variants. Generated reprs for public configuration, session models and auth-key exchange results also omit secret-bearing fields, including proxy configuration, storage objects, API hashes, bot tokens, auth-key bytes, DC secrets, phone numbers, raw peer payloads and session metadata. Do not log raw session mappings directly; use the public error and redaction helpers when debugging.
Bare code fields are also hidden for auth.recoverPassword, auth.checkRecoveryPassword, account.confirmPasswordEmail and emailVerificationCode in nested redacted rendering. Ordinary RPC status codes and unrelated objects with a code field retain their diagnostic value.
Field-level repr omission only makes ordinary dataclass reprs safer. It does not redact direct attribute access, dataclasses.asdict(), session serialization, pickling or process memory; those paths intentionally retain the original values. Continue to use the redaction helpers for arbitrary mappings, generated raw requests and other debug output.
Operational Constraints
Section titled “Operational Constraints”Keep durable session files private to the service account. Rotate MINIPROTO_SESSION_KEY by creating a new authorized session or by adding a future explicit re-encryption command. Do not commit SQLite session files, environment files containing session keys, phone numbers, API hashes, bot tokens or generated auth material.