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