Skip to content
Myr

Make safe_join able to safely join multiple paths

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

authored by Giampaolo Eusebi · committed by Giampaolo Eusebi · parent 9c236d3 · Browse files · 2 files+20 added−14 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+20 added−14 removed

  • other:+20 added−14 removed

2 files+20 added−14 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: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