Skip to content
Myr

move html escaping to dedicated section

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

authored by David Lord · committed by David Lord · parent 56e75ea · Browse files · 1 file+31 added−11 removed
This commit

State

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

Diff stats

1 file+31 added−11 removed

  • other:+31 added−11 removed

1 file+31 added−11 removed

modified:docs/quickstart.rst+31 added-11 removed
166 unmodified lines
167168Have another debugger in mind? See :ref:`working-with-debuggers`.169170171HTML Escaping172-------------173174When returning HTML (the default response type in Flask), any175user-provided values rendered in the output must be escaped to protect176from injection attacks. HTML templates rendered with Jinja, introduced177later, will do this automatically.178179:func:`~markupsafe.escape`, shown here, can be used manually. It is180omitted in most examples for brevity, but you should always be aware of181how you're using untrusted data.182183.. code-block:: python184185 from markupsafe import escape186187 @app.route("/<name>")188 def hello(name):189 return f"Hello, {escape(name)}!"190191If a user managed to submit the name ``<script>alert("bad")</script>``,192escaping causes it to be rendered as text, rather than running the193script in the user's browser.194195``<name>`` in the route captures a value from the URL and passes it to196the view function. These variable rules are explained below.197198199Routing200-------201202Modern web applications use meaningful URLs to help users. Users are more
25 unmodified lines
228229 @app.route('/user/<username>')230 def show_user_profile(username):231 # show the user profile for that user204 return f'User {escape(username)}'232 return f'User {username}'233234 @app.route('/post/<int:post_id>')235 def show_post(post_id):236 # show the post with the given id, the id is an integer209 return f'Post {escape(post_id)}'237 return f'Post {post_id}'238239 @app.route('/path/<path:subpath>')240 def show_subpath(subpath):241 # show the subpath after /path/214 return f'Subpath {escape(subpath)}'242 return f'Subpath {subpath}'243244Converter types:245246========== ==========================================
3 unmodified lines
250``path`` like ``string`` but also accepts slashes251``uuid`` accepts UUID strings252========== ==========================================253226.. note:: HTML escaping254228 When returning HTML (the default response type in Flask), any user229 input rendered in the output must be escaped to protect from230 injection attacks. HTML templates in Jinja, introduced later, will231 do this automatically. :func:`~markupsafe.escape`, shown above, can232 be used manually.233234255Unique URLs / Redirection Behavior256``````````````````````````````````257258The following two rules differ in their use of a trailing slash. ::
664 unmodified lines