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