From 3a700b5b8b53606fd98ef8294a56f9510f7290f8 Mon Sep 17 00:00:00 2001
From: Aarni Koskela <akx@iki.fi>
Date: Wed, 28 Apr 2021 10:33:40 +0300
Subject: [PATCH 1/2] Run locale identifiers through `os.path.basename()`

---
 babel/localedata.py      |   24 ++++++++++++++++++++++--
 tests/test_localedata.py |   43 ++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 62 insertions(+), 5 deletions(-)

--- a/babel/localedata.py
+++ b/babel/localedata.py
@@ -13,6 +13,8 @@
 """
 
 import os
+import re
+import sys
 import threading
 from collections import MutableMapping
 from itertools import chain
@@ -33,6 +35,7 @@ def get_base_dir():
 _cache = {}
 _cache_lock = threading.RLock()
 _dirname = os.path.join(get_base_dir(), 'locale-data')
+_windows_reserved_name_re = re.compile("^(con|prn|aux|nul|com[0-9]|lpt[0-9])$", re.I)
 
 
 def normalize_locale(name):
@@ -49,6 +52,22 @@ def normalize_locale(name):
             return locale_id
 
 
+def resolve_locale_filename(name):
+    """
+    Resolve a locale identifier to a `.dat` path on disk.
+    """
+
+    # Clean up any possible relative paths.
+    name = os.path.basename(name)
+
+    # Ensure we're not left with one of the Windows reserved names.
+    if sys.platform == "win32" and _windows_reserved_name_re.match(os.path.splitext(name)[0]):
+        raise ValueError("Name %s is invalid on Windows" % name)
+
+    # Build the path.
+    return os.path.join(_dirname, '%s.dat' % name)
+
+
 def exists(name):
     """Check whether locale data is available for the given locale.
 
@@ -60,7 +79,7 @@ def exists(name):
         return False
     if name in _cache:
         return True
-    file_found = os.path.exists(os.path.join(_dirname, '%s.dat' % name))
+    file_found = os.path.exists(resolve_locale_filename(name))
     return True if file_found else bool(normalize_locale(name))
 
 
@@ -102,6 +121,7 @@ def load(name, merge_inherited=True):
     :raise `IOError`: if no locale data file is found for the given locale
                       identifer, or one of the locales it inherits from
     """
+    name = os.path.basename(name)
     _cache_lock.acquire()
     try:
         data = _cache.get(name)
@@ -119,7 +139,7 @@ def load(name, merge_inherited=True):
                     else:
                         parent = '_'.join(parts[:-1])
                 data = load(parent).copy()
-            filename = os.path.join(_dirname, '%s.dat' % name)
+            filename = resolve_locale_filename(name)
             with open(filename, 'rb') as fileobj:
                 if name != 'root' and merge_inherited:
                     merge(data, pickle.load(fileobj))
--- a/tests/test_localedata.py
+++ b/tests/test_localedata.py
@@ -11,12 +11,18 @@
 # individuals. For the exact contribution history, see the revision
 # history and logs, available at http://babel.edgewall.org/log/.
 
-import unittest
+import os
+import pickle
 import random
-from operator import methodcaller
 import sys
+import tempfile
+import unittest
+from operator import methodcaller
+
+import pytest
+
+from babel import localedata, Locale, UnknownLocaleError
 
-from babel import localedata, numbers
 
 class MergeResolveTestCase(unittest.TestCase):
 
@@ -64,6 +70,37 @@ class MergeResolveTestCase(unittest.Test
         }, dict(d.items()))
 
 
+def test_locale_name_cleanup():
+    """
+    Test that locale identifiers are cleaned up to avoid directory traversal.
+    """
+    no_exist_name = os.path.join(tempfile.gettempdir(), "babel%d.dat" % random.randint(1, 99999))
+    with open(no_exist_name, "wb") as f:
+        pickle.dump({}, f)
+
+    try:
+        name = os.path.splitext(os.path.relpath(no_exist_name, localedata._dirname))[0]
+    except ValueError:
+        if sys.platform == "win32":
+            unittest.SkipTest("unable to form relpath")
+        raise
+
+    assert not localedata.exists(name)
+    with pytest.raises(IOError):
+        localedata.load(name)
+    with pytest.raises(UnknownLocaleError):
+        Locale(name)
+
+
+@pytest.mark.skipif(sys.platform != "win32", reason="windows-only test")
+def test_reserved_locale_names():
+    for name in ("con", "aux", "nul", "prn", "com8", "lpt5"):
+        with pytest.raises(ValueError):
+            localedata.load(name)
+        with pytest.raises(ValueError):
+            Locale(name)
+
+
 def test_load():
     assert localedata.load('en_US')['languages']['sv'] == 'Swedish'
     assert localedata.load('en_US') is localedata.load('en_US')
