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
Provenance
- Commit
9f9e1fd- Tree
f6dc428- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
9c236d3- Parent 2
06a170e
3 files changed+63 added−15 removed
modified:flask/helpers.py+18 added-14 removed
561 unmodified lines562 rv.headers.pop('x-sendfile', None)563 return rv564 565 566-def safe_join(directory, filename):567- """Safely join `directory` and `filename`.566+def safe_join(directory, *pathnames):567+ """Safely join `directory` and zero or more untrusted `pathnames`568+ components.569 570 Example usage::571 572 @app.route('/wiki/<path:filename>')1 unmodified line574 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...577 577- :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 directory595 596 597 def send_from_directory(directory, filename, **options):598 """Send a file from a given directory with :func:`send_file`. This323 unmodified linesmodified:tests/test_helpers.py+43 added-1 removed
13 unmodified lines14 import os15 import datetime16 import flask17 from logging import StreamHandler18-from werkzeug.exceptions import BadRequest18+from werkzeug.exceptions import BadRequest, NotFound19 from werkzeug.http import parse_cache_control_header, parse_options_header20 from werkzeug.http import http_date21 from flask._compat import StringIO, text_type22 698 unmodified lines721 c = app.test_client()722 rv = c.get('/?name=World')723 assert rv.data == b'Hello World!'724 assert called == [42]725+726+727+class TestSafeJoin(object):728+729+ 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+ )747+748+ for args, expected in passing:749+ assert flask.safe_join(*args) == expected750+751+ 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+ )763+764+ for args in failing:765+ with pytest.raises(NotFound):766+ print(flask.safe_join(*args))modified:CHANGES+2 added
7 unmodified lines8 9 - 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``).14 15 Version 0.11.116 --------------17 565 unmodified lines