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