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
Provenance
- Commit
2a65794- Tree
f10265f- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
88120e9
2 files changed+26 added−21 removed
modified:flask/helpers.py+14 added-8 removed
636 unmodified lines637 :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 """641+642+ parts = [directory]643+644 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('../'):647+648+ 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 directory655 656+ parts.append(filename)657 658+ return posixpath.join(*parts)659+660+661 def 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 linesmodified:tests/test_helpers.py+12 added-13 removed
901 unmodified lines902 assert called == [42]903 904 905 class TestSafeJoin(object):906-906 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 lines929 # 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 )940 941 for args in failing:942 with pytest.raises(NotFound):1 unmodified line