Skip to content
Myr

Merge pull request #1730 from geusebi/master

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

make safe_join behave like os.path.join with *args

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 9c236d3, 06a170e · Browse files · 3 files+63 added−15 removed
This commit

State

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

Diff stats

3 files+63 added−15 removed

  • tests:+43 added−1 removed
  • other:+20 added−14 removed

3 files+63 added−15 removed

modified:flask/helpers.py+18 added-14 removed
561 unmodified lines
562 rv.headers.pop('x-sendfile', None)563 return rv564565566def safe_join(directory, filename):567 """Safely join `directory` and `filename`.566def safe_join(directory, *pathnames):567 """Safely join `directory` and zero or more untrusted `pathnames`568 components.569570 Example usage::571572 @app.route('/wiki/<path:filename>')
1 unmodified line
574 filename = safe_join(app.config['WIKI_FOLDER'], filename)575 with open(filename, 'rb') as fd:576 content = fd.read() # Read and process the file content...577577 :param directory: the base directory.578 :param filename: the untrusted filename relative to that directory.579 :raises: :class:`~werkzeug.exceptions.NotFound` if the resulting path580 would fall out of `directory`.578 :param directory: the trusted base directory.579 :param pathnames: the untrusted pathnames relative to that directory.580 :raises: :class:`~werkzeug.exceptions.NotFound` if one or more passed581 paths fall out of its boundaries.582 """583 for filename in pathnames:584 if filename != '':582 filename = posixpath.normpath(filename)583 for sep in _os_alt_seps:584 if sep in filename:585 filename = posixpath.normpath(filename)586 for sep in _os_alt_seps:587 if sep in filename:588 raise NotFound()589 if os.path.isabs(filename) or \590 filename == '..' or \591 filename.startswith('../'):592 raise NotFound()586 if os.path.isabs(filename) or \587 filename == '..' or \588 filename.startswith('../'):589 raise NotFound()590 return os.path.join(directory, filename)593 directory = os.path.join(directory, filename)594 return directory595596597def send_from_directory(directory, filename, **options):598 """Send a file from a given directory with :func:`send_file`. This
323 unmodified lines
modified:tests/test_helpers.py+43 added-1 removed
13 unmodified lines
14import os15import datetime16import flask17from logging import StreamHandler18from werkzeug.exceptions import BadRequest18from werkzeug.exceptions import BadRequest, NotFound19from werkzeug.http import parse_cache_control_header, parse_options_header20from werkzeug.http import http_date21from flask._compat import StringIO, text_type22
698 unmodified lines
721 c = app.test_client()722 rv = c.get('/?name=World')723 assert rv.data == b'Hello World!'724 assert called == [42]725726727class TestSafeJoin(object):728729 def test_safe_join(self):730 # Valid combinations of *args and expected joined paths.731 passing = (732 (('a/b/c', ), 'a/b/c'),733 (('/', 'a/', 'b/', 'c/', ), '/a/b/c'),734 (('a', 'b', 'c', ), 'a/b/c'),735 (('/a', 'b/c', ), '/a/b/c'),736 (('a/b', 'X/../c'), 'a/b/c', ),737 (('/a/b', 'c/X/..'), '/a/b/c', ),738 # If last path is '' add a slash739 (('/a/b/c', '', ), '/a/b/c/', ),740 # Preserve dot slash741 (('/a/b/c', './', ), '/a/b/c/.', ),742 (('a/b/c', 'X/..'), 'a/b/c/.', ),743 # Base directory is always considered safe744 (('../', 'a/b/c'), '../a/b/c'),745 (('/..', ), '/..'),746 )747748 for args, expected in passing:749 assert flask.safe_join(*args) == expected750751 def test_safe_join_exceptions(self):752 # Should raise werkzeug.exceptions.NotFound on unsafe joins.753 failing = (754 # path.isabs and ``..'' checks755 ('/a', 'b', '/c'),756 ('/a', '../b/c', ),757 ('/a', '..', 'b/c'),758 # Boundaries violations after path normalization759 ('/a', 'b/../b/../../c', ),760 ('/a', 'b', 'c/../..'),761 ('/a', 'b/../../c', ),762 )763764 for args in failing:765 with pytest.raises(NotFound):766 print(flask.safe_join(*args))
modified:CHANGES+2 added
7 unmodified lines
89- the cli command now responds to `--version`.10- Mimetype guessing for ``send_file`` has been removed, as per issue ``#104``.11 See pull request ``#1849``.12- Make ``flask.safe_join`` able to join multiple paths like ``os.path.join``13 (pull request ``#1730``).1415Version 0.11.116--------------17
565 unmodified lines