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