From 3aeabb729fabbe486ee39d31eeb07605ad03a1ff Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sun, 30 Mar 2025 16:42:03 +0200 Subject: [PATCH 1/3] Test bad magic number in gettext MO files --- Lib/test/test_gettext.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 0fbd90dcb485f8..96953f45e62e37 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -37,6 +37,9 @@ bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA ''' +# Invalid magic number ('ABCD' b64-encoded) +GNU_MO_DATA_BAD_MAGIC_NUMBER = b'\QUJDRA==' + # This data contains an invalid major version number (5) # An unexpected major version number should be treated as an error when # parsing a .mo file @@ -109,6 +112,7 @@ LOCALEDIR = os.path.join('xx', 'LC_MESSAGES') MOFILE = os.path.join(LOCALEDIR, 'gettext.mo') +MOFILE_BAD_MAGIC_NUMBER = os.path.join(LOCALEDIR, 'gettext_bad_magic_number.mo') MOFILE_BAD_MAJOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_major_version.mo') MOFILE_BAD_MINOR_VERSION = os.path.join(LOCALEDIR, 'gettext_bad_minor_version.mo') UMOFILE = os.path.join(LOCALEDIR, 'ugettext.mo') @@ -129,6 +133,8 @@ def setUpClass(cls): os.makedirs(LOCALEDIR) with open(MOFILE, 'wb') as fp: fp.write(base64.decodebytes(GNU_MO_DATA)) + with open(MOFILE_BAD_MAGIC_NUMBER, 'wb') as fp: + fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAGIC_NUMBER)) with open(MOFILE_BAD_MAJOR_VERSION, 'wb') as fp: fp.write(base64.decodebytes(GNU_MO_DATA_BAD_MAJOR_VERSION)) with open(MOFILE_BAD_MINOR_VERSION, 'wb') as fp: @@ -223,6 +229,16 @@ def test_bindtextdomain(self): def test_textdomain(self): self.assertEqual(gettext.textdomain(), 'gettext') + def test_bad_magic_number(self): + with open(MOFILE_BAD_MAGIC_NUMBER, 'rb') as fp: + with self.assertRaises(OSError) as cm: + gettext.GNUTranslations(fp) + + exception = cm.exception + self.assertEqual(exception.errno, 0) + self.assertEqual(exception.strerror, "Bad magic number") + self.assertEqual(exception.filename, MOFILE_BAD_MAGIC_NUMBER) + def test_bad_major_version(self): with open(MOFILE_BAD_MAJOR_VERSION, 'rb') as fp: with self.assertRaises(OSError) as cm: From 8bd4db0b906102ce3d5992797b387c514e942b1b Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sun, 30 Mar 2025 17:24:32 +0200 Subject: [PATCH 2/3] Fix invalid escape --- Lib/test/test_gettext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 96953f45e62e37..4f2edd4864eda5 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -38,7 +38,7 @@ ''' # Invalid magic number ('ABCD' b64-encoded) -GNU_MO_DATA_BAD_MAGIC_NUMBER = b'\QUJDRA==' +GNU_MO_DATA_BAD_MAGIC_NUMBER = rb'\QUJDRA==' # This data contains an invalid major version number (5) # An unexpected major version number should be treated as an error when From c05ab3102114e08d3c916a49a74e23914a6191d3 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sun, 30 Mar 2025 21:15:24 +0200 Subject: [PATCH 3/3] Use base64.b64encode directly --- Lib/test/test_gettext.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index 4f2edd4864eda5..40371d0a45fea3 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -37,8 +37,8 @@ bmsgd2luayAoaW4gIm15IG90aGVyIGNvbnRleHQiKQB3aW5rIHdpbmsA ''' -# Invalid magic number ('ABCD' b64-encoded) -GNU_MO_DATA_BAD_MAGIC_NUMBER = rb'\QUJDRA==' +# .mo file with an invalid magic number +GNU_MO_DATA_BAD_MAGIC_NUMBER = base64.b64encode(b'ABCD') # This data contains an invalid major version number (5) # An unexpected major version number should be treated as an error when pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy