Skip to content

nntplib — unverified context

Improper Certificate Validation Using nntplib

The Python class nntplib.NNTP_SSL by default creates an SSL context that does not verify the server's certificate if the context parameter is unset or has a value of None. This means that an attacker can easily impersonate a legitimate server and fool your application into connecting to it.

If you use nntplib.NNTP_SSL or starttls without a context set, you are opening your application up to a number of security risks, including:

  • Machine-in-the-middle attacks
  • Session hijacking
  • Data theft

Example

nntplib_nntp_ssl_context_unset.py
1
2
3
4
5
6
7
8
import nntplib


s = nntplib.NNTP_SSL("news.gmane.io")
s.login("user", "password")
f = open("article.txt", "rb")
s.post(f)
s.quit()
Example Output
> precli tests/unit/rules/python/stdlib/nntplib/examples/nntplib_nntp_ssl_context_unset.py
⚠️  Warning on line 4 in tests/unit/rules/python/stdlib/nntplib/examples/nntplib_nntp_ssl_context_unset.py
PY024: Improper Certificate Validation
The 'nntplib.NNTP_SSL' function does not properly validate certificates when context is unset or None.

Remediation

Set the value of the context keyword argument to ssl.create_default_context() to ensure the connection is fully verified.

nntplib_nntp_ssl_context_unset.py
import nntplib
import ssl


s = nntplib.NNTP_SSL(
    "news.gmane.io",
    context=ssl.create_default_context(),
)
s.login("user", "password")
f = open("article.txt", "rb")
s.post(f)
s.quit()

Default Configuration

enabled = true
level = "warning"

See also

New in version 0.3.14