Skip to content

Commit 11700af

Browse files
authored
Merge pull request mjs#277 from growbots/default-ssl
Establish secure connections by default
2 parents f849e44 + 20b5078 commit 11700af

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

doc/src/releases.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
Changed
88
-------
9+
- Connections to servers use SSL/TLS by default (`ssl=True`)
910
- XXX Use built-in TLS when sensible.
1011
- Logs are now handled by the Python logging module. `debug` and `log_file`
1112
are not used anymore.

imapclient/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def get_config_defaults():
2828
return dict(
2929
username=getenv("username", None),
3030
password=getenv("password", None),
31-
ssl=False,
31+
ssl=True,
3232
ssl_check_hostname=True,
3333
ssl_verify_cert=True,
3434
ssl_ca_file=None,

imapclient/imapclient.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,17 @@ class IMAPClient(object):
8585
"""A connection to the IMAP server specified by *host* is made when
8686
this class is instantiated.
8787
88-
*port* defaults to 143, or 993 if *ssl* is ``True``.
88+
*port* defaults to 993, or 143 if *ssl* is ``False``.
8989
9090
If *use_uid* is ``True`` unique message UIDs be used for all calls
9191
that accept message ids (defaults to ``True``).
9292
93-
If *ssl* is ``True`` an SSL connection will be made (defaults to
94-
``False``).
93+
If *ssl* is ``True`` (the default) a secure connection will be made.
94+
Otherwise an insecure connection over plain text will be
95+
established.
9596
9697
If *ssl* is ``True`` the optional *ssl_context* argument can be
97-
used to provide a ``backports.ssl.SSLContext`` instance used to
98+
used to provide an ``ssl.SSLContext`` instance used to
9899
control SSL/TLS connection parameters. If this is not provided a
99100
sensible default context will be used.
100101
@@ -122,7 +123,7 @@ class IMAPClient(object):
122123
AbortError = imaplib.IMAP4.abort
123124
ReadOnlyError = imaplib.IMAP4.readonly
124125

125-
def __init__(self, host, port=None, use_uid=True, ssl=False, stream=False,
126+
def __init__(self, host, port=None, use_uid=True, ssl=True, stream=False,
126127
ssl_context=None, timeout=None):
127128
if stream:
128129
if port is not None:
@@ -132,6 +133,11 @@ def __init__(self, host, port=None, use_uid=True, ssl=False, stream=False,
132133
elif port is None:
133134
port = ssl and 993 or 143
134135

136+
if ssl and port == 143:
137+
logger.warning("Attempting to establish an encrypted connection "
138+
"to a port (143) often used for unencrypted "
139+
"connections")
140+
135141
self.host = host
136142
self.port = port
137143
self.ssl = ssl
@@ -146,7 +152,8 @@ def __init__(self, host, port=None, use_uid=True, ssl=False, stream=False,
146152
self._cached_capabilities = None
147153

148154
self._imap = self._create_IMAP4()
149-
logger.debug("Connected to host %s", self.host)
155+
logger.debug("Connected to host %s over %s", self.host,
156+
"SSL/TLS" if ssl else "plain text")
150157

151158
# Small hack to make imaplib log everything to its own logger
152159
imaplib_logger = IMAPlibLoggerAdapter(

tests/test_init.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_plain(self):
2525
fakeIMAP4 = Mock()
2626
self.imap4.IMAP4WithTimeout.return_value = fakeIMAP4
2727

28-
imap = IMAPClient('1.2.3.4', timeout=sentinel.timeout)
28+
imap = IMAPClient('1.2.3.4', ssl=False, timeout=sentinel.timeout)
2929

3030
self.assertEqual(imap._imap, fakeIMAP4)
3131
self.imap4.IMAP4WithTimeout.assert_called_with(
@@ -42,7 +42,7 @@ def test_SSL(self):
4242
fakeIMAP4_TLS = Mock()
4343
self.tls.IMAP4_TLS.return_value = fakeIMAP4_TLS
4444

45-
imap = IMAPClient('1.2.3.4', ssl=True, ssl_context=sentinel.context,
45+
imap = IMAPClient('1.2.3.4', ssl_context=sentinel.context,
4646
timeout=sentinel.timeout)
4747

4848
self.assertEqual(imap._imap, fakeIMAP4_TLS)
@@ -58,7 +58,7 @@ def test_SSL(self):
5858
def test_stream(self):
5959
self.imaplib.IMAP4_stream.return_value = sentinel.IMAP4_stream
6060

61-
imap = IMAPClient('command', stream=True)
61+
imap = IMAPClient('command', stream=True, ssl=False)
6262

6363
self.assertEqual(imap._imap, sentinel.IMAP4_stream)
6464
self.imaplib.IMAP4_stream.assert_called_with('command')

0 commit comments

Comments
 (0)