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
Provenance
- Commit
57f9623- Tree
b34f6ba- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
56e75ea
1 file changed+31 added−11 removed
modified:docs/quickstart.rst+31 added-11 removed
166 unmodified lines167 168 Have another debugger in mind? See :ref:`working-with-debuggers`.169 170 171+HTML Escaping172+-------------173+174+When returning HTML (the default response type in Flask), any175+user-provided values rendered in the output must be escaped to protect176+from injection attacks. HTML templates rendered with Jinja, introduced177+later, will do this automatically.178+179+:func:`~markupsafe.escape`, shown here, can be used manually. It is180+omitted in most examples for brevity, but you should always be aware of181+how you're using untrusted data.182+183+.. code-block:: python184+185+ from markupsafe import escape186+187+ @app.route("/<name>")188+ def hello(name):189+ return f"Hello, {escape(name)}!"190+191+If a user managed to submit the name ``<script>alert("bad")</script>``,192+escaping causes it to be rendered as text, rather than running the193+script in the user's browser.194+195+``<name>`` in the route captures a value from the URL and passes it to196+the view function. These variable rules are explained below.197+198+199 Routing200 -------201 202 Modern web applications use meaningful URLs to help users. Users are more25 unmodified lines228 229 @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}'233 234 @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}'238 239 @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}'243 244 Converter types:245 246 ========== ==========================================3 unmodified lines250 ``path`` like ``string`` but also accepts slashes251 ``uuid`` accepts UUID strings252 ========== ==========================================253 226-.. note:: HTML escaping254 228- 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.233-234-255 Unique URLs / Redirection Behavior256 ``````````````````````````````````257 258 The following two rules differ in their use of a trailing slash. ::664 unmodified lines