Skip to content

Commit 1ed756a

Browse files
committed
add Response.max_cookie_size config
1 parent 465b48e commit 1ed756a

File tree

6 files changed

+65
-5
lines changed

6 files changed

+65
-5
lines changed

CHANGES.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,14 @@ unreleased
145145
(`#2635`_)
146146
- A single trailing slash is stripped from the blueprint ``url_prefix``
147147
when it is registered with the app. (`#2629`_)
148-
- :meth:`Request.get_json() <flask.Request.get_json>` doesn't cache the
148+
- :meth:`Request.get_json` doesn't cache the
149149
result if parsing fails when ``silent`` is true. (`#2651`_)
150-
- :func:`request.get_json <flask.Request.get_json>` no longer accepts
151-
arbitrary encodings. Incoming JSON should be encoded using UTF-8 per
152-
: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 Flask
152+
will autodetect UTF-8, -16, or -32. (`#2691`_)
153+
- Added :data:`MAX_COOKIE_SIZE` and :attr:`Response.max_cookie_size` to
154+
control when Werkzeug warns about large cookies that browsers may
155+
ignore. (`#2693`_)
153156

154157
.. _pallets/meta#24: https://github.com/pallets/meta/issues/24
155158
.. _#1421: https://github.com/pallets/flask/issues/1421
@@ -196,6 +199,7 @@ unreleased
196199
.. _#2629: https://github.com/pallets/flask/pull/2629
197200
.. _#2651: https://github.com/pallets/flask/issues/2651
198201
.. _#2691: https://github.com/pallets/flask/pull/2691
202+
.. _#2693: https://github.com/pallets/flask/pull/2693
199203

200204

201205
Version 0.12.2

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Response Objects
8585
----------------
8686

8787
.. autoclass:: flask.Response
88-
:members: set_cookie, data, mimetype, is_json, get_json
88+
:members: set_cookie, max_cookie_size, data, mimetype, is_json, get_json
8989

9090
.. attribute:: headers
9191

docs/config.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ The following configuration values are used internally by Flask:
343343

344344
Default: ``False``
345345

346+
.. py:data:: MAX_COOKIE_SIZE
347+
348+
Warn if cookie headers are larger than this many bytes. Defaults to
349+
``4093``. Larger cookies may be silently ignored by browsers. Set to
350+
``0`` to disable the warning.
351+
346352
.. versionadded:: 0.4
347353
``LOGGER_NAME``
348354

@@ -381,6 +387,8 @@ The following configuration values are used internally by Flask:
381387
Added :data:`SESSION_COOKIE_SAMESITE` to control the session
382388
cookie's ``SameSite`` option.
383389

390+
Added :data:`MAX_COOKIE_SIZE` to control a warning from Werkzeug.
391+
384392

385393
Configuring from Files
386394
----------------------

flask/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ class Flask(_PackageBoundObject):
305305
'JSONIFY_PRETTYPRINT_REGULAR': False,
306306
'JSONIFY_MIMETYPE': 'application/json',
307307
'TEMPLATES_AUTO_RELOAD': None,
308+
'MAX_COOKIE_SIZE': 4093,
308309
})
309310

310311
#: The rule object to use for URL rules created. This is used by

flask/wrappers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,26 @@ class Response(ResponseBase, JSONMixin):
191191
.. versionchanged:: 1.0
192192
JSON support is added to the response, like the request. This is useful
193193
when testing to get the test client response data as JSON.
194+
195+
.. versionchanged:: 1.0
196+
197+
Added :attr:`max_cookie_size`.
194198
"""
195199

196200
default_mimetype = 'text/html'
197201

198202
def _get_data_for_json(self, cache):
199203
return self.get_data()
204+
205+
@property
206+
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` in
210+
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 context
216+
return super(Response, self).max_cookie_size

tests/test_basic.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,3 +1917,33 @@ def run_simple_mock(hostname, port, *args, **kwargs):
19171917
monkeypatch.setattr(werkzeug.serving, 'run_simple', run_simple_mock)
19181918
app.config['SERVER_NAME'] = 'pocoo.org:8080'
19191919
app.run(host, port)
1920+
1921+
1922+
def test_max_cookie_size(app, client, recwarn):
1923+
app.config['MAX_COOKIE_SIZE'] = 100
1924+
1925+
# outside app context, default to Werkzeug static value,
1926+
# which is also the default config
1927+
response = flask.Response()
1928+
default = flask.Flask.default_config['MAX_COOKIE_SIZE']
1929+
assert response.max_cookie_size == default
1930+
1931+
# inside app context, use app config
1932+
with app.app_context():
1933+
assert flask.Response().max_cookie_size == 100
1934+
1935+
@app.route('/')
1936+
def index():
1937+
r = flask.Response('', status=204)
1938+
r.set_cookie('foo', 'bar' * 100)
1939+
return r
1940+
1941+
client.get('/')
1942+
assert len(recwarn) == 1
1943+
w = recwarn.pop()
1944+
assert 'cookie is too large' in str(w.message)
1945+
1946+
app.config['MAX_COOKIE_SIZE'] = 0
1947+
1948+
client.get('/')
1949+
assert len(recwarn) == 0

0 commit comments

Comments
 (0)