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