add Response.max_cookie_size config
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 465b48e · Browse files · 6 files·+65 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+65 added−5 removed
- tests:+30 added−0 removed
- other:+35 added−5 removed
Provenance
- Commit
1ed756a- Tree
b6c8989- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
465b48e
6 files changed+65 added−5 removed
modified:docs/api.rst+1 added-1 removed
83 unmodified lines84 Response Objects85 ----------------86 87 .. autoclass:: flask.Response88- :members: set_cookie, data, mimetype, is_json, get_json88+ :members: set_cookie, max_cookie_size, data, mimetype, is_json, get_json89 90 .. attribute:: headers91 92 A :class:`~werkzeug.datastructures.Headers` object representing the response headers.730 unmodified linesmodified:docs/config.rst+8 added
341 unmodified lines342 appears to be loaded.343 344 Default: ``False``345 346+.. py:data:: MAX_COOKIE_SIZE347+348+ Warn if cookie headers are larger than this many bytes. Defaults to349+ ``4093``. Larger cookies may be silently ignored by browsers. Set to350+ ``0`` to disable the warning.351+352 .. versionadded:: 0.4353 ``LOGGER_NAME``354 355 .. versionadded:: 0.530 unmodified lines386 387 Added :data:`SESSION_COOKIE_SAMESITE` to control the session388 cookie's ``SameSite`` option.389 390+ Added :data:`MAX_COOKIE_SIZE` to control a warning from Werkzeug.391 392+393 Configuring from Files394 ----------------------395 396 Configuration becomes more useful if you can store it in a separate file,262 unmodified linesmodified:flask/app.py+1 added
303 unmodified lines304 'JSON_SORT_KEYS': True,305 'JSONIFY_PRETTYPRINT_REGULAR': False,306 'JSONIFY_MIMETYPE': 'application/json',307 'TEMPLATES_AUTO_RELOAD': None,308+ 'MAX_COOKIE_SIZE': 4093,309 })310 311 #: The rule object to use for URL rules created. This is used by312 #: :meth:`add_url_rule`. Defaults to :class:`werkzeug.routing.Rule`.1995 unmodified linesmodified:flask/wrappers.py+17 added
189 unmodified lines190 191 .. versionchanged:: 1.0192 JSON support is added to the response, like the request. This is useful193 when testing to get the test client response data as JSON.194+195+ .. versionchanged:: 1.0196+197+ Added :attr:`max_cookie_size`.198 """199 200 default_mimetype = 'text/html'201 202 def _get_data_for_json(self, cache):203 return self.get_data()204+205+ @property206+ def max_cookie_size(self):207+ """Read-only view of the :data:`MAX_COOKIE_SIZE` config key.208+209+ See :attr:`~werkzeug.wrappers.BaseResponse.max_cookie_size` in210+ Werkzeug's docs.211+ """212+ if current_app:213+ return current_app.config['MAX_COOKIE_SIZE']214+215+ # return Werkzeug's default when not in an app context216+ return super(Response, self).max_cookie_sizemodified:tests/test_basic.py+30 added
1915 unmodified lines1916 1917 monkeypatch.setattr(werkzeug.serving, 'run_simple', run_simple_mock)1918 app.config['SERVER_NAME'] = 'pocoo.org:8080'1919 app.run(host, port)1920+1921+1922+def test_max_cookie_size(app, client, recwarn):1923+ app.config['MAX_COOKIE_SIZE'] = 1001924+1925+ # outside app context, default to Werkzeug static value,1926+ # which is also the default config1927+ response = flask.Response()1928+ default = flask.Flask.default_config['MAX_COOKIE_SIZE']1929+ assert response.max_cookie_size == default1930+1931+ # inside app context, use app config1932+ with app.app_context():1933+ assert flask.Response().max_cookie_size == 1001934+1935+ @app.route('/')1936+ def index():1937+ r = flask.Response('', status=204)1938+ r.set_cookie('foo', 'bar' * 100)1939+ return r1940+1941+ client.get('/')1942+ assert len(recwarn) == 11943+ w = recwarn.pop()1944+ assert 'cookie is too large' in str(w.message)1945+1946+ app.config['MAX_COOKIE_SIZE'] = 01947+1948+ client.get('/')1949+ assert len(recwarn) == 0modified:CHANGES.rst+8 added-4 removed
143 unmodified lines144 passing ``subdomain_matching=True`` to the ``Flask`` constructor.145 (`#2635`_)146 - A single trailing slash is stripped from the blueprint ``url_prefix``147 when it is registered with the app. (`#2629`_)148-- :meth:`Request.get_json() <flask.Request.get_json>` doesn't cache the148+- :meth:`Request.get_json` doesn't cache the149 result if parsing fails when ``silent`` is true. (`#2651`_)150-- :func:`request.get_json <flask.Request.get_json>` no longer accepts151- arbitrary encodings. Incoming JSON should be encoded using UTF-8 per152- :rfc:`8259`, but Flask will autodetect UTF-8, -16, or -32. (`#2691`_)150+- :func:`Request.get_json` no longer accepts arbitrary encodings.151+ Incoming JSON should be encoded using UTF-8 per :rfc:`8259`, but Flask152+ will autodetect UTF-8, -16, or -32. (`#2691`_)153+- Added :data:`MAX_COOKIE_SIZE` and :attr:`Response.max_cookie_size` to154+ control when Werkzeug warns about large cookies that browsers may155+ ignore. (`#2693`_)156 157 .. _pallets/meta#24: https://github.com/pallets/meta/issues/24158 .. _#1421: https://github.com/pallets/flask/issues/1421159 .. _#1489: https://github.com/pallets/flask/pull/148938 unmodified lines198 .. _#2635: https://github.com/pallets/flask/pull/2635199 .. _#2629: https://github.com/pallets/flask/pull/2629200 .. _#2651: https://github.com/pallets/flask/issues/2651201 .. _#2691: https://github.com/pallets/flask/pull/2691202+.. _#2693: https://github.com/pallets/flask/pull/2693203 204 205 Version 0.12.2206 --------------618 unmodified lines