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