Skip to content
Myr

safe_join on Windows uses posixpath

Before adoption: this commit came in with the history the project adopted, so no landing records it.

fixes #2033 closes #2059

authored by David Lord · committed by David Lord · parent 88120e9 · Browse files · 2 files+26 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+26 added−21 removed

  • tests:+12 added−13 removed
  • other:+14 added−8 removed

2 files+26 added−21 removed

modified:flask/helpers.py+14 added-8 removed
636 unmodified lines
637 :param pathnames: the untrusted pathnames relative to that directory.638 :raises: :class:`~werkzeug.exceptions.NotFound` if one or more passed639 paths fall out of its boundaries.640 """641642 parts = [directory]643644 for filename in pathnames:645 if filename != '':646 filename = posixpath.normpath(filename)644 for sep in _os_alt_seps:645 if sep in filename:646 raise NotFound()647 if os.path.isabs(filename) or \648 filename == '..' or \649 filename.startswith('../'):647648 if (649 any(sep in filename for sep in _os_alt_seps)650 or os.path.isabs(filename)651 or filename == '..'652 or filename.startswith('../')653 ):654 raise NotFound()651 directory = os.path.join(directory, filename)652 return directory655656 parts.append(filename)657658 return posixpath.join(*parts)659660661def send_from_directory(directory, filename, **options):662 """Send a file from a given directory with :func:`send_file`. This663 is a secure way to quickly expose static files from an upload folder664 or something similar.
342 unmodified lines
modified:tests/test_helpers.py+12 added-13 removed
901 unmodified lines
902 assert called == [42]903904905class TestSafeJoin(object):906906 def test_safe_join(self):907 # Valid combinations of *args and expected joined paths.908 passing = (910 (('a/b/c', ), 'a/b/c'),911 (('/', 'a/', 'b/', 'c/', ), '/a/b/c'),912 (('a', 'b', 'c', ), 'a/b/c'),913 (('/a', 'b/c', ), '/a/b/c'),914 (('a/b', 'X/../c'), 'a/b/c', ),915 (('/a/b', 'c/X/..'), '/a/b/c', ),909 (('a/b/c',), 'a/b/c'),910 (('/', 'a/', 'b/', 'c/'), '/a/b/c'),911 (('a', 'b', 'c'), 'a/b/c'),912 (('/a', 'b/c'), '/a/b/c'),913 (('a/b', 'X/../c'), 'a/b/c'),914 (('/a/b', 'c/X/..'), '/a/b/c'),915 # If last path is '' add a slash917 (('/a/b/c', '', ), '/a/b/c/', ),916 (('/a/b/c', ''), '/a/b/c/'),917 # Preserve dot slash919 (('/a/b/c', './', ), '/a/b/c/.', ),920 (('a/b/c', 'X/..'), 'a/b/c/.', ),918 (('/a/b/c', './'), '/a/b/c/.'),919 (('a/b/c', 'X/..'), 'a/b/c/.'),920 # Base directory is always considered safe921 (('../', 'a/b/c'), '../a/b/c'),922 (('/..', ), '/..'),923 )
5 unmodified lines
929 # Should raise werkzeug.exceptions.NotFound on unsafe joins.930 failing = (931 # path.isabs and ``..'' checks932 ('/a', 'b', '/c'),934 ('/a', '../b/c', ),933 ('/a', '../b/c'),934 ('/a', '..', 'b/c'),935 # Boundaries violations after path normalization937 ('/a', 'b/../b/../../c', ),936 ('/a', 'b/../b/../../c'),937 ('/a', 'b', 'c/../..'),939 ('/a', 'b/../../c', ),938 ('/a', 'b/../../c'),939 )940941 for args in failing:942 with pytest.raises(NotFound):
1 unmodified line