Merge pull request #4996 from pallets/deprecate-markupsafe-export
Before adoption: this commit came in with the history the project adopted, so no landing records it.
deprecate markupsafe exports
authored by David Lord · committed by GitHub · parents 1ee22e1, 9c02f07 · Browse files · 8 files·+41 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+41 added−20 removed
- tests:+13 added−11 removed
- other:+28 added−9 removed
Provenance
- Commit
704b689- Tree
31ade21- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
1ee22e1- Parent 2
9c02f07
8 files changed+41 added−20 removed
modified:docs/api.rst-4 removed
215 unmodified lines216 .. autofunction:: send_file217 218 .. autofunction:: send_from_directory219 220-.. autofunction:: escape220 222-.. autoclass:: Markup223- :members: escape, unescape, striptags224-221 Message Flashing222 ----------------223 224 .. autofunction:: flash516 unmodified linesmodified:docs/security.rst+1 added-1 removed
21 unmodified lines22 in templates, but there are still other places where you have to be23 careful:24 25 - generating HTML without the help of Jinja226-- calling :class:`~flask.Markup` on data submitted by users26+- calling :class:`~markupsafe.Markup` on data submitted by users27 - sending out HTML from uploaded files, never do that, use the28 ``Content-Disposition: attachment`` header to prevent that problem.29 - sending out textfiles from uploaded files. Some browsers are using30 content-type guessing based on the first few bytes so users could244 unmodified linesmodified:docs/templating.rst+1 added-1 removed
113 unmodified lines114 markdown to HTML converter.115 116 There are three ways to accomplish that:117 118-- In the Python code, wrap the HTML string in a :class:`~flask.Markup`118+- In the Python code, wrap the HTML string in a :class:`~markupsafe.Markup`119 object before passing it to the template. This is in general the120 recommended way.121 - Inside the template, use the ``|safe`` filter to explicitly mark a122 string as safe HTML (``{{ myvariable|safe }}``)107 unmodified linesmodified:src/flask/__init__.py+24 added-3 removed
1-from markupsafe import escape2-from markupsafe import Markup3-1 from . import json as json2 from .app import Flask as Flask3 from .app import Request as Request4 from .app import Response as Response59 unmodified lines64 stacklevel=2,65 )66 return __request_ctx_stack67 68+ if name == "escape":69+ import warnings70+ from markupsafe import escape71+72+ warnings.warn(73+ "'flask.escape' is deprecated and will be removed in Flask 2.4. Import"74+ " 'markupsafe.escape' instead.",75+ DeprecationWarning,76+ stacklevel=2,77+ )78+ return escape79+80+ if name == "escape":81+ import warnings82+ from markupsafe import Markup83+84+ warnings.warn(85+ "'flask.Markup' is deprecated and will be removed in Flask 2.4. Import"86+ " 'markupsafe.Markup' instead.",87+ DeprecationWarning,88+ stacklevel=2,89+ )90+ return Markup91+92 raise AttributeError(name)modified:tests/test_basic.py+9 added-8 removed
7 unmodified lines8 from platform import python_implementation9 10 import pytest11 import werkzeug.serving12+from markupsafe import Markup13 from werkzeug.exceptions import BadRequest14 from werkzeug.exceptions import Forbidden15 from werkzeug.exceptions import NotFound16 from werkzeug.http import parse_date455 unmodified lines472 @app.route("/")473 def dump_session_contents():474 flask.session["t"] = (1, 2, 3)475 flask.session["b"] = b"\xff"475- flask.session["m"] = flask.Markup("<html>")476+ flask.session["m"] = Markup("<html>")477 flask.session["u"] = the_uuid478 flask.session["d"] = now479 flask.session["t_tag"] = {" t": "not-a-tuple"}480 flask.session["di_t_tag"] = {" t__": "not-a-tuple"}5 unmodified lines486 s = flask.session487 assert s["t"] == (1, 2, 3)488 assert type(s["b"]) == bytes489 assert s["b"] == b"\xff"489- assert type(s["m"]) == flask.Markup490- assert s["m"] == flask.Markup("<html>")490+ assert type(s["m"]) == Markup491+ assert s["m"] == Markup("<html>")492 assert s["u"] == the_uuid493 assert s["d"] == now494 assert s["t_tag"] == {" t": "not-a-tuple"}495 assert s["di_t_tag"] == {" t__": "not-a-tuple"}115 unmodified lines611 @app.route("/")612 def index():613 flask.flash("Hello World")614 flask.flash("Hello World", "error")614- flask.flash(flask.Markup("<em>Testing</em>"), "warning")615+ flask.flash(Markup("<em>Testing</em>"), "warning")616 return ""617 618 @app.route("/test/")619 def test():620 messages = flask.get_flashed_messages()621 assert list(messages) == [622 "Hello World",623 "Hello World",623- flask.Markup("<em>Testing</em>"),624+ Markup("<em>Testing</em>"),625 ]626 return ""627 628 @app.route("/test_with_categories/")2 unmodified lines631 assert len(messages) == 3632 assert list(messages) == [633 ("message", "Hello World"),634 ("error", "Hello World"),634- ("warning", flask.Markup("<em>Testing</em>")),635+ ("warning", Markup("<em>Testing</em>")),636 ]637 return ""638 639 @app.route("/test_filter/")10 unmodified lines650 category_filter=["message", "warning"], with_categories=True651 )652 assert list(messages) == [653 ("message", "Hello World"),653- ("warning", flask.Markup("<em>Testing</em>")),654+ ("warning", Markup("<em>Testing</em>")),655 ]656 return ""657 658 @app.route("/test_filters_without_returning_categories/")659 def test_filters2():660 messages = flask.get_flashed_messages(category_filter=["message", "warning"])661 assert len(messages) == 2662 assert messages[0] == "Hello World"662- assert messages[1] == flask.Markup("<em>Testing</em>")663+ assert messages[1] == Markup("<em>Testing</em>")664 return ""665 666 # Create new test client on each test to clean flashed messages.667 1253 unmodified linesmodified:tests/test_json_tag.py+1 added-1 removed
1 unmodified line2 from datetime import timezone3 from uuid import uuid44 5 import pytest6+from markupsafe import Markup7 7-from flask import Markup8 from flask.json.tag import JSONTag9 from flask.json.tag import TaggedJSONSerializer10 11 75 unmodified linesmodified:tests/test_templating.py+3 added-2 removed
1 unmodified line2 3 import pytest4 import werkzeug.serving5 from jinja2 import TemplateNotFound6+from markupsafe import Markup7 8 import flask9 10 62 unmodified lines73 74 @app.route("/")75 def index():76 return flask.render_template(76- "escaping_template.html", text=text, html=flask.Markup(text)77+ "escaping_template.html", text=text, html=Markup(text)78 )79 80 lines = client.get("/").data.splitlines()81 assert lines == [11 unmodified lines93 94 @app.route("/")95 def index():96 return flask.render_template(96- "non_escaping_template.txt", text=text, html=flask.Markup(text)97+ "non_escaping_template.txt", text=text, html=Markup(text)98 )99 100 lines = client.get("/").data.splitlines()101 assert lines == [350 unmodified linesmodified:CHANGES.rst+2 added
19 unmodified lines20 - ``json_encoder`` and ``json_decoder`` attributes on app and blueprint, and the21 corresponding ``json.JSONEncoder`` and ``JSONDecoder`` classes, are removed.22 - The ``json.htmlsafe_dumps`` and ``htmlsafe_dump`` functions are removed.23 24+- Importing ``escape`` and ``Markup`` from ``flask`` is deprecated. Import them25+ directly from ``markupsafe`` instead. :pr:`4996`26 - Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.27 :pr:`4947`28 - Ensure subdomains are applied with nested blueprints. :issue:`4834`29 1418 unmodified lines