Script 'mail_helper' called by obssrc Hello community, here is the log from the commit of package python-pyotp for openSUSE:Factory checked in at 2026-12-03 13:59:26 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/python-pyotp (Old) and /work/SRC/openSUSE:Factory/.python-pyotp.new.1340718 (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "python-pyotp" Thu Dec 3 13:59:26 2026 rev:13 rq:1402670 version:2.12.4 Changes: -------- --- /work/SRC/openSUSE:Factory/python-pyotp/python-pyotp.changes 2026-11-27 19:48:42.667235257 +0100 +++ /work/SRC/openSUSE:Factory/.python-pyotp.new.1340718/python-pyotp.changes 2026-12-03 14:02:27.881343510 +0100 @@ -1,0 +2,12 @@ +Thu Dec 3 07:53:51 UTC 2026 - Martin Hauke <[email protected]> + +- Update to version 2.12.4 + * Emit InsecureSecretWarning for short OTP secrets (#213). + +------------------------------------------------------------------- +Wed Dec 2 19:56:35 UTC 2026 - Martin Hauke <[email protected]> + +- Update to version 2.12.3 + * Accept 80-bit OTP seeds and warn below 128 bits (#212). + +------------------------------------------------------------------- Old: ---- pyotp-2.12.2.tar.gz New: ---- pyotp-2.12.4.tar.gz ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ python-pyotp.spec ++++++ --- /var/tmp/diff_new_pack.IpkEFx/_old 2026-12-03 14:02:28.658376409 +0100 +++ /var/tmp/diff_new_pack.IpkEFx/_new 2026-12-03 14:02:28.660376494 +0100 @@ -18,7 +18,7 @@ %{?sle15_python_module_pythons} Name: python-pyotp -Version: 2.12.2 +Version: 2.12.4 Release: 0 Summary: Python One Time Password Library License: MIT ++++++ pyotp-2.12.2.tar.gz -> pyotp-2.12.4.tar.gz ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pyotp-2.12.2/Changes.rst new/pyotp-2.12.4/Changes.rst --- old/pyotp-2.12.2/Changes.rst 2020-02-02 01:00:00.000000000 +0100 +++ new/pyotp-2.12.4/Changes.rst 2020-02-02 01:00:00.000000000 +0100 @@ -1,3 +1,13 @@ +Changes for v2.12.4 (2026-12-02) +================================ + +- Emit InsecureSecretWarning for short OTP secrets (#213) + +Changes for v2.12.3 (2026-12-02) +================================ + +- Accept 80-bit OTP seeds and warn below 128 bits (#212) + Changes for v2.12.2 (2026-11-26) ================================ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pyotp-2.12.2/PKG-INFO new/pyotp-2.12.4/PKG-INFO --- old/pyotp-2.12.2/PKG-INFO 2020-02-02 01:00:00.000000000 +0100 +++ new/pyotp-2.12.4/PKG-INFO 2020-02-02 01:00:00.000000000 +0100 @@ -1,6 +1,6 @@ Metadata-Version: 2.5 Name: PyOTP -Version: 2.12.2 +Version: 2.12.4 Summary: Python One Time Password Library Project-URL: Homepage, https://github.com/pyauth/pyotp Project-URL: Documentation, https://github.com/pyauth/pyotp @@ -105,13 +105,13 @@ import pyotp - totp = pyotp.TOTP('base32secret32323232323232') + totp = pyotp.TOTP('base32secret23456723456723456723') totp.now() # Current OTP; changes with the clock. # A fixed Unix timestamp makes these example outputs reproducible. - totp.at(1234567890) # => '763850' - totp.verify('763850', for_time=1234567890) # => True - totp.verify('763850', for_time=1234567920) # => False + totp.at(1234567890) # => '756795' + totp.verify('756795', for_time=1234567890) # => True + totp.verify('756795', for_time=1234567920) # => False Preventing replay attacks ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -164,14 +164,14 @@ import pyotp - hotp = pyotp.HOTP('base32secret32323232323232') - hotp.at(0) # => '995369' - hotp.at(1) # => '511453' - hotp.at(1401) # => '666948' + hotp = pyotp.HOTP('base32secret23456723456723456723') + hotp.at(0) # => '023812' + hotp.at(1) # => '219284' + hotp.at(1401) # => '375068' # OTP verified with a counter - hotp.verify('666948', counter=1401) # => True - hotp.verify('666948', counter=1402) # => False + hotp.verify('375068', counter=1401) # => True + hotp.verify('375068', counter=1402) # => False Generating a Secret Key ~~~~~~~~~~~~~~~~~~~~~~~ @@ -196,13 +196,13 @@ .. code:: python - pyotp.totp.TOTP('JBSWY3DPEHPK3PXPJBSWY3DP').provisioning_uri(name='[email protected]', issuer_name='Secure App') + pyotp.totp.TOTP('JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP').provisioning_uri(name='[email protected]', issuer_name='Secure App') - >>> 'otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DP&issuer=Secure%20App' + >>> 'otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP&issuer=Secure%20App' - pyotp.hotp.HOTP('JBSWY3DPEHPK3PXPJBSWY3DP').provisioning_uri(name="[email protected]", issuer_name="Secure App", initial_count=0) + pyotp.hotp.HOTP('JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP').provisioning_uri(name="[email protected]", issuer_name="Secure App", initial_count=0) - >>> 'otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DP&issuer=Secure%20App&counter=0' + >>> 'otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP&issuer=Secure%20App&counter=0' This URL can then be rendered as a QR Code (for example, using https://github.com/soldair/node-qrcode) which can then be scanned and added to the users list of OTP credentials. @@ -211,11 +211,11 @@ .. code:: python - pyotp.parse_uri('otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DP&issuer=Secure%20App') + pyotp.parse_uri('otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP&issuer=Secure%20App') >>> <pyotp.totp.TOTP object at 0xFFFFFFFF> - pyotp.parse_uri('otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DP&issuer=Secure%20App&counter=0') + pyotp.parse_uri('otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP&issuer=Secure%20App&counter=0') >>> <pyotp.hotp.HOTP object at 0xFFFFFFFF> @@ -224,25 +224,29 @@ Scan the following barcode with your phone's OTP app (e.g. Google Authenticator): -.. image:: https://quickchart.io/qr?size=250&text=otpauth%3A%2F%2Ftotp%2Falice%40google.com%3Fsecret%3DJBSWY3DPEHPK3PXPJBSWY3DP +.. image:: https://quickchart.io/qr?size=250&text=otpauth%3A%2F%2Ftotp%2Falice%40google.com%3Fsecret%3DJBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP Now run the following and compare the output: .. code:: python import pyotp - totp = pyotp.TOTP("JBSWY3DPEHPK3PXPJBSWY3DP") + totp = pyotp.TOTP("JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP") print("Current OTP:", totp.now()) Adjusting the minimum seed length ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +By default, PyOTP accepts seeds of at least 80 bits (10 bytes) after Base32 decoding and raises ``ValueError`` for +shorter seeds. PyOTP logs a warning when constructing an OTP with an accepted seed shorter than 128 bits +(16 bytes), including when importing a provisioning URI with ``pyotp.parse_uri()``. For new credentials, +``pyotp.random_base32()`` generates a 160-bit seed by default. + `RFC 4226, section 4 <https://www.rfc-editor.org/rfc/rfc4226#section-4>`_ requires a minimum seed length of 128 bits and recommends 160 bits. `NIST SP 800-63B-4, section 3.1.4.1 <https://pages.nist.gov/800-63-4/sp800-63b/authenticators/#sf-otp>`_ requires the secret key and its algorithm to -provide at least 112 bits of security strength. By default, PyOTP checks that secret inputs are at least 112 bits -long after Base32 decoding. To specify a different minimum seed length, subclass ``pyotp.TOTP`` or ``pyotp.HOTP`` -and adjust the ``min_secret_length_bits`` class variable inherited from ``pyotp.OTP``: +provide at least 112 bits of security strength. To require a 128-bit seed, set ``min_secret_length_bits`` on a +subclass of ``pyotp.TOTP`` or ``pyotp.HOTP``. .. code:: python diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pyotp-2.12.2/README.rst new/pyotp-2.12.4/README.rst --- old/pyotp-2.12.2/README.rst 2020-02-02 01:00:00.000000000 +0100 +++ new/pyotp-2.12.4/README.rst 2020-02-02 01:00:00.000000000 +0100 @@ -66,13 +66,13 @@ import pyotp - totp = pyotp.TOTP('base32secret32323232323232') + totp = pyotp.TOTP('base32secret23456723456723456723') totp.now() # Current OTP; changes with the clock. # A fixed Unix timestamp makes these example outputs reproducible. - totp.at(1234567890) # => '763850' - totp.verify('763850', for_time=1234567890) # => True - totp.verify('763850', for_time=1234567920) # => False + totp.at(1234567890) # => '756795' + totp.verify('756795', for_time=1234567890) # => True + totp.verify('756795', for_time=1234567920) # => False Preventing replay attacks ~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -125,14 +125,14 @@ import pyotp - hotp = pyotp.HOTP('base32secret32323232323232') - hotp.at(0) # => '995369' - hotp.at(1) # => '511453' - hotp.at(1401) # => '666948' + hotp = pyotp.HOTP('base32secret23456723456723456723') + hotp.at(0) # => '023812' + hotp.at(1) # => '219284' + hotp.at(1401) # => '375068' # OTP verified with a counter - hotp.verify('666948', counter=1401) # => True - hotp.verify('666948', counter=1402) # => False + hotp.verify('375068', counter=1401) # => True + hotp.verify('375068', counter=1402) # => False Generating a Secret Key ~~~~~~~~~~~~~~~~~~~~~~~ @@ -157,13 +157,13 @@ .. code:: python - pyotp.totp.TOTP('JBSWY3DPEHPK3PXPJBSWY3DP').provisioning_uri(name='[email protected]', issuer_name='Secure App') + pyotp.totp.TOTP('JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP').provisioning_uri(name='[email protected]', issuer_name='Secure App') - >>> 'otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DP&issuer=Secure%20App' + >>> 'otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP&issuer=Secure%20App' - pyotp.hotp.HOTP('JBSWY3DPEHPK3PXPJBSWY3DP').provisioning_uri(name="[email protected]", issuer_name="Secure App", initial_count=0) + pyotp.hotp.HOTP('JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP').provisioning_uri(name="[email protected]", issuer_name="Secure App", initial_count=0) - >>> 'otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DP&issuer=Secure%20App&counter=0' + >>> 'otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP&issuer=Secure%20App&counter=0' This URL can then be rendered as a QR Code (for example, using https://github.com/soldair/node-qrcode) which can then be scanned and added to the users list of OTP credentials. @@ -172,11 +172,11 @@ .. code:: python - pyotp.parse_uri('otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DP&issuer=Secure%20App') + pyotp.parse_uri('otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP&issuer=Secure%20App') >>> <pyotp.totp.TOTP object at 0xFFFFFFFF> - pyotp.parse_uri('otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DP&issuer=Secure%20App&counter=0') + pyotp.parse_uri('otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP&issuer=Secure%20App&counter=0') >>> <pyotp.hotp.HOTP object at 0xFFFFFFFF> @@ -185,25 +185,29 @@ Scan the following barcode with your phone's OTP app (e.g. Google Authenticator): -.. image:: https://quickchart.io/qr?size=250&text=otpauth%3A%2F%2Ftotp%2Falice%40google.com%3Fsecret%3DJBSWY3DPEHPK3PXPJBSWY3DP +.. image:: https://quickchart.io/qr?size=250&text=otpauth%3A%2F%2Ftotp%2Falice%40google.com%3Fsecret%3DJBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP Now run the following and compare the output: .. code:: python import pyotp - totp = pyotp.TOTP("JBSWY3DPEHPK3PXPJBSWY3DP") + totp = pyotp.TOTP("JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP") print("Current OTP:", totp.now()) Adjusting the minimum seed length ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +By default, PyOTP accepts seeds of at least 80 bits (10 bytes) after Base32 decoding and raises ``ValueError`` for +shorter seeds. PyOTP logs a warning when constructing an OTP with an accepted seed shorter than 128 bits +(16 bytes), including when importing a provisioning URI with ``pyotp.parse_uri()``. For new credentials, +``pyotp.random_base32()`` generates a 160-bit seed by default. + `RFC 4226, section 4 <https://www.rfc-editor.org/rfc/rfc4226#section-4>`_ requires a minimum seed length of 128 bits and recommends 160 bits. `NIST SP 800-63B-4, section 3.1.4.1 <https://pages.nist.gov/800-63-4/sp800-63b/authenticators/#sf-otp>`_ requires the secret key and its algorithm to -provide at least 112 bits of security strength. By default, PyOTP checks that secret inputs are at least 112 bits -long after Base32 decoding. To specify a different minimum seed length, subclass ``pyotp.TOTP`` or ``pyotp.HOTP`` -and adjust the ``min_secret_length_bits`` class variable inherited from ``pyotp.OTP``: +provide at least 112 bits of security strength. To require a 128-bit seed, set ``min_secret_length_bits`` on a +subclass of ``pyotp.TOTP`` or ``pyotp.HOTP``. .. code:: python diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pyotp-2.12.2/src/pyotp/__init__.py new/pyotp-2.12.4/src/pyotp/__init__.py --- old/pyotp-2.12.2/src/pyotp/__init__.py 2020-02-02 01:00:00.000000000 +0100 +++ new/pyotp-2.12.4/src/pyotp/__init__.py 2020-02-02 01:00:00.000000000 +0100 @@ -9,7 +9,7 @@ from . import contrib from .compat import random -from .exceptions import OTPVerificationError, PyOTPError +from .exceptions import InsecureSecretWarning, OTPVerificationError, PyOTPError from .hotp import HOTP from .otp import OTP from .totp import TOTP diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pyotp-2.12.2/src/pyotp/exceptions.py new/pyotp-2.12.4/src/pyotp/exceptions.py --- old/pyotp-2.12.2/src/pyotp/exceptions.py 2020-02-02 01:00:00.000000000 +0100 +++ new/pyotp-2.12.4/src/pyotp/exceptions.py 2020-02-02 01:00:00.000000000 +0100 @@ -4,3 +4,7 @@ class OTPVerificationError(PyOTPError): """Raised when an OTP does not match any permitted counter.""" + + +class InsecureSecretWarning(UserWarning): + """Issued when an OTP secret is shorter than 128 bits.""" diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pyotp-2.12.2/src/pyotp/otp.py new/pyotp-2.12.4/src/pyotp/otp.py --- old/pyotp-2.12.2/src/pyotp/otp.py 2020-02-02 01:00:00.000000000 +0100 +++ new/pyotp-2.12.4/src/pyotp/otp.py 2020-02-02 01:00:00.000000000 +0100 @@ -3,18 +3,22 @@ import base64 import hashlib import hmac +import warnings from typing import Any, ClassVar +from .exceptions import InsecureSecretWarning + class OTP: """ Base class for OTP handlers. The Base32 secret and its decoded length are validated at construction. + Constructing an OTP with a secret shorter than 128 bits emits an InsecureSecretWarning. Subclasses can override ``min_secret_length_bits`` to adjust the minimum decoded secret length. """ - min_secret_length_bits: ClassVar[int] = 112 + min_secret_length_bits: ClassVar[int] = 80 def __init__( self, @@ -31,11 +35,19 @@ self.digest = digest if digest in [hashlib.md5, hashlib.shake_128]: raise ValueError("selected digest function must generate digest size greater than or equals to 18 bytes") - self.secret = s self.name = name or "Secret" self.issuer = issuer - # Validate the Base32 encoding and decoded key length before use. - self.byte_secret() + self._set_secret(s) + + def _set_secret(self, secret: str) -> None: + self.secret = secret + length_bits = len(self.byte_secret()) * 8 + if length_bits < 128: + warnings.warn( + f"Loaded a {length_bits}-bit OTP secret. Use a secret of at least 128 bits.", + InsecureSecretWarning, + stacklevel=4, + ) def generate_otp(self, input: int) -> str: """ @@ -59,11 +71,7 @@ return str_code[-self.digits :] def byte_secret(self) -> bytes: - """Decode the secret and enforce ``min_secret_length_bits`` (112 bits by default). - - See NIST SP 800-63B-4, sections 3.1.4.1 and 3.1.4.2: - https://pages.nist.gov/800-63-4/sp800-63b.html - """ + """Decode the secret and enforce ``min_secret_length_bits`` (80 bits by default).""" secret = self.secret missing_padding = len(secret) % 8 if missing_padding != 0: diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/pyotp-2.12.2/test.py new/pyotp-2.12.4/test.py --- old/pyotp-2.12.2/test.py 2020-02-02 01:00:00.000000000 +0100 +++ new/pyotp-2.12.4/test.py 2020-02-02 01:00:00.000000000 +0100 @@ -7,10 +7,10 @@ import sys import time import unittest +import warnings from typing import ClassVar from unittest.mock import patch from urllib.parse import parse_qsl, urlencode, urlparse -from warnings import warn sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) import pyotp @@ -29,22 +29,41 @@ def test_decoded_seed_length(self): for otp_type in (pyotp.OTP, pyotp.HOTP, pyotp.TOTP, pyotp.contrib.Steam): - for length in (0, 1, 5, 10, 13, 14, 15, 20): + for length in (0, 1, 9, 10, 14, 15, 16, 20): raw = bytes(range(length)) encoded = base64.b32encode(raw).decode("ascii") for seed in (encoded, encoded.rstrip("="), encoded.lower()): with self.subTest(otp_type=otp_type, length=length, seed=seed): - if length < 14: - with self.assertRaisesRegex(ValueError, "at least 112 bits after decoding"): + if length < 10: + with self.assertRaisesRegex(ValueError, "at least 80 bits after decoding"): otp_type(seed) - else: + continue + with warnings.catch_warnings(record=True) as captured: + warnings.simplefilter("always") otp = otp_type(seed) + expected_count = 1 if length < 16 else 0 + self.assertEqual(len(captured), expected_count) + if length < 16: + self.assertIs(captured[0].category, pyotp.InsecureSecretWarning) + self.assertIn("128 bits", str(captured[0].message)) + self.assertNotIn(seed, str(captured[0].message)) self.assertEqual(otp.byte_secret(), raw) self.assertEqual(len(otp.generate_otp(0)), 5 if otp_type is pyotp.contrib.Steam else 6) + self.assertEqual(len(captured), expected_count) + + def test_short_seed_warning_points_to_caller(self): + self.assertTrue(issubclass(pyotp.InsecureSecretWarning, UserWarning)) + for otp_type in (pyotp.HOTP, pyotp.TOTP): + with self.subTest(otp_type=otp_type): + with self.assertWarns(pyotp.InsecureSecretWarning) as captured: + caller_line = sys._getframe().f_lineno + 1 + otp_type("wrn3pqx5uqxqvnqr") + self.assertEqual(captured.filename, __file__) + self.assertEqual(captured.lineno, caller_line) def test_subclass_minimum_seed_length(self): for otp_type in (pyotp.OTP, pyotp.HOTP, pyotp.TOTP, pyotp.contrib.Steam): - for minimum_bits in (80, 113, 128, 160): + for minimum_bits in (72, 113, 128, 160): class CustomOTP(otp_type): min_secret_length_bits: ClassVar[int] = minimum_bits @@ -59,10 +78,14 @@ with self.assertRaisesRegex(ValueError, f"at least {minimum_bits} bits after decoding"): CustomOTP(seed) else: - otp = CustomOTP(seed) + if length < 16: + with self.assertWarns(pyotp.InsecureSecretWarning): + otp = CustomOTP(seed) + else: + otp = CustomOTP(seed) self.assertEqual(otp.byte_secret(), raw) self.assertEqual(len(otp.generate_otp(0)), 5 if otp_type is pyotp.contrib.Steam else 6) - self.assertEqual(otp_type.min_secret_length_bits, 112) + self.assertEqual(otp_type.min_secret_length_bits, 80) def test_subclass_minimum_seed_length_after_secret_change(self): for otp_type in (pyotp.OTP, pyotp.HOTP, pyotp.TOTP, pyotp.contrib.Steam): @@ -75,7 +98,18 @@ otp.secret = base64.b32encode(bytes(range(15))).decode("ascii") with self.assertRaisesRegex(ValueError, "at least 128 bits after decoding"): otp.generate_otp(0) - self.assertEqual(len(otp_type(otp.secret).byte_secret()), 15) + with self.assertWarns(pyotp.InsecureSecretWarning): + self.assertEqual(len(otp_type(otp.secret).byte_secret()), 15) + + def test_decodes_reassigned_secret_on_use(self): + otp = pyotp.TOTP("GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ") + with warnings.catch_warnings(): + warnings.simplefilter("error", pyotp.InsecureSecretWarning) + otp.secret = "wrn3pqx5uqxqvnqr" + self.assertTrue(otp.verify("102705", for_time=1297553958)) + otp.secret = "SOME_SECRET" + with self.assertRaises(ValueError): + otp.at(0) def test_lower_minimum_seed_length_preserves_base32_validation(self): class CustomOTP(pyotp.OTP): @@ -95,19 +129,19 @@ def test_modified_short_secrets_are_rejected_by_verification(self): valid_secret = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ" - for length in (10, 13): + for length in (1, 9): secret = base64.b32encode(bytes(range(length))).decode("ascii") with self.subTest(length=length): hotp = pyotp.HOTP(valid_secret) hotp.secret = secret - with self.assertRaisesRegex(ValueError, "at least 112 bits after decoding"): + with self.assertRaisesRegex(ValueError, "at least 80 bits after decoding"): hotp.verify("123456", counter=0) for otp_type in (pyotp.TOTP, pyotp.contrib.Steam): otp = otp_type(valid_secret) otp.secret = secret - with self.assertRaisesRegex(ValueError, "at least 112 bits after decoding"): + with self.assertRaisesRegex(ValueError, "at least 80 bits after decoding"): otp.verify("123456", for_time=0) - with self.assertRaisesRegex(ValueError, "at least 112 bits after decoding"): + with self.assertRaisesRegex(ValueError, "at least 80 bits after decoding"): otp.verify_and_get_timecode("123456", for_time=0) def test_digit_range(self): @@ -314,7 +348,7 @@ totp = pyotp.TOTP(base64.b32encode(secret), digits=8, digest=digest) for utime, code in self.RFC_VALUES[(digest, secret)]: if utime > sys.maxsize: - warn( + warnings.warn( "32-bit platforms use native functions to handle timestamps, so they fail this test" + " (and will fail after 19 January 2038)" ) @@ -330,10 +364,10 @@ self.assertEqual(totp.at(1234567890), "005924") self.assertEqual(totp.at(2000000000), "279037") - def test_reject_legacy_google_authenticator_secret(self): - # This non-RFC 80-bit fixture produced 102705 at Unix timestamp 1297553958. - with self.assertRaisesRegex(ValueError, "at least 112 bits after decoding"): - pyotp.TOTP("wrn3pqx5uqxqvnqr") + def test_legacy_google_authenticator_secret(self): + with self.assertWarns(pyotp.InsecureSecretWarning): + totp = pyotp.TOTP("wrn3pqx5uqxqvnqr") + self.assertTrue(totp.verify("102705", for_time=1297553958)) def test_validate_totp(self): totp = pyotp.TOTP("wrn3pqx5uqxqvnqrwrn3pqx5uqxqvnqr") @@ -644,8 +678,9 @@ class SteamTOTP(unittest.TestCase): - def test_reject_legacy_example_secret(self): - # Preserve the old non-RFC known-answer vectors, whose key is only 80 bits. + def test_legacy_example_secret(self): + with self.assertWarns(pyotp.InsecureSecretWarning): + steam = pyotp.contrib.Steam("BASE32SECRET3232") for timestamp, code in ( (0, "2TC8B"), (30, "YKKK4"), @@ -655,8 +690,7 @@ (946681223, "7VP3X"), ): with self.subTest(timestamp=timestamp, code=code): - with self.assertRaisesRegex(ValueError, "at least 112 bits after decoding"): - pyotp.contrib.Steam("BASE32SECRET3232") + self.assertEqual(steam.at(timestamp), code) def test_match_examples(self): steam = pyotp.contrib.Steam("BASE32SECRET32323232323232") @@ -838,9 +872,19 @@ class ParseUriTest(unittest.TestCase): + def test_warns_each_time_a_short_secret_is_loaded(self): + for kind, suffix in (("hotp", "&counter=0"), ("totp", ""), ("totp", "&encoder=steam")): + uri = f"otpauth://{kind}/Example?secret=GEZDGNBVGY3TQOJQ{suffix}" + with self.subTest(uri=uri): + with warnings.catch_warnings(record=True) as captured: + warnings.simplefilter("always", pyotp.InsecureSecretWarning) + for _ in range(2): + pyotp.parse_uri(uri) + self.assertEqual([item.category for item in captured], [pyotp.InsecureSecretWarning] * 2) + def test_invalid_secrets_are_rejected(self): for otp_type, encoder in (("hotp", None), ("totp", None), ("totp", "steam")): - for secret in ("wrn3pqx5uqxqvnqr", "SOME_SECRET"): + for secret in ("MZXW6YTBOI", "SOME_SECRET"): query = {"secret": secret} if encoder is not None: query["encoder"] = encoder @@ -850,26 +894,28 @@ def test_hotp_factory_can_override_minimum_secret_length(self): class LegacyHOTP(pyotp.HOTP): - min_secret_length_bits: ClassVar[int] = 80 + min_secret_length_bits: ClassVar[int] = 72 - uri = "otpauth://hotp/Issuer:Name?secret=GEZDGNBVGY3TQOJQ&issuer=Issuer&counter=7" - with self.assertRaisesRegex(ValueError, "at least 112 bits after decoding"): + uri = "otpauth://hotp/Issuer:Name?secret=GEZDGNBVGY3TQOI&issuer=Issuer&counter=7" + with self.assertRaisesRegex(ValueError, "at least 80 bits after decoding"): pyotp.parse_uri(uri) - otp = pyotp.parse_uri(uri, hotp_factory=LegacyHOTP) + with self.assertWarns(pyotp.InsecureSecretWarning): + otp = pyotp.parse_uri(uri, hotp_factory=LegacyHOTP) self.assertIs(type(otp), LegacyHOTP) - self.assertEqual(otp.byte_secret(), b"1234567890") + self.assertEqual(otp.byte_secret(), b"123456789") self.assertEqual((otp.issuer, otp.name, otp.initial_count), ("Issuer", "Name", 7)) def test_totp_factory_can_override_minimum_secret_length(self): class LegacyTOTP(pyotp.TOTP): - min_secret_length_bits: ClassVar[int] = 80 + min_secret_length_bits: ClassVar[int] = 72 - uri = "otpauth://totp/Issuer:Name?secret=GEZDGNBVGY3TQOJQ&digits=8&period=60" - with self.assertRaisesRegex(ValueError, "at least 112 bits after decoding"): + uri = "otpauth://totp/Issuer:Name?secret=GEZDGNBVGY3TQOI&digits=8&period=60" + with self.assertRaisesRegex(ValueError, "at least 80 bits after decoding"): pyotp.parse_uri(uri) - otp = pyotp.parse_uri(uri, totp_factory=LegacyTOTP) + with self.assertWarns(pyotp.InsecureSecretWarning): + otp = pyotp.parse_uri(uri, totp_factory=LegacyTOTP) self.assertIs(type(otp), LegacyTOTP) - self.assertEqual(otp.byte_secret(), b"1234567890") + self.assertEqual(otp.byte_secret(), b"123456789") self.assertEqual((otp.issuer, otp.name, otp.digits, otp.interval), ("Issuer", "Name", 8, 60)) def test_factories_are_keyword_only(self):
