Skip to content
Myr

Add safe_join: returns the filename used by send_from_directory.

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

authored by Simon Sapin · committed by Armin Ronacher · parent 0e4cd2e · Browse files · 3 files+30 added−8 removed
This commit

State

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

Trailers

  • Signed-off-by: Armin Ronacher <armin.ronacher@active-4.com>

Diff stats

3 files+30 added−8 removed

  • other:+30 added−8 removed

3 files+30 added−8 removed

modified:docs/api.rst+2 added
242 unmodified lines
243.. autofunction:: send_file244245.. autofunction:: send_from_directory246247.. autofunction:: safe_join248249.. autofunction:: escape250251.. autoclass:: Markup252 :members: escape, unescape, striptags
193 unmodified lines
modified:flask/__init__.py+1 added-1 removed
17 unmodified lines
18from .app import Flask, Request, Response19from .config import Config20from .helpers import url_for, jsonify, json_available, flash, \21 send_file, send_from_directory, get_flashed_messages, \22 get_template_attribute, make_response22 get_template_attribute, make_response, safe_join23from .globals import current_app, g, request, session, _request_ctx_stack24from .ctx import has_request_context25from .module import Module26from .templating import render_template, render_template_string
9 unmodified lines
modified:flask/helpers.py+27 added-7 removed
386 unmodified lines
387 rv.headers.pop('x-sendfile', None)388 return rv389390391def safe_join(directory, filename):392 """Safely join `directory` and `filename`.393 394 :param directory: the base directory.395 :param filename: the untrusted filename relative to that directory.396 :raises: :class:`~werkzeug.exceptions.NotFound` if the retsulting path397 would fall out of `directory`.398399 Example usage::400401 @app.route('/wiki/<path:filename>')402 def wiki_page(filename):403 filename = safe_join(app.config['WIKI_FOLDER'], filename)404 with open(filename, 'rb') as fd:405 content = fd.read() # Read and process the file content...406407 """408 filename = posixpath.normpath(filename)409 for sep in _os_alt_seps:410 if sep in filename:411 raise NotFound()412 if os.path.isabs(filename) or filename.startswith('../'):413 raise NotFound()414 return os.path.join(directory, filename)415416417def send_from_directory(directory, filename, **options):418 """Send a file from a given directory with :func:`send_file`. This419 is a secure way to quickly expose static files from an upload folder420 or something similar.
19 unmodified lines
440 download.441 :param options: optional keyword arguments that are directly442 forwarded to :func:`send_file`.443 """418 filename = posixpath.normpath(filename)419 for sep in _os_alt_seps:420 if sep in filename:421 raise NotFound()422 if os.path.isabs(filename) or filename.startswith('../'):423 raise NotFound()424 filename = os.path.join(directory, filename)444 filename = safe_join(directory, filename)445 if not os.path.isfile(filename):446 raise NotFound()447 return send_file(filename, conditional=True, **options)448
68 unmodified lines